123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- #include "cliface.h"
- uint16_t CLI_init
- (
- void
- )
- {
- return 0;
- }
- uint16_t CLI_process_line
- (
- uint8_t * buffer,
- uint32_t buffer_len,
- CLI_COMMAND * cmd_list,
- uint32_t cmd_count
- )
- {
- uint32_t argc;
- uint8_t * argv[CLI_MAX_ARGS];
- uint8_t * cmd;
- uint32_t index;
-
- CLI_NULL_CHECK(buffer);
-
- for (; CLI_IS_WHITE_SPACE(*buffer) && (0 != buffer_len); buffer++, buffer_len--);
-
- if (0 == buffer_len)
- {
- CLI_ERR(
- "[CLI] Empty command line\n");
- return 0xffff;
- }
-
- argc = 0;
- for (cmd = buffer + 1; cmd < (buffer + buffer_len); cmd++)
- {
-
- if (CLI_IS_CMD_SEPARATOR(*cmd))
- {
- #ifdef AT_CMD
- if(('\r' == *cmd) && ('\n' == *(cmd+1)))
- {
- *cmd = '\0';
-
- break;
- }
- if(' ' == *cmd)
- {
- *cmd = '\0';
- break;
- }
- #endif
- *cmd = '\0';
- }
-
- else if ('\0' == (*(cmd - 1)))
- {
- argv[argc++] = cmd;
- }
- else
- {
-
- }
- }
- CLI_TRC(
- "[CLI] Command %s, Number of arguments %d\n", buffer, argc);
- {
- uint8_t ai;
- for (ai = 0; ai < argc; ai++)
- {
- CLI_TRC(
- "Arg [%02X] %s\n", ai, argv[ai]);
- }
- }
-
- cmd = buffer;
-
- for (index = 0; index < cmd_count; index++)
- {
- if (0 == CLI_STR_COMPARE(buffer, cmd_list[index].cmd))
- {
- cmd_list[index].cmd_hdlr(argc, argv);
- break;
- }
- }
- return 0;
- }
- #define IS_SPACE(c) ((' ' == (c)) || ('\t' == (c)))
- #define IS_DIGIT(c) (('0' <= (c)) && ('9' >= (c)))
- #define IS_UPPER(c) (('A' <= (c)) && ('F' >= (c)))
- #define IS_LOWER(c) (('a' <= (c)) && ('f' >= (c)))
- #define IS_ALPHA(c) IS_LOWER(c) || IS_UPPER(c)
- int32_t CLI_strtoi
- (
- uint8_t *data,
- uint16_t data_length,
- uint8_t base
- )
- {
- int32_t value;
- uint16_t index;
- int8_t sign_adj;
- uint8_t c;
- c = 0;
-
- for (index = 0; index < data_length; index++)
- {
- c = data[index];
- if (IS_SPACE(c))
- {
- continue;
- }
- else
- {
- break;
- }
- }
- value = 0;
- sign_adj = 1;
-
- if ('-' == c)
- {
- sign_adj = (int8_t)-1;
- index++;
- }
-
- for (; index < data_length; index++)
- {
- c = data[index];
-
- if (IS_DIGIT(c))
- {
- value *= base;
- value += (c - '0');
- }
- else if (IS_LOWER(c))
- {
- value *= base;
- value += (c - 'a' + 10);
- }
- else if (IS_UPPER(c))
- {
- value *= base;
- value += (c - 'A' + 10);
- }
- else
- {
- break;
- }
- }
- return (sign_adj * value);
- }
- uint16_t CLI_strtoarray
- (
- uint8_t * data,
- uint16_t data_length,
- uint8_t * output_array,
- uint16_t output_array_len
- )
- {
- int32_t index;
- uint8_t c0, c1;
- uint8_t base;
- uint16_t output_index;
-
- base = 16;
- c0 = 0;
- c1 = 0;
-
- memset(output_array, 0, output_array_len);
-
- if (data_length > (2 * output_array_len))
- {
- return 0xFFFF;
- }
-
- output_index = output_array_len - 1;
- for (index = data_length - 1; index >= 0; index -= 2)
- {
- if (0 != index)
- {
- c1 = data[index];
- c0 = data[index - 1];
- }
- else
- {
- c1 = data[index];
- c0 = '0';
- }
-
- if (IS_DIGIT(c0))
- {
- c0 = (c0 - '0');
- }
- else if (IS_LOWER(c0))
- {
- c0 = (c0 - 'a' + 10);
- }
- else if (IS_UPPER(c0))
- {
- c0 = (c0 - 'A' + 10);
- }
- else
- {
- return 0xFFFF;
- }
-
- if (IS_DIGIT(c1))
- {
- c1 = (c1 - '0');
- }
- else if (IS_LOWER(c1))
- {
- c1 = (c1 - 'a' + 10);
- }
- else if (IS_UPPER(c1))
- {
- c1 = (c1 - 'A' + 10);
- }
- else
- {
- return 0xFFFF;
- }
- output_array[output_index] = c0 * base + c1;
- output_index--;
- }
- return 0;
- }
- uint16_t CLI_strtoarray_le
- (
- uint8_t * data,
- uint16_t data_length,
- uint8_t * output_array,
- uint16_t output_array_len
- )
- {
- int32_t index;
- uint8_t c0, c1;
- uint8_t base;
- uint16_t output_index;
-
- base = 16;
- c0 = 0;
- c1 = 0;
-
- memset(output_array, 0, output_array_len);
-
- if (data_length > (2 * output_array_len))
- {
- return 0xFFFF;
- }
-
- output_index = 0;
- for (index = data_length - 1; index >= 0; index -= 2)
- {
- if (0 != index)
- {
- c1 = data[index];
- c0 = data[index - 1];
- }
- else
- {
- c1 = data[index];
- c0 = '0';
- }
-
- if (IS_DIGIT(c0))
- {
- c0 = (c0 - '0');
- }
- else if (IS_LOWER(c0))
- {
- c0 = (c0 - 'a' + 10);
- }
- else if (IS_UPPER(c0))
- {
- c0 = (c0 - 'A' + 10);
- }
- else
- {
- return 0xFFFF;
- }
-
- if (IS_DIGIT(c1))
- {
- c1 = (c1 - '0');
- }
- else if (IS_LOWER(c1))
- {
- c1 = (c1 - 'a' + 10);
- }
- else if (IS_UPPER(c1))
- {
- c1 = (c1 - 'A' + 10);
- }
- else
- {
- return 0xFFFF;
- }
- output_array[output_index] = c0 * base + c1;
- output_index++;
- }
- return 0;
- }
|