ll | bd9cc17 | 2022-12-30 16:36:18 +0800 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <log/log.h> |
| 4 | #include "atci_at_util.h" |
| 5 | |
| 6 | #undef LOG_TAG |
| 7 | #define LOG_TAG "AT_COMMON" |
| 8 | |
| 9 | int atci_at_skip_space(char **p_cur) { |
| 10 | if (*p_cur == NULL) { |
| 11 | return SYS_FAIL; |
| 12 | } |
| 13 | while (**p_cur == ATCI_SPACE) { |
| 14 | (*p_cur)++; |
| 15 | } |
| 16 | return SYS_SUCC; |
| 17 | } |
| 18 | int atci_at_hasmore(char **p_cur) { |
| 19 | if (*p_cur == NULL || **p_cur == ATCI_END_CHAR) { |
| 20 | return SYS_FAIL; |
| 21 | } |
| 22 | return SYS_SUCC; |
| 23 | } |
| 24 | |
| 25 | int atci_at_to_equal(char **p_cur) { |
| 26 | if (*p_cur == NULL) { |
| 27 | return SYS_FAIL; |
| 28 | } |
| 29 | *p_cur = strchr(*p_cur, ATCI_EQUAL); |
| 30 | if (*p_cur == NULL) { |
| 31 | return SYS_FAIL; |
| 32 | } |
| 33 | (*p_cur)++; |
| 34 | return SYS_SUCC; |
| 35 | } |
| 36 | int atci_at_to_colon(char **p_cur) { |
| 37 | if (*p_cur == NULL) { |
| 38 | return SYS_FAIL; |
| 39 | } |
| 40 | *p_cur = strchr(*p_cur, ATCI_COLON); |
| 41 | if (*p_cur == NULL) { |
| 42 | return SYS_FAIL; |
| 43 | } |
| 44 | (*p_cur)++; |
| 45 | return SYS_SUCC; |
| 46 | } |
| 47 | int atci_at_skip_next_comma(char **p_cur) { |
| 48 | if (*p_cur == NULL) { |
| 49 | return SYS_FAIL; |
| 50 | } |
| 51 | *p_cur = strchr(*p_cur, ATCI_COMMA); |
| 52 | if (*p_cur == NULL) { |
| 53 | return SYS_FAIL; |
| 54 | } |
| 55 | (*p_cur)++; |
| 56 | return SYS_SUCC; |
| 57 | } |
| 58 | int atci_at_get_next_key(char **p_cur, char **p_out) { |
| 59 | if (*p_cur == NULL) { |
| 60 | return SYS_FAIL; |
| 61 | } |
| 62 | atci_at_skip_space(p_cur); |
| 63 | if (**p_cur == ATCI_DOUBLE_QUOTE) { |
| 64 | (*p_cur)++; |
| 65 | *p_out = strsep(p_cur, "\""); |
| 66 | atci_at_skip_next_comma(p_cur); |
| 67 | } else { |
| 68 | *p_out = strsep(p_cur, ","); |
| 69 | } |
| 70 | return SYS_SUCC; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Parses the next integer in the line and places it in *p_out |
| 75 | * "uns" is indicate in unsigned or not |
| 76 | * returns SYS_SUCC on success |
| 77 | * returns SYS_FAIL on fail |
| 78 | * updates *p_cur |
| 79 | * "base" is the same as the base param in strtol |
| 80 | */ |
| 81 | int atci_at_get_nextint_base(char **p_cur, int *p_out, int base, int uns) { |
| 82 | char *ret; |
| 83 | if (*p_cur == NULL) { |
| 84 | return SYS_FAIL; |
| 85 | } |
| 86 | if (SYS_FAIL == atci_at_get_next_key(p_cur, &ret)) { |
| 87 | return SYS_FAIL; |
| 88 | } else { |
| 89 | long l; |
| 90 | char *end; |
| 91 | if (uns) { |
| 92 | l = strtoul(ret, &end, base); |
| 93 | } else { |
| 94 | l = strtol(ret, &end, base); |
| 95 | } |
| 96 | *p_out = (int) l; |
| 97 | if (end == ret) { |
| 98 | return SYS_FAIL; |
| 99 | } |
| 100 | } |
| 101 | return SYS_SUCC; |
| 102 | } |
| 103 | int atci_at_get_nextint(char **p_cur, int *p_out) { |
| 104 | return atci_at_get_nextint_base(p_cur, p_out, 10, 0); |
| 105 | } |
| 106 | int atci_at_get_nexthexint(char **p_cur, int *p_out) { |
| 107 | return atci_at_get_nextint_base(p_cur, p_out, 16, 1); |
| 108 | } |
| 109 | int atci_get_cmd_mode(char *line) { |
| 110 | int reasult = AT_WRONG_MODE; |
| 111 | char *p_cur = NULL; |
| 112 | if (NULL == line) { |
| 113 | reasult = AT_WRONG_MODE; |
| 114 | RLOGD("atci_get_cmd_mode error, input is NULL"); |
| 115 | return reasult; |
| 116 | } |
| 117 | p_cur = strchr(line, ATCI_EQUAL); |
| 118 | if (NULL == p_cur) { |
| 119 | p_cur = strchr(line, ATCI_QUESTION_MARK); |
| 120 | if (NULL == p_cur) { |
| 121 | reasult = AT_ACTIVE_MODE; |
| 122 | } else { |
| 123 | reasult = AT_READ_MODE; |
| 124 | } |
| 125 | } else { |
| 126 | p_cur++; |
| 127 | atci_at_skip_space(&p_cur); |
| 128 | if (ATCI_QUESTION_MARK == *p_cur) { |
| 129 | reasult = AT_TEST_MODE; |
| 130 | } else { |
| 131 | reasult = AT_SET_MODE; |
| 132 | } |
| 133 | } |
| 134 | RLOGD("atci_get_cmd_mode success[%d]", reasult); |
| 135 | return reasult; |
| 136 | } |
| 137 | |
| 138 | char* atci_get_cmd_prefix(char *line) { |
| 139 | int buf_len; |
| 140 | char *prefix; |
| 141 | char *end_ptr; |
| 142 | if (NULL == line) { |
| 143 | RLOGD("input is null"); |
| 144 | return NULL; |
| 145 | } |
| 146 | end_ptr = line; |
| 147 | while (!ATCI_IS_CAHR(*end_ptr, ATCI_EQUAL) |
| 148 | && !ATCI_IS_CAHR(*end_ptr, ATCI_QUESTION_MARK) |
| 149 | && !ATCI_IS_CAHR(*end_ptr, ATCI_END_CHAR) |
| 150 | && !ATCI_IS_CAHR(*end_ptr, ATCI_CR) && !ATCI_IS_CAHR(*end_ptr, ATCI_LF)) { |
| 151 | end_ptr++; |
| 152 | } |
| 153 | buf_len = end_ptr - line + 1; |
| 154 | prefix = (char *) calloc(buf_len, 1); |
| 155 | if (prefix) { |
| 156 | int i; |
| 157 | char *in_ptr = line; |
| 158 | char *out_ptr = prefix; |
| 159 | for (i = 0; i < buf_len - 1; i++) { |
| 160 | if (!ATCI_IS_CAHR(*in_ptr, ATCI_SPACE)) { |
| 161 | *out_ptr = ATCI_UPPER_TO_LOWER(*in_ptr); |
| 162 | out_ptr++; |
| 163 | } |
| 164 | in_ptr++; |
| 165 | } |
| 166 | *out_ptr = ATCI_END_CHAR; |
| 167 | } |
| 168 | RLOGD("get cmd prefix [%d][%s]", buf_len, prefix); |
| 169 | return prefix; |
| 170 | } |
| 171 | |