]> Git Repo - esp-hosted.git/blob - host/linux/host_control/c_support/test_utils.c
Merge branch 'pr129_vuhailongkl97' into 'master'
[esp-hosted.git] / host / linux / host_control / c_support / test_utils.c
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 <stdio.h>
22 #include <sys/types.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netdb.h>
29 #include <linux/if.h>
30 #include <linux/if_arp.h>
31 #include <errno.h>
32 #include "ctrl_api.h"
33 #include "ctrl_config.h"
34 #include <time.h>
35
36 /***** Please Read *****/
37 /* Before use : User must enter user configuration parameter in "ctrl_config.h" file */
38
39 #define STA_INTERFACE                                     "ethsta0"
40 #define AP_INTERFACE                                      "ethap0"
41
42 #define WIFI_VENDOR_IE_ELEMENT_ID                         0xDD
43 #define OFFSET                                            4
44 #define VENDOR_OUI_0                                      1
45 #define VENDOR_OUI_1                                      2
46 #define VENDOR_OUI_2                                      3
47 #define VENDOR_OUI_TYPE                                   22
48
49 #define CTRL_CMD_DEFAULT_REQ() {                          \
50   .msg_type = CTRL_REQ,                                   \
51   .ctrl_resp_cb = NULL,                                   \
52   .cmd_timeout_sec = DEFAULT_CTRL_RESP_TIMEOUT /*30 sec*/ \
53 }
54
55 #define CLEANUP_CTRL_MSG(msg) do {                        \
56   if (msg) {                                              \
57     if (msg->free_buffer_handle) {                        \
58       if (msg->free_buffer_func) {                        \
59         msg->free_buffer_func(msg->free_buffer_handle);   \
60         msg->free_buffer_handle = NULL;                   \
61       }                                                   \
62     }                                                     \
63     free(msg);                                            \
64     msg = NULL;                                           \
65   }                                                       \
66 } while(0);
67
68 #define YES 1
69 #define NO  0
70 #define MIN_TIMESTAMP_STR_SIZE 30
71
72 typedef struct {
73         int event;
74         ctrl_resp_cb_t fun;
75 } event_callback_table_t;
76
77 static char * get_timestamp(char *str, uint16_t str_size)
78 {
79         if (str && str_size>=MIN_TIMESTAMP_STR_SIZE) {
80                 time_t t = time(NULL);
81                 struct tm tm = *localtime(&t);
82                 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);
83                 return str;
84         }
85         return NULL;
86 }
87
88 static int ctrl_app_event_callback(ctrl_cmd_t * app_event)
89 {
90         char ts[MIN_TIMESTAMP_STR_SIZE] = {'\0'};
91
92         if (!app_event || (app_event->msg_type != CTRL_EVENT)) {
93                 if (app_event)
94                         printf("Msg type is not event[%u]\n",app_event->msg_type);
95                 goto fail_parsing;
96         }
97
98         if ((app_event->msg_id <= CTRL_EVENT_BASE) ||
99             (app_event->msg_id >= CTRL_EVENT_MAX)) {
100                 printf("Event Msg ID[%u] is not correct\n",app_event->msg_id);
101                 goto fail_parsing;
102         }
103
104         switch(app_event->msg_id) {
105
106                 case CTRL_EVENT_ESP_INIT: {
107                         printf("%s App EVENT: ESP INIT\n",
108                                 get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE));
109                         break;
110                 } case CTRL_EVENT_HEARTBEAT: {
111                         printf("%s App EVENT: Heartbeat event [%d]\n",
112                                 get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE),
113                                         app_event->u.e_heartbeat.hb_num);
114                         break;
115                 } case CTRL_EVENT_STATION_DISCONNECT_FROM_AP: {
116                         printf("%s App EVENT: Station mode: Disconnect Reason[%u]\n",
117                                 get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), app_event->resp_event_status);
118                         break;
119                 } case CTRL_EVENT_STATION_DISCONNECT_FROM_ESP_SOFTAP: {
120                         char *p = app_event->u.e_sta_disconnected.mac;
121                         if (p && strlen(p)) {
122                                 printf("%s App EVENT: SoftAP mode: Disconnect MAC[%s]\n",
123                                         get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), p);
124                         }
125                         break;
126                 } default: {
127                         printf("%s Invalid event[%u] to parse\n",
128                                 get_timestamp(ts, MIN_TIMESTAMP_STR_SIZE), app_event->msg_id);
129                         break;
130                 }
131         }
132         CLEANUP_CTRL_MSG(app_event);
133         return SUCCESS;
134
135 fail_parsing:
136         CLEANUP_CTRL_MSG(app_event);
137         return FAILURE;
138 }
139
140 static void process_failed_responses(ctrl_cmd_t *app_msg)
141 {
142         uint8_t request_failed_flag = true;
143
144         /* Identify general issue, common for all control requests */
145         switch (app_msg->resp_event_status) {
146                 case CTRL_ERR_REQ_IN_PROG:
147                         printf("Error reported: Command In progress, Please wait\n");
148                         break;
149                 case CTRL_ERR_REQUEST_TIMEOUT:
150                         printf("Error reported: Response Timeout\n");
151                         break;
152                 case CTRL_ERR_MEMORY_FAILURE:
153                         printf("Error reported: Memory allocation failed\n");
154                         break;
155                 case CTRL_ERR_UNSUPPORTED_MSG:
156                         printf("Error reported: Unsupported control msg\n");
157                         break;
158                 case CTRL_ERR_INCORRECT_ARG:
159                         printf("Error reported: Invalid or out of range parameter values\n");
160                         break;
161                 case CTRL_ERR_PROTOBUF_ENCODE:
162                         printf("Error reported: Protobuf encode failed\n");
163                         break;
164                 case CTRL_ERR_PROTOBUF_DECODE:
165                         printf("Error reported: Protobuf decode failed\n");
166                         break;
167                 case CTRL_ERR_SET_ASYNC_CB:
168                         printf("Error reported: Failed to set aync callback\n");
169                         break;
170                 case CTRL_ERR_TRANSPORT_SEND:
171                         printf("Error reported: Problem while sending data on serial driver\n");
172                         break;
173                 default:
174                         request_failed_flag = false;
175                         break;
176         }
177
178         /* if control request failed, no need to proceed for response checking */
179         if (request_failed_flag)
180                 return;
181
182         /* Identify control request specific issue */
183         switch (app_msg->msg_id) {
184
185                 case CTRL_RESP_OTA_END:
186                 case CTRL_RESP_OTA_BEGIN:
187                 case CTRL_RESP_OTA_WRITE: {
188                         /* intentional fallthrough */
189                         printf("OTA procedure failed\n");
190                         break;
191                 } case CTRL_RESP_CONNECT_AP: {
192                         if (app_msg->resp_event_status == CTRL_ERR_NO_AP_FOUND) {
193                                 printf("SSID: not found/connectable\n");
194                         } else if (app_msg->resp_event_status ==
195                                         CTRL_ERR_INVALID_PASSWORD) {
196                                 printf("Invalid password for SSID\n");
197                         } else {
198                                 printf("Failed to connect with AP \n");
199                         }
200                         break;
201                 } case CTRL_RESP_START_SOFTAP: {
202                         printf("Failed to start SoftAP\n");
203                         break;
204                 }
205                 case CTRL_RESP_STOP_SOFTAP:
206                 case CTRL_RESP_GET_SOFTAP_CONFIG: {
207                         printf("Possibly softap is not running/started\n");
208                         break;
209                 } default: {
210                         printf("Failed Control Response\n");
211                         break;
212                 }
213         }
214 }
215
216 static int process_resp_disconnect_ap(ctrl_cmd_t * app_msg)
217 {
218         int ret = SUCCESS, sockfd = 0;
219
220         ret = create_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP, &sockfd);
221         if (ret < 0) {
222                 printf("Failure to open socket\n");
223                 return FAILURE;
224         }
225
226         ret = interface_down(sockfd, STA_INTERFACE);
227         if (ret == SUCCESS) {
228                 printf("%s interface down\n", STA_INTERFACE);
229         } else {
230                 printf("Unable to down %s interface\n", STA_INTERFACE);
231                 goto close_sock;
232         }
233
234         ret = close_socket(sockfd);
235         if (ret < 0) {
236                 printf("Failure to close socket\n");
237                 return FAILURE;
238         }
239
240         return SUCCESS;
241
242 close_sock:
243         ret = close_socket(sockfd);
244         if (ret < 0) {
245                 printf("Failure to close socket\n");
246         }
247         return FAILURE;
248 }
249
250 static int process_resp_connect_ap(ctrl_cmd_t * app_msg)
251 {
252         int ret = SUCCESS, sockfd = 0;
253
254         if (!strlen(app_msg->u.wifi_ap_config.out_mac)) {
255                 printf("Failure: station mac is empty\n");
256                 return FAILURE;
257         }
258
259         ret = create_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP, &sockfd);
260         if (ret < 0) {
261                 printf("Failure to open socket\n");
262                 return FAILURE;
263         }
264
265         ret = interface_down(sockfd, STA_INTERFACE);
266         if (ret == SUCCESS) {
267                 printf("%s interface down\n", STA_INTERFACE);
268         } else {
269                 printf("Unable to down %s interface\n", STA_INTERFACE);
270                 goto close_sock;
271         }
272
273         ret = set_hw_addr(sockfd, STA_INTERFACE, app_msg->u.wifi_ap_config.out_mac);
274         if (ret == SUCCESS) {
275                 printf("MAC address %s set to %s interface\n",
276                                 app_msg->u.wifi_ap_config.out_mac, STA_INTERFACE);
277         } else {
278                 printf("Unable to set MAC address to %s interface\n", STA_INTERFACE);
279                 goto close_sock;
280         }
281
282         ret = interface_up(sockfd, STA_INTERFACE);
283         if (ret == SUCCESS) {
284                 printf("%s interface up\n", STA_INTERFACE);
285         } else {
286                 printf("Unable to up %s interface\n", STA_INTERFACE);
287                 goto close_sock;
288         }
289
290         ret = close_socket(sockfd);
291         if (ret < 0) {
292                 printf("Failure to close socket\n");
293                 return FAILURE;
294         }
295         return SUCCESS;
296
297 close_sock:
298         ret = close_socket(sockfd);
299         if (ret < 0) {
300                 printf("Failure to close socket\n");
301         }
302         return FAILURE;
303 }
304
305 static int process_resp_stop_softap(ctrl_cmd_t * app_msg)
306 {
307         int ret = SUCCESS, sockfd = 0;
308
309         ret = create_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP, &sockfd);
310         if (ret < 0) {
311                 printf("Failure to open socket\n");
312                 return FAILURE;
313         }
314
315         ret = interface_down(sockfd, AP_INTERFACE);
316         if (ret == SUCCESS) {
317                 printf("%s interface down\n", AP_INTERFACE);
318         } else {
319                 printf("Unable to down %s interface\n", AP_INTERFACE);
320                 goto close_sock;
321         }
322
323         ret = close_socket(sockfd);
324         if (ret < 0) {
325                 printf("Failure to close socket\n");
326                 return FAILURE;
327         }
328         return SUCCESS;
329
330 close_sock:
331         ret = close_socket(sockfd);
332         if (ret < 0) {
333                 printf("Failure to close socket\n");
334         }
335         return FAILURE;
336 }
337
338 static int process_resp_start_softap(ctrl_cmd_t * app_msg)
339 {
340         int ret = SUCCESS, sockfd = 0;
341
342         if (!strlen(app_msg->u.wifi_softap_config.out_mac)) {
343                 printf("Failure: softap mac is empty\n");
344                 return FAILURE;
345         }
346
347         ret = create_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP, &sockfd);
348         if (ret < 0) {
349                 printf("Failure to open socket\n");
350                 return FAILURE;
351         }
352
353         ret = interface_down(sockfd, AP_INTERFACE);
354         if (ret == SUCCESS) {
355                 printf("%s interface down\n", AP_INTERFACE);
356         } else {
357                 printf("Unable to down %s interface\n", AP_INTERFACE);
358                 goto close_sock;
359         }
360
361         ret = set_hw_addr(sockfd, AP_INTERFACE, app_msg->u.wifi_softap_config.out_mac);
362         if (ret == SUCCESS) {
363                 printf("MAC address %s set to %s interface\n",
364                                 app_msg->u.wifi_softap_config.out_mac, AP_INTERFACE);
365         } else {
366                 printf("Unable to set MAC address to %s interface\n", AP_INTERFACE);
367                 goto close_sock;
368         }
369
370         ret = interface_up(sockfd, AP_INTERFACE);
371         if (ret == SUCCESS) {
372                 printf("%s interface up\n", AP_INTERFACE);
373         } else {
374                 printf("Unable to up %s interface\n", AP_INTERFACE);
375                 goto close_sock;
376         }
377
378         ret = close_socket(sockfd);
379         if (ret < 0) {
380                 printf("Failure to close socket\n");
381                 return FAILURE;
382         }
383         return SUCCESS;
384
385 close_sock:
386         ret = close_socket(sockfd);
387         if (ret < 0) {
388                 printf("Failure to close socket\n");
389         }
390         return FAILURE;
391 }
392
393
394 int unregister_event_callbacks(void)
395 {
396         int ret = SUCCESS;
397         int evt = 0;
398         for (evt=CTRL_EVENT_BASE+1; evt<CTRL_EVENT_MAX; evt++) {
399                 if (CALLBACK_SET_SUCCESS != reset_event_callback(evt) ) {
400                         printf("reset event callback failed for event[%u]\n", evt);
401                         ret = FAILURE;
402                 }
403         }
404         return ret;
405 }
406
407 int register_event_callbacks(void)
408 {
409         int ret = SUCCESS;
410         int evt = 0;
411
412         event_callback_table_t events[] = {
413                 { CTRL_EVENT_ESP_INIT,                           ctrl_app_event_callback },
414                 { CTRL_EVENT_HEARTBEAT,                          ctrl_app_event_callback },
415                 { CTRL_EVENT_STATION_DISCONNECT_FROM_AP,         ctrl_app_event_callback },
416                 { CTRL_EVENT_STATION_DISCONNECT_FROM_ESP_SOFTAP, ctrl_app_event_callback },
417         };
418
419         for (evt=0; evt<sizeof(events)/sizeof(event_callback_table_t); evt++) {
420                 if (CALLBACK_SET_SUCCESS != set_event_callback(events[evt].event, events[evt].fun) ) {
421                         printf("event callback register failed for event[%u]\n", events[evt].event);
422                         ret = FAILURE;
423                         break;
424                 }
425         }
426         return ret;
427 }
428
429
430 int ctrl_app_resp_callback(ctrl_cmd_t * app_resp)
431 {
432         uint16_t i = 0;
433         if (!app_resp || (app_resp->msg_type != CTRL_RESP)) {
434                 if (app_resp)
435                         printf("Msg type is not response[%u]\n",app_resp->msg_type);
436                 goto fail_resp;
437         }
438
439         if ((app_resp->msg_id <= CTRL_RESP_BASE) || (app_resp->msg_id >= CTRL_RESP_MAX)) {
440                 printf("Response Msg ID[%u] is not correct\n",app_resp->msg_id);
441                 goto fail_resp;
442         }
443
444         if (app_resp->resp_event_status != SUCCESS) {
445                 process_failed_responses(app_resp);
446                 goto fail_resp;
447         }
448
449         switch(app_resp->msg_id) {
450
451                 case CTRL_RESP_GET_MAC_ADDR: {
452                         printf("mac address is %s\n", app_resp->u.wifi_mac.mac);
453                         break;
454                 } case CTRL_RESP_SET_MAC_ADDRESS : {
455                         printf("MAC address is set\n");
456                         break;
457                 } case CTRL_RESP_GET_WIFI_MODE : {
458                         printf("wifi mode is : ");
459                         switch (app_resp->u.wifi_mode.mode) {
460                                 case WIFI_MODE_STA:     printf("station\n");        break;
461                                 case WIFI_MODE_AP:      printf("softap\n");         break;
462                                 case WIFI_MODE_APSTA:   printf("station+softap\n"); break;
463                                 case WIFI_MODE_NONE:    printf("none");             break;
464                                 default:                printf("unknown\n");        break;
465                         }
466                         break;
467                 } case CTRL_RESP_SET_WIFI_MODE : {
468                         printf("wifi mode is set\n");
469                         break;
470                 } case CTRL_RESP_GET_AP_SCAN_LIST : {
471                         wifi_ap_scan_list_t * w_scan_p = &app_resp->u.wifi_ap_scan;
472                         wifi_scanlist_t *list = w_scan_p->out_list;
473
474                         if (!w_scan_p->count) {
475                                 printf("No AP found \n");
476                                 goto finish_resp;
477                         }
478                         if (!list) {
479                                 printf("Failed to get scanned AP list \n");
480                                 goto fail_resp;
481                         } else {
482
483                                 printf("Number of available APs is %d \n", w_scan_p->count);
484                                 for (i=0; i<w_scan_p->count; i++) {
485                                         printf("%d) ssid \"%s\" bssid \"%s\" rssi \"%d\" channel \"%d\" auth mode \"%d\" \n",\
486                                                         i, list[i].ssid, list[i].bssid, list[i].rssi,
487                                                         list[i].channel, list[i].encryption_mode);
488                                 }
489                         }
490                         break;
491                 } case CTRL_RESP_GET_AP_CONFIG : {
492                         wifi_ap_config_t *p = &app_resp->u.wifi_ap_config;
493                         if (0 == strncmp(SUCCESS_STR, p->status, strlen(SUCCESS_STR))) {
494                                 printf("AP's ssid '%s'\n", p->ssid);
495                                 printf("AP's bssid i.e. MAC address %s\n", p->bssid);
496                                 printf("AP's channel number %d\n", p->channel);
497                                 printf("AP's rssi %d\n", p->rssi);
498                                 printf("AP's encryption mode %d\n", p->encryption_mode);
499                         } else {
500                                 printf("Station mode status: %s\n",p->status);
501                         }
502                         break;
503                 } case CTRL_RESP_CONNECT_AP : {
504                         if (process_resp_connect_ap(app_resp))
505                                 goto fail_resp;
506                         break;
507                 } case CTRL_RESP_DISCONNECT_AP : {
508                         printf("Disconnected from AP \n");
509                         if (process_resp_disconnect_ap(app_resp))
510                                 goto fail_resp;
511                         break;
512                 } case CTRL_RESP_GET_SOFTAP_CONFIG : {
513                         softap_config_t * resp_p = &app_resp->u.wifi_softap_config;
514
515                         printf("softAP ssid %s \n", resp_p->ssid);
516                         printf("softAP pwd %s \n", resp_p->pwd);
517                         printf("softAP channel ID %d \n", resp_p->channel);
518                         printf("softAP encryption mode %d \n", resp_p->encryption_mode);
519                         printf("softAP max connections %d \n", resp_p->max_connections);
520                         printf("softAP ssid broadcast status %d \n", resp_p->ssid_hidden);
521                         printf("softAP bandwidth mode %d \n", resp_p->bandwidth);
522
523                         break;
524                 } case CTRL_RESP_SET_SOFTAP_VND_IE : {
525                         printf("Success in set vendor specific ie\n");
526                         break;
527                 } case CTRL_RESP_START_SOFTAP : {
528                         printf("esp32 softAP started \n");
529                         if (process_resp_start_softap(app_resp))
530                                 goto fail_resp;
531                         break;
532                 } case CTRL_RESP_GET_SOFTAP_CONN_STA_LIST : {
533                         int count = app_resp->u.wifi_softap_con_sta.count;
534                         wifi_connected_stations_list_t *stations_list =
535                                 app_resp->u.wifi_softap_con_sta.out_list;
536
537                         printf("sta list count: %u\n",count);
538                         if (!count) {
539                                 printf("No station found \n");
540                                 goto fail_resp;
541                         }
542
543                         if (!stations_list) {
544                                 printf("Failed to get connected stations list \n");
545                         } else if (count) {
546                                 for (i=0; i<count; i++) {
547                                         printf("%d th stations's bssid \"%s\" rssi \"%d\" \n",i, \
548                                                         stations_list[i].bssid, stations_list[i].rssi);
549                                 }
550                         }
551                         break;
552                 } case CTRL_RESP_STOP_SOFTAP : {
553                         printf("esp32 softAP stopped\n");
554                         if (process_resp_stop_softap(app_resp))
555                                 goto fail_resp;
556                         break;
557                 } case CTRL_RESP_SET_PS_MODE : {
558                         printf("Wifi power save mode set\n");
559                         break;
560                 } case CTRL_RESP_GET_PS_MODE : {
561                         printf("Wifi power save mode is: ");
562
563                         switch(app_resp->u.wifi_ps.ps_mode) {
564                                 case WIFI_PS_MIN_MODEM:
565                                         printf("Min\n");
566                                         break;
567                                 case WIFI_PS_MAX_MODEM:
568                                         printf("Max\n");
569                                         break;
570                                 default:
571                                         printf("Invalid\n");
572                                         break;
573                         }
574                         break;
575                 } case CTRL_RESP_OTA_BEGIN : {
576                         printf("OTA begin success\n");
577                         break;
578                 } case CTRL_RESP_OTA_WRITE : {
579                         printf("OTA write success\n");
580                         break;
581                 } case CTRL_RESP_OTA_END : {
582                         printf("OTA end success\n");
583                         break;
584                 } case CTRL_RESP_SET_WIFI_MAX_TX_POWER: {
585                         printf("Set wifi max tx power success\n");
586                         break;
587                 } case CTRL_RESP_GET_WIFI_CURR_TX_POWER: {
588                         printf("wifi curr tx power : %d\n",
589                                         app_resp->u.wifi_tx_power.power);
590                         break;
591                 } case CTRL_RESP_CONFIG_HEARTBEAT: {
592                         printf("Heartbeat operation successful\n");
593                         break;
594                 } default: {
595                         printf("Invalid Response[%u] to parse\n", app_resp->msg_id);
596                         break;
597                 }
598         }
599
600 finish_resp:
601         CLEANUP_CTRL_MSG(app_resp);
602         return SUCCESS;
603
604 fail_resp:
605         CLEANUP_CTRL_MSG(app_resp);
606         return FAILURE;
607 }
608
609 int test_get_wifi_mode(void)
610 {
611         /* implemented Asynchronous */
612         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
613
614         /* register callback for reply */
615         req.ctrl_resp_cb = ctrl_app_resp_callback;
616
617         wifi_get_mode(req);
618
619         return SUCCESS;
620 }
621
622
623 int test_set_wifi_mode(int mode)
624 {
625         /* implemented synchronous */
626         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
627         ctrl_cmd_t *resp = NULL;
628
629         req.u.wifi_mode.mode = mode;
630         resp = wifi_set_mode(req);
631
632         return ctrl_app_resp_callback(resp);
633 }
634
635 int test_set_wifi_mode_station(void)
636 {
637         return test_set_wifi_mode(WIFI_MODE_STA);
638 }
639
640 int test_set_wifi_mode_softap(void)
641 {
642         return test_set_wifi_mode(WIFI_MODE_AP);
643 }
644
645 int test_set_wifi_mode_station_softap(void)
646 {
647         return test_set_wifi_mode(WIFI_MODE_APSTA);
648 }
649
650 int test_set_wifi_mode_none(void)
651 {
652         return test_set_wifi_mode(WIFI_MODE_NONE);
653 }
654
655 int test_get_wifi_mac_addr(int mode)
656 {
657         /* implemented synchronous */
658         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
659         ctrl_cmd_t *resp = NULL;
660
661         req.u.wifi_mac.mode = mode;
662         resp = wifi_get_mac(req);
663
664         return ctrl_app_resp_callback(resp);
665 }
666
667 int test_station_mode_get_mac_addr(void)
668 {
669         return test_get_wifi_mac_addr(WIFI_MODE_STA);
670 }
671
672 int test_set_mac_addr(int mode, char *mac)
673 {
674         /* implemented synchronous */
675         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
676         ctrl_cmd_t *resp = NULL;
677
678         int ret = test_set_wifi_mode(mode);
679         if (ret == SUCCESS) {
680                 req.u.wifi_mac.mode = mode;
681                 strncpy(req.u.wifi_mac.mac, mac, MAX_MAC_STR_LEN);
682                 req.u.wifi_mac.mac[MAX_MAC_STR_LEN-1] = '\0';
683
684                 resp = wifi_set_mac(req);
685                 return ctrl_app_resp_callback(resp);
686         }
687         return ret;
688 }
689
690 int test_station_mode_set_mac_addr_of_esp(void)
691 {
692         return test_set_mac_addr(WIFI_MODE_STA, STATION_MODE_MAC_ADDRESS);
693 }
694
695 int test_softap_mode_set_mac_addr_of_esp(void)
696 {
697         return test_set_mac_addr(WIFI_MODE_STA, SOFTAP_MODE_MAC_ADDRESS);
698 }
699
700 int test_softap_mode_get_mac_addr(void)
701 {
702         return test_get_wifi_mac_addr(WIFI_MODE_AP);
703 }
704
705 int test_station_mode_connect(void)
706 {
707         /* implemented Asynchronous */
708         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
709
710         strcpy((char *)&req.u.wifi_ap_config.ssid, STATION_MODE_SSID);
711         strcpy((char *)&req.u.wifi_ap_config.pwd, STATION_MODE_PWD);
712         strcpy((char *)&req.u.wifi_ap_config.bssid, STATION_MODE_BSSID);
713         req.u.wifi_ap_config.is_wpa3_supported = STATION_MODE_IS_WPA3_SUPPORTED;
714         req.u.wifi_ap_config.listen_interval = STATION_MODE_LISTEN_INTERVAL;
715
716         /* register callback for handling reply asynch-ly */
717         req.ctrl_resp_cb = ctrl_app_resp_callback;
718
719         wifi_connect_ap(req);
720
721         return SUCCESS;
722 }
723
724 int test_station_mode_get_info(void)
725 {
726         /* implemented synchronous */
727         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
728         ctrl_cmd_t *resp = NULL;
729
730         resp = wifi_get_ap_config(req);
731
732         return ctrl_app_resp_callback(resp);
733 }
734
735 int test_get_available_wifi(void)
736 {
737         /* implemented synchronous */
738         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
739         ctrl_cmd_t *resp = NULL;
740
741         resp = wifi_ap_scan_list(req);
742
743         return ctrl_app_resp_callback(resp);
744 }
745
746 int test_station_mode_disconnect(void)
747 {
748         /* implemented synchronous */
749         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
750         ctrl_cmd_t *resp = NULL;
751
752         resp = wifi_disconnect_ap(req);
753
754         return ctrl_app_resp_callback(resp);
755 }
756
757 int test_softap_mode_start(void)
758 {
759         /* implemented synchronous */
760         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
761         ctrl_cmd_t *resp = NULL;
762
763         strncpy((char *)&req.u.wifi_softap_config.ssid,
764                         SOFTAP_MODE_SSID, MAX_MAC_STR_LEN-1);
765         strncpy((char *)&req.u.wifi_softap_config.pwd,
766                         SOFTAP_MODE_PWD, MAX_MAC_STR_LEN-1);
767         req.u.wifi_softap_config.channel = SOFTAP_MODE_CHANNEL;
768         req.u.wifi_softap_config.encryption_mode = SOFTAP_MODE_ENCRYPTION_MODE;
769         req.u.wifi_softap_config.max_connections = SOFTAP_MODE_MAX_ALLOWED_CLIENTS;
770         req.u.wifi_softap_config.ssid_hidden = SOFTAP_MODE_SSID_HIDDEN;
771         req.u.wifi_softap_config.bandwidth = SOFTAP_MODE_BANDWIDTH;
772
773         resp = wifi_start_softap(req);
774
775         return ctrl_app_resp_callback(resp);
776 }
777
778 int test_softap_mode_get_info(void)
779 {
780         /* implemented synchronous */
781         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
782         ctrl_cmd_t *resp = NULL;
783
784         resp = wifi_get_softap_config(req);
785
786         return ctrl_app_resp_callback(resp);
787 }
788
789 int test_softap_mode_connected_clients_info(void)
790 {
791         /* implemented synchronous */
792         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
793         ctrl_cmd_t *resp = NULL;
794
795         resp = wifi_get_softap_connected_station_list(req);
796
797         return ctrl_app_resp_callback(resp);
798 }
799
800 int test_softap_mode_stop(void)
801 {
802         /* implemented synchronous */
803         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
804         ctrl_cmd_t *resp = NULL;
805
806         resp = wifi_stop_softap(req);
807
808         return ctrl_app_resp_callback(resp);
809 }
810
811 int test_set_wifi_power_save_mode(int psmode)
812 {
813         /* implemented synchronous */
814         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
815         ctrl_cmd_t *resp = NULL;
816
817         req.u.wifi_ps.ps_mode = psmode;
818         resp = wifi_set_power_save_mode(req);
819
820         return ctrl_app_resp_callback(resp);
821 }
822
823 int test_set_wifi_power_save_mode_max(void)
824 {
825         return test_set_wifi_power_save_mode(WIFI_PS_MAX_MODEM);
826 }
827
828 int test_set_wifi_power_save_mode_min(void)
829 {
830         return test_set_wifi_power_save_mode(WIFI_PS_MIN_MODEM);
831 }
832
833 int test_get_wifi_power_save_mode(void)
834 {
835         /* implemented synchronous */
836         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
837         ctrl_cmd_t *resp = NULL;
838
839         resp = wifi_get_power_save_mode(req);
840
841         return ctrl_app_resp_callback(resp);
842 }
843
844 int test_reset_vendor_specific_ie(void)
845 {
846         /* implemented synchronous */
847         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
848         ctrl_cmd_t *resp = NULL;
849         char *data = "Example vendor IE data";
850
851         char *v_data = (char*)calloc(1, strlen(data));
852         if (!v_data) {
853                 printf("Failed to allocate memory \n");
854                 return FAILURE;
855         }
856         memcpy(v_data, data, strlen(data));
857
858         req.u.wifi_softap_vendor_ie.enable = false;
859         req.u.wifi_softap_vendor_ie.type   = WIFI_VND_IE_TYPE_BEACON;
860         req.u.wifi_softap_vendor_ie.idx    = WIFI_VND_IE_ID_0;
861         req.u.wifi_softap_vendor_ie.vnd_ie.element_id = WIFI_VENDOR_IE_ELEMENT_ID;
862         req.u.wifi_softap_vendor_ie.vnd_ie.length = strlen(data)+OFFSET;
863         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[0] = VENDOR_OUI_0;
864         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[1] = VENDOR_OUI_1;
865         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[2] = VENDOR_OUI_2;
866         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui_type = VENDOR_OUI_TYPE;
867         req.u.wifi_softap_vendor_ie.vnd_ie.payload = (uint8_t *)v_data;
868         req.u.wifi_softap_vendor_ie.vnd_ie.payload_len = strlen(data);
869
870         req.free_buffer_func = free;
871         req.free_buffer_handle = v_data;
872
873         resp = wifi_set_vendor_specific_ie(req);
874
875         return ctrl_app_resp_callback(resp);
876 }
877 void free_hook(void *ptr)
878 {
879         if (ptr) {
880                 printf("Freeing 0x%p\n",ptr);
881                 free(ptr);
882                 ptr = NULL;
883         }
884 }
885
886 int test_set_vendor_specific_ie(void)
887 {
888         /* implemented synchronous */
889         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
890         ctrl_cmd_t *resp = NULL;
891         char *data = "Example vendor IE data";
892
893         char *v_data = (char*)calloc(1, strlen(data));
894         if (!v_data) {
895                 printf("Failed to allocate memory \n");
896                 return FAILURE;
897         }
898         memcpy(v_data, data, strlen(data));
899
900         req.u.wifi_softap_vendor_ie.enable = true;
901         req.u.wifi_softap_vendor_ie.type   = WIFI_VND_IE_TYPE_BEACON;
902         req.u.wifi_softap_vendor_ie.idx    = WIFI_VND_IE_ID_0;
903         req.u.wifi_softap_vendor_ie.vnd_ie.element_id = WIFI_VENDOR_IE_ELEMENT_ID;
904         req.u.wifi_softap_vendor_ie.vnd_ie.length = strlen(data)+OFFSET;
905         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[0] = VENDOR_OUI_0;
906         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[1] = VENDOR_OUI_1;
907         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui[2] = VENDOR_OUI_2;
908         req.u.wifi_softap_vendor_ie.vnd_ie.vendor_oui_type = VENDOR_OUI_TYPE;
909         req.u.wifi_softap_vendor_ie.vnd_ie.payload = (uint8_t *)v_data;
910         req.u.wifi_softap_vendor_ie.vnd_ie.payload_len = strlen(data);
911
912         req.free_buffer_func = free_hook;
913         req.free_buffer_handle = v_data;
914
915         resp = wifi_set_vendor_specific_ie(req);
916
917         return ctrl_app_resp_callback(resp);
918 }
919
920 int test_ota_begin(void)
921 {
922         /* implemented synchronous */
923         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
924         ctrl_cmd_t *resp = NULL;
925
926         resp = ota_begin(req);
927
928         return ctrl_app_resp_callback(resp);
929 }
930
931 int test_ota_write(uint8_t* ota_data, uint32_t ota_data_len)
932 {
933         /* implemented synchronous */
934         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
935         ctrl_cmd_t *resp = NULL;
936
937         req.u.ota_write.ota_data = ota_data;
938         req.u.ota_write.ota_data_len = ota_data_len;
939
940         resp = ota_write(req);
941
942         return ctrl_app_resp_callback(resp);
943 }
944
945 int test_ota_end(void)
946 {
947         /* implemented synchronous */
948         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
949         ctrl_cmd_t *resp = NULL;
950
951         resp = ota_end(req);
952
953         return ctrl_app_resp_callback(resp);
954 }
955
956 int test_ota(char* image_path)
957 {
958         FILE* f = NULL;
959         char ota_chunk[CHUNK_SIZE] = {0};
960         int ret = test_ota_begin();
961         if (ret == SUCCESS) {
962                 f = fopen(image_path,"rb");
963                 if (f == NULL) {
964                         printf("Failed to open file %s \n", image_path);
965                         return FAILURE;
966                 } else {
967                         printf("Success in opening %s file \n", image_path);
968                 }
969                 while (!feof(f)) {
970                         fread(&ota_chunk, CHUNK_SIZE, 1, f);
971                         ret = test_ota_write((uint8_t* )&ota_chunk, CHUNK_SIZE);
972                         if (ret) {
973                                 printf("OTA procedure failed!!\n");
974                                 test_ota_end();
975                                 return FAILURE;
976                         }
977                 }
978                 ret = test_ota_end();
979                 if (ret) {
980                         return FAILURE;
981                 }
982         } else {
983                 return FAILURE;
984         }
985         printf("ESP32 will restart after 5 sec\n");
986         return SUCCESS;
987 }
988
989 int test_wifi_set_max_tx_power(int in_power)
990 {
991         /* implemented synchronous */
992         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
993         ctrl_cmd_t *resp = NULL;
994
995         req.u.wifi_tx_power.power = in_power;
996         resp = wifi_set_max_tx_power(req);
997
998         return ctrl_app_resp_callback(resp);
999 }
1000
1001 int test_wifi_get_curr_tx_power()
1002 {
1003         /* implemented synchronous */
1004         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
1005         ctrl_cmd_t *resp = NULL;
1006
1007         resp = wifi_get_curr_tx_power(req);
1008
1009         return ctrl_app_resp_callback(resp);
1010 }
1011
1012 int test_config_heartbeat(void)
1013 {
1014         /* implemented synchronous */
1015         ctrl_cmd_t *resp = NULL;
1016         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
1017         req.u.e_heartbeat.enable = YES;
1018         req.u.e_heartbeat.duration = HEARTBEAT_DURATION_SEC;
1019
1020         resp = config_heartbeat(req);
1021
1022         return ctrl_app_resp_callback(resp);
1023 }
1024
1025 int test_disable_heartbeat(void)
1026 {
1027         /* implemented synchronous */
1028         ctrl_cmd_t *resp = NULL;
1029         ctrl_cmd_t req = CTRL_CMD_DEFAULT_REQ();
1030         req.u.e_heartbeat.enable = NO;
1031
1032         resp = config_heartbeat(req);
1033
1034         return ctrl_app_resp_callback(resp);
1035 }
This page took 0.088289 seconds and 4 git commands to generate.