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 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19 #include <string.h>
20 #include "at_api.h"
21 #include "app.h"
22 #ifdef ALIOS_SUPPORT
23 #include "aos/cli.h"
24 #endif
25 #include "duet_gpio.h"
26 #include "msm_ble_api.h"
27
string_asc_to_hex(char param)28 static uint8_t string_asc_to_hex(char param)
29 {
30 uint8_t val;
31
32 if ((param >= '0') && (param <= '9')) {
33 val = param - '0';
34 } else if ((param >= 'A') && (param <= 'F')) {
35 val = (uint8_t)(param - 'A') + 0xA;
36 } else if ((param >= 'a') && (param <= 'f')) {
37 val = (uint8_t)(param - 'a') + 0xA;
38 } else {
39 val = 0xA;
40 }
41 return val;
42 }
43
sonata_string_to_array(char * input_str,uint8_t input_length,uint8_t * hex_addr)44 static void sonata_string_to_array(char *input_str, uint8_t input_length, uint8_t *hex_addr)
45 {
46 int i;
47 input_length = (input_length + 1) & 0xFFFE;
48 for (i = 0; i < input_length; i++) {
49 if (i % 2 || i == 1) {
50 hex_addr[i >> 1] = string_asc_to_hex(input_str[i]) | (hex_addr[i >> 1] & 0xF0);
51 } else {
52 hex_addr[i >> 1] = (string_asc_to_hex(input_str[i]) << 4) | (hex_addr[i >> 1] & 0xF);
53 }
54 }
55 }
56
57 duet_gpio_dev_t g_duet_gpio11;
58 duet_gpio_dev_t g_duet_gpio12;
59 duet_gpio_dev_t g_duet_gpio6;
60
lega_ble_adv(char * pwbuf,int blen,int argc,char ** argv)61 void lega_ble_adv(char *pwbuf, int blen, int argc, char **argv)
62 {
63 int c = 1;
64
65 if (strcmp(argv[c], "start") == 0 && argc > 2) {
66
67 printf("adv start!!!!!!!!!!! \r\n");
68 uint8_t hex_value[32];
69 memset(hex_value, 0, sizeof(hex_value));
70 uint16_t hex_length = (strlen(argv[c + 1]) + 1) / 2;
71 sonata_string_to_array(argv[c + 1], strlen(argv[c + 1]), hex_value);
72 duet_gpio_output_toggle(&g_duet_gpio11);
73 duet_gpio_output_toggle(&g_duet_gpio12);
74 duet_gpio_output_toggle(&g_duet_gpio6);
75 #ifdef HARMONYOS_TEMP
76 #else
77 ms_test_ble_adv_start(hex_value, hex_length, NULL, 0);
78 #endif
79 } else if (strcmp(argv[c], "stop") == 0) {
80 app_ble_advertising_stop(0);
81 }
82 }
83
84 #ifdef ALIOS_SUPPORT
85 static struct cli_command adv_cmd = {
86 .name = "adv",
87 .help = "adv start|stop [adv data] [rsp data]",
88 .function = lega_ble_adv,
89 };
90 #endif
91
lega_ble_scan(char * pwbuf,int blen,int argc,char ** argv)92 void lega_ble_scan(char *pwbuf, int blen, int argc, char **argv)
93 {
94 int c = 0;
95
96 for (c = 1; c < argc; c++) {
97 if (strcmp(argv[c], "start") == 0) {
98 sonata_api_app_timer_set(8, 20);
99 sonata_api_app_timer_active(8);
100 } else if (strcmp(argv[c], "stop") == 0) {
101 app_ble_stop_scanning();
102 }
103 }
104 }
105
106 #ifdef ALIOS_SUPPORT
107 static struct cli_command scan_cmd = {
108 .name = "scan",
109 .help = "scan start|stop",
110 .function = lega_ble_scan,
111 };
112 #endif
113
lega_ble_adv_open_close_test(char * pwbuf,int blen,int argc,char ** argv)114 void lega_ble_adv_open_close_test(char *pwbuf, int blen, int argc, char **argv)
115 {
116 int c = 1;
117 if (strcmp(argv[c], "start") == 0) {
118 app_ble_set_test_count(200);
119 sonata_api_app_timer_set(6, 20);
120 sonata_api_app_timer_active(6);
121 } else if (strcmp(argv[c], "stop") == 0) {
122 app_ble_set_test_count(0);
123 }
124 }
125
126 #ifdef ALIOS_SUPPORT
127 static struct cli_command adv_per_cmd = {
128 .name = "adv-per",
129 .help = "adv-per start|stop [adv data] ",
130 .function = lega_ble_adv_open_close_test,
131 };
132 #endif
133
lega_ble_adv_update_test(char * pwbuf,int blen,int argc,char ** argv)134 void lega_ble_adv_update_test(char *pwbuf, int blen, int argc, char **argv)
135 {
136 int c = 1;
137 if (strcmp(argv[c], "start") == 0) {
138 app_ble_set_test_count(200);
139 sonata_api_app_timer_set(7, 20);
140 sonata_api_app_timer_active(7);
141 } else if (strcmp(argv[c], "stop") == 0) {
142 app_ble_set_test_count(0);
143 }
144 }
145
146 #ifdef ALIOS_SUPPORT
147 static struct cli_command update_cmd = {
148 .name = "adv-update",
149 .help = "adv-update start|stop ",
150 .function = lega_ble_adv_update_test,
151 };
152 #endif
153
lega_ble_connect_test(char * pwbuf,int blen,int argc,char ** argv)154 void lega_ble_connect_test(char *pwbuf, int blen, int argc, char **argv)
155 {
156 int c = 1;
157 if (strcmp(argv[c], "start") == 0) {
158 uint8_t hex_value[6];
159 sonata_string_to_array(argv[c + 1], strlen(argv[c + 1]), hex_value);
160 app_ble_set_test_target(hex_value);
161 sonata_api_app_timer_set(3, 20);
162 sonata_api_app_timer_active(3);
163
164 } else {
165 sonata_ble_stop_initiating();
166 }
167 }
168
169 #ifdef ALIOS_SUPPORT
170 static struct cli_command connect_cmd = {
171 .name = "connect",
172 .help = "connect start|stop target_addr",
173 .function = lega_ble_connect_test,
174 };
175 #endif
176
lega_ble_write_test(char * pwbuf,int blen,int argc,char ** argv)177 void lega_ble_write_test(char *pwbuf, int blen, int argc, char **argv)
178 {
179 int c = 1;
180 if (strcmp(argv[c], "start") == 0) {
181 sonata_api_app_timer_set(4, 20);
182 sonata_api_app_timer_active(4);
183 app_ble_set_test_interval(atoi(argv[c + 1]));
184
185 } else {
186 sonata_api_app_timer_clear(4);
187 }
188 }
189
190 #ifdef ALIOS_SUPPORT
191 static struct cli_command write_cmd = {
192 .name = "write",
193 .help = "write start| stop {interval}",
194 .function = lega_ble_write_test,
195 };
196 #endif
197
lega_ble_disconnect_test(char * pwbuf,int blen,int argc,char ** argv)198 void lega_ble_disconnect_test(char *pwbuf, int blen, int argc, char **argv)
199 {
200 int c = 1;
201 if (argc == 2) {
202 app_ble_disconnect(0);
203 }
204 }
205
206 #ifdef ALIOS_SUPPORT
207 static struct cli_command disconnect_cmd = {
208 .name = "disconnect",
209 .help = "disconnect target_addr",
210 .function = lega_ble_disconnect_test,
211 };
212 #endif
213
lega_ble_test_mode(char * pwbuf,int blen,int argc,char ** argv)214 void lega_ble_test_mode(char *pwbuf, int blen, int argc, char **argv)
215 {
216 int c = 1;
217 if (strcmp(argv[c], "on") == 0) {
218 printf("ble test on \r\n");
219 // gpio1 output
220 g_duet_gpio11.port = GPIO11_INDEX;
221 g_duet_gpio11.config = DUET_OUTPUT_PUSH_PULL;
222 g_duet_gpio11.priv = NULL;
223 duet_gpio_init(&g_duet_gpio11);
224 duet_gpio_output_low(&g_duet_gpio11);
225
226 g_duet_gpio6.port = GPIO6_INDEX;
227 g_duet_gpio6.config = DUET_OUTPUT_PUSH_PULL;
228 g_duet_gpio6.priv = NULL;
229 duet_gpio_init(&g_duet_gpio6);
230 duet_gpio_output_low(&g_duet_gpio6);
231
232 g_duet_gpio12.port = GPIO12_INDEX;
233 g_duet_gpio12.config = DUET_OUTPUT_PUSH_PULL;
234 g_duet_gpio12.priv = NULL;
235 duet_gpio_init(&g_duet_gpio12);
236 duet_gpio_output_low(&g_duet_gpio12);
237
238 duet_gpio_output_low(&g_duet_gpio11);
239
240 app_set_ble_test_mode(true);
241 } else {
242 app_set_ble_test_mode(false);
243 }
244 }
245
246 #ifdef ALIOS_SUPPORT
247 static struct cli_command test_cmd = {
248 .name = "ble-test-mode",
249 .help = "ble-test-mode {on|off}",
250 .function = lega_ble_test_mode,
251 };
252 #endif
253
lega_ble_adv_state(char * pwbuf,int blen,int argc,char ** argv)254 void lega_ble_adv_state(char *pwbuf, int blen, int argc, char **argv)
255 {
256 extern void app_print_adv_status(void);
257 app_print_adv_status();
258 sonata_api_app_timer_set(5, 500);
259 sonata_api_app_timer_active(5);
260 }
261
262 #ifdef ALIOS_SUPPORT
263 static struct cli_command adv_state = {
264 .name = "printf-adv-state",
265 .help = "printf-adv-state",
266 .function = lega_ble_adv_state,
267 };
268 #endif
269
lega_ble_set_uuid(char * pwbuf,int blen,int argc,char ** argv)270 void lega_ble_set_uuid(char *pwbuf, int blen, int argc, char **argv)
271 {
272 int c = 1;
273 uint8_t hex_value[6];
274 sonata_string_to_array(argv[c], strlen(argv[c]), hex_value);
275 app_ble_set_test_write_uuid(hex_value);
276 app_ble_set_test_read_uuid(hex_value);
277
278 ble_set_max_mtu(200);
279 }
280
281 #ifdef ALIOS_SUPPORT
282 static struct cli_command set_test_uuid = {
283 .name = "test-uuid",
284 .help = "test-uuid {uuid}",
285 .function = lega_ble_set_uuid,
286 };
287 #endif
288
lega_ble_add_test_service(char * pwbuf,int blen,int argc,char ** argv)289 void lega_ble_add_test_service(char *pwbuf, int blen, int argc, char **argv)
290 {
291 #ifdef HARMONYOS_TEMP
292 #else
293 ms_add_test_service();
294 #endif
295 }
296
297 #ifdef ALIOS_SUPPORT
298 static struct cli_command add_test_service = {
299 .name = "add-test-service",
300 .help = "add-test-service",
301 .function = lega_ble_add_test_service,
302 };
303
lega_ble_at_init(void)304 void lega_ble_at_init(void)
305 {
306 aos_cli_register_command(&scan_cmd);
307 aos_cli_register_command(&adv_cmd);
308 aos_cli_register_command(&adv_per_cmd);
309 aos_cli_register_command(&update_cmd);
310 aos_cli_register_command(&connect_cmd);
311 aos_cli_register_command(&write_cmd);
312 aos_cli_register_command(&disconnect_cmd);
313 aos_cli_register_command(&test_cmd);
314 aos_cli_register_command(&adv_state);
315 aos_cli_register_command(&set_test_uuid);
316 aos_cli_register_command(&add_test_service);
317 }
318 #endif
319