1 /*
2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /**
17 ****************************************************************************************
18 *
19 * @file (atcmdpuls_ble.h)
20 *
21 * @brief
22 *
23 ****************************************************************************************
24 */
25
26 /*
27 * INCLUDE FILES
28 ****************************************************************************************
29 */
30 #include "atcmdplus_ble.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <string.h>
35 #include "lega_at_api.h"
36 #include "app.h"
37 #include "sonata_gap.h"
38 #include "sonata_gatt_api.h"
39
40 /*
41 * MACRO DEFINES
42 ****************************************************************************************
43 */
44 #define PARA_ID_0 (0)
45 #define PARA_ID_1 (1)
46 #define PARA_ID_2 (2)
47 #define PARA_ID_3 (3)
48 #define PARA_ID_4 (4)
49
50 /*
51 * VARIABLE DECLARATIONS
52 ****************************************************************************************
53 */
54
char2HexValue(char ch)55 uint8_t char2HexValue(char ch)
56 {
57 uint8_t result = 0;
58
59 if (ch >= '0' && ch <= '9') {
60 result = ch - '0';
61 } else if (ch >= 'a' && ch <= 'z') {
62 result = (ch - 'a') + 10;
63 } else if (ch >= 'A' && ch <= 'Z') {
64 result = (ch - 'A') + 10;
65 } else {
66 result = -1;
67 }
68 return result;
69 }
70
hexValue2Char(uint8_t value,uint8_t * chars)71 void hexValue2Char(uint8_t value, uint8_t *chars)
72 {
73 uint8_t d1 = value & 0xF;
74 uint8_t d2 = value >> 4;
75 chars[0] = d2 > 9 ? 'A' + d2 - 10 : '0' + d2;
76 chars[1] = d1 > 9 ? 'A' + d1 - 10 : '0' + d1;
77 }
macChar2Value(uint8_t * chars,uint8_t * mac,bool colon)78 void macChar2Value(uint8_t *chars, uint8_t *mac, bool colon)
79 {
80 uint8_t d0, d1, d2, d3, d4, d5;
81 if (colon) {
82 d0 = char2HexValue(chars[0]) * 16 + char2HexValue(chars[1]);
83 d1 = char2HexValue(chars[3]) * 16 + char2HexValue(chars[4]);
84 d2 = char2HexValue(chars[6]) * 16 + char2HexValue(chars[7]);
85 d3 = char2HexValue(chars[9]) * 16 + char2HexValue(chars[10]);
86 d4 = char2HexValue(chars[12]) * 16 + char2HexValue(chars[13]);
87 d5 = char2HexValue(chars[15]) * 16 + char2HexValue(chars[16]);
88 mac[0] = d5; // MAC in stack should reverse with Real MAC
89 mac[1] = d4;
90 mac[2] = d3;
91 mac[3] = d2;
92 mac[4] = d1;
93 mac[5] = d0;
94 } else {
95 d0 = char2HexValue(chars[0]) * 16 + char2HexValue(chars[1]);
96 d1 = char2HexValue(chars[2]) * 16 + char2HexValue(chars[3]);
97 d2 = char2HexValue(chars[4]) * 16 + char2HexValue(chars[5]);
98 d3 = char2HexValue(chars[6]) * 16 + char2HexValue(chars[7]);
99 d4 = char2HexValue(chars[8]) * 16 + char2HexValue(chars[9]);
100 d5 = char2HexValue(chars[10]) * 16 + char2HexValue(chars[11]);
101 mac[0] = d5; // MAC in stack should reverse with Real MAC
102 mac[1] = d4;
103 mac[2] = d3;
104 mac[3] = d2;
105 mac[4] = d1;
106 mac[5] = d0;
107 }
108 }
109
macValue2Char(uint8_t * mac,uint8_t * chars,bool colon)110 uint8_t macValue2Char(uint8_t *mac, uint8_t *chars, bool colon)
111 {
112 if (colon) {
113 hexValue2Char(mac[5], &chars[0]);
114 chars[2] = ':';
115 hexValue2Char(mac[4], &chars[3]);
116 chars[5] = ':';
117 hexValue2Char(mac[3], &chars[6]);
118 chars[8] = ':';
119 hexValue2Char(mac[2], &chars[9]);
120 chars[11] = ':';
121 hexValue2Char(mac[1], &chars[12]);
122 chars[14] = ':';
123 hexValue2Char(mac[0], &chars[15]);
124 return 17;
125 } else {
126 hexValue2Char(mac[5], &chars[0]);
127 hexValue2Char(mac[4], &chars[2]);
128 hexValue2Char(mac[3], &chars[4]);
129 hexValue2Char(mac[2], &chars[6]);
130 hexValue2Char(mac[1], &chars[8]);
131 hexValue2Char(mac[0], &chars[10]);
132 return 12;
133 }
134 }
135
apcmdplue_print_command(int argc,char ** argv)136 void apcmdplue_print_command(int argc, char **argv)
137 {
138 APP_TRC("-------------------[%d]\r\n", argc);
139 if (argc > 0) {
140 APP_TRC(" P0:%s\r\n", argv[PARA_ID_0]);
141 }
142 if (argc > 1) {
143 APP_TRC(" P1:%s\r\n", argv[PARA_ID_1]);
144 }
145 if (argc > 2) {
146 APP_TRC(" P2:%s\r\n", argv[PARA_ID_2]);
147 }
148 if (argc > 3) {
149 APP_TRC(" P3:%s\r\n", argv[PARA_ID_3]);
150 }
151 if (argc > 4) {
152 APP_TRC(" P4:%s\r\n", argv[PARA_ID_4]);
153 }
154 APP_TRC("-------------------\r\n");
155
156 }
157
atcmdplus_adv(int argc,char ** argv)158 int atcmdplus_adv(int argc, char **argv)
159 {
160 apcmdplue_print_command(argc, argv);
161 if (strcmp(argv[PARA_ID_1], "1") == 0) {
162 app_ble_config_legacy_advertising();
163 } else if (strcmp(argv[PARA_ID_1], "0") == 0) {
164 app_ble_advertising_stop(0);
165 }
166 return CONFIG_OK;
167 }
168
atcmdplus_scan(int argc,char ** argv)169 int atcmdplus_scan(int argc, char **argv)
170 {
171 apcmdplue_print_command(argc, argv);
172 if (strcmp(argv[PARA_ID_1], "1") == 0) {
173 app_ble_config_scanning();
174 } else if (strcmp(argv[PARA_ID_1], "0") == 0) {
175 app_ble_stop_scanning();
176 }
177 return CONFIG_OK;
178 }
179
atcmdplus_conn(int argc,char ** argv)180 int atcmdplus_conn(int argc, char **argv)
181 {
182 apcmdplue_print_command(argc, argv);
183 if (argc != 2) {
184 return PARAM_RANGE;
185 }
186
187 uint8_t targetAddr[SONATA_GAP_BD_ADDR_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
188 macChar2Value((uint8_t *)argv[PARA_ID_1], targetAddr, false);
189
190 app_ble_set_target_address(targetAddr);
191 app_ble_config_initiating();
192 return CONFIG_OK;
193 }
194
atcmdplus_uuid(int argc,char ** argv)195 int atcmdplus_uuid(int argc, char **argv)
196 {
197 apcmdplue_print_command(argc, argv);
198 app_uuids uuids;
199 uint16_t d1 = char2HexValue(argv[PARA_ID_1][0]) * 16 + char2HexValue(argv[PARA_ID_1][1]);
200 uint16_t d0 = char2HexValue(argv[PARA_ID_1][2]) * 16 + char2HexValue(argv[PARA_ID_1][3]);
201 uuids.service = ((d1 * 0X100) + d0);
202
203 d1 = char2HexValue(argv[PARA_ID_2][0]) * 16 + char2HexValue(argv[PARA_ID_2][1]);
204 d0 = char2HexValue(argv[PARA_ID_2][2]) * 16 + char2HexValue(argv[PARA_ID_2][3]);
205 uuids.read = ((d1 * 0X100) + d0);
206
207 d1 = char2HexValue(argv[PARA_ID_3][0]) * 16 + char2HexValue(argv[PARA_ID_3][1]);
208 d0 = char2HexValue(argv[PARA_ID_3][2]) * 16 + char2HexValue(argv[PARA_ID_3][3]);
209 uuids.write = ((d1 * 0X100) + d0);
210
211 d1 = char2HexValue(argv[PARA_ID_4][0]) * 16 + char2HexValue(argv[PARA_ID_4][1]);
212 d0 = char2HexValue(argv[PARA_ID_4][2]) * 16 + char2HexValue(argv[PARA_ID_4][3]);
213 uuids.ntf = ((d1 * 0X100) + d0);
214 app_ble_set_uuids(uuids.service, uuids.read, uuids.write, uuids.ntf);
215 return CONFIG_OK;
216 }
217
atcmdplus_discovery(int argc,char ** argv)218 int atcmdplus_discovery(int argc, char **argv)
219 {
220 uint8_t conidx = char2HexValue(argv[PARA_ID_1][0]);
221 sonata_ble_gatt_disc_all_svc(conidx); // --->app_gatt_disc_svc_callback()
222 return CONFIG_OK;
223 }
224
atcmdplus_lesend(int argc,char ** argv)225 int atcmdplus_lesend(int argc, char **argv)
226 {
227 apcmdplue_print_command(argc, argv);
228
229 uint8_t conidx = char2HexValue(argv[PARA_ID_1][0]);
230 uint16_t dataLen = char2HexValue(argv[PARA_ID_2][0]); // Todo only support 1 char now.
231 uint8_t *data = (uint8_t *)argv[PARA_ID_3];
232 app_ble_master_write_data(conidx, dataLen, data);
233 return CONFIG_OK;
234 }
235
atcmdplus_ntf(int argc,char ** argv)236 int atcmdplus_ntf(int argc, char **argv)
237 {
238 apcmdplue_print_command(argc, argv);
239 app_uuids *uuids = app_ble_get_uuids();
240 if (uuids->service == 0) {
241 return PARAM_RANGE;
242 }
243 uint8_t idValue = *argv[PARA_ID_1] - '0';
244
245 if (strcmp(argv[PARA_ID_2], "1") == 0) {
246 app_ble_master_turn_ntf(idValue, true);
247 } else if (strcmp(argv[PARA_ID_2], "0") == 0) {
248 app_ble_master_turn_ntf(idValue, false);
249 }
250 return CONFIG_OK;
251 }
252
atcmdplus_ledisc(int argc,char ** argv)253 int atcmdplus_ledisc(int argc, char **argv)
254 {
255 apcmdplue_print_command(argc, argv);
256 uint8_t idValue = *argv[PARA_ID_1] - '0';
257 app_ble_disconnect(idValue);
258 return CONFIG_OK;
259 }
260
atcmdplus_chinfo(int argc,char ** argv)261 int atcmdplus_chinfo(int argc, char **argv)
262 {
263 apcmdplue_print_command(argc, argv);
264 actives *act = app_get_active();
265 uint8_t out[50] = {0};
266 for (int i = 0; i < APP_ACTIVE_MAX; ++i) {
267 if (act[i].runing == true) {
268 uint8_t offset = 0;
269 offset = macValue2Char(act[i].peer, &out[0], true);
270 out[offset] = ',';
271 offset += 1;
272 out[offset] = act[i].assign_id + '0';
273 offset += 1;
274 out[offset] = ',';
275 offset += 1;
276 out[offset] = '3';
277 offset += 1;
278 for (int i = 0; i < offset; ++i) {
279 printf("%c", out[i]);
280 }
281 printf("\r\n");
282 }
283 }
284 return CONFIG_OK;
285 }
286
atcmdplus_test(int argc,char ** argv)287 int atcmdplus_test(int argc, char **argv)
288 {
289 printf("AT CMD PLUS, %s, \r\n", __FUNCTION__);
290 return CONFIG_OK;
291 }
292
293 #if (LOSCFG_USE_SHELL == 1)
294 #include "shell.h"
295 #include "shcmd.h"
296 #include "target_config.h"
297 #endif
298 /*
299 * FUNCTION DECLARATIONS
300 ****************************************************************************************
301 */
302
atcmdplus_ble_register(void)303 void atcmdplus_ble_register(void)
304 {
305 #if (LOSCFG_USE_SHELL == 1)
306 osCmdReg(CMD_TYPE_STD, "le_bletest", 0, (CMD_CBK_FUNC)atcmdplus_test);
307 osCmdReg(CMD_TYPE_STD, "le_adv", 0, (CMD_CBK_FUNC)atcmdplus_adv);
308 osCmdReg(CMD_TYPE_STD, "le_scan", 0, (CMD_CBK_FUNC)atcmdplus_scan);
309 osCmdReg(CMD_TYPE_STD, "le_conn", 0, (CMD_CBK_FUNC)atcmdplus_conn);
310 osCmdReg(CMD_TYPE_STD, "le_uuid", 0, (CMD_CBK_FUNC)atcmdplus_uuid);
311 osCmdReg(CMD_TYPE_STD, "le_discovery", 0, (CMD_CBK_FUNC)atcmdplus_discovery);
312 osCmdReg(CMD_TYPE_STD, "le_lesend", 0, (CMD_CBK_FUNC)atcmdplus_lesend);
313 osCmdReg(CMD_TYPE_STD, "le_ntf", 0, (CMD_CBK_FUNC)atcmdplus_ntf);
314 osCmdReg(CMD_TYPE_STD, "le_ledisc", 0, (CMD_CBK_FUNC)atcmdplus_ledisc);
315 osCmdReg(CMD_TYPE_STD, "le_chinfo", 0, (CMD_CBK_FUNC)atcmdplus_chinfo);
316 #endif
317 }
318
319