blob: fd8f9b798f0a5e745c8e513e9a315ed84028aa8a [file] [log] [blame]
qs.xiong799dab02022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen87ff5172022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong799dab02022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen87ff5172022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050013#include <string.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen87ff5172022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050024
qs.xiong799dab02022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen87ff5172022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongd189c542022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen87ff5172022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chen6a9361d2022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
you.chen61c7aee2022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
you.chen6a9361d2022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chenc7357f22022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen87ff5172022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen61c7aee2022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen87ff5172022-05-06 11:30:57 +080062
qs.xiong3e6e2e62022-09-29 11:37:32 +080063static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen87ff5172022-05-06 11:30:57 +080064
65
66typedef struct __curr_status_info {
67 ap_info_s *ap;
68 char * state;
69 int net_no;
70}curr_status_info;
qs.xiong99b48d62022-04-07 05:41:29 -040071
72#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong99b48d62022-04-07 05:41:29 -040073{\
you.chen87ff5172022-05-06 11:30:57 +080074 perror((str));\
75 return (value);\
qs.xiong99b48d62022-04-07 05:41:29 -040076}
77
you.chen87ff5172022-05-06 11:30:57 +080078#define CHECK_IDX(idx, type) do { \
79 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
80 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
81 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
82 return -1; \
83 } \
84 }while (0)
85
86#define CHECK_WPA_CTRL(index) int ret = 0;\
87 size_t reply_len = MAX_RET; \
88 char cmd_reply[MAX_RET]={0}; \
qs.xiong3e6e2e62022-09-29 11:37:32 +080089 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen87ff5172022-05-06 11:30:57 +080090 do{ \
qs.xiong3e6e2e62022-09-29 11:37:32 +080091 if (NULL == g_lynq_wpa_ctrl[index]) { \
92 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
93 if (NULL == g_lynq_wpa_ctrl[index]) { \
94 printf("wpa_ctrl_open fail\n"); \
95 return -1; \
96 } \
97 } \
98 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
you.chen87ff5172022-05-06 11:30:57 +080099 }while(0)
100
101#define DO_REQUEST(cmd_str) do { \
102 reply_len = MAX_RET;\
103 cmd_reply[0] = '\0'; \
qs.xiong3e6e2e62022-09-29 11:37:32 +0800104 printf("to call [%s]\n", cmd_str); \
105 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen87ff5172022-05-06 11:30:57 +0800106 if (ret != 0) { \
you.chen5e363602022-05-08 12:20:18 +0800107 printf("call "#cmd_str" fail %d\n", ret); \
you.chen87ff5172022-05-06 11:30:57 +0800108 return ret; \
109 } \
110 cmd_reply[reply_len+1] = '\0'; \
111 printf("cmd replay [ %s ]\n", cmd_reply); \
112 }while(0)
113
114#define DO_OK_FAIL_REQUEST(cmd_str) do { \
115 DO_REQUEST(cmd_str); \
116 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chen5e363602022-05-08 12:20:18 +0800117 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen87ff5172022-05-06 11:30:57 +0800118 return -1; \
119 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chen5e363602022-05-08 12:20:18 +0800120 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen87ff5172022-05-06 11:30:57 +0800121 return -1; \
122 } \
123 }while (0)
124
125
126
127static void APWatcherThreadProc() {
128 size_t len = MAX_RET;
129 char msg_notify[MAX_RET];
130
you.chenc7357f22022-05-16 17:55:28 +0800131 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen01276462022-05-25 10:09:47 +0800132 g_ap_watcher_stop_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +0800133
134 while (g_ap_watcher_stop_flag == 0) {
you.chenc7357f22022-05-16 17:55:28 +0800135 if (lynq_wpa_ctrl == NULL) {
136 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
137 if (lynq_wpa_ctrl == NULL) {
138 usleep(100*1000);
139 continue;
140 }
141
142 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chen6a9361d2022-05-18 10:28:19 +0800143 g_ap_watcher_started_flag = 1;
you.chenc7357f22022-05-16 17:55:28 +0800144 }
145
you.chen87ff5172022-05-06 11:30:57 +0800146 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
147 usleep(100*1000);
148 continue;
149 }
you.chenc7357f22022-05-16 17:55:28 +0800150 memset(msg_notify, 0, MAX_RET);
151 len = MAX_RET;
you.chen87ff5172022-05-06 11:30:57 +0800152 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
153 msg_notify[len+1] = '\0';
154 printf("ap------> %s\n", msg_notify);
155 if (g_ap_callback_func == NULL) {
156 continue;
157 }
158 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
159 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
160 }
161 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
162 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
163 }
164 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
165 } // end while (g_ap_watcher_stop_flag == 0)
you.chen01276462022-05-25 10:09:47 +0800166 if (lynq_wpa_ctrl != NULL) {
167 wpa_ctrl_detach(lynq_wpa_ctrl);
168 wpa_ctrl_close(lynq_wpa_ctrl);
169 }
qs.xiong99b48d62022-04-07 05:41:29 -0400170}
171
you.chen87ff5172022-05-06 11:30:57 +0800172static void STAWatcherThreadProc() {
173 size_t len = MAX_RET;
174 char msg_notify[MAX_RET];
175 char *pReason;
176 error_number_s error;
qs.xiongd189c542022-03-31 00:58:23 -0400177
you.chenc7357f22022-05-16 17:55:28 +0800178 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen01276462022-05-25 10:09:47 +0800179 g_sta_watcher_stop_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +0800180
181 while (g_sta_watcher_stop_flag == 0) {
you.chenc7357f22022-05-16 17:55:28 +0800182 if (lynq_wpa_ctrl == NULL) {
183 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
184 if (lynq_wpa_ctrl == NULL) {
185 usleep(100*1000);
186 continue;
187 }
188
189 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chen6a9361d2022-05-18 10:28:19 +0800190 g_sta_watcher_started_flag = 1;
you.chenc7357f22022-05-16 17:55:28 +0800191 }
192
you.chen87ff5172022-05-06 11:30:57 +0800193 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
194 usleep(100*1000);
195 continue;
196 }
you.chenc7357f22022-05-16 17:55:28 +0800197 memset(msg_notify, 0, MAX_RET);
198 len = MAX_RET;
you.chen87ff5172022-05-06 11:30:57 +0800199 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
200 msg_notify[len+1] = '\0';
201 printf("sta ------> %s\n", msg_notify);
202 if (strstr(msg_notify, state_scan_result) != NULL) {
203 g_sta_scan_finish_flag = 1;
204 }
205
206 if (g_sta_callback_func == NULL) {
207 continue;
208 }
209 error = -1;
210 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
211 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
212 }
213 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
214 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
215 }
216 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
217 pReason = strstr(msg_notify, "reason=");
218 if (pReason != NULL) {
219 pReason += strlen("reason=");
you.chen01276462022-05-25 10:09:47 +0800220 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen87ff5172022-05-06 11:30:57 +0800221 error = LYNQ_TIME_OUT;
222 }
you.chen01276462022-05-25 10:09:47 +0800223 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen87ff5172022-05-06 11:30:57 +0800224 error = LYNQ_PSW_ERROR;
225 }
226 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
227 }
228 }
229 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
230 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
231 }
232 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
233 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
234 }
235 }
236 }
you.chen01276462022-05-25 10:09:47 +0800237 if (lynq_wpa_ctrl != NULL) {
238 wpa_ctrl_detach(lynq_wpa_ctrl);
239 wpa_ctrl_close(lynq_wpa_ctrl);
240 }
qs.xiongd189c542022-03-31 00:58:23 -0400241}
242
qs.xiong799dab02022-03-14 09:12:12 -0400243int lynq_wifi_enable(void)
244{
you.chen87ff5172022-05-06 11:30:57 +0800245 int ret = 0;
you.chenc7357f22022-05-16 17:55:28 +0800246 int i;
you.chen87ff5172022-05-06 11:30:57 +0800247 const char * cmd_check_service =
248 "state=`systemctl is-active wg870_drv_insmod.service`\n"
249 "[ \"\"$state == \"active\" ] && exit 0\n"
250 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
251// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
252// return 0;
253// }
qs.xiong799dab02022-03-14 09:12:12 -0400254
you.chen87ff5172022-05-06 11:30:57 +0800255 ret = system(cmd_check_service);
256 if (ret != 0) {
you.chenc7357f22022-05-16 17:55:28 +0800257 printf("service state %d\n", ret);
qs.xiong3e6e2e62022-09-29 11:37:32 +0800258 return -1;
you.chen87ff5172022-05-06 11:30:57 +0800259 }
qs.xiong8f6900c2022-10-11 10:47:08 +0800260/************only for GSW connected ap devices number no more than 8*************/
261 ret = system("wl maxassoc 8");
262 if(ret != 0){
263 printf("limit connect device cmd error\n");
264 return -1;
265 }
266/************only for GSW connected ap devices number no more than 8 by qs.xiong 20221011*************/
you.chenc7357f22022-05-16 17:55:28 +0800267 for (i=0; i<10; i++) {
you.chen6a9361d2022-05-18 10:28:19 +0800268 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chenc7357f22022-05-16 17:55:28 +0800269 break;
270 }
271 usleep(300*1000);
272 }
273
274 if (i >= 10) {
qs.xiong3e6e2e62022-09-29 11:37:32 +0800275 return -1;
you.chenc7357f22022-05-16 17:55:28 +0800276 }
277
you.chenfcea58c2022-06-06 17:18:18 +0800278 //@todo delete add temp check for socket avilable start (20220606)
279 for (i=0; i<60; i++)
280 {
281 if (system("netstat -an | grep -q DGRAM") == 0) {
282 break;
283 }
284 sleep(1);
285 }
286
287 if (i >= 60)
288 {
qs.xiong3e6e2e62022-09-29 11:37:32 +0800289 return -1;
you.chenfcea58c2022-06-06 17:18:18 +0800290 }
291 //@todo delete add temp check for socket avilable end (20220606)
292
you.chen6a9361d2022-05-18 10:28:19 +0800293 if (0 != system("ifconfig | grep -q ap0")) {
you.chenc7357f22022-05-16 17:55:28 +0800294 system("connmanctl enable wifi");
you.chen6a9361d2022-05-18 10:28:19 +0800295 usleep(300*1000);
you.chenbe9e2c12022-06-07 10:36:00 +0800296 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chenc7357f22022-05-16 17:55:28 +0800297 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chen6a9361d2022-05-18 10:28:19 +0800298 usleep(300*1000);
you.chenc7357f22022-05-16 17:55:28 +0800299 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chen6a9361d2022-05-18 10:28:19 +0800300 usleep(300*1000);
you.chenbe9e2c12022-06-07 10:36:00 +0800301 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chenc7357f22022-05-16 17:55:28 +0800302 }
303
you.chen87ff5172022-05-06 11:30:57 +0800304 if (g_ap_watcher_pid == 0 ) {
305 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
306 if(ret<0){
qs.xiong3e6e2e62022-09-29 11:37:32 +0800307 return -1;
you.chen87ff5172022-05-06 11:30:57 +0800308 }
309 }
310
311 if (g_sta_watcher_pid == 0 ) {
312 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
313 if(ret<0){
qs.xiong3e6e2e62022-09-29 11:37:32 +0800314 return -1;
you.chen87ff5172022-05-06 11:30:57 +0800315 }
316 }
317
you.chen6a9361d2022-05-18 10:28:19 +0800318 for (i=0; i<10; i++) {
319 usleep(300*1000);
320 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
321 break;
322 }
323 }
324
you.chen87ff5172022-05-06 11:30:57 +0800325 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -0500326}
327
qs.xiong799dab02022-03-14 09:12:12 -0400328int lynq_wifi_disable(void)
329{
you.chen87ff5172022-05-06 11:30:57 +0800330 g_ap_watcher_stop_flag = 1;
331 g_sta_watcher_stop_flag = 1;
332 if (g_ap_watcher_pid != 0)
333 pthread_join(g_ap_watcher_pid, NULL);
334 if (g_sta_watcher_pid != 0)
335 pthread_join(g_sta_watcher_pid, NULL);
336 if (g_lynq_wpa_ctrl[0] != NULL)
337 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
338 if (g_lynq_wpa_ctrl[1] != NULL)
339 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
340 g_ap_watcher_pid = 0;
341 g_sta_watcher_pid = 0;
342 g_lynq_wpa_ctrl[0] = NULL;
343 g_lynq_wpa_ctrl[1] = NULL;
344 system("systemctl stop wg870_drv_insmod.service");
qs.xiong8d42bb92022-03-02 09:43:11 -0500345 return 0;
346}
qs.xiong799dab02022-03-14 09:12:12 -0400347
you.chen87ff5172022-05-06 11:30:57 +0800348static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
349
350 char lynq_cmd_get[128]={0};
351
352 if (out_put == NULL) {
353 printf("output ptr is null\n");
354 return -1;
355 }
356 if (param_name == NULL) {
357 printf("param ptr is null");
358 return -1;
359 }
360 if (param_name[0] == '\0') {
361 printf("param is empty");
362 return -1;
363 }
364
365 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
366
367 CHECK_WPA_CTRL(interface);
368
369 DO_REQUEST(lynq_cmd_get);
370
371 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
372 return -1;
373 }
374
you.chen6a9361d2022-05-18 10:28:19 +0800375// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen87ff5172022-05-06 11:30:57 +0800376 memcpy(out_put, cmd_reply, reply_len + 1);
377 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500378}
qs.xiong799dab02022-03-14 09:12:12 -0400379
you.chen87ff5172022-05-06 11:30:57 +0800380static int lynq_split(char * str, int len, char delimiter, char * results[]) {
381 int ret = 0;
382 char * end = str + len - 1;
383 results[ret++] = str;
384 while(str < end) {
385 if (*str == delimiter) {
386 *str++ = '\0';
387 results[ret++] = str;
388 continue;
389 }
390 str++;
391 }
392 if (*str == delimiter) {
393 *str = '\0';
394 }
qs.xiong799dab02022-03-14 09:12:12 -0400395
you.chen87ff5172022-05-06 11:30:57 +0800396 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -0500397}
398
you.chen87ff5172022-05-06 11:30:57 +0800399static void trim_space(char * p, int count) {
400 char * begin = p;
401 p += count;
402 printf("%C-%C||\n", *begin, *p);
403 while (p >= begin ) {
404 if (*p == ' ') {
405 *p-- = '\0';
406 }
407 else {
408 break;
409 }
410 }
411}
412
413static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
414 FILE * fp;
415 int len, ret;
416 int count, count_words, index;
417 int mac_start, mac_end;
418 int ip_start, ip_end;
419 char *split_lines[128] = {0};
420 char *buff;
421 const char * ip_header = "IP address";
422 const char * mac_header = "HW address";
423 const char * zero_mac = "00:00:00:00:00:00";
424
425 fp = fopen("/proc/net/arp", "rb");
426 if (NULL == fp) {
427 printf("open file fail\n");
428 return -1;
429 }
430
431 buff = alloca(MAX_RET);
432 fseek(fp, 0, SEEK_SET);
433 len = fread(buff, 1, MAX_RET, fp);
434 fclose(fp);
435 if (len <= 0) {
436 printf("read file fail\n");
437 return -1;
438 }
439 printf("file : %s\n", buff);
440
441 count = lynq_split(buff, len, '\n', split_lines);
442 printf("----- %s\n", split_lines[0]);
443
444 mac_end = 0;
445 count_words = strlen(split_lines[0]);
446 if (strstr(split_lines[0], mac_header) != NULL) {
447 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
448 mac_end = mac_start + strlen(mac_header) + 1;
449 while (mac_end < count_words) {
450 if (split_lines[0][mac_end] != ' ') {
451 break;
452 }
453 mac_end++;
454 }
455 }
456
457 ip_end = 0;
458 if (strstr(split_lines[0], ip_header) != NULL) {
459 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
460 ip_end = ip_start + strlen(ip_header) + 1;
461 while (ip_end < count_words) {
462 if (split_lines[0][ip_end] != ' ') {
463 break;
464 }
465 ip_end++;
466 }
467 }
468
469 if (mac_end == 0 || ip_end == 0) {
470 return 0;
471 }
472
473 ret = 0;
474 for(index = 1;index < count; index++) {
475 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
476 continue;
477 }
478 mac_list[ret] = malloc(mac_end - mac_start + 1);
479 ip_list[ret] = malloc(ip_end - ip_start + 1);
480 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
481 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
482 trim_space(mac_list[ret], mac_end - mac_start - 1);
483 trim_space(ip_list[ret], ip_end - ip_start - 1);
484 ret++;
485 }
486
487 return ret;
488}
489
you.chen61c7aee2022-08-06 17:01:16 +0800490static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
491{
492 int i;
493 if (mac_list != NULL && mac_cnt > 0) {
494 for(i = 0; i< mac_cnt; i++)
495 {
496 if (NULL != mac_list[i])
497 {
498 free(mac_list[i]);
499 mac_list[i] = NULL;
500 }
501 }
502 }
503 if (ip_list != NULL && ip_cnt > 0) {
504 for(i = 0; i< mac_cnt; i++)
505 {
506 if (NULL != ip_list[i])
507 {
508 free(ip_list[i]);
509 ip_list[i] = NULL;
510 }
511 }
512 }
513}
514
you.chen87ff5172022-05-06 11:30:57 +0800515static int get_hostname_by_ip(char *ip, char *hostname) {
516 struct in_addr addr ={0};
517 struct hostent *ht;
518
519 if (ip == NULL || *ip == '\0' || hostname == NULL) {
520 return -1;
521 }
522
523 if (inet_aton(ip, &addr) == 0) {
524 printf("---inet_aton fail\n");
525 return -1;
526 }
527
528 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
529
530 if (ht == NULL) {
531 printf("---gethostbyaddr fail\n");
532 herror(NULL);
533 return -1;
534 }
535
536 strcpy(hostname, ht->h_name);
537
538 return 0;
539}
540
541static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
542{
543 int count, index, words_count;
544 char * split_lines[128]= {0};
545 char * split_words[128] = {0};
546 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
547
548 CHECK_WPA_CTRL(ap_sta);
549
550 DO_REQUEST(lynq_wifi_list_networks);
551
552 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
553
554 //@todo check ssid field to compatible
555
556 ret = 0;
557 for(index=1; index < count; index++) {
558 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
559 if (words_count > 2) {
560 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
561 net_no_list[ret++] = atoi(split_words[0]);
562 }
563 }
564 }
565
566 return ret;
567}
568
569static int lynq_add_network(int ap_sta) {
you.chenc7357f22022-05-16 17:55:28 +0800570 size_t i=0;
you.chen87ff5172022-05-06 11:30:57 +0800571 CHECK_WPA_CTRL(ap_sta);
572 const char *lynq_wifi_add_network = "ADD_NETWORK";
573
574 DO_REQUEST(lynq_wifi_add_network);
you.chen5e363602022-05-08 12:20:18 +0800575 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen87ff5172022-05-06 11:30:57 +0800576 return -1;
577 }
578
you.chenc7357f22022-05-16 17:55:28 +0800579 for(i=0;i<reply_len;i++) {
you.chen87ff5172022-05-06 11:30:57 +0800580 if(cmd_reply[i] == '\n') {
581 cmd_reply[i] = '\0';
582 break;
583 }
584 }
585 return atoi(cmd_reply);
586}
you.chen5e363602022-05-08 12:20:18 +0800587
you.chen87ff5172022-05-06 11:30:57 +0800588static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
589{
590 int count, index;
591 int net_no_list[128];
592
593
594 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
595 for (index=0; index < count; index++) {
596 if (net_no_list[index] == net_no) {
597 return 0;
598 }
599 }
600
601 if (count >= 1)
602 index = net_no_list[count - 1];
603 else
604 index = -1;
605
606 while (index < net_no ) {
607 index = lynq_add_network(ap_sta);
608 if (index >= net_no) { // required network no created
609 return 0;
610 }
you.chen5e363602022-05-08 12:20:18 +0800611 else if( index < 0) {
612 printf("add network fail\n");
613 return -1;
614 }
you.chen87ff5172022-05-06 11:30:57 +0800615 }
616
617 if (index < 0)
618 return -1;
619
620 return 0;
621}
622
623static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
624 if (freq > 5000 && freq < 6000) {
625 return LYNQ_WIFI_5G_band;
626 }
627 else if (freq > 2000 && freq < 3000) {
628 return LYNQ_WIFI_2G_band;
629 }
630 return LYNQ_WIFI_2_and_5G_band;
631}
632
633static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
634 if (key_mgmt != NULL) {
635 if (memcmp( key_mgmt, "NONE", 4) == 0) {
636 return LYNQ_WIFI_AUTH_OPEN;
637 }
638 else if (memcmp( key_mgmt, "WEP", 3) == 0){
639 return LYNQ_WIFI_AUTH_WEP;
640 }
641 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
642 return LYNQ_WIFI_AUTH_WPA_PSK;
643 }
644 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
645 return LYNQ_WIFI_AUTH_WPA2_PSK;
646 }
647 }
648
649 return -1;
650}
651
652static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
653 if (flag != NULL) {
654 if (strstr( flag, "WPA2-PSK") != NULL){
655 return LYNQ_WIFI_AUTH_WPA2_PSK;
656 }
657 else if (strstr( flag, "WPA-PSK") != NULL){
658 return LYNQ_WIFI_AUTH_WPA_PSK;
659 }
660 else if (strstr( flag, "WEP") != NULL){
661 return LYNQ_WIFI_AUTH_WEP;
662 }
663 else if (strstr( flag, "NONE") != NULL) {
664 return LYNQ_WIFI_AUTH_OPEN;
665 }
666 }
667
668 return -1;
669}
670
671static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
672 switch (bw) {
673 case 10:
674 return LYNQ_WIFI_BANDWIDTH_HT10;
675 break;
676 case 20:
677 return LYNQ_WIFI_BANDWIDTH_HT20;
678 break;
679 case 40:
680 return LYNQ_WIFI_BANDWIDTH_HT40;
681 break;
682 case 80:
683 return LYNQ_WIFI_BANDWIDTH_HT80;
684 break;
685 default:
686 break;
687 }
688
689 return -1;
690}
691
692static int inner_get_status_info(int interface, curr_status_info *curr_state) {
693 int i, count;
694 char *p;
695 const char *lynq_status_cmd = "STATUS";
696 const char * FLAG_SSID = "ssid=";
697 const char * FLAG_SBSID = "bssid=";
698 const char * FLAG_KEY_MGMT = "key_mgmt=";
699 const char * FLAG_FREQ = "freq=";
700 const char * FLAG_STATE = "wpa_state=";
701 const char * FLAG_ID = "id=";
702 char *split_lines[128] = {0};
703
704 CHECK_WPA_CTRL(interface);
705
706 if (curr_state == NULL) {
707 return -1;
708 }
709
710 DO_REQUEST(lynq_status_cmd);
711
712 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
713
714 curr_state->net_no = -1;
715 ret = -1;
716 for(i=0; i < count; i++) {
717 if (curr_state->ap != NULL) {
you.chen87ff5172022-05-06 11:30:57 +0800718 p = strstr(split_lines[i], FLAG_SBSID);
719 if (p != NULL) {
720 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
721 ret = 0;
722 continue;
723 }
you.chendad3f9f2022-06-21 16:53:48 +0800724 p = strstr(split_lines[i], FLAG_SSID);
725 if (p != NULL) {
726 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
727 ret = 0;
728 continue;
729 }
you.chen87ff5172022-05-06 11:30:57 +0800730 p = strstr(split_lines[i], FLAG_KEY_MGMT);
731 if (p != NULL) {
you.chenb98d1cf2022-07-15 17:56:48 +0800732 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
733 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen87ff5172022-05-06 11:30:57 +0800734 ret = 0;
735 continue;
736 }
737 p = strstr(split_lines[i], FLAG_FREQ);
738 if (p != NULL) {
739 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
740 ret = 0;
741 continue;
742 }
743 } // end if (ap != NULL)
744 if (curr_state->state != NULL) {
745 p = strstr(split_lines[i], FLAG_STATE);
746 if (p != NULL) {
747 strcpy(curr_state->state, p + strlen(FLAG_STATE));
748 ret = 0;
749 continue;
750 }
751
752 } //end else if (state != NULL)
you.chen5e363602022-05-08 12:20:18 +0800753 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen87ff5172022-05-06 11:30:57 +0800754 ret = 0;
you.chenb98d1cf2022-07-15 17:56:48 +0800755 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen87ff5172022-05-06 11:30:57 +0800756 printf("net_no %d, -- %s\n", curr_state->net_no, p);
757 }
758 }
759
760 return ret;
761}
762
763
qs.xiongd189c542022-03-31 00:58:23 -0400764int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -0500765{
you.chen87ff5172022-05-06 11:30:57 +0800766 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong8d42bb92022-03-02 09:43:11 -0500767
you.chen87ff5172022-05-06 11:30:57 +0800768 if (ap_ssid == NULL) {
769 printf("ap_ssid is null\n");
770 return -1;
771 }
772 else {
773 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
774 }
qs.xiong799dab02022-03-14 09:12:12 -0400775
you.chen87ff5172022-05-06 11:30:57 +0800776 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
777 return -1;
778 }
qs.xiong799dab02022-03-14 09:12:12 -0400779
you.chen87ff5172022-05-06 11:30:57 +0800780 CHECK_IDX(idx, CTRL_AP);
781
782 CHECK_WPA_CTRL(CTRL_AP);
783
784 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
785
786 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
787 DO_OK_FAIL_REQUEST(cmd_save_config);
788
qs.xiong8d42bb92022-03-02 09:43:11 -0500789 return 0;
you.chen87ff5172022-05-06 11:30:57 +0800790
qs.xiong8d42bb92022-03-02 09:43:11 -0500791}
792
you.chen87ff5172022-05-06 11:30:57 +0800793int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -0500794{
you.chenc7357f22022-05-16 17:55:28 +0800795 int len;
you.chen87ff5172022-05-06 11:30:57 +0800796 CHECK_IDX(idx, CTRL_AP);
you.chenc7357f22022-05-16 17:55:28 +0800797 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
798 return -1;
799 len = strlen(ap_ssid);
800 if (ap_ssid[0] == '\"') {
801 memmove(ap_ssid, ap_ssid + 1, len - 1);
802 len -= 1;
803 }
804 if (len > 0 && ap_ssid[len-1] == '\"') {
805 ap_ssid[len-1] = '\0';
806 }
807 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500808}
809
qs.xiongd189c542022-03-31 00:58:23 -0400810int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -0500811{
you.chen87ff5172022-05-06 11:30:57 +0800812 char lynq_wifi_frequency_cmd[128]={0};
813 char lynq_cmd_mode[128]={0};
814 char lynq_cmd_slect[128]={0};
qs.xiong8d42bb92022-03-02 09:43:11 -0500815
you.chen87ff5172022-05-06 11:30:57 +0800816 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
817 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
818 }
qs.xiong99b48d62022-04-07 05:41:29 -0400819
you.chen87ff5172022-05-06 11:30:57 +0800820 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
821 return -1;
822 }
qs.xiong799dab02022-03-14 09:12:12 -0400823
you.chen87ff5172022-05-06 11:30:57 +0800824 CHECK_IDX(idx, CTRL_AP);
825
826 CHECK_WPA_CTRL(CTRL_AP);
827
828 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
829 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
830 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
831
you.chenc7357f22022-05-16 17:55:28 +0800832 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen87ff5172022-05-06 11:30:57 +0800833 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
834 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
835 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -0500836
qs.xiong799dab02022-03-14 09:12:12 -0400837 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500838}
839
qs.xiongd189c542022-03-31 00:58:23 -0400840int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -0500841{
you.chen87ff5172022-05-06 11:30:57 +0800842 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -0400843
you.chen87ff5172022-05-06 11:30:57 +0800844 CHECK_IDX(idx, CTRL_AP);
qs.xiongd189c542022-03-31 00:58:23 -0400845
you.chen87ff5172022-05-06 11:30:57 +0800846 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
847 return -1;
848 }
849 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongd189c542022-03-31 00:58:23 -0400850
851 return 0;
852}
853
qs.xiongd189c542022-03-31 00:58:23 -0400854int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
855{
you.chen87ff5172022-05-06 11:30:57 +0800856 CHECK_IDX(idx, CTRL_AP);
857 switch(bandwidth){
858 case LYNQ_WIFI_BANDWIDTH_HT10:
859 {
860 printf("bandwith [%d] not support now\n", bandwidth);
861 return -1;
862 }
863 case LYNQ_WIFI_BANDWIDTH_HT20:
864 {
865 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
866 system("wl down");
867 if (system(lynq_cmd_bandwith) != 0 ){
868 return -1;
869 }
870 system("wl up");
871 break;
872 }
873 case LYNQ_WIFI_BANDWIDTH_HT40:
874 {
875 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
876 sprintf(lynq_cmd_bandwith, "wl chanspec ");
877 system("wl down");
878 if (system(lynq_cmd_bandwith) != 0 ){
879 return -1;
880 }
881 system("wl up");
882 break;
883 }
884 case LYNQ_WIFI_BANDWIDTH_HT80:
885 {
886 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
887 system("wl down");
888 if (system(lynq_cmd_bandwith) != 0 ){
889 return -1;
890 }
891 system("wl up");
892 break;
893 }
894 default:
895 {
896 printf("auth type [%d] not support now\n", bandwidth);
897 return -1;
898 }
899 }
qs.xiongd189c542022-03-31 00:58:23 -0400900
901
you.chen87ff5172022-05-06 11:30:57 +0800902 return 0;
qs.xiongd189c542022-03-31 00:58:23 -0400903}
you.chen87ff5172022-05-06 11:30:57 +0800904
qs.xiongd189c542022-03-31 00:58:23 -0400905int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
906{
you.chen87ff5172022-05-06 11:30:57 +0800907 int count = 0;
908 int index = 0;
909 char *split_words[128] = {0};
910 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongd189c542022-03-31 00:58:23 -0400911
you.chen87ff5172022-05-06 11:30:57 +0800912 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -0500913
you.chen87ff5172022-05-06 11:30:57 +0800914 CHECK_WPA_CTRL(CTRL_AP);
915
916 DO_REQUEST(lynq_chanspec_cmd);
917
918 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
919 for(;index < count; index++) {
920 if (strncmp(split_words[index], "bw", 2) != 0) {
921 continue;
922 }
923
924 index++;
925 if (index >= count) {
926 return -1;
927 }
928
929 printf("bw %s\n", split_words[index]);
930 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
931 return 0;
932 }
933
934 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -0500935}
qs.xiong7cc23cb2022-04-14 03:50:45 -0400936
qs.xiongd189c542022-03-31 00:58:23 -0400937int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong8d42bb92022-03-02 09:43:11 -0500938{
you.chen87ff5172022-05-06 11:30:57 +0800939 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong6a886062022-04-14 06:17:01 -0400940
you.chen87ff5172022-05-06 11:30:57 +0800941 CHECK_IDX(idx, CTRL_AP);
qs.xiong6a886062022-04-14 06:17:01 -0400942
you.chen87ff5172022-05-06 11:30:57 +0800943 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong799dab02022-03-14 09:12:12 -0400944
you.chen87ff5172022-05-06 11:30:57 +0800945 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
946 return -1;
947 }
948
949 system("wl down");
950 if (system(lynq_cmd_channel) != 0 ){
951 return -1;
952 }
953 system("wl up");
954 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500955}
qs.xiong7cc23cb2022-04-14 03:50:45 -0400956
qs.xiongd189c542022-03-31 00:58:23 -0400957int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong8d42bb92022-03-02 09:43:11 -0500958{
you.chen87ff5172022-05-06 11:30:57 +0800959 int count = 0;
960 int index = 0;
961 char *split_words[128] = {0};
962 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongd189c542022-03-31 00:58:23 -0400963
you.chen87ff5172022-05-06 11:30:57 +0800964 CHECK_IDX(idx, CTRL_AP);
qs.xiong799dab02022-03-14 09:12:12 -0400965
you.chen87ff5172022-05-06 11:30:57 +0800966 CHECK_WPA_CTRL(CTRL_AP);
967
968 DO_REQUEST(lynq_chanspec_cmd);
969
970 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
971 for(;index < count; index++) {
972 printf("---- %s\n",split_words[index]);
973 if (strncmp(split_words[index], "channel", 2) != 0) {
974 continue;
975 }
976
977 index++;
978 if (index >= count) {
979 return -1;
980 }
981
982 *channel = atoi(split_words[index]);
983 return 0;
984 }
985
986 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -0500987}
988
989
you.chen87ff5172022-05-06 11:30:57 +0800990int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong8d42bb92022-03-02 09:43:11 -0500991{
you.chenc7357f22022-05-16 17:55:28 +0800992 char ssid[MAX_CMD] = {0};
993 int freq = 0;
994 char lynq_auth_cmd[64]={0};
995 char lynq_auth_alg_cmd[64]={0};
996 char lynq_psk_cmd[64]={0};
997 char lynq_pairwise_cmd[64]={0};
998 lynq_wifi_auth_s org_auth;
you.chen87ff5172022-05-06 11:30:57 +0800999 CHECK_IDX(idx, CTRL_AP);
1000
you.chenc7357f22022-05-16 17:55:28 +08001001 CHECK_WPA_CTRL(CTRL_AP);
1002
you.chen87ff5172022-05-06 11:30:57 +08001003 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1004 return -1;
1005 }
1006
you.chen01276462022-05-25 10:09:47 +08001007 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chenc7357f22022-05-16 17:55:28 +08001008 if (org_auth == auth) {
1009 return 0;
1010 }
1011 else {
1012 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1013 ssid[0] = '\0';
1014 }
1015 lynq_wifi_ap_frequency_get(idx, &freq);
1016
1017 DO_OK_FAIL_REQUEST(cmd_disconnect);
1018 DO_OK_FAIL_REQUEST(cmd_remove_all);
1019 if (ssid[0] != '\0') {
1020 lynq_wifi_ap_ssid_set(idx, ssid);
1021 }
1022 if (freq != 0) {
1023 lynq_wifi_ap_frequency_set(idx, freq);
1024 }
1025 }
1026 }
you.chen87ff5172022-05-06 11:30:57 +08001027
qs.xiong8d42bb92022-03-02 09:43:11 -05001028 switch(auth){
qs.xiongd189c542022-03-31 00:58:23 -04001029 case LYNQ_WIFI_AUTH_OPEN:
you.chenc7357f22022-05-16 17:55:28 +08001030 {
you.chen87ff5172022-05-06 11:30:57 +08001031 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen01276462022-05-25 10:09:47 +08001032 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong8d42bb92022-03-02 09:43:11 -05001033
you.chen87ff5172022-05-06 11:30:57 +08001034 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -05001035 break;
1036 }
you.chenc7357f22022-05-16 17:55:28 +08001037 case LYNQ_WIFI_AUTH_WEP:
1038 {
1039 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen01276462022-05-25 10:09:47 +08001040 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chenc7357f22022-05-16 17:55:28 +08001041 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1042
1043 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1044 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1045 break;
1046 }
qs.xiongd189c542022-03-31 00:58:23 -04001047 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen87ff5172022-05-06 11:30:57 +08001048 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong8d42bb92022-03-02 09:43:11 -05001049 {
you.chen87ff5172022-05-06 11:30:57 +08001050 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1051 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1052 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1053 }
1054 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chenc7357f22022-05-16 17:55:28 +08001055 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chen5e363602022-05-08 12:20:18 +08001056 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen87ff5172022-05-06 11:30:57 +08001057 }
1058// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1059// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1060 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong8d42bb92022-03-02 09:43:11 -05001061
you.chen87ff5172022-05-06 11:30:57 +08001062 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1063 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1064 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -05001065 break;
you.chen87ff5172022-05-06 11:30:57 +08001066 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001067 default:
you.chen87ff5172022-05-06 11:30:57 +08001068 {
1069 printf("auth type [%d] not support now\n", auth);
qs.xiong8d42bb92022-03-02 09:43:11 -05001070 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001071 }
1072 }
you.chenc7357f22022-05-16 17:55:28 +08001073 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -05001074
qs.xiong8d42bb92022-03-02 09:43:11 -05001075 return 0;
1076}
1077
you.chen87ff5172022-05-06 11:30:57 +08001078int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001079{
you.chen87ff5172022-05-06 11:30:57 +08001080 char lynq_auth_str[MAX_RET] = {0};
you.chenc7357f22022-05-16 17:55:28 +08001081 char lynq_auth_alg_str[MAX_RET] = {0};
1082 char lynq_proto_str[MAX_RET] = {0};
you.chen87ff5172022-05-06 11:30:57 +08001083
1084 CHECK_IDX(idx, CTRL_AP);
1085
1086 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1087 return -1;
1088 }
1089
1090 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chenc7357f22022-05-16 17:55:28 +08001091 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1092 *auth = LYNQ_WIFI_AUTH_OPEN;
1093 }
1094 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1095 *auth = LYNQ_WIFI_AUTH_WEP;
1096 }
1097 else {
1098 *auth = LYNQ_WIFI_AUTH_OPEN;
1099 }
you.chen87ff5172022-05-06 11:30:57 +08001100 }
you.chen01276462022-05-25 10:09:47 +08001101 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chen6a9361d2022-05-18 10:28:19 +08001102 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen01276462022-05-25 10:09:47 +08001103 *auth = -1;
you.chenc7357f22022-05-16 17:55:28 +08001104 }
1105 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1106 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1107 }
1108 else {
1109 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1110 }
you.chen87ff5172022-05-06 11:30:57 +08001111 }
you.chen01276462022-05-25 10:09:47 +08001112 else {
1113 *auth = -1;
1114 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001115
you.chenc7357f22022-05-16 17:55:28 +08001116 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001117}
qs.xiong799dab02022-03-14 09:12:12 -04001118
qs.xiong799dab02022-03-14 09:12:12 -04001119
qs.xiongd189c542022-03-31 00:58:23 -04001120int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001121{
you.chen87ff5172022-05-06 11:30:57 +08001122 char LYNQ_WIFI_CMD[128]={0};
1123 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1124 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongd189c542022-03-31 00:58:23 -04001125
you.chen87ff5172022-05-06 11:30:57 +08001126 CHECK_IDX(idx, CTRL_AP);
1127
1128 CHECK_WPA_CTRL(CTRL_AP);
1129
1130// system("connmanctl enable wifi");
1131// system("connmanctl tether wifi on cy-test 12345678");
1132// system("ifconfig wlan0 down");
1133// system("ifconfig wlan0 up");
1134// system("ifconfig wlan0 up");
1135
1136 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1137 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1138
1139 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1140 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1141
qs.xiong8d42bb92022-03-02 09:43:11 -05001142 return 0;
1143}
1144
qs.xiongd189c542022-03-31 00:58:23 -04001145int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001146{
you.chen87ff5172022-05-06 11:30:57 +08001147 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong8d42bb92022-03-02 09:43:11 -05001148}
1149
qs.xiongd189c542022-03-31 00:58:23 -04001150int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001151{
you.chen87ff5172022-05-06 11:30:57 +08001152 char LYNQ_WIFI_CMD[128]={0};
qs.xiong799dab02022-03-14 09:12:12 -04001153
you.chen87ff5172022-05-06 11:30:57 +08001154 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001155
you.chen87ff5172022-05-06 11:30:57 +08001156 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001157
you.chen87ff5172022-05-06 11:30:57 +08001158 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1159
1160 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1161
you.chenefc7f3f2022-05-06 17:50:16 +08001162// system("connmanctl tether wifi off");
you.chen87ff5172022-05-06 11:30:57 +08001163
qs.xiong8d42bb92022-03-02 09:43:11 -05001164 return 0;
1165}
qs.xiong799dab02022-03-14 09:12:12 -04001166
qs.xiongd189c542022-03-31 00:58:23 -04001167int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001168{
you.chen87ff5172022-05-06 11:30:57 +08001169 char lynq_disable_cmd[128] = {0};
1170 char lynq_select_cmd[128] = {0};
1171 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong99b48d62022-04-07 05:41:29 -04001172
you.chen87ff5172022-05-06 11:30:57 +08001173 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001174
you.chen87ff5172022-05-06 11:30:57 +08001175 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001176
you.chen87ff5172022-05-06 11:30:57 +08001177 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1178 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1179
1180 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1181 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1182 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong799dab02022-03-14 09:12:12 -04001183
1184 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001185}
1186
qs.xiongd189c542022-03-31 00:58:23 -04001187int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001188{
you.chen87ff5172022-05-06 11:30:57 +08001189 char lynq_disable_cmd[128] = {0};
1190 char lynq_select_cmd[128] = {0};
1191 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongd189c542022-03-31 00:58:23 -04001192
you.chen87ff5172022-05-06 11:30:57 +08001193 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001194
you.chen87ff5172022-05-06 11:30:57 +08001195 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001196
you.chen87ff5172022-05-06 11:30:57 +08001197 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1198 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1199
1200 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1201 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1202 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -05001203
1204 return 0;
1205}
qs.xiongd189c542022-03-31 00:58:23 -04001206
you.chen87ff5172022-05-06 11:30:57 +08001207int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05001208{
qs.xiongd189c542022-03-31 00:58:23 -04001209 int pass_len;
you.chenc7357f22022-05-16 17:55:28 +08001210 char lynq_tmp_cmd[MAX_CMD] = {0};
1211 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong6ad4d7d2022-06-27 11:34:53 +08001212 if( password == NULL ){
1213 return -1;
1214 }
1215 pass_len=strlen(password);
you.chenc7357f22022-05-16 17:55:28 +08001216 lynq_wifi_auth_s auth = -1;
you.chen87ff5172022-05-06 11:30:57 +08001217 if(pass_len < 8 || pass_len >= 64){
qs.xiongd189c542022-03-31 00:58:23 -04001218 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001219 }
qs.xiongd189c542022-03-31 00:58:23 -04001220
you.chen87ff5172022-05-06 11:30:57 +08001221 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001222
you.chenc7357f22022-05-16 17:55:28 +08001223 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1224 return -1;
1225 }
1226 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1227 return -1;
1228 }
1229
you.chen87ff5172022-05-06 11:30:57 +08001230 CHECK_WPA_CTRL(CTRL_AP);
1231
you.chenc7357f22022-05-16 17:55:28 +08001232 if (auth == LYNQ_WIFI_AUTH_WEP) {
1233 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1234 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1235 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1236 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1237 }
1238 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1239 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1240 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1241 }
1242 else {
1243 return -1;
1244 }
you.chen87ff5172022-05-06 11:30:57 +08001245
you.chen87ff5172022-05-06 11:30:57 +08001246 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong99b48d62022-04-07 05:41:29 -04001247
qs.xiongd189c542022-03-31 00:58:23 -04001248 return 0;
1249}
1250
you.chen87ff5172022-05-06 11:30:57 +08001251int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongd189c542022-03-31 00:58:23 -04001252{
you.chen87ff5172022-05-06 11:30:57 +08001253 FILE * fp;
1254 int len, ret;
1255 int count, index;
1256 char *split_lines[128] = {0};
1257 char *buff, *p;
qs.xiong99b48d62022-04-07 05:41:29 -04001258
you.chen87ff5172022-05-06 11:30:57 +08001259 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001260
you.chen87ff5172022-05-06 11:30:57 +08001261 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1262// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1263 if (NULL == fp) {
1264 printf("open file fail\n");
1265 return -1;
1266 }
qs.xiong99b48d62022-04-07 05:41:29 -04001267
you.chen87ff5172022-05-06 11:30:57 +08001268 buff = alloca(MAX_RET);
1269 fseek(fp, 0, SEEK_SET);
1270 len = fread(buff, 1, MAX_RET, fp);
1271 fclose(fp);
qs.xiong99b48d62022-04-07 05:41:29 -04001272
you.chen87ff5172022-05-06 11:30:57 +08001273 for(index=0; index < len; index ++) {
1274 if (memcmp(buff + index, "network={", 9) != 0) {
1275 continue;
1276 }
1277 p = buff + index + 9;
1278 for (; index < len; index ++ ) {
1279 if (buff[index] != '}') {
1280 continue;
1281 }
1282 buff[index] = '\0';
1283 break;
1284 }
1285 len = buff + index - p;
1286 }
1287
1288 count = lynq_split(p, len, '\n', split_lines);
1289
1290 ret = -1;
1291 for(index=0; index < count; index++) {
1292 p = strstr(split_lines[index], "psk=");
you.chenc7357f22022-05-16 17:55:28 +08001293 if (p != NULL) {
1294 p += 4;
1295 if (*p == '\"') {
1296 p++;
1297 }
you.chen87ff5172022-05-06 11:30:57 +08001298 }
you.chenc7357f22022-05-16 17:55:28 +08001299 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1300 p += 9;
1301 if (*p == '\"') {
1302 p++;
1303 }
1304 }
1305 else {
1306 continue;
you.chen87ff5172022-05-06 11:30:57 +08001307 }
1308
1309 strcpy(password, p);
1310
1311 while(*password != '\0') {
1312 if (*password == '\"') {
1313 *password = '\0';
1314 break;
1315 }
1316 password++;
1317 }
1318 ret = 0;
1319 break;
1320 } //end for(index=0; index < count; index++)
1321
1322 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -05001323}
1324
you.chen87ff5172022-05-06 11:30:57 +08001325static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1326 char lynq_auth_str[MAX_RET] = {0};
you.chen5e363602022-05-08 12:20:18 +08001327 char lynq_proto_str[MAX_RET] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04001328
you.chen87ff5172022-05-06 11:30:57 +08001329 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1330 return -1;
1331 }
1332
1333 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chen5e363602022-05-08 12:20:18 +08001334
1335 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1336 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1337 if (strcmp(lynq_proto_str, "RSN") == 0) {
1338 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1339 }
1340 }
1341 }
you.chen87ff5172022-05-06 11:30:57 +08001342 return 0;
1343}
1344
1345int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05001346{
you.chen87ff5172022-05-06 11:30:57 +08001347 int pass_len, net_no, count, index;
1348 char lynq_tmp_cmd[300]={0};
1349 int net_no_list[128];
1350 lynq_wifi_auth_s net_auth;
1351 pass_len=strlen(password);
1352 if(pass_len < 8 || pass_len >= 64){
1353 return -1;
1354 }
1355
1356 CHECK_IDX(idx, CTRL_STA);
1357
1358 net_no = -1;
1359 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1360
1361 for (index=0; index < count; index++) {
1362 net_auth = -1;
1363 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1364 net_no = net_no_list[index];
1365 break;
1366 }
1367 }
1368
1369 if (net_no < 0) {
1370 return -1;
1371 }
1372
1373 CHECK_WPA_CTRL(CTRL_STA);
1374
1375 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1376
1377 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1378 DO_OK_FAIL_REQUEST(cmd_save_config);
1379
1380 return 0;
1381}
1382
1383int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1384
1385 FILE * fp;
you.chen0c938042022-08-06 16:59:10 +08001386 int len, ret, network_len;
you.chen87ff5172022-05-06 11:30:57 +08001387 int count, index;
1388 char *split_lines[128] = {0};
1389 char *buff, *p;
1390
you.chen0c938042022-08-06 16:59:10 +08001391 network_len = 0;
1392 p = NULL;
1393
you.chen87ff5172022-05-06 11:30:57 +08001394 CHECK_IDX(idx, CTRL_STA);
1395
you.chen0c938042022-08-06 16:59:10 +08001396 if (NULL == password) {
1397 printf("bad param\n");
1398 return -1;
1399 }
1400
you.chen87ff5172022-05-06 11:30:57 +08001401 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1402 if (NULL == fp) {
1403 printf("open file fail\n");
1404 return -1;
1405 }
1406
1407 buff = alloca(MAX_RET);
1408 fseek(fp, 0, SEEK_SET);
1409 len = fread(buff, 1, MAX_RET, fp);
1410 fclose(fp);
1411
1412 for(index=0; index < len; index ++) {
1413 for(; index < len; index ++) {
1414 if (memcmp(buff + index, "network={", 9) != 0) {
1415 continue;
1416 }
1417 p = buff + index + 9;
1418 for (; index < len; index ++ ) {
1419 if (buff[index] != '}') {
1420 continue;
1421 }
1422 buff[index] = '\0';
1423 break;
1424 }
you.chen0c938042022-08-06 16:59:10 +08001425 network_len = buff + index - p;
1426 break;
you.chen87ff5172022-05-06 11:30:57 +08001427 }
1428
1429 if (strstr(p, ap->ap_ssid) != NULL) {
1430 break;
1431 }
1432 }
1433
you.chen0c938042022-08-06 16:59:10 +08001434 if (index >= len || NULL == p || network_len <= 0) {
you.chen87ff5172022-05-06 11:30:57 +08001435 return -1;
1436 }
1437
you.chen0c938042022-08-06 16:59:10 +08001438 count = lynq_split(p, network_len, '\n', split_lines);
you.chen87ff5172022-05-06 11:30:57 +08001439
1440 ret = -1;
1441 for(index=0; index < count; index++) {
1442 p = strstr(split_lines[index], "psk=");
you.chenc7357f22022-05-16 17:55:28 +08001443 if (p != NULL) {
1444 p += 4;
1445 if (*p == '\"') {
1446 p++;
1447 }
you.chen87ff5172022-05-06 11:30:57 +08001448 }
you.chenc7357f22022-05-16 17:55:28 +08001449 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1450 p += 9;
1451 if (*p == '\"') {
1452 p++;
1453 }
1454 }
1455 else {
1456 continue;
you.chen87ff5172022-05-06 11:30:57 +08001457 }
1458
1459 strcpy(password, p);
1460
1461 while(*password != '\0') {
1462 if (*password == '\"') {
1463 *password = '\0';
1464 break;
1465 }
1466 password++;
1467 }
1468 ret = 0;
1469 break;
1470 } //end for(index=0; index < count; index++)
1471
1472 return ret;
1473}
1474
1475static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1476{
qs.xiong99b48d62022-04-07 05:41:29 -04001477 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongd189c542022-03-31 00:58:23 -04001478
you.chen87ff5172022-05-06 11:30:57 +08001479 if (sta_ssid == NULL) {
1480 printf("sta_ssid is null\n");
qs.xiongd189c542022-03-31 00:58:23 -04001481 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001482 }
1483
1484 CHECK_WPA_CTRL(CTRL_STA);
1485
1486 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1487
1488 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1489// DO_OK_FAIL_REQUEST(cmd_save_config);
1490
qs.xiong8d42bb92022-03-02 09:43:11 -05001491 return 0;
1492
1493}
1494
you.chen87ff5172022-05-06 11:30:57 +08001495static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong8d42bb92022-03-02 09:43:11 -05001496{
you.chen87ff5172022-05-06 11:30:57 +08001497 char lynq_disable_cmd[128]={0};
1498 char lynq_select_cmd[128]={0};
1499
1500 CHECK_WPA_CTRL(CTRL_STA);
1501
1502 if (save != 0) {
you.chen948e0032022-06-07 18:01:16 +08001503 if (start_flag != 0)
1504 {
1505 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1506 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1507 }
1508 else
1509 {
1510 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1511 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1512 }
you.chen87ff5172022-05-06 11:30:57 +08001513 DO_OK_FAIL_REQUEST(cmd_save_config);
1514 }
1515
1516 if (start_flag == 0) {
you.chenc7357f22022-05-16 17:55:28 +08001517 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen87ff5172022-05-06 11:30:57 +08001518 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1519 }
1520 else {
1521 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1522 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1523 }
1524
1525 return 0;
1526}
1527
1528int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1529{
1530 CHECK_IDX(idx, CTRL_STA);
1531
you.chenc7357f22022-05-16 17:55:28 +08001532 curr_status_info curr_state;
1533 ap_info_s ap_info;
1534 curr_state.ap = &ap_info;
1535 curr_state.state = NULL;
1536
1537 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1538 strcpy(sta_ssid, ap_info.ap_ssid);
1539 return 0;
1540 }
1541
1542 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001543}
1544
1545int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1546{
you.chen61c7aee2022-08-06 17:01:16 +08001547 scan_info_s *scan_list = NULL;
1548 saved_ap_info_s *save_list = NULL;
you.chen87ff5172022-05-06 11:30:57 +08001549 int scan_len=0;
1550 int save_len=0;
1551 int best_index = -1;
1552 int best_scan_index = -1;
1553 int best_rssi = 0;
you.chen61c7aee2022-08-06 17:01:16 +08001554 int i, j, ret;
1555
1556 ret = -1;
you.chen87ff5172022-05-06 11:30:57 +08001557
1558 CHECK_IDX(idx, CTRL_STA);
1559 if (info == NULL) {
1560 return -1;
1561 }
1562
1563 curr_status_info curr_state;
1564 ap_info_s ap_info;
you.chen61c7aee2022-08-06 17:01:16 +08001565 char status[64];
you.chen87ff5172022-05-06 11:30:57 +08001566
you.chen61c7aee2022-08-06 17:01:16 +08001567 memset(&ap_info, 0, sizeof (ap_info));
1568 memset(status, 0, sizeof (status));
1569
1570 curr_state.ap = &ap_info;
1571 curr_state.state = status;
1572
1573 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen87ff5172022-05-06 11:30:57 +08001574 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen61c7aee2022-08-06 17:01:16 +08001575 if (strcmp(status, STATE_COMPLETED) == 0)
1576 {
1577 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1578 }
1579 else
1580 {
1581 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1582 }
you.chen87ff5172022-05-06 11:30:57 +08001583 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen61c7aee2022-08-06 17:01:16 +08001584 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen87ff5172022-05-06 11:30:57 +08001585 return 0;
1586 }
1587
you.chen61c7aee2022-08-06 17:01:16 +08001588 lynq_wifi_sta_start_scan(idx);
1589
you.chen87ff5172022-05-06 11:30:57 +08001590 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen61c7aee2022-08-06 17:01:16 +08001591 if (NULL != scan_list)
1592 {
1593 free(scan_list);
1594 }
you.chen87ff5172022-05-06 11:30:57 +08001595 return -1;
1596 }
1597
1598 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen61c7aee2022-08-06 17:01:16 +08001599 if (NULL != scan_list)
1600 {
1601 free(scan_list);
1602 }
1603 if (NULL != save_list)
1604 {
1605 free(save_list);
1606 }
you.chen87ff5172022-05-06 11:30:57 +08001607 return -1;
1608 }
1609
1610 for (i=0; i < save_len; i++) {
1611 for (j=0; j < scan_len; j++) {
1612 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1613 && save_list[i].base_info.auth == scan_list[j].auth) {
1614 if (best_rssi == 0) {
you.chen61c7aee2022-08-06 17:01:16 +08001615 best_index = i;
you.chen87ff5172022-05-06 11:30:57 +08001616 best_rssi = scan_list[j].rssi;
1617 }
1618 else if (best_rssi > scan_list[j].rssi) {
1619 best_index = i;
1620 best_scan_index = j;
1621 best_rssi = scan_list[j].rssi;
1622 }
1623 break;
1624 }
1625 }
1626 }
1627
1628 if (best_index >= 0) {
1629 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1630 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1631 info->rssi = best_rssi;
you.chen61c7aee2022-08-06 17:01:16 +08001632 ret = 0;
you.chen87ff5172022-05-06 11:30:57 +08001633 }
1634
you.chen61c7aee2022-08-06 17:01:16 +08001635 if (NULL != scan_list)
1636 {
1637 free(scan_list);
1638 }
1639 if (NULL != save_list)
1640 {
1641 free(save_list);
1642 }
1643
1644 return ret;
you.chen87ff5172022-05-06 11:30:57 +08001645}
1646
1647static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1648{
1649 char lynq_auth_cmd[64]={0};
1650 char lynq_ket_mgmt_cmd[64]={0};
1651 char lynq_pairwise_cmd[64]={0};
1652 char lynq_psk_cmd[64]={0};
1653
1654 CHECK_WPA_CTRL(CTRL_STA);
1655
qs.xiong799dab02022-03-14 09:12:12 -04001656 switch(auth){
you.chen87ff5172022-05-06 11:30:57 +08001657 case LYNQ_WIFI_AUTH_OPEN:
1658 {
1659 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04001660
you.chen87ff5172022-05-06 11:30:57 +08001661 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1662// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong99b48d62022-04-07 05:41:29 -04001663 break;
1664 }
you.chen87ff5172022-05-06 11:30:57 +08001665 case LYNQ_WIFI_AUTH_WPA_PSK:
1666 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongd189c542022-03-31 00:58:23 -04001667 {
you.chen87ff5172022-05-06 11:30:57 +08001668 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1669 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1670 }
1671 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen5e363602022-05-08 12:20:18 +08001672 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen87ff5172022-05-06 11:30:57 +08001673 }
1674 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1675 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04001676
you.chen87ff5172022-05-06 11:30:57 +08001677 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1678 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1679 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong99b48d62022-04-07 05:41:29 -04001680
you.chen87ff5172022-05-06 11:30:57 +08001681 if (password != NULL) {
1682 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1683 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1684 }
qs.xiong99b48d62022-04-07 05:41:29 -04001685
you.chen87ff5172022-05-06 11:30:57 +08001686// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongd189c542022-03-31 00:58:23 -04001687 break;
you.chen87ff5172022-05-06 11:30:57 +08001688 }
qs.xiongd189c542022-03-31 00:58:23 -04001689 default:
1690 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001691 }
qs.xiong99b48d62022-04-07 05:41:29 -04001692
qs.xiongd189c542022-03-31 00:58:23 -04001693 return 0;
qs.xiong799dab02022-03-14 09:12:12 -04001694}
qs.xiong8d42bb92022-03-02 09:43:11 -05001695
you.chen87ff5172022-05-06 11:30:57 +08001696static int inner_get_curr_net_no(int interface) {
1697 curr_status_info curr_state;
1698 curr_state.ap = NULL;
1699 curr_state.state = NULL;
1700
1701 if (0 != inner_get_status_info(interface, &curr_state)) {
1702 return -1;
1703 }
1704
1705 return curr_state.net_no;
1706}
1707
1708int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001709{
you.chen87ff5172022-05-06 11:30:57 +08001710 int net_no;
1711 CHECK_IDX(idx, CTRL_STA);
qs.xiongd189c542022-03-31 00:58:23 -04001712
you.chen87ff5172022-05-06 11:30:57 +08001713 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong8d42bb92022-03-02 09:43:11 -05001714
you.chen87ff5172022-05-06 11:30:57 +08001715 if (net_no < 0) {
1716 return -1;
1717 }
1718
1719 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong8d42bb92022-03-02 09:43:11 -05001720}
1721
you.chen87ff5172022-05-06 11:30:57 +08001722int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong8d42bb92022-03-02 09:43:11 -05001723{
you.chen87ff5172022-05-06 11:30:57 +08001724 int count, net_no, index;
1725 int net_no_list[128];
1726 lynq_wifi_auth_s net_auth;
qs.xiongd189c542022-03-31 00:58:23 -04001727
you.chen87ff5172022-05-06 11:30:57 +08001728 if (ssid == NULL || *ssid == '\0') {
1729 printf("bad ssid\n");
1730 return -1;
1731 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001732
you.chen87ff5172022-05-06 11:30:57 +08001733 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1734 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1735 printf("bad password\n");
1736 return -1;
1737 }
1738 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001739
you.chen87ff5172022-05-06 11:30:57 +08001740 CHECK_IDX(idx, CTRL_STA);
1741
1742 net_no = -1;
1743 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1744
1745 for (index=0; index < count; index++) {
1746 net_auth = -1;
1747 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1748 net_no = net_no_list[index];
1749 break;
1750 }
1751 }
1752
1753 if (net_no < 0) {
1754 net_no = lynq_add_network(CTRL_STA);
1755 if (net_no == -1) {
1756 return -1;
1757 }
1758
1759 printf("net no is %d\n", net_no);
1760 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1761 return -1;
1762 }
1763 }
1764
1765 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1766 return -1;
1767 }
1768
1769 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong8d42bb92022-03-02 09:43:11 -05001770}
1771
you.chen87ff5172022-05-06 11:30:57 +08001772int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -05001773{
you.chen87ff5172022-05-06 11:30:57 +08001774 ap_info_s ap;
1775 curr_status_info curr_state;
1776 ap.ap_ssid[0] = '\0';
qs.xiong99b48d62022-04-07 05:41:29 -04001777
you.chen87ff5172022-05-06 11:30:57 +08001778 if (ssid == NULL || *ssid == '\0') {
1779 return -1;
1780 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001781
you.chen87ff5172022-05-06 11:30:57 +08001782 CHECK_IDX(idx, CTRL_STA);
qs.xiong99b48d62022-04-07 05:41:29 -04001783
you.chen87ff5172022-05-06 11:30:57 +08001784 curr_state.ap = &ap;
you.chenefc7f3f2022-05-06 17:50:16 +08001785 curr_state.state = NULL;
1786
you.chen87ff5172022-05-06 11:30:57 +08001787 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1788 return 0;
1789 }
qs.xiong799dab02022-03-14 09:12:12 -04001790
you.chen87ff5172022-05-06 11:30:57 +08001791 if (strcmp(ap.ap_ssid, ssid) != 0) {
1792 return 0;
1793 }
1794
1795 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong8d42bb92022-03-02 09:43:11 -05001796}
qs.xiong99b48d62022-04-07 05:41:29 -04001797
you.chen5e363602022-05-08 12:20:18 +08001798int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1799{
you.chen948e0032022-06-07 18:01:16 +08001800 const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
you.chenbe9e2c12022-06-07 10:36:00 +08001801 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong8d42bb92022-03-02 09:43:11 -05001802
you.chen87ff5172022-05-06 11:30:57 +08001803 CHECK_IDX(idx, CTRL_STA);
you.chen5e363602022-05-08 12:20:18 +08001804 CHECK_WPA_CTRL(CTRL_STA);
1805
1806 system("connmanctl enable wifi");
1807
you.chen6a9361d2022-05-18 10:28:19 +08001808 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen87ff5172022-05-06 11:30:57 +08001809 return -1;
1810 }
qs.xionge5164a82022-03-15 08:03:26 -04001811
you.chenbe9e2c12022-06-07 10:36:00 +08001812 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chen948e0032022-06-07 18:01:16 +08001813 system(lynq_reconfigure_cmd);
you.chenbe9e2c12022-06-07 10:36:00 +08001814 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chen5e363602022-05-08 12:20:18 +08001815
1816 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001817}
1818
you.chen87ff5172022-05-06 11:30:57 +08001819int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong99b48d62022-04-07 05:41:29 -04001820{
you.chen5e363602022-05-08 12:20:18 +08001821 char lynq_disable_network_cmd[MAX_CMD];
1822 curr_status_info curr_state;
1823 ap_info_s ap_info;
you.chen87ff5172022-05-06 11:30:57 +08001824
you.chen5e363602022-05-08 12:20:18 +08001825 CHECK_IDX(idx, CTRL_STA);
1826 CHECK_WPA_CTRL(CTRL_STA);
1827
1828 curr_state.ap = &ap_info;
1829 curr_state.state = NULL;
1830
1831 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1832 return 0;
1833 }
1834
1835 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1836 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1837
1838 DO_OK_FAIL_REQUEST(cmd_save_config);
1839
1840 return 0;
1841// return system("connmanctl disable wifi");
qs.xiongd189c542022-03-31 00:58:23 -04001842}
qs.xiong8d42bb92022-03-02 09:43:11 -05001843
you.chen87ff5172022-05-06 11:30:57 +08001844//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1845// int i, count;
1846// char *p;
1847// const char * FLAG_SSID = "ssid=";
1848// const char * FLAG_SBSID = "bssid=";
1849// const char * FLAG_KEY_MGMT = "key_mgmt=";
1850// const char * FLAG_FREQ = "freq=";
1851// char lynq_sta_cmd[MAX_CMD];
1852// char *split_lines[128] = {0};
1853
1854// CHECK_WPA_CTRL(CTRL_AP);
1855
1856// sprintf(lynq_sta_cmd, "STA %s", bssid);
1857
1858// DO_REQUEST(lynq_sta_cmd);
1859
1860// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1861
1862// for(i=0; i < count; i++) {
1863// p = strstr(split_lines[i], FLAG_SSID);
1864// if (p != NULL) {
1865// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1866// continue;
1867// }
1868// }
1869
1870// lynq_get_interface_ip(idx, ap->ap_ip);
1871// lynq_ap_password_set(idx, ap->psw);
1872
1873// return 0;
1874//}
1875
1876static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1877 curr_status_info curr_state;
1878 curr_state.ap = ap;
1879 curr_state.state = NULL;
1880 return inner_get_status_info(interface, &curr_state);
1881}
1882
1883int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04001884{
you.chen87ff5172022-05-06 11:30:57 +08001885 int ip_count, index, i, line_count;
1886 const char *lynq_first_sta_cmd = "STA-FIRST";
1887 char lynq_next_sta_cmd[MAX_CMD];
1888 char *bssid[1024] = {0};
1889 char *mac_list[128] = {0};
1890 char *ip_list[128] = {0};
1891 char *split_lines[128] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04001892
you.chen87ff5172022-05-06 11:30:57 +08001893 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001894
you.chen87ff5172022-05-06 11:30:57 +08001895 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001896
you.chen87ff5172022-05-06 11:30:57 +08001897// ap_info_s * tmp_ap;
1898// device_info_s * tmp_list;
1899 if (ap == NULL || list == NULL || len == NULL) {
1900 printf("bad input param");
1901 return -1;
1902 }
1903
1904// ap = &tmp_ap;
1905// list = &tmp_list;
1906 *ap = malloc(sizeof (ap_info_s));
1907
1908 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1909 return -1;
1910 }
1911
1912 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1913 lynq_ap_password_get(idx, (*ap)->psw);
1914
1915 ip_count = get_ip_mac_list(mac_list, ip_list);
1916 printf("get count %d\n", ip_count);
1917
1918 DO_REQUEST(lynq_first_sta_cmd);
1919
1920 index = 0;
1921 while (reply_len > 0) {
1922 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1923 break;
1924 }
1925 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1926 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1927 strcpy(bssid[index], split_lines[0]);
1928 index++;
1929 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1930 reply_len = MAX_RET;
1931 cmd_reply[0] = '\0';
qs.xiong3e6e2e62022-09-29 11:37:32 +08001932 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen61c7aee2022-08-06 17:01:16 +08001933 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1934 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen87ff5172022-05-06 11:30:57 +08001935 break;
1936 }
1937 }
1938
1939 *len = index;
1940
1941 *list = malloc(sizeof(device_info_s) * (*len));
1942 for (index=0; index < *len; index++) {
1943 strcpy((*list)[index].sta_mac, bssid[index]);
1944 for(i=0;i < ip_count; i++ ) {
1945 if (strcmp(bssid[index], mac_list[i]) == 0) {
1946 strcpy((*list)[index].sta_ip, ip_list[i]);
1947 break;
1948 }
1949 }
1950 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1951 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1952 free(bssid[index]);
1953 }
1954
you.chen61c7aee2022-08-06 17:01:16 +08001955 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
1956
you.chen87ff5172022-05-06 11:30:57 +08001957 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04001958}
1959
you.chen87ff5172022-05-06 11:30:57 +08001960int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04001961{
you.chen87ff5172022-05-06 11:30:57 +08001962 int i, count, index, count_words;
1963 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1964 char *split_lines[128] = {0};
1965 char *split_words[128] = {0};
1966 scan_info_s * p;
qs.xiong99b48d62022-04-07 05:41:29 -04001967
you.chen87ff5172022-05-06 11:30:57 +08001968 if (list == NULL || len == NULL) {
1969 return -1;
1970 }
qs.xiong99b48d62022-04-07 05:41:29 -04001971
you.chen61c7aee2022-08-06 17:01:16 +08001972 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
1973 {
1974 usleep(100 * 1000);
1975 }
1976
you.chen87ff5172022-05-06 11:30:57 +08001977 CHECK_IDX(idx, CTRL_STA);
1978
1979 CHECK_WPA_CTRL(CTRL_STA);
1980
1981 DO_REQUEST(lynq_scan_result_cmd);
1982
1983 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1984 *len = count - 1;
1985 *list = malloc(sizeof (scan_info_s) * *len);
1986
1987 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1988 for (index=0; index <count_words; index++) {
1989 printf("----header: %s\n", split_words[index]);
1990 }
1991
1992 for(index = 1;index < count; index++) {
1993 printf("---- %s\n",split_lines[index]);
1994 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1995 if (count_words < 4)
1996 continue;
1997 printf("count: %d, %s\n", count_words, split_words[0]);
1998 //bssid / frequency / signal level / flags / ssid
1999 p = (*list) + index - 1;
2000 strcpy(p->mac, split_words[0]);
2001 p->band = convert_band_from_freq(atoi(split_words[1]));
2002 p->rssi = -1 * atoi( split_words[2]);
2003 p->auth = convert_max_auth_from_flag(split_words[3]);
2004 strcpy(p->ssid, split_words[4]);
2005 }
2006
2007 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04002008}
qs.xiong99b48d62022-04-07 05:41:29 -04002009
you.chen87ff5172022-05-06 11:30:57 +08002010int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2011{
2012 int count, net_no, index;
2013 int net_no_list[128];
2014 lynq_wifi_auth_s net_auth;
2015 char lynq_remove_cmd[MAX_CMD];
2016
2017 if (ssid == NULL || *ssid == '\0') {
2018 printf("bad ssid\n");
2019 return -1;
2020 }
2021
2022 CHECK_IDX(idx, CTRL_STA);
2023
2024 CHECK_WPA_CTRL(CTRL_STA);
2025
2026 net_no = -1;
2027 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2028
2029 for (index=0; index < count; index++) {
2030 net_auth = -1;
2031 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2032 net_no = net_no_list[index];
2033 break;
2034 }
2035 }
2036
2037 if (net_no < 0) {
2038 return 0;
2039 }
2040
2041 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2042
2043 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2044 DO_OK_FAIL_REQUEST(cmd_save_config);
2045
2046 return 0;
2047}
2048
2049int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2050{
you.chen0c938042022-08-06 16:59:10 +08002051 int count, index, ssid_len;
you.chen87ff5172022-05-06 11:30:57 +08002052 int net_no_list[128];
2053 char freq[16];
you.chen0c938042022-08-06 16:59:10 +08002054 char *ssid_ptr;
you.chen87ff5172022-05-06 11:30:57 +08002055
2056 if (list == NULL || len == NULL) {
2057 printf("bad param\n");
2058 return -1;
2059 }
2060
2061 CHECK_IDX(idx, CTRL_STA);
2062
2063// CHECK_WPA_CTRL(CTRL_STA);
2064
2065 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2066 printf("count is %d\n", count);
2067
2068 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen0c938042022-08-06 16:59:10 +08002069 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen87ff5172022-05-06 11:30:57 +08002070 *len = count;
2071
2072 for (index=0; index < count; index++) {
2073 printf("to get ssid %d\n", index);
2074 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen0c938042022-08-06 16:59:10 +08002075
2076 ssid_ptr = (*list)[index].base_info.ap_ssid;
2077 ssid_len = strlen(ssid_ptr);
2078 if (ssid_ptr[0] == '\"') {
2079 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2080 ssid_len -= 1;
2081 }
2082 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2083 ssid_ptr[ssid_len - 1] = '\0';
2084 }
2085
you.chen87ff5172022-05-06 11:30:57 +08002086 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen87ff5172022-05-06 11:30:57 +08002087 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen87ff5172022-05-06 11:30:57 +08002088 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2089 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2090 }
2091 else {
2092 (*list)[index].base_info.band = -1;
2093 }
2094
you.chen0c938042022-08-06 16:59:10 +08002095 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen87ff5172022-05-06 11:30:57 +08002096 }
2097
2098 return 0;
2099}
2100
2101int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2102{
2103 const char *lynq_scan_cmd = "SCAN";
2104
2105 CHECK_IDX(idx, CTRL_STA);
2106
2107 CHECK_WPA_CTRL(CTRL_STA);
2108
2109 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen61c7aee2022-08-06 17:01:16 +08002110 g_sta_scan_finish_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +08002111
2112 return 0;
2113}
2114
2115int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2116 if (cb == NULL) {
2117 return -1;
2118 }
2119
2120 g_ap_callback_priv = priv;
2121 g_ap_callback_func = cb;
2122
2123 return 0;
2124}
2125
2126int lynq_unreg_ap_event_callback(void * priv) {
2127 if (g_ap_callback_priv == priv) {
2128 g_ap_callback_func = NULL;
2129 g_ap_callback_priv = NULL;
2130 return 0;
2131 }
2132 return -1;
2133}
2134
2135int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2136 if (cb == NULL) {
2137 return -1;
2138 }
2139
2140 g_sta_callback_priv = priv;
2141 g_sta_callback_func = cb;
2142
2143 return 0;
2144}
2145
2146int lynq_unreg_sta_event_callback(void * priv) {
2147 if (g_sta_callback_priv == priv) {
2148 g_sta_callback_func = NULL;
2149 g_sta_callback_priv = NULL;
2150 return 0;
2151 }
2152 return -1;
2153}
2154
2155
2156static int inner_get_status_info_state (int interface, char *state) {
2157 curr_status_info curr_state;
2158 curr_state.ap = NULL;
2159 curr_state.state = state;
2160 return inner_get_status_info(interface, &curr_state);
2161}
2162
2163int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2164{
2165 char state[MAX_CMD];
you.chen87ff5172022-05-06 11:30:57 +08002166 CHECK_IDX(idx, CTRL_AP);
2167
2168 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2169 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2170 return 0;
2171 }
2172
2173 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2174 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2175 }
2176 else {
2177 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2178 }
2179
2180 return 0;
2181}
2182
2183int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2184 char state[MAX_CMD];
you.chen87ff5172022-05-06 11:30:57 +08002185 CHECK_IDX(idx, CTRL_STA);
2186
2187 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2188 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2189 return 0;
2190 }
2191
2192 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2193 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2194 }
2195 else {
2196 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2197 }
2198
2199 return 0;
2200}
2201
2202int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2203// CHECK_IDX(idx, CTRL_AP);
2204// int ret = 0;
2205// size_t reply_len = MAX_RET;
2206// char cmd_reply[MAX_RET]={0};
2207// const char * cmd_str = "GET country";
2208// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2209// do{
2210// if (NULL == s_lynq_wpa_ctrl) {
2211// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2212// if (NULL == s_lynq_wpa_ctrl ) {
2213// printf("wpa_ctrl_open fail\n");
2214// return -1;
2215// }
2216// }
2217// }while(0);
2218
2219// do {
2220// reply_len = MAX_RET;
2221// cmd_reply[0] = '\0';
2222// printf("to call [%s]\n", cmd_str);
qs.xiong3e6e2e62022-09-29 11:37:32 +08002223// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen87ff5172022-05-06 11:30:57 +08002224// if (ret != 0) {
2225// printf("call ##cmd_str fail %d\n", ret);
2226// return ret;
2227// }
2228// cmd_reply[reply_len+1] = '\0';
2229// printf("cmd replay [ %s ]\n", cmd_reply);
2230// }while(0);
2231
2232 FILE *fp;
2233 size_t i = 0;
2234 char lynq_cmd_ret[MAX_RET]={0};
2235
2236// CHECK_IDX(idx, CTRL_AP);
2237
2238 if((fp=popen("wl country","r"))==NULL)
2239 {
2240 perror("popen error!");
2241 return -1;
2242 }
2243 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2244 {
2245 perror("fread fail!");
2246 return -1;
2247 }
2248
2249 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2250 if (lynq_cmd_ret[i] == ' ') {
2251 lynq_cmd_ret[i] = '\0';
2252 break;
2253 }
2254 }
2255
2256 strcpy(country_code,lynq_cmd_ret);
2257 printf("---country code %s\n", country_code);
2258
2259 int ret=pclose(fp);
2260 if(ret==-1)
2261 {
2262 perror("close file faild");
2263 }
2264
2265 return 0;
2266}
2267
2268int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2269// const char * cmd_str = "GET country";
2270// CHECK_IDX(idx, CTRL_AP);
2271// CHECK_WPA_CTRL(CTRL_STA);
2272
2273// DO_REQUEST(cmd_str);
2274// printf("result %s\n", cmd_reply);
2275
2276 if (country_code == NULL || *country_code == '\0') {
2277 printf("bad country code\n");
2278 return -1;
2279 }
2280
2281 char lynq_country_cmd[MAX_CMD];
2282 sprintf(lynq_country_cmd, "wl country %s", country_code);
2283 if (system(lynq_country_cmd) == 0) {
2284 return 0;
2285 }
2286
2287 return -1;
2288}
2289
2290int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2291{
2292 if (mac == NULL) {
2293 return -1;
2294 }
2295
2296 CHECK_IDX(idx, CTRL_STA);
2297 ap_info_s ap;
2298 ap.ap_mac[0] = '\0';
2299
2300 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2301 return -1;
2302 }
2303 strcpy(mac, ap.ap_mac);
2304
2305 return 0;
2306}
2307
2308int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2309{
you.chen61c7aee2022-08-06 17:01:16 +08002310 struct ifaddrs *ifaddr_header, *ifaddr;
2311 struct in_addr * ifa;
2312 const char * ifaName = "wlan0";
2313 if (ip == NULL)
2314 {
you.chendad3f9f2022-06-21 16:53:48 +08002315 return -1;
you.chen61c7aee2022-08-06 17:01:16 +08002316 }
you.chendad3f9f2022-06-21 16:53:48 +08002317
you.chen61c7aee2022-08-06 17:01:16 +08002318 if (idx == 1) {
you.chenc7357f22022-05-16 17:55:28 +08002319 ifaName = "tether";
you.chen61c7aee2022-08-06 17:01:16 +08002320 }
2321 else if (idx != 0) {
you.chen87ff5172022-05-06 11:30:57 +08002322 return -1;
you.chen61c7aee2022-08-06 17:01:16 +08002323 }
you.chen87ff5172022-05-06 11:30:57 +08002324
you.chen61c7aee2022-08-06 17:01:16 +08002325 if (getifaddrs(&ifaddr_header) == -1)
2326 {
you.chen87ff5172022-05-06 11:30:57 +08002327 perror("getifaddrs");
2328 return -1;
2329 //exit(EXIT_FAILURE);
you.chen61c7aee2022-08-06 17:01:16 +08002330 }
you.chen87ff5172022-05-06 11:30:57 +08002331
2332
you.chen61c7aee2022-08-06 17:01:16 +08002333 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2334 {
2335 if (ifaddr->ifa_addr == NULL)
you.chen87ff5172022-05-06 11:30:57 +08002336 continue;
you.chen61c7aee2022-08-06 17:01:16 +08002337 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2338 {
2339 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2340 {
2341 // is a valid IP4 Address
2342 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2343 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2344 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2345 freeifaddrs(ifaddr_header);
2346 printf("ip %s\n", ip);
2347 return 0;
2348 }
2349 }
2350 }
2351 return -1;
you.chen87ff5172022-05-06 11:30:57 +08002352}
2353
2354int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2355{
2356 int count;
2357 size_t i;
2358 char *split_words[128] = {0};
2359 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2360
2361 CHECK_WPA_CTRL(idx);
2362
2363 DO_REQUEST(lynq_get_mac_cmd);
2364
2365 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2366 return -1;
2367 }
2368
2369 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2370
2371 if (count < 2) {
2372 return -1;
2373 }
2374
2375 for (i=0; i < strlen(split_words[1]); i++ ) {
2376 if (split_words[1][i] != ' ') {
2377 break;
2378 }
2379 }
2380
2381 strcpy(mac, split_words[1] + i);
2382
2383 return 0;
2384}
2385
2386int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2387{
2388// int count;
2389// char *split_words[128] = {0};
2390// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2391
2392// if (rssi == NULL) {
2393// return -1;
2394// }
2395
2396// CHECK_IDX(idx, CTRL_STA);
2397
2398// CHECK_WPA_CTRL(CTRL_STA);
2399
2400// DO_REQUEST(lynq_get_rssi_cmd);
2401
2402// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2403// return -1;
2404// }
2405
2406// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2407
2408// if (count < 2) {
2409// return -1;
2410// }
2411
2412// *rssi = atoi(split_words[1]) * -1;
2413
2414 FILE *fp;
2415 size_t i = 0;
2416 char lynq_cmd_ret[MAX_RET]={0};
2417
2418// CHECK_IDX(idx, CTRL_AP);
2419
you.chenfcea58c2022-06-06 17:18:18 +08002420 if((fp=popen("wl rssi","r"))==NULL)
you.chen87ff5172022-05-06 11:30:57 +08002421 {
2422 perror("popen error!");
2423 return -1;
2424 }
2425 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2426 {
2427 perror("fread fail!");
2428 return -1;
2429 }
you.chenfcea58c2022-06-06 17:18:18 +08002430 *rssi = atoi(lynq_cmd_ret) * -1;
you.chen87ff5172022-05-06 11:30:57 +08002431
2432 return 0;
2433}
2434
2435int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2436{
2437 if (band == NULL) {
2438 return -1;
2439 }
2440
2441 CHECK_IDX(idx, CTRL_STA);
2442 ap_info_s ap;
2443 ap.band = -1;
2444
2445 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2446 return -1;
2447 }
2448 *band = ap.band;
2449
2450 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04002451}
you.chendad3f9f2022-06-21 16:53:48 +08002452
2453int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2454{
2455 int index;
2456 int ip_count = 0;
2457 char bssid[1024] = {0};
2458 char *mac_list[128] = {0};
2459 char *ip_list[128] = {0};
2460
2461 if (ip == NULL)
2462 {
2463 printf("invalid param");
2464 return -1;
2465 }
2466
2467 CHECK_IDX(idx, CTRL_STA);
2468
2469 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2470 {
2471 return -1;
2472 }
2473
2474 ip_count = get_ip_mac_list(mac_list, ip_list);
2475
2476 for (index=0; index < ip_count; index++)
2477 {
2478 if (strcmp(bssid, mac_list[index]) == 0)
2479 {
2480 strcpy(ip, ip_list[index]);
you.chen61c7aee2022-08-06 17:01:16 +08002481
2482 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2483
you.chendad3f9f2022-06-21 16:53:48 +08002484 return 0;
2485 }
2486 }
2487
2488 printf("not found\n");
you.chen61c7aee2022-08-06 17:01:16 +08002489 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2490
you.chendad3f9f2022-06-21 16:53:48 +08002491 return -1;
2492}
2493
2494