3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * email: daniel.otte@rub.de
23 * license: GPLv3 or later
25 * components to help implementing simple command based interaction
33 #include <avr/pgmspace.h>
34 #include "string-extras.h"
38 cli_rx_fpt cli_rx = NULL;
39 cli_tx_fpt cli_tx = NULL;
43 * \brief output a character to the console
47 void cli_putc(char c){
53 * \brief get a character from the console
54 * Gets a character from the console input and blocks
55 * until a character is recieved
57 uint16_t cli_getc(void){
60 return ((uint16_t)-1);
64 * \brief get a character from the console
65 * Gets a char from the console input (like cli_getc())
66 * and echos it back to the console if echo is enabled.
68 uint16_t cli_getc_cecho(void){
72 if(cli_tx && cli_echo)
76 return ((uint16_t)-1);
80 * \brief ouputs a zero-terminated string from ram to the console
82 void cli_putstr(char* s){
91 * \brief ouputs a zero-terminated string from flash to the console
93 void cli_putstr_P(PGM_P s){
98 c = pgm_read_byte(s++);
106 * \brief reads a line or max n characters from the console
107 * Writes characters from the console into the supplyed buffer until a '\r'
108 * character is recieved or until n character a read (whatever happens first).
109 * The string will always be terminated by a '\0' character, so the buffer
110 * should have at least a size of n+1.
112 uint8_t cli_getsn(char* s, uint16_t n){
116 while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){
120 return (c=='\r')?0:1;
124 * \brief dumps the contents of a buffer to the console
125 * Dumps length bytes from data to the console ouput. The dump
126 * will have 2*n continous hexadecimal characters.
128 void cli_hexdump(void* data, uint16_t length){
129 char hex_tab[] = {'0', '1', '2', '3',
136 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
137 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
138 data = (uint8_t*)data +1;
143 * \brief dumps the contents of a buffer to the console
144 * Like cli_hexdump but bytes are seperated with a single space
145 * on the console output.
147 void cli_hexdump2(void* data, uint16_t length){
148 char hex_tab[] = {'0', '1', '2', '3',
155 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
156 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
158 data = (uint8_t*)data +1;
164 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
165 cmdlist_entry_t item;
170 cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
171 " <command> - <params> - <address>\r\n"));
173 item.cmd_name = (void*)pgm_read_word(cmdlist+0);
174 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
175 item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
176 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
177 if(item.cmd_name==NULL){
181 cli_putstr_P(item.cmd_name);
182 i=maxcmdlength-strlen_P(item.cmd_name);
185 cli_putstr_P(PSTR(" - "));
186 if(item.cmd_param_str==NULL){
187 cli_putstr_P(PSTR("none \t- 0x"));
189 if(item.cmd_param_str==(void*)1){
190 cli_putstr_P(PSTR("yes \t- 0x"));
192 cli_putstr_P(item.cmd_param_str);
193 cli_putstr_P(PSTR(" \t- 0x"));
196 cli_hexdump(&item.cmd_function, 2);
197 cli_putstr_P(PSTR("\r\n"));
201 void echo_ctrl(char* s){
203 if(s==NULL || *s=='\0'){
204 cli_putstr_P(PSTR("\r\necho is "));
205 cli_putstr_P(cli_echo?PSTR("on"):PSTR("off"));
206 cli_putstr_P(PSTR("\r\n"));
209 if(!strcmp_P(s, PSTR("true")) || !strcmp_P(s, PSTR("on")) || *s=='1'){
212 if(!strcmp_P(s, PSTR("false")) || !strcmp_P(s, PSTR("off")) || *s=='0'){
217 typedef void(*str_fpt)(char*);
219 #define CLI_BACKSPACE 8
220 #define CLI_TABULATOR 9
222 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
223 PGM_VOID_P cmdlist_orig = cmdlist;
224 if(*cmd=='\0' || *cmd=='#')
226 if(!strcmp_P(cmd, PSTR("exit")))
228 if((!strcmp_P(cmd, PSTR("help"))) || (!strcmp_P(cmd, PSTR("?")))){
229 cli_auto_help(maxcmdlength, cmdlist);
232 uint16_t fwlength=firstword_length(cmd);
234 memcpy(fw, cmd, fwlength);
236 cmdlist_entry_t item;
238 item.cmd_name = (void*)pgm_read_word(cmdlist+0);
239 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
240 item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
241 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
242 }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name));
243 if(item.cmd_name==NULL){
244 cli_auto_help(maxcmdlength, cmdlist_orig);
246 if(item.cmd_function==NULL)
248 switch((uint16_t)item.cmd_param_str){
253 if(cmd[fwlength]=='\0'){
254 ((str_fpt)item.cmd_function)(cmd+fwlength);
256 ((str_fpt)item.cmd_function)(cmd+fwlength+1);
260 cli_putstr_P(PSTR("\r\nparam parsing currently not implemented!\r\n"));
268 uint16_t max_cmd_length(PGM_VOID_P cmdlist){
272 str = (char*)pgm_read_word(cmdlist);
273 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
282 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
284 char ref[maxcmdlength+1];
287 /* check if we are behind the first word */
289 if(!isgraph(buffer[i++]))
293 itemstr = (char*)pgm_read_word(cmdlist);
296 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
297 if(!strncmp_P(buffer, itemstr, i)){
299 strcpy_P(ref, itemstr);
301 ref[stridentcnt_P(ref, itemstr)]='\0';
305 i = strcmp(buffer, ref);
311 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){
313 uint16_t len=strlen(buffer);
315 itemstr = (char*)pgm_read_word(cmdlist);
317 cli_putstr_P(PSTR("\r\n>"));
321 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
322 if(!strncmp_P(buffer, itemstr, len)){
323 cli_putstr_P(PSTR("\r\n "));
324 cli_putstr_P(itemstr);
329 int8_t cmd_interface(PGM_VOID_P cmd_desc){
330 uint16_t cli_buffer_size;
331 uint16_t cli_buffer_index;
333 uint8_t completion_failed=0;
336 uint16_t maxcmdlength = max_cmd_length(cmd_desc);
337 cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
347 if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
351 memset(cli_buffer, 0, cli_buffer_size);
353 cli_putstr_P(PSTR("\r\n>"));
358 if(cli_buffer_index==0)
361 cli_buffer[cli_buffer_index] = '\0';
362 if(cli_echo && cli_tx){
367 if(completion_failed || cli_buffer_index==0){
369 cli_option_listing(cli_buffer, cmd_desc);
371 uint16_t old_idx = cli_buffer_index;
373 ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
374 cli_buffer_index = strlen(cli_buffer);
375 if(cli_echo && cli_tx){
376 while(old_idx<cli_buffer_index){
377 cli_tx(cli_buffer[old_idx++]);
384 if(cli_echo && cli_tx){
387 if(cli_buffer_index+1==cli_buffer_size){
388 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
392 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
394 cli_buffer[cli_buffer_index++] = c;