1 /******************************************************************************
2 *
3 * Copyright 2004-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * BTA AG AT command interpreter.
22 *
23 ******************************************************************************/
24 #define LOG_TAG "bta_ag_at"
25
26 #include <cstdint>
27
28 #include "bt_target.h" // Must be first to define build configuration:
29
30 #include "bta/ag/bta_ag_at.h"
31 #include "bta/ag/bta_ag_int.h"
32 #include "bta/include/utl.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/log.h"
35
36 /*****************************************************************************
37 * Constants
38 ****************************************************************************/
39
40 /******************************************************************************
41 *
42 * Function bta_ag_at_init
43 *
44 * Description Initialize the AT command parser control block.
45 *
46 *
47 * Returns void
48 *
49 *****************************************************************************/
bta_ag_at_init(tBTA_AG_AT_CB * p_cb)50 void bta_ag_at_init(tBTA_AG_AT_CB* p_cb) {
51 p_cb->p_cmd_buf = nullptr;
52 p_cb->cmd_pos = 0;
53 }
54
55 /******************************************************************************
56 *
57 * Function bta_ag_at_reinit
58 *
59 * Description Re-initialize the AT command parser control block. This
60 * function resets the AT command parser state and frees
61 * any GKI buffer.
62 *
63 *
64 * Returns void
65 *
66 *****************************************************************************/
bta_ag_at_reinit(tBTA_AG_AT_CB * p_cb)67 void bta_ag_at_reinit(tBTA_AG_AT_CB* p_cb) {
68 osi_free_and_reset((void**)&p_cb->p_cmd_buf);
69 p_cb->cmd_pos = 0;
70 }
71
72 /******************************************************************************
73 *
74 * Function bta_ag_process_at
75 *
76 * Description Parse AT commands. This function will take the input
77 * character string and parse it for AT commands according to
78 * the AT command table passed in the control block.
79 *
80 *
81 * Returns void
82 *
83 *****************************************************************************/
bta_ag_process_at(tBTA_AG_AT_CB * p_cb,char * p_end)84 void bta_ag_process_at(tBTA_AG_AT_CB* p_cb, char* p_end) {
85 uint16_t idx;
86 uint8_t arg_type;
87 char* p_arg;
88 int16_t int_arg = 0;
89 /* loop through at command table looking for match */
90 for (idx = 0; p_cb->p_at_tbl[idx].p_cmd[0] != 0; idx++) {
91 if (!utl_strucmp(p_cb->p_at_tbl[idx].p_cmd, p_cb->p_cmd_buf)) {
92 break;
93 }
94 }
95
96 /* if there is a match; verify argument type */
97 if (p_cb->p_at_tbl[idx].p_cmd[0] != 0) {
98 /* start of argument is p + strlen matching command */
99 p_arg = p_cb->p_cmd_buf + strlen(p_cb->p_at_tbl[idx].p_cmd);
100 if (p_arg > p_end) {
101 (*p_cb->p_err_cback)((tBTA_AG_SCB*)p_cb->p_user, false, nullptr);
102 return;
103 }
104
105 /* if no argument */
106 if (p_arg[0] == 0) {
107 arg_type = BTA_AG_AT_NONE;
108 }
109 /* else if arg is '?' and it is last character */
110 else if (p_arg[0] == '?' && p_arg[1] == 0) {
111 /* we have a read */
112 arg_type = BTA_AG_AT_READ;
113 }
114 /* else if arg is '=' */
115 else if (p_arg[0] == '=' && p_arg[1] != 0) {
116 if (p_arg[1] == '?' && p_arg[2] == 0) {
117 /* we have a test */
118 arg_type = BTA_AG_AT_TEST;
119 } else {
120 /* we have a set */
121 arg_type = BTA_AG_AT_SET;
122
123 /* skip past '=' */
124 p_arg++;
125 }
126 } else
127 /* else it is freeform argument */
128 {
129 arg_type = BTA_AG_AT_FREE;
130 }
131
132 /* if arguments match command capabilities */
133 if ((arg_type & p_cb->p_at_tbl[idx].arg_type) != 0) {
134 /* if it's a set integer check max, min range */
135 if (arg_type == BTA_AG_AT_SET &&
136 p_cb->p_at_tbl[idx].fmt == BTA_AG_AT_INT) {
137 int_arg = utl_str2int(p_arg);
138 if (int_arg < (int16_t)p_cb->p_at_tbl[idx].min ||
139 int_arg > (int16_t)p_cb->p_at_tbl[idx].max) {
140 /* arg out of range; error */
141 LOG_WARN("arg out of range");
142 (*p_cb->p_err_cback)((tBTA_AG_SCB*)p_cb->p_user, false, nullptr);
143 } else {
144 (*p_cb->p_cmd_cback)((tBTA_AG_SCB*)p_cb->p_user,
145 p_cb->p_at_tbl[idx].command_id, arg_type, p_arg,
146 p_end, int_arg);
147 }
148 } else {
149 (*p_cb->p_cmd_cback)((tBTA_AG_SCB*)p_cb->p_user,
150 p_cb->p_at_tbl[idx].command_id, arg_type, p_arg,
151 p_end, int_arg);
152 }
153 } else {
154 /* else error */
155 LOG_WARN("Incoming arg type 0x%x does not match cmd arg type 0x%x",
156 arg_type, p_cb->p_at_tbl[idx].arg_type);
157 (*p_cb->p_err_cback)((tBTA_AG_SCB*)p_cb->p_user, false, nullptr);
158 }
159 } else {
160 /* else no match call error callback */
161 LOG_WARN("Unmatched command index %d", idx);
162 (*p_cb->p_err_cback)((tBTA_AG_SCB*)p_cb->p_user, true, p_cb->p_cmd_buf);
163 }
164 }
165
166 /******************************************************************************
167 *
168 * Function bta_ag_at_parse
169 *
170 * Description Parse AT commands. This function will take the input
171 * character string and parse it for AT commands according to
172 * the AT command table passed in the control block.
173 *
174 *
175 * Returns void
176 *
177 *****************************************************************************/
bta_ag_at_parse(tBTA_AG_AT_CB * p_cb,char * p_buf,uint16_t len)178 void bta_ag_at_parse(tBTA_AG_AT_CB* p_cb, char* p_buf, uint16_t len) {
179 int i = 0;
180 char* p_save;
181
182 if (p_cb->p_cmd_buf == nullptr) {
183 p_cb->p_cmd_buf = (char*)osi_malloc(p_cb->cmd_max_len);
184 p_cb->cmd_pos = 0;
185 }
186
187 for (i = 0; i < len;) {
188 while (p_cb->cmd_pos < p_cb->cmd_max_len - 1 && i < len) {
189 /* Skip null characters between AT commands. */
190 if ((p_cb->cmd_pos == 0) && (p_buf[i] == 0)) {
191 i++;
192 continue;
193 }
194
195 p_cb->p_cmd_buf[p_cb->cmd_pos] = p_buf[i++];
196 if (p_cb->p_cmd_buf[p_cb->cmd_pos] == '\r' ||
197 p_cb->p_cmd_buf[p_cb->cmd_pos] == '\n') {
198 p_cb->p_cmd_buf[p_cb->cmd_pos] = 0;
199 if ((p_cb->cmd_pos > 2) &&
200 (p_cb->p_cmd_buf[0] == 'A' || p_cb->p_cmd_buf[0] == 'a') &&
201 (p_cb->p_cmd_buf[1] == 'T' || p_cb->p_cmd_buf[1] == 't')) {
202 p_save = p_cb->p_cmd_buf;
203 char* p_end = p_cb->p_cmd_buf + p_cb->cmd_pos;
204 p_cb->p_cmd_buf += 2;
205 bta_ag_process_at(p_cb, p_end);
206 p_cb->p_cmd_buf = p_save;
207 }
208
209 p_cb->cmd_pos = 0;
210
211 } else if (p_cb->p_cmd_buf[p_cb->cmd_pos] == 0x1A ||
212 p_cb->p_cmd_buf[p_cb->cmd_pos] == 0x1B) {
213 p_cb->p_cmd_buf[++p_cb->cmd_pos] = 0;
214 (*p_cb->p_err_cback)((tBTA_AG_SCB*)p_cb->p_user, true, p_cb->p_cmd_buf);
215 p_cb->cmd_pos = 0;
216 } else {
217 ++p_cb->cmd_pos;
218 }
219 }
220
221 if (i < len) p_cb->cmd_pos = 0;
222 }
223 }
224