]>
Commit | Line | Data |
---|---|---|
5be0d74f YM |
1 | /* |
2 | * Espressif Systems Wireless LAN device driver | |
3 | * | |
4 | * Copyright (C) 2015-2021 Espressif Systems (Shanghai) PTE LTD | |
5 | * | |
6 | * This software file (the "File") is distributed by Espressif Systems (Shanghai) | |
7 | * PTE LTD under the terms of the GNU General Public License Version 2, June 1991 | |
8 | * (the "License"). You may use, redistribute and/or modify this File in | |
9 | * accordance with the terms and conditions of the License, a copy of which | |
10 | * is available by writing to the Free Software Foundation, Inc., | |
11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the | |
12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | |
13 | * | |
14 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE | |
16 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about | |
17 | * this warranty disclaimer. | |
18 | */ | |
19 | ||
20 | ||
21 | #include "common.h" | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <time.h> | |
25 | #include "ctrl_api.h" | |
26 | #include "string.h" | |
27 | #include "cmsis_os.h" | |
28 | #include "serial_drv.h" | |
913f9031 | 29 | #include "platform_wrapper.h" |
5be0d74f YM |
30 | |
31 | ||
32 | /***** Please Read *****/ | |
33 | /* Before use : User must enter user configuration parameter in "ctrl_config.h" file */ | |
34 | ||
35 | #define STA_INTERFACE "ethsta0" | |
36 | #define AP_INTERFACE "ethap0" | |
37 | ||
38 | #define WIFI_VENDOR_IE_ELEMENT_ID 0xDD | |
39 | #define OFFSET 4 | |
40 | #define VENDOR_OUI_0 1 | |
41 | #define VENDOR_OUI_1 2 | |
42 | #define VENDOR_OUI_2 3 | |
43 | #define VENDOR_OUI_TYPE 22 | |
44 | ||
45 | ||
46 | #define CTRL_CMD_DEFAULT_REQ() { \ | |
47 | .msg_type = CTRL_REQ, \ | |
48 | .ctrl_resp_cb = NULL, \ | |
49 | .cmd_timeout_sec = DEFAULT_CTRL_RESP_TIMEOUT /*30 sec*/ \ | |
50 | } | |
51 | ||
52 | #define CLEANUP_CTRL_MSG(msg) do { \ | |
53 | if (msg) { \ | |
54 | if (msg->free_buffer_handle) { \ | |
55 | if (msg->free_buffer_func) { \ | |
56 | msg->free_buffer_func(msg->free_buffer_handle); \ | |
57 | msg->free_buffer_handle = NULL; \ | |
58 | } \ | |
59 | } \ | |
60 | free(msg); \ | |
61 | msg = NULL; \ | |
62 | } \ | |
63 | } while(0); | |
64 | ||
4c4ccc7b YM |
65 | #define YES 1 |
66 | #define NO 0 | |
67 | #define MIN_TIMESTAMP_STR_SIZE 30 | |
68 | #define HEARTBEAT_DURATION_SEC 20 | |
5be0d74f YM |
69 | |
70 | ||
71 | typedef struct { | |
72 | int event; | |
73 | ctrl_resp_cb_t fun; | |
74 | } event_callback_table_t; | |
75 | ||
76 | static char * get_timestamp(char *str, uint16_t str_size) | |
77 | { | |
78 | if (str && str_size>=MIN_TIMESTAMP_STR_SIZE) { | |
79 | time_t t = time(NULL); | |
80 | struct tm tm = *localtime(&t); | |
81 | sprintf(str, "%d-%02d-%02d %02d:%02d:%02d > ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); | |
82 | return str; | |
83 | } | |
84 | return NULL; | |
85 | } | |
86 | ||
87 | static int ctrl_app_event_callback(ctrl_cmd_t * app_event) | |
88 | { | |
89 | char ts[MIN_TIMESTAMP_STR_SIZE] = {'\0'}; | |
90 | ||
91 | if (!app_event || (app_event->msg_type != CTRL_EVENT)) { | |
92 | if (app_event) | |
93 | printf("Msg type is not event[%u]\n\r",app_event->msg_type); | |
94 | goto fail_parsing; | |
95 | } | |
96 | ||
97 | if ((app_event->msg_id <= CTRL_EVENT_BASE) || | |
98 | (app_event->msg_id >= CTRL_EVENT_MAX)) { | |
99 | printf("Event Msg ID[%u] is not correct\n\r",app_event->msg_id); | |
100 | goto fail_parsing; | |
101 | } | |
102 | ||
103 | switch(app_event->msg_id) { | |
104 | ||
105 | case CTRL_EVENT_ESP_INIT: { | |
106 | printf("%s App EVENT: ESP INIT\n\r", | |
107 | get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE)); | |
108 | break; | |
109 | } case CTRL_EVENT_HEARTBEAT: { | |
110 | printf("%s App EVENT: Heartbeat event [%lu]\n\r", | |
111 | get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), | |
112 | app_event->u.e_heartbeat.hb_num); | |
113 | break; | |
114 | } case CTRL_EVENT_STATION_DISCONNECT_FROM_AP: { | |
115 | printf("%s App EVENT: Station mode: Disconnect Reason[%u]\n\r", | |
116 | get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), app_event->resp_event_status); | |
117 | break; | |
118 | } case CTRL_EVENT_STATION_DISCONNECT_FROM_ESP_SOFTAP: { | |
119 | char *p = app_event->u.e_sta_disconnected.mac; | |
120 | if (p && strlen(p)) { | |
121 | printf("%s App EVENT: SoftAP mode: Disconnect MAC[%s]\n\r", | |
122 | get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), p); | |
123 | } | |
124 | break; | |
125 | } default: { | |
126 | printf("%s Invalid event[%u] to parse\n\r", | |
127 | get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), app_event->msg_id); | |
128 | break; | |
129 | } | |
130 | } | |
131 | CLEANUP_CTRL_MSG(app_event); | |
132 | return SUCCESS; | |
133 | ||
134 | fail_parsing: | |
135 | CLEANUP_CTRL_MSG(app_event); | |
136 | return FAILURE; | |
137 | } | |
138 | ||
139 | static void process_failed_responses(ctrl_cmd_t *app_msg) | |
140 | { | |
141 | uint8_t request_failed_flag = true; | |
142 | ||
143 | /* Identify general issue, common for all control requests */ | |
144 | switch (app_msg->resp_event_status) { | |
145 | case CTRL_ERR_REQ_IN_PROG: | |
146 | printf("Error reported: Command In progress, Please wait\n\r"); | |
147 | break; | |
148 | case CTRL_ERR_REQUEST_TIMEOUT: | |
149 | printf("Error reported: Response Timeout\n\r"); | |
150 | break; | |
151 | case CTRL_ERR_MEMORY_FAILURE: | |
152 | printf("Error reported: Memory allocation failed\n\r"); | |
153 | break; | |
154 | case CTRL_ERR_UNSUPPORTED_MSG: | |
155 | printf("Error reported: Unsupported control msg\n\r"); | |
156 | break; | |
157 | case CTRL_ERR_INCORRECT_ARG: | |
158 | printf("Error reported: Invalid or out of range parameter values\n\r"); | |
159 | break; | |
160 | case CTRL_ERR_PROTOBUF_ENCODE: | |
161 | printf("Error reported: Protobuf encode failed\n\r"); | |
162 | break; | |
163 | case CTRL_ERR_PROTOBUF_DECODE: | |
164 | printf("Error reported: Protobuf decode failed\n\r"); | |
165 | break; | |
166 | case CTRL_ERR_SET_ASYNC_CB: | |
167 | printf("Error reported: Failed to set aync callback\n\r"); | |
168 | break; | |
169 | case CTRL_ERR_TRANSPORT_SEND: | |
170 | printf("Error reported: Problem while sending data on serial driver\n\r"); | |
171 | break; | |
172 | default: | |
173 | request_failed_flag = false; | |
174 | break; | |
175 | } | |
176 | ||
177 | /* if control request failed, no need to proceed for response checking */ | |
178 | if (request_failed_flag) | |
179 | return; | |
180 | ||
181 | /* Identify control request specific issue */ | |
182 | switch (app_msg->msg_id) { | |
183 | ||
184 | case CTRL_RESP_OTA_END: | |
185 | case CTRL_RESP_OTA_BEGIN: | |
186 | case CTRL_RESP_OTA_WRITE: { | |
187 | /* intentional fallthrough */ | |
188 | printf("OTA procedure failed\n\r"); | |
189 | break; | |
190 | } case CTRL_RESP_CONNECT_AP: { | |
191 | if (app_msg->resp_event_status == CTRL_ERR_NO_AP_FOUND) { | |
192 | printf("SSID: not found/connectable\n\r"); | |
193 | } else if (app_msg->resp_event_status == | |
194 | CTRL_ERR_INVALID_PASSWORD) { | |
195 | printf("Invalid password for SSID\n\r"); | |
196 | } else { | |
197 | printf("Failed to connect with AP\n\r"); | |
198 | } | |
199 | break; | |
200 | } case CTRL_RESP_START_SOFTAP: { | |
201 | printf("Failed to start SoftAP\n\r"); | |
202 | break; | |
203 | } | |
204 | case CTRL_RESP_STOP_SOFTAP: | |
205 | case CTRL_RESP_GET_SOFTAP_CONFIG: { | |
206 | printf("Possibly softap is not running/started\n\r"); | |
207 | break; | |
208 | } default: { | |
209 | printf("Failed Control Response\n\r"); | |
210 | break; | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | ||
216 | int unregister_event_callbacks(void) | |
217 | { | |
218 | int ret = SUCCESS; | |
219 | int evt = 0; | |
220 | for (evt=CTRL_EVENT_BASE+1; evt<CTRL_EVENT_MAX; evt++) { | |
221 | if (CALLBACK_SET_SUCCESS != reset_event_callback(evt) ) { | |
222 | printf("reset event callback failed for event[%u]\n\r", evt); | |
223 | ret = FAILURE; | |
224 | } | |
225 | } | |
226 | return ret; | |
227 | } | |
228 | ||
229 | int register_event_callbacks(void) | |
230 | { | |
231 | int ret = SUCCESS; | |
232 | int evt = 0; | |
233 | ||
234 | event_callback_table_t events[] = { | |
235 | { CTRL_EVENT_ESP_INIT, ctrl_app_event_callback }, | |
236 | { CTRL_EVENT_HEARTBEAT, ctrl_app_event_callback }, | |
237 | { CTRL_EVENT_STATION_DISCONNECT_FROM_AP, ctrl_app_event_callback }, | |
238 | { CTRL_EVENT_STATION_DISCONNECT_FROM_ESP_SOFTAP, ctrl_app_event_callback }, | |
239 | }; | |
240 | ||
241 | for (evt=0; evt<sizeof(events)/sizeof(event_callback_table_t); evt++) { | |
242 | if (CALLBACK_SET_SUCCESS != set_event_callback(events[evt].event, events[evt].fun) ) { | |
243 | printf("event callback register failed for event[%u]\n\r", events[evt].event); | |
244 | ret = FAILURE; | |
245 | break; | |
246 | } | |
247 | } | |
248 | return ret; | |
249 | } | |
250 | ||
251 | ||
252 | int ctrl_app_resp_callback(ctrl_cmd_t * app_resp) | |
253 | { | |
254 | uint16_t i = 0; | |
255 | if (!app_resp || (app_resp->msg_type != CTRL_RESP)) { | |
256 | if (app_resp) | |
257 | printf("Msg type is not response[%u]\n\r",app_resp->msg_type); | |
258 | goto fail_resp; | |
259 | } | |
260 | ||
261 | if ((app_resp->msg_id <= CTRL_RESP_BASE) || (app_resp->msg_id >= CTRL_RESP_MAX)) { | |
262 | printf("Response Msg ID[%u] is not correct\n\r",app_resp->msg_id); | |
263 | goto fail_resp; | |
264 | } | |
265 | ||
266 | if (app_resp->resp_event_status != SUCCESS) { | |
267 | process_failed_responses(app_resp); | |
268 | goto fail_resp; | |
269 | } | |
270 | ||
271 | switch(app_resp->msg_id) { | |
272 | ||
273 | case CTRL_RESP_GET_MAC_ADDR: { | |
274 | printf("mac address is %s\n\r", app_resp->u.wifi_mac.mac); | |
275 | break; | |
276 | } case CTRL_RESP_SET_MAC_ADDRESS : { | |
277 | printf("MAC address is set\n\r"); | |
278 | break; | |
279 | } case CTRL_RESP_GET_WIFI_MODE : { | |
280 | printf("wifi mode is : "); | |
281 | switch (app_resp->u.wifi_mode.mode) { | |
282 | case WIFI_MODE_STA: printf("station\n\r"); break; | |
283 | case WIFI_MODE_AP: printf("softap\n\r"); break; | |
284 | case WIFI_MODE_APSTA: printf("station+softap\n\r"); break; | |
4c4ccc7b | 285 | case WIFI_MODE_NONE: printf("none\n\r"); break; |
5be0d74f YM |
286 | default: printf("unknown\n\r"); break; |
287 | } | |
288 | break; | |
289 | } case CTRL_RESP_SET_WIFI_MODE : { | |
290 | printf("wifi mode is set\n\r"); | |
291 | break; | |
292 | } case CTRL_RESP_GET_AP_SCAN_LIST : { | |
293 | wifi_ap_scan_list_t * w_scan_p = &app_resp->u.wifi_ap_scan; | |
294 | wifi_scanlist_t *list = w_scan_p->out_list; | |
295 | ||
296 | if (!w_scan_p->count) { | |
297 | printf("No AP found\n\r"); | |
298 | goto finish_resp; | |
299 | } | |
300 | if (!list) { | |
301 | printf("Failed to get scanned AP list\n\r"); | |
302 | goto fail_resp; | |
303 | } else { | |
304 | ||
305 | printf("Number of available APs is %d\n\r", w_scan_p->count); | |
306 | for (i=0; i<w_scan_p->count; i++) { | |
307 | printf("%d) ssid \"%s\" bssid \"%s\" rssi \"%d\" channel \"%d\" auth mode \"%d\" \n\r",\ | |
308 | i, list[i].ssid, list[i].bssid, list[i].rssi, | |
309 | list[i].channel, list[i].encryption_mode); | |
310 | } | |
913f9031 | 311 | msleep(1); |
5be0d74f YM |
312 | } |
313 | break; | |
314 | } case CTRL_RESP_GET_AP_CONFIG : { | |
315 | wifi_ap_config_t *p = &app_resp->u.wifi_ap_config; | |
316 | if (0 == strncmp(SUCCESS_STR, p->status, strlen(SUCCESS_STR))) { | |
317 | printf("AP's ssid '%s'\n\r", p->ssid); | |
318 | printf("AP's bssid i.e. MAC address %s\n\r", p->bssid); | |
319 | printf("AP's channel number %d\n\r", p->channel); | |
320 | printf("AP's rssi %d\n\r", p->rssi); | |
321 | printf("AP's encryption mode %d\n\r", p->encryption_mode); | |
322 | } else { | |
323 | printf("Station mode status: %s\n\r",p->status); | |
324 | } | |
325 | break; | |
326 | } case CTRL_RESP_CONNECT_AP : { | |
327 | printf("Connected\n\r"); | |
328 | break; | |
329 | } case CTRL_RESP_DISCONNECT_AP : { | |
330 | printf("Disconnected from AP\n\r"); | |
331 | break; | |
332 | } case CTRL_RESP_GET_SOFTAP_CONFIG : { | |
333 | softap_config_t * resp_p = &app_resp->u.wifi_softap_config; | |
334 | ||
335 | printf("softAP ssid %s\n\r", resp_p->ssid); | |
336 | printf("softAP pwd %s\n\r", resp_p->pwd); | |
337 | printf("softAP channel ID %d\n\r", resp_p->channel); | |
338 | printf("softAP encryption mode %d\n\r", resp_p->encryption_mode); | |
339 | printf("softAP max connections %d\n\r", resp_p->max_connections); | |
340 | printf("softAP ssid broadcast status %d \n", resp_p->ssid_hidden); | |
341 | printf("softAP bandwidth mode %d\n\r", resp_p->bandwidth); | |
342 | ||
343 | break; | |
344 | } case CTRL_RESP_SET_SOFTAP_VND_IE : { | |
345 | printf("Success in set vendor specific ie\n\r"); | |
346 | break; | |
347 | } case CTRL_RESP_START_SOFTAP : { | |
348 | printf("esp32 softAP started\n\r"); | |
349 | break; | |
350 | } case CTRL_RESP_GET_SOFTAP_CONN_STA_LIST : { | |
351 | int count = app_resp->u.wifi_softap_con_sta.count; | |
352 | wifi_connected_stations_list_t *stations_list = | |
353 | app_resp->u.wifi_softap_con_sta.out_list; | |
354 | ||
355 | printf("sta list count: %u\n\r",count); | |
356 | if (!count) { | |
357 | printf("No station found\n\r"); | |
358 | goto fail_resp; | |
359 | } | |
360 | ||
361 | if (!stations_list) { | |
362 | printf("Failed to get connected stations list\n\r"); | |
363 | } else if (count) { | |
364 | for (i=0; i<count; i++) { | |
365 | printf("%d th stations's bssid \"%s\" rssi \"%d\" \n\r",i, \ | |
366 | stations_list[i].bssid, stations_list[i].rssi); | |
367 | } | |
368 | } | |
369 | break; | |
370 | } case CTRL_RESP_STOP_SOFTAP : { | |
371 | printf("esp32 softAP stopped\n\r"); | |
372 | break; | |
373 | } case CTRL_RESP_SET_PS_MODE : { | |
374 | printf("Wifi power save mode set\n\r"); | |
375 | break; | |
376 | } case CTRL_RESP_GET_PS_MODE : { | |
377 | printf("Wifi power save mode is: "); | |
378 | ||
379 | switch(app_resp->u.wifi_ps.ps_mode) { | |
380 | case WIFI_PS_MIN_MODEM: | |
381 | printf("Min\n\r"); | |
382 | break; | |
383 | case WIFI_PS_MAX_MODEM: | |
384 | printf("Max\n\r"); | |
385 | break; | |
386 | default: | |
387 | printf("Invalid\n\r"); | |
388 | break; | |
389 | } | |
390 | break; | |
391 | } case CTRL_RESP_OTA_BEGIN : { | |
392 | printf("OTA begin success\n\r"); | |
393 | break; | |
394 | } case CTRL_RESP_OTA_WRITE : { | |
395 | printf("OTA write success\n\r"); | |
396 | break; | |
397 | } case CTRL_RESP_OTA_END : { | |
398 | printf("OTA end success\n\r"); | |
399 | break; | |
400 | } case CTRL_RESP_SET_WIFI_MAX_TX_POWER: { | |
401 | printf("Set wifi max tx power success\n\r"); | |
402 | break; | |
403 | } case CTRL_RESP_GET_WIFI_CURR_TX_POWER: { | |
404 | printf("wifi curr tx power : %d\n\r", | |
405 | app_resp->u.wifi_tx_power.power); | |
406 | break; | |
407 | } case CTRL_RESP_CONFIG_HEARTBEAT: { | |
408 | printf("Heartbeat operation successful\n\r"); | |
409 | break; | |
410 | } default: { | |
411 | printf("Invalid Response[%u] to parse\n\r", app_resp->msg_id); | |
412 | break; | |
413 | } | |
414 | } | |
415 | ||
416 | finish_resp: | |
417 | CLEANUP_CTRL_MSG(app_resp); | |
418 | return SUCCESS; | |
419 | ||
420 | fail_resp: | |
421 | CLEANUP_CTRL_MSG(app_resp); | |
422 | return FAILURE; | |
423 | } | |
424 | ||
425 | int test_get_wifi_mode(void) | |
426 | { | |
427 | /* implemented Asynchronous */ | |
428 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
429 | ||
430 | /* register callback for reply */ | |
431 | req.ctrl_resp_cb = ctrl_app_resp_callback; | |
432 | ||
433 | wifi_get_mode(req); | |
434 | ||
435 | return SUCCESS; | |
436 | } | |
437 | ||
438 | ||
439 | int test_set_wifi_mode(int mode) | |
440 | { | |
441 | /* implemented synchronous */ | |
442 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
443 | ctrl_cmd_t *resp = NULL; | |
444 | ||
5be0d74f YM |
445 | req.u.wifi_mode.mode = mode; |
446 | resp = wifi_set_mode(req); | |
447 | ||
448 | return ctrl_app_resp_callback(resp); | |
449 | } | |
450 | ||
451 | int test_set_wifi_mode_station(void) | |
452 | { | |
453 | return test_set_wifi_mode(WIFI_MODE_STA); | |
454 | } | |
455 | ||
456 | int test_set_wifi_mode_softap(void) | |
457 | { | |
458 | return test_set_wifi_mode(WIFI_MODE_AP); | |
459 | } | |
460 | ||
461 | int test_set_wifi_mode_station_softap(void) | |
462 | { | |
463 | return test_set_wifi_mode(WIFI_MODE_APSTA); | |
464 | } | |
465 | ||
466 | int test_set_wifi_mode_none(void) | |
467 | { | |
468 | return test_set_wifi_mode(WIFI_MODE_NONE); | |
469 | } | |
470 | ||
471 | int test_get_wifi_mac_addr(int mode, char *out_mac) | |
472 | { | |
473 | int ret = SUCCESS; | |
474 | ctrl_cmd_t *resp = NULL; | |
475 | ||
476 | if (!out_mac) | |
477 | return FAILURE; | |
478 | ||
479 | out_mac[0]='\0'; | |
480 | ||
481 | /* implemented synchronous */ | |
482 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
483 | ||
484 | req.u.wifi_mac.mode = mode; | |
485 | resp = wifi_get_mac(req); | |
486 | ||
487 | ret = ctrl_app_resp_callback(resp); | |
488 | if (ret) { | |
489 | printf("Failed response for mac\n\r"); | |
490 | return FAILURE; | |
491 | } else { | |
492 | if(!resp->u.wifi_mac.mac) { | |
4c4ccc7b | 493 | printf("NULL MAC returned\n\r"); |
5be0d74f YM |
494 | return FAILURE; |
495 | } | |
496 | strncpy(out_mac, resp->u.wifi_mac.mac, MAX_MAC_STR_LEN); | |
497 | out_mac[MAX_MAC_STR_LEN-1] = '\0'; | |
498 | } | |
499 | return SUCCESS; | |
500 | } | |
501 | ||
502 | int test_station_mode_get_mac_addr(char *mac) | |
503 | { | |
504 | return test_get_wifi_mac_addr(WIFI_MODE_STA, mac); | |
505 | } | |
506 | ||
507 | int test_set_mac_addr(int mode, char *mac) | |
508 | { | |
509 | /* implemented synchronous */ | |
510 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
511 | ctrl_cmd_t *resp = NULL; | |
512 | ||
513 | int ret = test_set_wifi_mode(mode); | |
514 | if (ret == SUCCESS) { | |
515 | req.u.wifi_mac.mode = mode; | |
516 | strncpy(req.u.wifi_mac.mac, mac, MAX_MAC_STR_LEN); | |
517 | req.u.wifi_mac.mac[MAX_MAC_STR_LEN-1] = '\0'; | |
518 | ||
519 | resp = wifi_set_mac(req); | |
520 | return ctrl_app_resp_callback(resp); | |
521 | } | |
522 | return ret; | |
523 | } | |
524 | ||
525 | ||
526 | int test_softap_mode_get_mac_addr(char *mac) | |
527 | { | |
528 | return test_get_wifi_mac_addr(WIFI_MODE_AP, mac); | |
529 | } | |
530 | ||
531 | int test_async_station_mode_connect(char *ssid, char *pwd, char *bssid, | |
532 | int is_wpa3_supported, int listen_interval) | |
533 | { | |
534 | /* implemented Asynchronous */ | |
535 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
536 | ||
537 | strcpy((char *)&req.u.wifi_ap_config.ssid, ssid); | |
538 | strcpy((char *)&req.u.wifi_ap_config.pwd, pwd); | |
539 | strcpy((char *)&req.u.wifi_ap_config.bssid, bssid); | |
540 | req.u.wifi_ap_config.is_wpa3_supported = is_wpa3_supported; | |
541 | req.u.wifi_ap_config.listen_interval = listen_interval; | |
542 | ||
543 | /* register callback for handling reply asynch-ly */ | |
544 | req.ctrl_resp_cb = ctrl_app_resp_callback; | |
545 | ||
546 | wifi_connect_ap(req); | |
547 | ||
548 | return SUCCESS; | |
549 | } | |
550 | ||
551 | int test_station_mode_connect(char *ssid, char *pwd, char *bssid, | |
552 | int is_wpa3_supported, int listen_interval) | |
553 | { | |
554 | /* implemented Asynchronous */ | |
555 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
556 | ctrl_cmd_t *resp = NULL; | |
557 | ||
558 | strcpy((char *)&req.u.wifi_ap_config.ssid, ssid); | |
559 | strcpy((char *)&req.u.wifi_ap_config.pwd, pwd); | |
560 | strcpy((char *)&req.u.wifi_ap_config.bssid, bssid); | |
561 | req.u.wifi_ap_config.is_wpa3_supported = is_wpa3_supported; | |
562 | req.u.wifi_ap_config.listen_interval = listen_interval; | |
563 | ||
564 | resp = wifi_connect_ap(req); | |
565 | ||
566 | return ctrl_app_resp_callback(resp); | |
567 | } | |
568 | ||
569 | int test_station_mode_get_info(void) | |
570 | { | |
571 | /* implemented synchronous */ | |
572 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
573 | ctrl_cmd_t *resp = NULL; | |
574 | ||
575 | resp = wifi_get_ap_config(req); | |
576 | ||
577 | return ctrl_app_resp_callback(resp); | |
578 | } | |
579 | ||
580 | int test_get_available_wifi(void) | |
581 | { | |
582 | /* implemented synchronous */ | |
583 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
913f9031 YM |
584 | req.cmd_timeout_sec = 300; |
585 | ||
5be0d74f YM |
586 | ctrl_cmd_t *resp = NULL; |
587 | ||
588 | resp = wifi_ap_scan_list(req); | |
589 | ||
590 | return ctrl_app_resp_callback(resp); | |
591 | } | |
592 | ||
593 | int test_station_mode_disconnect(void) | |
594 | { | |
595 | /* implemented synchronous */ | |
596 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
597 | ctrl_cmd_t *resp = NULL; | |
598 | ||
599 | resp = wifi_disconnect_ap(req); | |
600 | ||
601 | return ctrl_app_resp_callback(resp); | |
602 | } | |
603 | ||
604 | int test_softap_mode_start(char *ssid, char *pwd, int channel, | |
605 | int encryption_mode, int max_conn, int ssid_hidden, int bw) | |
606 | { | |
607 | /* implemented synchronous */ | |
608 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
609 | ctrl_cmd_t *resp = NULL; | |
610 | ||
611 | strncpy((char *)&req.u.wifi_softap_config.ssid, | |
612 | ssid, MAX_MAC_STR_LEN-1); | |
613 | strncpy((char *)&req.u.wifi_softap_config.pwd, | |
614 | pwd, MAX_MAC_STR_LEN-1); | |
615 | req.u.wifi_softap_config.channel = channel; | |
616 | req.u.wifi_softap_config.encryption_mode = encryption_mode; | |
617 | req.u.wifi_softap_config.max_connections = max_conn; | |
618 | req.u.wifi_softap_config.ssid_hidden = ssid_hidden; | |
619 | req.u.wifi_softap_config.bandwidth = bw; | |
620 | ||
621 | resp = wifi_start_softap(req); | |
622 | ||
623 | return ctrl_app_resp_callback(resp); | |
624 | } | |
625 | ||
626 | int test_softap_mode_get_info(void) | |
627 | { | |
628 | /* implemented synchronous */ | |
629 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
630 | ctrl_cmd_t *resp = NULL; | |
631 | ||
632 | resp = wifi_get_softap_config(req); | |
633 | ||
634 | return ctrl_app_resp_callback(resp); | |
635 | } | |
636 | ||
637 | int test_softap_mode_connected_clients_info(void) | |
638 | { | |
639 | /* implemented synchronous */ | |
640 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
641 | ctrl_cmd_t *resp = NULL; | |
642 | ||
643 | resp = wifi_get_softap_connected_station_list(req); | |
644 | ||
645 | return ctrl_app_resp_callback(resp); | |
646 | } | |
647 | ||
648 | int test_softap_mode_stop(void) | |
649 | { | |
650 | /* implemented synchronous */ | |
651 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
652 | ctrl_cmd_t *resp = NULL; | |
653 | ||
654 | resp = wifi_stop_softap(req); | |
655 | ||
656 | return ctrl_app_resp_callback(resp); | |
657 | } | |
658 | ||
659 | int test_set_wifi_power_save_mode(int psmode) | |
660 | { | |
661 | /* implemented synchronous */ | |
662 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
663 | ctrl_cmd_t *resp = NULL; | |
664 | ||
665 | req.u.wifi_ps.ps_mode = psmode; | |
666 | resp = wifi_set_power_save_mode(req); | |
667 | ||
668 | return ctrl_app_resp_callback(resp); | |
669 | } | |
670 | ||
671 | int test_set_wifi_power_save_mode_max(void) | |
672 | { | |
673 | return test_set_wifi_power_save_mode(WIFI_PS_MAX_MODEM); | |
674 | } | |
675 | ||
676 | int test_set_wifi_power_save_mode_min(void) | |
677 | { | |
678 | return test_set_wifi_power_save_mode(WIFI_PS_MIN_MODEM); | |
679 | } | |
680 | ||
681 | int test_get_wifi_power_save_mode(void) | |
682 | { | |
683 | /* implemented synchronous */ | |
684 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
685 | ctrl_cmd_t *resp = NULL; | |
686 | ||
687 | resp = wifi_get_power_save_mode(req); | |
688 | ||
689 | return ctrl_app_resp_callback(resp); | |
690 | } | |
691 | ||
692 | int test_reset_vendor_specific_ie(void) | |
693 | { | |
694 | /* implemented synchronous */ | |
695 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
696 | ctrl_cmd_t *resp = NULL; | |
697 | char *data = "Example vendor IE data"; | |
698 | ||
699 | char *v_data = (char*)calloc(1, strlen(data)); | |
700 | if (!v_data) { | |
701 | printf("Failed to allocate memory \n"); | |
702 | return FAILURE; | |
703 | } | |
704 | memcpy(v_data, data, strlen(data)); | |
705 | ||
706 | req.u.wifi_softap_vendor_ie.enable = false; | |
707 | req.u.wifi_softap_vendor_ie.type = WIFI_VND_IE_TYPE_BEACON; | |
708 | req.u.wifi_softap_vendor_ie.idx = WIFI_VND_IE_ID_0; | |
709 | req.u.wifi_softap_vendor_ie.vnd_ie.element_id = WIFI_VENDOR_IE_ELEMENT_ID; | |
710 | req.u.wifi_softap_vendor_ie.vnd_ie.length = strlen(data)+OFFSET; | |
711 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[0] = VENDOR_OUI_0; | |
712 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[1] = VENDOR_OUI_1; | |
713 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[2] = VENDOR_OUI_2; | |
714 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui_type = VENDOR_OUI_TYPE; | |
715 | req.u.wifi_softap_vendor_ie.vnd_ie.payload = (uint8_t *)v_data; | |
716 | req.u.wifi_softap_vendor_ie.vnd_ie.payload_len = strlen(data); | |
717 | ||
718 | req.free_buffer_func = free; | |
719 | req.free_buffer_handle = v_data; | |
720 | ||
721 | resp = wifi_set_vendor_specific_ie(req); | |
722 | ||
723 | return ctrl_app_resp_callback(resp); | |
724 | } | |
725 | ||
726 | int test_set_vendor_specific_ie(void) | |
727 | { | |
728 | /* implemented synchronous */ | |
729 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
730 | ctrl_cmd_t *resp = NULL; | |
731 | char *data = "Example vendor IE data"; | |
732 | ||
733 | char *v_data = (char*)calloc(1, strlen(data)); | |
734 | if (!v_data) { | |
735 | printf("Failed to allocate memory \n"); | |
736 | return FAILURE; | |
737 | } | |
738 | memcpy(v_data, data, strlen(data)); | |
739 | ||
740 | req.u.wifi_softap_vendor_ie.enable = true; | |
741 | req.u.wifi_softap_vendor_ie.type = WIFI_VND_IE_TYPE_BEACON; | |
742 | req.u.wifi_softap_vendor_ie.idx = WIFI_VND_IE_ID_0; | |
743 | req.u.wifi_softap_vendor_ie.vnd_ie.element_id = WIFI_VENDOR_IE_ELEMENT_ID; | |
744 | req.u.wifi_softap_vendor_ie.vnd_ie.length = strlen(data)+OFFSET; | |
745 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[0] = VENDOR_OUI_0; | |
746 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[1] = VENDOR_OUI_1; | |
747 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[2] = VENDOR_OUI_2; | |
748 | req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui_type = VENDOR_OUI_TYPE; | |
749 | req.u.wifi_softap_vendor_ie.vnd_ie.payload = (uint8_t *)v_data; | |
750 | req.u.wifi_softap_vendor_ie.vnd_ie.payload_len = strlen(data); | |
751 | ||
752 | req.free_buffer_func = free; | |
753 | req.free_buffer_handle = v_data; | |
754 | ||
755 | resp = wifi_set_vendor_specific_ie(req); | |
756 | ||
757 | return ctrl_app_resp_callback(resp); | |
758 | } | |
759 | ||
760 | int test_ota_begin(void) | |
761 | { | |
762 | /* implemented synchronous */ | |
763 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
764 | ctrl_cmd_t *resp = NULL; | |
765 | ||
766 | resp = ota_begin(req); | |
767 | ||
768 | return ctrl_app_resp_callback(resp); | |
769 | } | |
770 | ||
771 | int test_ota_write(uint8_t* ota_data, uint32_t ota_data_len) | |
772 | { | |
773 | /* implemented synchronous */ | |
774 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
775 | ctrl_cmd_t *resp = NULL; | |
776 | ||
777 | req.u.ota_write.ota_data = ota_data; | |
778 | req.u.ota_write.ota_data_len = ota_data_len; | |
779 | ||
780 | resp = ota_write(req); | |
781 | ||
782 | return ctrl_app_resp_callback(resp); | |
783 | } | |
784 | ||
785 | int test_ota_end(void) | |
786 | { | |
787 | /* implemented synchronous */ | |
788 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
789 | ctrl_cmd_t *resp = NULL; | |
790 | ||
791 | resp = ota_end(req); | |
792 | ||
793 | return ctrl_app_resp_callback(resp); | |
794 | } | |
795 | ||
796 | int test_ota(char* image_path) | |
797 | { | |
798 | #if 0 | |
799 | FILE* f = NULL; | |
800 | char ota_chunk[CHUNK_SIZE] = {0}; | |
801 | int ret = test_ota_begin(); | |
802 | if (ret == SUCCESS) { | |
803 | f = fopen(image_path,"rb"); | |
804 | if (f == NULL) { | |
805 | printf("Failed to open file %s \n", image_path); | |
806 | return FAILURE; | |
807 | } else { | |
808 | printf("Success in opening %s file \n", image_path); | |
809 | } | |
810 | while (!feof(f)) { | |
811 | fread(&ota_chunk, CHUNK_SIZE, 1, f); | |
812 | ret = test_ota_write((uint8_t* )&ota_chunk, CHUNK_SIZE); | |
813 | if (ret) { | |
814 | printf("OTA procedure failed!!\n"); | |
815 | /* TODO: Do we need to do OTA end irrespective of success/failure? */ | |
816 | test_ota_end(); | |
817 | return FAILURE; | |
818 | } | |
819 | } | |
820 | ret = test_ota_end(); | |
821 | if (ret) { | |
822 | return FAILURE; | |
823 | } | |
824 | } else { | |
825 | return FAILURE; | |
826 | } | |
827 | printf("ESP32 will restart after 5 sec\n"); | |
828 | return SUCCESS; | |
829 | #endif | |
830 | printf("For OTA, user need to integrate HTTP client lib and then invoke OTA\n\r"); | |
831 | return FAILURE; | |
832 | } | |
833 | ||
834 | int test_wifi_set_max_tx_power(int in_power) | |
835 | { | |
836 | /* implemented synchronous */ | |
837 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
838 | ctrl_cmd_t *resp = NULL; | |
839 | ||
840 | req.u.wifi_tx_power.power = in_power; | |
841 | resp = wifi_set_max_tx_power(req); | |
842 | ||
843 | return ctrl_app_resp_callback(resp); | |
844 | } | |
845 | ||
846 | int test_wifi_get_curr_tx_power() | |
847 | { | |
848 | /* implemented synchronous */ | |
849 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
850 | ctrl_cmd_t *resp = NULL; | |
851 | ||
852 | resp = wifi_get_curr_tx_power(req); | |
853 | ||
854 | return ctrl_app_resp_callback(resp); | |
855 | } | |
856 | ||
857 | int test_config_heartbeat(void) | |
858 | { | |
859 | /* implemented synchronous */ | |
860 | ctrl_cmd_t *resp = NULL; | |
861 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
862 | req.u.e_heartbeat.enable = YES; | |
863 | req.u.e_heartbeat.duration = HEARTBEAT_DURATION_SEC; | |
864 | ||
865 | resp = config_heartbeat(req); | |
866 | ||
867 | return ctrl_app_resp_callback(resp); | |
868 | } | |
869 | ||
870 | int test_disable_heartbeat(void) | |
871 | { | |
872 | /* implemented synchronous */ | |
873 | ctrl_cmd_t *resp = NULL; | |
874 | ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ(); | |
875 | req.u.e_heartbeat.enable = NO; | |
876 | ||
877 | resp = config_heartbeat(req); | |
878 | ||
879 | return ctrl_app_resp_callback(resp); | |
880 | } |