blob: c6829762b972550b0ed738039a9dba19dd634727 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-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.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-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.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
45volatile int g_sta_scan_finish_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-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.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
61
62static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
63
64
65typedef struct __curr_status_info {
66 ap_info_s *ap;
67 char * state;
68 int net_no;
69}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040070
71#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040072{\
you.chen35020192022-05-06 11:30:57 +080073 perror((str));\
74 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040075}
76
you.chen35020192022-05-06 11:30:57 +080077#define CHECK_IDX(idx, type) do { \
78 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
79 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
80 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
81 return -1; \
82 } \
83 }while (0)
84
85#define CHECK_WPA_CTRL(index) int ret = 0;\
86 size_t reply_len = MAX_RET; \
87 char cmd_reply[MAX_RET]={0}; \
88 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
89 do{ \
90 if (NULL == g_lynq_wpa_ctrl[index]) { \
91 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
92 if (NULL == g_lynq_wpa_ctrl[index]) { \
93 printf("wpa_ctrl_open fail\n"); \
94 return -1; \
95 } \
96 } \
97 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
98 }while(0)
99
100#define DO_REQUEST(cmd_str) do { \
101 reply_len = MAX_RET;\
102 cmd_reply[0] = '\0'; \
103 printf("to call [%s]\n", cmd_str); \
104 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
105 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800106 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800107 return ret; \
108 } \
109 cmd_reply[reply_len+1] = '\0'; \
110 printf("cmd replay [ %s ]\n", cmd_reply); \
111 }while(0)
112
113#define DO_OK_FAIL_REQUEST(cmd_str) do { \
114 DO_REQUEST(cmd_str); \
115 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800116 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800117 return -1; \
118 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800119 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800120 return -1; \
121 } \
122 }while (0)
123
124
125
126static void APWatcherThreadProc() {
127 size_t len = MAX_RET;
128 char msg_notify[MAX_RET];
129
you.chen6c2dd9c2022-05-16 17:55:28 +0800130 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800131 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800132
133 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800134 if (lynq_wpa_ctrl == NULL) {
135 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
136 if (lynq_wpa_ctrl == NULL) {
137 usleep(100*1000);
138 continue;
139 }
140
141 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800142 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800143 }
144
you.chen35020192022-05-06 11:30:57 +0800145 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
146 usleep(100*1000);
147 continue;
148 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800149 memset(msg_notify, 0, MAX_RET);
150 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800151 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
152 msg_notify[len+1] = '\0';
153 printf("ap------> %s\n", msg_notify);
154 if (g_ap_callback_func == NULL) {
155 continue;
156 }
157 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
158 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
159 }
160 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
161 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
162 }
163 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
164 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800165 if (lynq_wpa_ctrl != NULL) {
166 wpa_ctrl_detach(lynq_wpa_ctrl);
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400169}
170
you.chen35020192022-05-06 11:30:57 +0800171static void STAWatcherThreadProc() {
172 size_t len = MAX_RET;
173 char msg_notify[MAX_RET];
174 char *pReason;
175 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400176
you.chen6c2dd9c2022-05-16 17:55:28 +0800177 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800178 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800179
180 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800181 if (lynq_wpa_ctrl == NULL) {
182 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
183 if (lynq_wpa_ctrl == NULL) {
184 usleep(100*1000);
185 continue;
186 }
187
188 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800189 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800190 }
191
you.chen35020192022-05-06 11:30:57 +0800192 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
193 usleep(100*1000);
194 continue;
195 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800196 memset(msg_notify, 0, MAX_RET);
197 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800198 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
199 msg_notify[len+1] = '\0';
200 printf("sta ------> %s\n", msg_notify);
201 if (strstr(msg_notify, state_scan_result) != NULL) {
202 g_sta_scan_finish_flag = 1;
203 }
204
205 if (g_sta_callback_func == NULL) {
206 continue;
207 }
208 error = -1;
209 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
210 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
211 }
212 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
213 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
214 }
215 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
216 pReason = strstr(msg_notify, "reason=");
217 if (pReason != NULL) {
218 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800219 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800220 error = LYNQ_TIME_OUT;
221 }
you.chen92fd5d32022-05-25 10:09:47 +0800222 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800223 error = LYNQ_PSW_ERROR;
224 }
225 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
226 }
227 }
228 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
229 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
230 }
231 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
232 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
233 }
234 }
235 }
you.chen92fd5d32022-05-25 10:09:47 +0800236 if (lynq_wpa_ctrl != NULL) {
237 wpa_ctrl_detach(lynq_wpa_ctrl);
238 wpa_ctrl_close(lynq_wpa_ctrl);
239 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400240}
241
qs.xiong1af5daf2022-03-14 09:12:12 -0400242int lynq_wifi_enable(void)
243{
you.chen35020192022-05-06 11:30:57 +0800244 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800245 int i;
you.chen35020192022-05-06 11:30:57 +0800246 const char * cmd_check_service =
247 "state=`systemctl is-active wg870_drv_insmod.service`\n"
248 "[ \"\"$state == \"active\" ] && exit 0\n"
249 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
250// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
251// return 0;
252// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400253
you.chen35020192022-05-06 11:30:57 +0800254 ret = system(cmd_check_service);
255 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800256 printf("service state %d\n", ret);
you.chen35020192022-05-06 11:30:57 +0800257 return -1;
258 }
259
you.chen6c2dd9c2022-05-16 17:55:28 +0800260 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800261 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800262 break;
263 }
264 usleep(300*1000);
265 }
266
267 if (i >= 10) {
268 return -1;
269 }
270
you.chen9f17e4d2022-06-06 17:18:18 +0800271 //@todo delete add temp check for socket avilable start (20220606)
272 for (i=0; i<60; i++)
273 {
274 if (system("netstat -an | grep -q DGRAM") == 0) {
275 break;
276 }
277 sleep(1);
278 }
279
280 if (i >= 60)
281 {
282 return -1;
283 }
284 //@todo delete add temp check for socket avilable end (20220606)
285
you.chena6fa5b22022-05-18 10:28:19 +0800286 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800287 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800288 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800289 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800290 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800291 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800292 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800293 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800294 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800295 }
296
you.chen35020192022-05-06 11:30:57 +0800297 if (g_ap_watcher_pid == 0 ) {
298 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
299 if(ret<0){
300 return -1;
301 }
302 }
303
304 if (g_sta_watcher_pid == 0 ) {
305 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
306 if(ret<0){
307 return -1;
308 }
309 }
310
you.chena6fa5b22022-05-18 10:28:19 +0800311 for (i=0; i<10; i++) {
312 usleep(300*1000);
313 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
314 break;
315 }
316 }
317
you.chen35020192022-05-06 11:30:57 +0800318 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500319}
320
qs.xiong1af5daf2022-03-14 09:12:12 -0400321int lynq_wifi_disable(void)
322{
you.chen35020192022-05-06 11:30:57 +0800323 g_ap_watcher_stop_flag = 1;
324 g_sta_watcher_stop_flag = 1;
325 if (g_ap_watcher_pid != 0)
326 pthread_join(g_ap_watcher_pid, NULL);
327 if (g_sta_watcher_pid != 0)
328 pthread_join(g_sta_watcher_pid, NULL);
329 if (g_lynq_wpa_ctrl[0] != NULL)
330 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
331 if (g_lynq_wpa_ctrl[1] != NULL)
332 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
333 g_ap_watcher_pid = 0;
334 g_sta_watcher_pid = 0;
335 g_lynq_wpa_ctrl[0] = NULL;
336 g_lynq_wpa_ctrl[1] = NULL;
337 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500338 return 0;
339}
qs.xiong1af5daf2022-03-14 09:12:12 -0400340
you.chen35020192022-05-06 11:30:57 +0800341static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
342
343 char lynq_cmd_get[128]={0};
344
345 if (out_put == NULL) {
346 printf("output ptr is null\n");
347 return -1;
348 }
349 if (param_name == NULL) {
350 printf("param ptr is null");
351 return -1;
352 }
353 if (param_name[0] == '\0') {
354 printf("param is empty");
355 return -1;
356 }
357
358 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
359
360 CHECK_WPA_CTRL(interface);
361
362 DO_REQUEST(lynq_cmd_get);
363
364 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
365 return -1;
366 }
367
you.chena6fa5b22022-05-18 10:28:19 +0800368// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800369 memcpy(out_put, cmd_reply, reply_len + 1);
370 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500371}
qs.xiong1af5daf2022-03-14 09:12:12 -0400372
you.chen35020192022-05-06 11:30:57 +0800373static int lynq_split(char * str, int len, char delimiter, char * results[]) {
374 int ret = 0;
375 char * end = str + len - 1;
376 results[ret++] = str;
377 while(str < end) {
378 if (*str == delimiter) {
379 *str++ = '\0';
380 results[ret++] = str;
381 continue;
382 }
383 str++;
384 }
385 if (*str == delimiter) {
386 *str = '\0';
387 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400388
you.chen35020192022-05-06 11:30:57 +0800389 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500390}
391
you.chen35020192022-05-06 11:30:57 +0800392static void trim_space(char * p, int count) {
393 char * begin = p;
394 p += count;
395 printf("%C-%C||\n", *begin, *p);
396 while (p >= begin ) {
397 if (*p == ' ') {
398 *p-- = '\0';
399 }
400 else {
401 break;
402 }
403 }
404}
405
406static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
407 FILE * fp;
408 int len, ret;
409 int count, count_words, index;
410 int mac_start, mac_end;
411 int ip_start, ip_end;
412 char *split_lines[128] = {0};
413 char *buff;
414 const char * ip_header = "IP address";
415 const char * mac_header = "HW address";
416 const char * zero_mac = "00:00:00:00:00:00";
417
418 fp = fopen("/proc/net/arp", "rb");
419 if (NULL == fp) {
420 printf("open file fail\n");
421 return -1;
422 }
423
424 buff = alloca(MAX_RET);
425 fseek(fp, 0, SEEK_SET);
426 len = fread(buff, 1, MAX_RET, fp);
427 fclose(fp);
428 if (len <= 0) {
429 printf("read file fail\n");
430 return -1;
431 }
432 printf("file : %s\n", buff);
433
434 count = lynq_split(buff, len, '\n', split_lines);
435 printf("----- %s\n", split_lines[0]);
436
437 mac_end = 0;
438 count_words = strlen(split_lines[0]);
439 if (strstr(split_lines[0], mac_header) != NULL) {
440 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
441 mac_end = mac_start + strlen(mac_header) + 1;
442 while (mac_end < count_words) {
443 if (split_lines[0][mac_end] != ' ') {
444 break;
445 }
446 mac_end++;
447 }
448 }
449
450 ip_end = 0;
451 if (strstr(split_lines[0], ip_header) != NULL) {
452 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
453 ip_end = ip_start + strlen(ip_header) + 1;
454 while (ip_end < count_words) {
455 if (split_lines[0][ip_end] != ' ') {
456 break;
457 }
458 ip_end++;
459 }
460 }
461
462 if (mac_end == 0 || ip_end == 0) {
463 return 0;
464 }
465
466 ret = 0;
467 for(index = 1;index < count; index++) {
468 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
469 continue;
470 }
471 mac_list[ret] = malloc(mac_end - mac_start + 1);
472 ip_list[ret] = malloc(ip_end - ip_start + 1);
473 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
474 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
475 trim_space(mac_list[ret], mac_end - mac_start - 1);
476 trim_space(ip_list[ret], ip_end - ip_start - 1);
477 ret++;
478 }
479
480 return ret;
481}
482
483static int get_hostname_by_ip(char *ip, char *hostname) {
484 struct in_addr addr ={0};
485 struct hostent *ht;
486
487 if (ip == NULL || *ip == '\0' || hostname == NULL) {
488 return -1;
489 }
490
491 if (inet_aton(ip, &addr) == 0) {
492 printf("---inet_aton fail\n");
493 return -1;
494 }
495
496 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
497
498 if (ht == NULL) {
499 printf("---gethostbyaddr fail\n");
500 herror(NULL);
501 return -1;
502 }
503
504 strcpy(hostname, ht->h_name);
505
506 return 0;
507}
508
509static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
510{
511 int count, index, words_count;
512 char * split_lines[128]= {0};
513 char * split_words[128] = {0};
514 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
515
516 CHECK_WPA_CTRL(ap_sta);
517
518 DO_REQUEST(lynq_wifi_list_networks);
519
520 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
521
522 //@todo check ssid field to compatible
523
524 ret = 0;
525 for(index=1; index < count; index++) {
526 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
527 if (words_count > 2) {
528 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
529 net_no_list[ret++] = atoi(split_words[0]);
530 }
531 }
532 }
533
534 return ret;
535}
536
537static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800538 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800539 CHECK_WPA_CTRL(ap_sta);
540 const char *lynq_wifi_add_network = "ADD_NETWORK";
541
542 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800543 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800544 return -1;
545 }
546
you.chen6c2dd9c2022-05-16 17:55:28 +0800547 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800548 if(cmd_reply[i] == '\n') {
549 cmd_reply[i] = '\0';
550 break;
551 }
552 }
553 return atoi(cmd_reply);
554}
you.chena6cd55a2022-05-08 12:20:18 +0800555
you.chen35020192022-05-06 11:30:57 +0800556static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
557{
558 int count, index;
559 int net_no_list[128];
560
561
562 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
563 for (index=0; index < count; index++) {
564 if (net_no_list[index] == net_no) {
565 return 0;
566 }
567 }
568
569 if (count >= 1)
570 index = net_no_list[count - 1];
571 else
572 index = -1;
573
574 while (index < net_no ) {
575 index = lynq_add_network(ap_sta);
576 if (index >= net_no) { // required network no created
577 return 0;
578 }
you.chena6cd55a2022-05-08 12:20:18 +0800579 else if( index < 0) {
580 printf("add network fail\n");
581 return -1;
582 }
you.chen35020192022-05-06 11:30:57 +0800583 }
584
585 if (index < 0)
586 return -1;
587
588 return 0;
589}
590
591static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
592 if (freq > 5000 && freq < 6000) {
593 return LYNQ_WIFI_5G_band;
594 }
595 else if (freq > 2000 && freq < 3000) {
596 return LYNQ_WIFI_2G_band;
597 }
598 return LYNQ_WIFI_2_and_5G_band;
599}
600
601static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
602 if (key_mgmt != NULL) {
603 if (memcmp( key_mgmt, "NONE", 4) == 0) {
604 return LYNQ_WIFI_AUTH_OPEN;
605 }
606 else if (memcmp( key_mgmt, "WEP", 3) == 0){
607 return LYNQ_WIFI_AUTH_WEP;
608 }
609 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
610 return LYNQ_WIFI_AUTH_WPA_PSK;
611 }
612 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
613 return LYNQ_WIFI_AUTH_WPA2_PSK;
614 }
615 }
616
617 return -1;
618}
619
620static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
621 if (flag != NULL) {
622 if (strstr( flag, "WPA2-PSK") != NULL){
623 return LYNQ_WIFI_AUTH_WPA2_PSK;
624 }
625 else if (strstr( flag, "WPA-PSK") != NULL){
626 return LYNQ_WIFI_AUTH_WPA_PSK;
627 }
628 else if (strstr( flag, "WEP") != NULL){
629 return LYNQ_WIFI_AUTH_WEP;
630 }
631 else if (strstr( flag, "NONE") != NULL) {
632 return LYNQ_WIFI_AUTH_OPEN;
633 }
634 }
635
636 return -1;
637}
638
639static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
640 switch (bw) {
641 case 10:
642 return LYNQ_WIFI_BANDWIDTH_HT10;
643 break;
644 case 20:
645 return LYNQ_WIFI_BANDWIDTH_HT20;
646 break;
647 case 40:
648 return LYNQ_WIFI_BANDWIDTH_HT40;
649 break;
650 case 80:
651 return LYNQ_WIFI_BANDWIDTH_HT80;
652 break;
653 default:
654 break;
655 }
656
657 return -1;
658}
659
660static int inner_get_status_info(int interface, curr_status_info *curr_state) {
661 int i, count;
662 char *p;
663 const char *lynq_status_cmd = "STATUS";
664 const char * FLAG_SSID = "ssid=";
665 const char * FLAG_SBSID = "bssid=";
666 const char * FLAG_KEY_MGMT = "key_mgmt=";
667 const char * FLAG_FREQ = "freq=";
668 const char * FLAG_STATE = "wpa_state=";
669 const char * FLAG_ID = "id=";
670 char *split_lines[128] = {0};
671
672 CHECK_WPA_CTRL(interface);
673
674 if (curr_state == NULL) {
675 return -1;
676 }
677
678 DO_REQUEST(lynq_status_cmd);
679
680 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
681
682 curr_state->net_no = -1;
683 ret = -1;
684 for(i=0; i < count; i++) {
685 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800686 p = strstr(split_lines[i], FLAG_SBSID);
687 if (p != NULL) {
688 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
689 ret = 0;
690 continue;
691 }
you.chenf58b3c92022-06-21 16:53:48 +0800692 p = strstr(split_lines[i], FLAG_SSID);
693 if (p != NULL) {
694 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
695 ret = 0;
696 continue;
697 }
you.chen35020192022-05-06 11:30:57 +0800698 p = strstr(split_lines[i], FLAG_KEY_MGMT);
699 if (p != NULL) {
700 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
701 ret = 0;
702 continue;
703 }
704 p = strstr(split_lines[i], FLAG_FREQ);
705 if (p != NULL) {
706 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
707 ret = 0;
708 continue;
709 }
710 } // end if (ap != NULL)
711 if (curr_state->state != NULL) {
712 p = strstr(split_lines[i], FLAG_STATE);
713 if (p != NULL) {
714 strcpy(curr_state->state, p + strlen(FLAG_STATE));
715 ret = 0;
716 continue;
717 }
718
719 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800720 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800721 ret = 0;
722 curr_state->net_no = atoi(p);
723 printf("net_no %d, -- %s\n", curr_state->net_no, p);
724 }
725 }
726
727 return ret;
728}
729
730
qs.xiongf1b525b2022-03-31 00:58:23 -0400731int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500732{
you.chen35020192022-05-06 11:30:57 +0800733 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500734
you.chen35020192022-05-06 11:30:57 +0800735 if (ap_ssid == NULL) {
736 printf("ap_ssid is null\n");
737 return -1;
738 }
739 else {
740 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
741 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400742
you.chen35020192022-05-06 11:30:57 +0800743 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
744 return -1;
745 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400746
you.chen35020192022-05-06 11:30:57 +0800747 CHECK_IDX(idx, CTRL_AP);
748
749 CHECK_WPA_CTRL(CTRL_AP);
750
751 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
752
753 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
754 DO_OK_FAIL_REQUEST(cmd_save_config);
755
qs.xiong7a105ce2022-03-02 09:43:11 -0500756 return 0;
you.chen35020192022-05-06 11:30:57 +0800757
qs.xiong7a105ce2022-03-02 09:43:11 -0500758}
759
you.chen35020192022-05-06 11:30:57 +0800760int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500761{
you.chen6c2dd9c2022-05-16 17:55:28 +0800762 int len;
you.chen35020192022-05-06 11:30:57 +0800763 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800764 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
765 return -1;
766 len = strlen(ap_ssid);
767 if (ap_ssid[0] == '\"') {
768 memmove(ap_ssid, ap_ssid + 1, len - 1);
769 len -= 1;
770 }
771 if (len > 0 && ap_ssid[len-1] == '\"') {
772 ap_ssid[len-1] = '\0';
773 }
774 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500775}
776
qs.xiongf1b525b2022-03-31 00:58:23 -0400777int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500778{
you.chen35020192022-05-06 11:30:57 +0800779 char lynq_wifi_frequency_cmd[128]={0};
780 char lynq_cmd_mode[128]={0};
781 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500782
you.chen35020192022-05-06 11:30:57 +0800783 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
784 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
785 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400786
you.chen35020192022-05-06 11:30:57 +0800787 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
788 return -1;
789 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400790
you.chen35020192022-05-06 11:30:57 +0800791 CHECK_IDX(idx, CTRL_AP);
792
793 CHECK_WPA_CTRL(CTRL_AP);
794
795 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
796 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
797 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
798
you.chen6c2dd9c2022-05-16 17:55:28 +0800799 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800800 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
801 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
802 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500803
qs.xiong1af5daf2022-03-14 09:12:12 -0400804 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500805}
806
qs.xiongf1b525b2022-03-31 00:58:23 -0400807int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500808{
you.chen35020192022-05-06 11:30:57 +0800809 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400810
you.chen35020192022-05-06 11:30:57 +0800811 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400812
you.chen35020192022-05-06 11:30:57 +0800813 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
814 return -1;
815 }
816 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400817
818 return 0;
819}
820
qs.xiongf1b525b2022-03-31 00:58:23 -0400821int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
822{
you.chen35020192022-05-06 11:30:57 +0800823 CHECK_IDX(idx, CTRL_AP);
824 switch(bandwidth){
825 case LYNQ_WIFI_BANDWIDTH_HT10:
826 {
827 printf("bandwith [%d] not support now\n", bandwidth);
828 return -1;
829 }
830 case LYNQ_WIFI_BANDWIDTH_HT20:
831 {
832 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
833 system("wl down");
834 if (system(lynq_cmd_bandwith) != 0 ){
835 return -1;
836 }
837 system("wl up");
838 break;
839 }
840 case LYNQ_WIFI_BANDWIDTH_HT40:
841 {
842 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
843 sprintf(lynq_cmd_bandwith, "wl chanspec ");
844 system("wl down");
845 if (system(lynq_cmd_bandwith) != 0 ){
846 return -1;
847 }
848 system("wl up");
849 break;
850 }
851 case LYNQ_WIFI_BANDWIDTH_HT80:
852 {
853 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
854 system("wl down");
855 if (system(lynq_cmd_bandwith) != 0 ){
856 return -1;
857 }
858 system("wl up");
859 break;
860 }
861 default:
862 {
863 printf("auth type [%d] not support now\n", bandwidth);
864 return -1;
865 }
866 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400867
868
you.chen35020192022-05-06 11:30:57 +0800869 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400870}
you.chen35020192022-05-06 11:30:57 +0800871
qs.xiongf1b525b2022-03-31 00:58:23 -0400872int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
873{
you.chen35020192022-05-06 11:30:57 +0800874 int count = 0;
875 int index = 0;
876 char *split_words[128] = {0};
877 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400878
you.chen35020192022-05-06 11:30:57 +0800879 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500880
you.chen35020192022-05-06 11:30:57 +0800881 CHECK_WPA_CTRL(CTRL_AP);
882
883 DO_REQUEST(lynq_chanspec_cmd);
884
885 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
886 for(;index < count; index++) {
887 if (strncmp(split_words[index], "bw", 2) != 0) {
888 continue;
889 }
890
891 index++;
892 if (index >= count) {
893 return -1;
894 }
895
896 printf("bw %s\n", split_words[index]);
897 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
898 return 0;
899 }
900
901 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500902}
qs.xiong0fb469a2022-04-14 03:50:45 -0400903
qs.xiongf1b525b2022-03-31 00:58:23 -0400904int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500905{
you.chen35020192022-05-06 11:30:57 +0800906 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400907
you.chen35020192022-05-06 11:30:57 +0800908 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400909
you.chen35020192022-05-06 11:30:57 +0800910 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400911
you.chen35020192022-05-06 11:30:57 +0800912 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
913 return -1;
914 }
915
916 system("wl down");
917 if (system(lynq_cmd_channel) != 0 ){
918 return -1;
919 }
920 system("wl up");
921 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500922}
qs.xiong0fb469a2022-04-14 03:50:45 -0400923
qs.xiongf1b525b2022-03-31 00:58:23 -0400924int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500925{
you.chen35020192022-05-06 11:30:57 +0800926 int count = 0;
927 int index = 0;
928 char *split_words[128] = {0};
929 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400930
you.chen35020192022-05-06 11:30:57 +0800931 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400932
you.chen35020192022-05-06 11:30:57 +0800933 CHECK_WPA_CTRL(CTRL_AP);
934
935 DO_REQUEST(lynq_chanspec_cmd);
936
937 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
938 for(;index < count; index++) {
939 printf("---- %s\n",split_words[index]);
940 if (strncmp(split_words[index], "channel", 2) != 0) {
941 continue;
942 }
943
944 index++;
945 if (index >= count) {
946 return -1;
947 }
948
949 *channel = atoi(split_words[index]);
950 return 0;
951 }
952
953 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500954}
955
956
you.chen35020192022-05-06 11:30:57 +0800957int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500958{
you.chen6c2dd9c2022-05-16 17:55:28 +0800959 char ssid[MAX_CMD] = {0};
960 int freq = 0;
961 char lynq_auth_cmd[64]={0};
962 char lynq_auth_alg_cmd[64]={0};
963 char lynq_psk_cmd[64]={0};
964 char lynq_pairwise_cmd[64]={0};
965 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +0800966 CHECK_IDX(idx, CTRL_AP);
967
you.chen6c2dd9c2022-05-16 17:55:28 +0800968 CHECK_WPA_CTRL(CTRL_AP);
969
you.chen35020192022-05-06 11:30:57 +0800970 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
971 return -1;
972 }
973
you.chen92fd5d32022-05-25 10:09:47 +0800974 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800975 if (org_auth == auth) {
976 return 0;
977 }
978 else {
979 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
980 ssid[0] = '\0';
981 }
982 lynq_wifi_ap_frequency_get(idx, &freq);
983
984 DO_OK_FAIL_REQUEST(cmd_disconnect);
985 DO_OK_FAIL_REQUEST(cmd_remove_all);
986 if (ssid[0] != '\0') {
987 lynq_wifi_ap_ssid_set(idx, ssid);
988 }
989 if (freq != 0) {
990 lynq_wifi_ap_frequency_set(idx, freq);
991 }
992 }
993 }
you.chen35020192022-05-06 11:30:57 +0800994
qs.xiong7a105ce2022-03-02 09:43:11 -0500995 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400996 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +0800997 {
you.chen35020192022-05-06 11:30:57 +0800998 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +0800999 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001000
you.chen35020192022-05-06 11:30:57 +08001001 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001002 break;
1003 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001004 case LYNQ_WIFI_AUTH_WEP:
1005 {
1006 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001007 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001008 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1009
1010 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1011 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1012 break;
1013 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001014 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001015 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001016 {
you.chen35020192022-05-06 11:30:57 +08001017 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1018 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1019 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1020 }
1021 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001022 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001023 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001024 }
1025// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1026// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1027 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001028
you.chen35020192022-05-06 11:30:57 +08001029 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1030 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1031 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001032 break;
you.chen35020192022-05-06 11:30:57 +08001033 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001034 default:
you.chen35020192022-05-06 11:30:57 +08001035 {
1036 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001037 return -1;
you.chen35020192022-05-06 11:30:57 +08001038 }
1039 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001040 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001041
qs.xiong7a105ce2022-03-02 09:43:11 -05001042 return 0;
1043}
1044
you.chen35020192022-05-06 11:30:57 +08001045int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001046{
you.chen35020192022-05-06 11:30:57 +08001047 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001048 char lynq_auth_alg_str[MAX_RET] = {0};
1049 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001050
1051 CHECK_IDX(idx, CTRL_AP);
1052
1053 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1054 return -1;
1055 }
1056
1057 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001058 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1059 *auth = LYNQ_WIFI_AUTH_OPEN;
1060 }
1061 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1062 *auth = LYNQ_WIFI_AUTH_WEP;
1063 }
1064 else {
1065 *auth = LYNQ_WIFI_AUTH_OPEN;
1066 }
you.chen35020192022-05-06 11:30:57 +08001067 }
you.chen92fd5d32022-05-25 10:09:47 +08001068 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001069 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001070 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001071 }
1072 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1073 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1074 }
1075 else {
1076 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1077 }
you.chen35020192022-05-06 11:30:57 +08001078 }
you.chen92fd5d32022-05-25 10:09:47 +08001079 else {
1080 *auth = -1;
1081 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001082
you.chen6c2dd9c2022-05-16 17:55:28 +08001083 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001084}
qs.xiong1af5daf2022-03-14 09:12:12 -04001085
qs.xiong1af5daf2022-03-14 09:12:12 -04001086
qs.xiongf1b525b2022-03-31 00:58:23 -04001087int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001088{
you.chen35020192022-05-06 11:30:57 +08001089 char LYNQ_WIFI_CMD[128]={0};
1090 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1091 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001092
you.chen35020192022-05-06 11:30:57 +08001093 CHECK_IDX(idx, CTRL_AP);
1094
1095 CHECK_WPA_CTRL(CTRL_AP);
1096
1097// system("connmanctl enable wifi");
1098// system("connmanctl tether wifi on cy-test 12345678");
1099// system("ifconfig wlan0 down");
1100// system("ifconfig wlan0 up");
1101// system("ifconfig wlan0 up");
1102
1103 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1104 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1105
1106 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1107 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1108
qs.xiong7a105ce2022-03-02 09:43:11 -05001109 return 0;
1110}
1111
qs.xiongf1b525b2022-03-31 00:58:23 -04001112int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001113{
you.chen35020192022-05-06 11:30:57 +08001114 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001115}
1116
qs.xiongf1b525b2022-03-31 00:58:23 -04001117int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001118{
you.chen35020192022-05-06 11:30:57 +08001119 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001120
you.chen35020192022-05-06 11:30:57 +08001121 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001122
you.chen35020192022-05-06 11:30:57 +08001123 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001124
you.chen35020192022-05-06 11:30:57 +08001125 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1126
1127 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1128
you.chenb4b121c2022-05-06 17:50:16 +08001129// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001130
qs.xiong7a105ce2022-03-02 09:43:11 -05001131 return 0;
1132}
qs.xiong1af5daf2022-03-14 09:12:12 -04001133
qs.xiongf1b525b2022-03-31 00:58:23 -04001134int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001135{
you.chen35020192022-05-06 11:30:57 +08001136 char lynq_disable_cmd[128] = {0};
1137 char lynq_select_cmd[128] = {0};
1138 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001139
you.chen35020192022-05-06 11:30:57 +08001140 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001141
you.chen35020192022-05-06 11:30:57 +08001142 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001143
you.chen35020192022-05-06 11:30:57 +08001144 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1145 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1146
1147 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1148 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1149 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001150
1151 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001152}
1153
qs.xiongf1b525b2022-03-31 00:58:23 -04001154int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001155{
you.chen35020192022-05-06 11:30:57 +08001156 char lynq_disable_cmd[128] = {0};
1157 char lynq_select_cmd[128] = {0};
1158 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001159
you.chen35020192022-05-06 11:30:57 +08001160 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001161
you.chen35020192022-05-06 11:30:57 +08001162 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001163
you.chen35020192022-05-06 11:30:57 +08001164 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1165 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1166
1167 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1168 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1169 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001170
1171 return 0;
1172}
qs.xiongf1b525b2022-03-31 00:58:23 -04001173
you.chen35020192022-05-06 11:30:57 +08001174int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001175{
qs.xiongf1b525b2022-03-31 00:58:23 -04001176 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001177 char lynq_tmp_cmd[MAX_CMD] = {0};
1178 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001179 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001180 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001181 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001182 return -1;
you.chen35020192022-05-06 11:30:57 +08001183 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001184
you.chen35020192022-05-06 11:30:57 +08001185 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001186
you.chen6c2dd9c2022-05-16 17:55:28 +08001187 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1188 return -1;
1189 }
1190 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1191 return -1;
1192 }
1193
you.chen35020192022-05-06 11:30:57 +08001194 CHECK_WPA_CTRL(CTRL_AP);
1195
you.chen6c2dd9c2022-05-16 17:55:28 +08001196 if (auth == LYNQ_WIFI_AUTH_WEP) {
1197 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1198 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1199 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1200 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1201 }
1202 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1203 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1204 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1205 }
1206 else {
1207 return -1;
1208 }
you.chen35020192022-05-06 11:30:57 +08001209
you.chen35020192022-05-06 11:30:57 +08001210 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001211
qs.xiongf1b525b2022-03-31 00:58:23 -04001212 return 0;
1213}
1214
you.chen35020192022-05-06 11:30:57 +08001215int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001216{
you.chen35020192022-05-06 11:30:57 +08001217 FILE * fp;
1218 int len, ret;
1219 int count, index;
1220 char *split_lines[128] = {0};
1221 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001222
you.chen35020192022-05-06 11:30:57 +08001223 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001224
you.chen35020192022-05-06 11:30:57 +08001225 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1226// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1227 if (NULL == fp) {
1228 printf("open file fail\n");
1229 return -1;
1230 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001231
you.chen35020192022-05-06 11:30:57 +08001232 buff = alloca(MAX_RET);
1233 fseek(fp, 0, SEEK_SET);
1234 len = fread(buff, 1, MAX_RET, fp);
1235 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001236
you.chen35020192022-05-06 11:30:57 +08001237 for(index=0; index < len; index ++) {
1238 if (memcmp(buff + index, "network={", 9) != 0) {
1239 continue;
1240 }
1241 p = buff + index + 9;
1242 for (; index < len; index ++ ) {
1243 if (buff[index] != '}') {
1244 continue;
1245 }
1246 buff[index] = '\0';
1247 break;
1248 }
1249 len = buff + index - p;
1250 }
1251
1252 count = lynq_split(p, len, '\n', split_lines);
1253
1254 ret = -1;
1255 for(index=0; index < count; index++) {
1256 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001257 if (p != NULL) {
1258 p += 4;
1259 if (*p == '\"') {
1260 p++;
1261 }
you.chen35020192022-05-06 11:30:57 +08001262 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001263 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1264 p += 9;
1265 if (*p == '\"') {
1266 p++;
1267 }
1268 }
1269 else {
1270 continue;
you.chen35020192022-05-06 11:30:57 +08001271 }
1272
1273 strcpy(password, p);
1274
1275 while(*password != '\0') {
1276 if (*password == '\"') {
1277 *password = '\0';
1278 break;
1279 }
1280 password++;
1281 }
1282 ret = 0;
1283 break;
1284 } //end for(index=0; index < count; index++)
1285
1286 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001287}
1288
you.chen35020192022-05-06 11:30:57 +08001289static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1290 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001291 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001292
you.chen35020192022-05-06 11:30:57 +08001293 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1294 return -1;
1295 }
1296
1297 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001298
1299 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1300 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1301 if (strcmp(lynq_proto_str, "RSN") == 0) {
1302 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1303 }
1304 }
1305 }
you.chen35020192022-05-06 11:30:57 +08001306 return 0;
1307}
1308
1309int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001310{
you.chen35020192022-05-06 11:30:57 +08001311 int pass_len, net_no, count, index;
1312 char lynq_tmp_cmd[300]={0};
1313 int net_no_list[128];
1314 lynq_wifi_auth_s net_auth;
1315 pass_len=strlen(password);
1316 if(pass_len < 8 || pass_len >= 64){
1317 return -1;
1318 }
1319
1320 CHECK_IDX(idx, CTRL_STA);
1321
1322 net_no = -1;
1323 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1324
1325 for (index=0; index < count; index++) {
1326 net_auth = -1;
1327 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1328 net_no = net_no_list[index];
1329 break;
1330 }
1331 }
1332
1333 if (net_no < 0) {
1334 return -1;
1335 }
1336
1337 CHECK_WPA_CTRL(CTRL_STA);
1338
1339 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1340
1341 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1342 DO_OK_FAIL_REQUEST(cmd_save_config);
1343
1344 return 0;
1345}
1346
1347int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1348
1349 FILE * fp;
1350 int len, ret;
1351 int count, index;
1352 char *split_lines[128] = {0};
1353 char *buff, *p;
1354
1355 CHECK_IDX(idx, CTRL_STA);
1356
1357 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1358 if (NULL == fp) {
1359 printf("open file fail\n");
1360 return -1;
1361 }
1362
1363 buff = alloca(MAX_RET);
1364 fseek(fp, 0, SEEK_SET);
1365 len = fread(buff, 1, MAX_RET, fp);
1366 fclose(fp);
1367
1368 for(index=0; index < len; index ++) {
1369 for(; index < len; index ++) {
1370 if (memcmp(buff + index, "network={", 9) != 0) {
1371 continue;
1372 }
1373 p = buff + index + 9;
1374 for (; index < len; index ++ ) {
1375 if (buff[index] != '}') {
1376 continue;
1377 }
1378 buff[index] = '\0';
1379 break;
1380 }
1381 len = buff + index - p;
1382 }
1383
1384 if (strstr(p, ap->ap_ssid) != NULL) {
1385 break;
1386 }
1387 }
1388
1389 if (index >= len) {
1390 return -1;
1391 }
1392
1393 count = lynq_split(p, len, '\n', split_lines);
1394
1395 ret = -1;
1396 for(index=0; index < count; index++) {
1397 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001398 if (p != NULL) {
1399 p += 4;
1400 if (*p == '\"') {
1401 p++;
1402 }
you.chen35020192022-05-06 11:30:57 +08001403 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001404 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1405 p += 9;
1406 if (*p == '\"') {
1407 p++;
1408 }
1409 }
1410 else {
1411 continue;
you.chen35020192022-05-06 11:30:57 +08001412 }
1413
1414 strcpy(password, p);
1415
1416 while(*password != '\0') {
1417 if (*password == '\"') {
1418 *password = '\0';
1419 break;
1420 }
1421 password++;
1422 }
1423 ret = 0;
1424 break;
1425 } //end for(index=0; index < count; index++)
1426
1427 return ret;
1428}
1429
1430static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1431{
qs.xiong97fa59b2022-04-07 05:41:29 -04001432 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001433
you.chen35020192022-05-06 11:30:57 +08001434 if (sta_ssid == NULL) {
1435 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001436 return -1;
you.chen35020192022-05-06 11:30:57 +08001437 }
1438
1439 CHECK_WPA_CTRL(CTRL_STA);
1440
1441 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1442
1443 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1444// DO_OK_FAIL_REQUEST(cmd_save_config);
1445
qs.xiong7a105ce2022-03-02 09:43:11 -05001446 return 0;
1447
1448}
1449
you.chen35020192022-05-06 11:30:57 +08001450static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001451{
you.chen35020192022-05-06 11:30:57 +08001452 char lynq_disable_cmd[128]={0};
1453 char lynq_select_cmd[128]={0};
1454
1455 CHECK_WPA_CTRL(CTRL_STA);
1456
1457 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001458 if (start_flag != 0)
1459 {
1460 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1461 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1462 }
1463 else
1464 {
1465 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1466 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1467 }
you.chen35020192022-05-06 11:30:57 +08001468 DO_OK_FAIL_REQUEST(cmd_save_config);
1469 }
1470
1471 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001472 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001473 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1474 }
1475 else {
1476 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1477 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1478 }
1479
1480 return 0;
1481}
1482
1483int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1484{
1485 CHECK_IDX(idx, CTRL_STA);
1486
you.chen6c2dd9c2022-05-16 17:55:28 +08001487 curr_status_info curr_state;
1488 ap_info_s ap_info;
1489 curr_state.ap = &ap_info;
1490 curr_state.state = NULL;
1491
1492 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1493 strcpy(sta_ssid, ap_info.ap_ssid);
1494 return 0;
1495 }
1496
1497 return -1;
you.chen35020192022-05-06 11:30:57 +08001498}
1499
1500int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1501{
1502 scan_info_s *scan_list;
1503 saved_ap_info_s *save_list;
1504 int scan_len=0;
1505 int save_len=0;
1506 int best_index = -1;
1507 int best_scan_index = -1;
1508 int best_rssi = 0;
1509 int i, j;
1510
1511 CHECK_IDX(idx, CTRL_STA);
1512 if (info == NULL) {
1513 return -1;
1514 }
1515
1516 curr_status_info curr_state;
1517 ap_info_s ap_info;
1518 curr_state.ap = &ap_info;
1519 curr_state.state = NULL;
1520
1521 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1522 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1523 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1524 lynq_get_connect_ap_rssi(idx, &info->rssi);
1525 return 0;
1526 }
1527
1528 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1529 return -1;
1530 }
1531
1532 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1533 return -1;
1534 }
1535
1536 for (i=0; i < save_len; i++) {
1537 for (j=0; j < scan_len; j++) {
1538 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1539 && save_list[i].base_info.auth == scan_list[j].auth) {
1540 if (best_rssi == 0) {
1541 best_rssi = scan_list[j].rssi;
1542 }
1543 else if (best_rssi > scan_list[j].rssi) {
1544 best_index = i;
1545 best_scan_index = j;
1546 best_rssi = scan_list[j].rssi;
1547 }
1548 break;
1549 }
1550 }
1551 }
1552
1553 if (best_index >= 0) {
1554 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1555 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1556 info->rssi = best_rssi;
1557 return 0;
1558 }
1559
1560 return -1;
1561}
1562
1563static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1564{
1565 char lynq_auth_cmd[64]={0};
1566 char lynq_ket_mgmt_cmd[64]={0};
1567 char lynq_pairwise_cmd[64]={0};
1568 char lynq_psk_cmd[64]={0};
1569
1570 CHECK_WPA_CTRL(CTRL_STA);
1571
qs.xiong1af5daf2022-03-14 09:12:12 -04001572 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001573 case LYNQ_WIFI_AUTH_OPEN:
1574 {
1575 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001576
you.chen35020192022-05-06 11:30:57 +08001577 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1578// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001579 break;
1580 }
you.chen35020192022-05-06 11:30:57 +08001581 case LYNQ_WIFI_AUTH_WPA_PSK:
1582 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001583 {
you.chen35020192022-05-06 11:30:57 +08001584 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1585 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1586 }
1587 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001588 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001589 }
1590 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1591 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001592
you.chen35020192022-05-06 11:30:57 +08001593 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1594 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1595 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001596
you.chen35020192022-05-06 11:30:57 +08001597 if (password != NULL) {
1598 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1599 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1600 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001601
you.chen35020192022-05-06 11:30:57 +08001602// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001603 break;
you.chen35020192022-05-06 11:30:57 +08001604 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001605 default:
1606 return -1;
you.chen35020192022-05-06 11:30:57 +08001607 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001608
qs.xiongf1b525b2022-03-31 00:58:23 -04001609 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001610}
qs.xiong7a105ce2022-03-02 09:43:11 -05001611
you.chen35020192022-05-06 11:30:57 +08001612static int inner_get_curr_net_no(int interface) {
1613 curr_status_info curr_state;
1614 curr_state.ap = NULL;
1615 curr_state.state = NULL;
1616
1617 if (0 != inner_get_status_info(interface, &curr_state)) {
1618 return -1;
1619 }
1620
1621 return curr_state.net_no;
1622}
1623
1624int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001625{
you.chen35020192022-05-06 11:30:57 +08001626 int net_no;
1627 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001628
you.chen35020192022-05-06 11:30:57 +08001629 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001630
you.chen35020192022-05-06 11:30:57 +08001631 if (net_no < 0) {
1632 return -1;
1633 }
1634
1635 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001636}
1637
you.chen35020192022-05-06 11:30:57 +08001638int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong7a105ce2022-03-02 09:43:11 -05001639{
you.chen35020192022-05-06 11:30:57 +08001640 int count, net_no, index;
1641 int net_no_list[128];
1642 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001643
you.chen35020192022-05-06 11:30:57 +08001644 if (ssid == NULL || *ssid == '\0') {
1645 printf("bad ssid\n");
1646 return -1;
1647 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001648
you.chen35020192022-05-06 11:30:57 +08001649 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1650 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1651 printf("bad password\n");
1652 return -1;
1653 }
1654 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001655
you.chen35020192022-05-06 11:30:57 +08001656 CHECK_IDX(idx, CTRL_STA);
1657
1658 net_no = -1;
1659 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1660
1661 for (index=0; index < count; index++) {
1662 net_auth = -1;
1663 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1664 net_no = net_no_list[index];
1665 break;
1666 }
1667 }
1668
1669 if (net_no < 0) {
1670 net_no = lynq_add_network(CTRL_STA);
1671 if (net_no == -1) {
1672 return -1;
1673 }
1674
1675 printf("net no is %d\n", net_no);
1676 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1677 return -1;
1678 }
1679 }
1680
1681 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1682 return -1;
1683 }
1684
1685 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001686}
1687
you.chen35020192022-05-06 11:30:57 +08001688int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001689{
you.chen35020192022-05-06 11:30:57 +08001690 ap_info_s ap;
1691 curr_status_info curr_state;
1692 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001693
you.chen35020192022-05-06 11:30:57 +08001694 if (ssid == NULL || *ssid == '\0') {
1695 return -1;
1696 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001697
you.chen35020192022-05-06 11:30:57 +08001698 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001699
you.chen35020192022-05-06 11:30:57 +08001700 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001701 curr_state.state = NULL;
1702
you.chen35020192022-05-06 11:30:57 +08001703 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1704 return 0;
1705 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001706
you.chen35020192022-05-06 11:30:57 +08001707 if (strcmp(ap.ap_ssid, ssid) != 0) {
1708 return 0;
1709 }
1710
1711 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001712}
qs.xiong97fa59b2022-04-07 05:41:29 -04001713
you.chena6cd55a2022-05-08 12:20:18 +08001714int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1715{
you.chenc29444e2022-06-07 18:01:16 +08001716 const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
you.chen01bfac32022-06-07 10:36:00 +08001717 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001718
you.chen35020192022-05-06 11:30:57 +08001719 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001720 CHECK_WPA_CTRL(CTRL_STA);
1721
1722 system("connmanctl enable wifi");
1723
you.chena6fa5b22022-05-18 10:28:19 +08001724 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001725 return -1;
1726 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001727
you.chen01bfac32022-06-07 10:36:00 +08001728 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001729 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001730 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001731
1732 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001733}
1734
you.chen35020192022-05-06 11:30:57 +08001735int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001736{
you.chena6cd55a2022-05-08 12:20:18 +08001737 char lynq_disable_network_cmd[MAX_CMD];
1738 curr_status_info curr_state;
1739 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001740
you.chena6cd55a2022-05-08 12:20:18 +08001741 CHECK_IDX(idx, CTRL_STA);
1742 CHECK_WPA_CTRL(CTRL_STA);
1743
1744 curr_state.ap = &ap_info;
1745 curr_state.state = NULL;
1746
1747 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1748 return 0;
1749 }
1750
1751 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1752 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1753
1754 DO_OK_FAIL_REQUEST(cmd_save_config);
1755
1756 return 0;
1757// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001758}
qs.xiong7a105ce2022-03-02 09:43:11 -05001759
you.chen35020192022-05-06 11:30:57 +08001760//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1761// int i, count;
1762// char *p;
1763// const char * FLAG_SSID = "ssid=";
1764// const char * FLAG_SBSID = "bssid=";
1765// const char * FLAG_KEY_MGMT = "key_mgmt=";
1766// const char * FLAG_FREQ = "freq=";
1767// char lynq_sta_cmd[MAX_CMD];
1768// char *split_lines[128] = {0};
1769
1770// CHECK_WPA_CTRL(CTRL_AP);
1771
1772// sprintf(lynq_sta_cmd, "STA %s", bssid);
1773
1774// DO_REQUEST(lynq_sta_cmd);
1775
1776// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1777
1778// for(i=0; i < count; i++) {
1779// p = strstr(split_lines[i], FLAG_SSID);
1780// if (p != NULL) {
1781// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1782// continue;
1783// }
1784// }
1785
1786// lynq_get_interface_ip(idx, ap->ap_ip);
1787// lynq_ap_password_set(idx, ap->psw);
1788
1789// return 0;
1790//}
1791
1792static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1793 curr_status_info curr_state;
1794 curr_state.ap = ap;
1795 curr_state.state = NULL;
1796 return inner_get_status_info(interface, &curr_state);
1797}
1798
1799int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001800{
you.chen35020192022-05-06 11:30:57 +08001801 int ip_count, index, i, line_count;
1802 const char *lynq_first_sta_cmd = "STA-FIRST";
1803 char lynq_next_sta_cmd[MAX_CMD];
1804 char *bssid[1024] = {0};
1805 char *mac_list[128] = {0};
1806 char *ip_list[128] = {0};
1807 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001808
you.chen35020192022-05-06 11:30:57 +08001809 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001810
you.chen35020192022-05-06 11:30:57 +08001811 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001812
you.chen35020192022-05-06 11:30:57 +08001813// ap_info_s * tmp_ap;
1814// device_info_s * tmp_list;
1815 if (ap == NULL || list == NULL || len == NULL) {
1816 printf("bad input param");
1817 return -1;
1818 }
1819
1820// ap = &tmp_ap;
1821// list = &tmp_list;
1822 *ap = malloc(sizeof (ap_info_s));
1823
1824 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1825 return -1;
1826 }
1827
1828 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1829 lynq_ap_password_get(idx, (*ap)->psw);
1830
1831 ip_count = get_ip_mac_list(mac_list, ip_list);
1832 printf("get count %d\n", ip_count);
1833
1834 DO_REQUEST(lynq_first_sta_cmd);
1835
1836 index = 0;
1837 while (reply_len > 0) {
1838 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1839 break;
1840 }
1841 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1842 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1843 strcpy(bssid[index], split_lines[0]);
1844 index++;
1845 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1846 reply_len = MAX_RET;
1847 cmd_reply[0] = '\0';
1848 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1849 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1850 break;
1851 }
1852 }
1853
1854 *len = index;
1855
1856 *list = malloc(sizeof(device_info_s) * (*len));
1857 for (index=0; index < *len; index++) {
1858 strcpy((*list)[index].sta_mac, bssid[index]);
1859 for(i=0;i < ip_count; i++ ) {
1860 if (strcmp(bssid[index], mac_list[i]) == 0) {
1861 strcpy((*list)[index].sta_ip, ip_list[i]);
1862 break;
1863 }
1864 }
1865 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1866 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1867 free(bssid[index]);
1868 }
1869
1870 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001871}
1872
you.chen35020192022-05-06 11:30:57 +08001873int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001874{
you.chen35020192022-05-06 11:30:57 +08001875 int i, count, index, count_words;
1876 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1877 char *split_lines[128] = {0};
1878 char *split_words[128] = {0};
1879 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001880
you.chen35020192022-05-06 11:30:57 +08001881 if (list == NULL || len == NULL) {
1882 return -1;
1883 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001884
you.chen35020192022-05-06 11:30:57 +08001885 CHECK_IDX(idx, CTRL_STA);
1886
1887 CHECK_WPA_CTRL(CTRL_STA);
1888
1889 DO_REQUEST(lynq_scan_result_cmd);
1890
1891 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1892 *len = count - 1;
1893 *list = malloc(sizeof (scan_info_s) * *len);
1894
1895 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1896 for (index=0; index <count_words; index++) {
1897 printf("----header: %s\n", split_words[index]);
1898 }
1899
1900 for(index = 1;index < count; index++) {
1901 printf("---- %s\n",split_lines[index]);
1902 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1903 if (count_words < 4)
1904 continue;
1905 printf("count: %d, %s\n", count_words, split_words[0]);
1906 //bssid / frequency / signal level / flags / ssid
1907 p = (*list) + index - 1;
1908 strcpy(p->mac, split_words[0]);
1909 p->band = convert_band_from_freq(atoi(split_words[1]));
1910 p->rssi = -1 * atoi( split_words[2]);
1911 p->auth = convert_max_auth_from_flag(split_words[3]);
1912 strcpy(p->ssid, split_words[4]);
1913 }
1914
1915 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001916}
qs.xiong97fa59b2022-04-07 05:41:29 -04001917
you.chen35020192022-05-06 11:30:57 +08001918int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1919{
1920 int count, net_no, index;
1921 int net_no_list[128];
1922 lynq_wifi_auth_s net_auth;
1923 char lynq_remove_cmd[MAX_CMD];
1924
1925 if (ssid == NULL || *ssid == '\0') {
1926 printf("bad ssid\n");
1927 return -1;
1928 }
1929
1930 CHECK_IDX(idx, CTRL_STA);
1931
1932 CHECK_WPA_CTRL(CTRL_STA);
1933
1934 net_no = -1;
1935 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1936
1937 for (index=0; index < count; index++) {
1938 net_auth = -1;
1939 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1940 net_no = net_no_list[index];
1941 break;
1942 }
1943 }
1944
1945 if (net_no < 0) {
1946 return 0;
1947 }
1948
1949 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1950
1951 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1952 DO_OK_FAIL_REQUEST(cmd_save_config);
1953
1954 return 0;
1955}
1956
1957int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1958{
1959 int count, index;
1960 int net_no_list[128];
1961 char freq[16];
1962
1963 if (list == NULL || len == NULL) {
1964 printf("bad param\n");
1965 return -1;
1966 }
1967
1968 CHECK_IDX(idx, CTRL_STA);
1969
1970// CHECK_WPA_CTRL(CTRL_STA);
1971
1972 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1973 printf("count is %d\n", count);
1974
1975 *list = malloc(sizeof (saved_ap_info_s) * count);
1976 *len = count;
1977
1978 for (index=0; index < count; index++) {
1979 printf("to get ssid %d\n", index);
1980 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1981 printf("to get bssid\n");
1982 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1983 printf("to get inner_get_network_auth\n");
1984 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1985 printf("to get frequency\n");
1986 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1987 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1988 }
1989 else {
1990 (*list)[index].base_info.band = -1;
1991 }
1992
1993 }
1994
1995 return 0;
1996}
1997
1998int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1999{
2000 const char *lynq_scan_cmd = "SCAN";
2001
2002 CHECK_IDX(idx, CTRL_STA);
2003
2004 CHECK_WPA_CTRL(CTRL_STA);
2005
2006 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
2007
2008 return 0;
2009}
2010
2011int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2012 if (cb == NULL) {
2013 return -1;
2014 }
2015
2016 g_ap_callback_priv = priv;
2017 g_ap_callback_func = cb;
2018
2019 return 0;
2020}
2021
2022int lynq_unreg_ap_event_callback(void * priv) {
2023 if (g_ap_callback_priv == priv) {
2024 g_ap_callback_func = NULL;
2025 g_ap_callback_priv = NULL;
2026 return 0;
2027 }
2028 return -1;
2029}
2030
2031int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2032 if (cb == NULL) {
2033 return -1;
2034 }
2035
2036 g_sta_callback_priv = priv;
2037 g_sta_callback_func = cb;
2038
2039 return 0;
2040}
2041
2042int lynq_unreg_sta_event_callback(void * priv) {
2043 if (g_sta_callback_priv == priv) {
2044 g_sta_callback_func = NULL;
2045 g_sta_callback_priv = NULL;
2046 return 0;
2047 }
2048 return -1;
2049}
2050
2051
2052static int inner_get_status_info_state (int interface, char *state) {
2053 curr_status_info curr_state;
2054 curr_state.ap = NULL;
2055 curr_state.state = state;
2056 return inner_get_status_info(interface, &curr_state);
2057}
2058
2059int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2060{
2061 char state[MAX_CMD];
2062 const char * STATE_COMPLETED = "COMPLETED";
2063 CHECK_IDX(idx, CTRL_AP);
2064
2065 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2066 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2067 return 0;
2068 }
2069
2070 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2071 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2072 }
2073 else {
2074 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2075 }
2076
2077 return 0;
2078}
2079
2080int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2081 char state[MAX_CMD];
2082 const char * STATE_COMPLETED = "COMPLETED";
2083 CHECK_IDX(idx, CTRL_STA);
2084
2085 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2086 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2087 return 0;
2088 }
2089
2090 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2091 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2092 }
2093 else {
2094 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2095 }
2096
2097 return 0;
2098}
2099
2100int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2101// CHECK_IDX(idx, CTRL_AP);
2102// int ret = 0;
2103// size_t reply_len = MAX_RET;
2104// char cmd_reply[MAX_RET]={0};
2105// const char * cmd_str = "GET country";
2106// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2107// do{
2108// if (NULL == s_lynq_wpa_ctrl) {
2109// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2110// if (NULL == s_lynq_wpa_ctrl ) {
2111// printf("wpa_ctrl_open fail\n");
2112// return -1;
2113// }
2114// }
2115// }while(0);
2116
2117// do {
2118// reply_len = MAX_RET;
2119// cmd_reply[0] = '\0';
2120// printf("to call [%s]\n", cmd_str);
2121// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
2122// if (ret != 0) {
2123// printf("call ##cmd_str fail %d\n", ret);
2124// return ret;
2125// }
2126// cmd_reply[reply_len+1] = '\0';
2127// printf("cmd replay [ %s ]\n", cmd_reply);
2128// }while(0);
2129
2130 FILE *fp;
2131 size_t i = 0;
2132 char lynq_cmd_ret[MAX_RET]={0};
2133
2134// CHECK_IDX(idx, CTRL_AP);
2135
2136 if((fp=popen("wl country","r"))==NULL)
2137 {
2138 perror("popen error!");
2139 return -1;
2140 }
2141 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2142 {
2143 perror("fread fail!");
2144 return -1;
2145 }
2146
2147 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2148 if (lynq_cmd_ret[i] == ' ') {
2149 lynq_cmd_ret[i] = '\0';
2150 break;
2151 }
2152 }
2153
2154 strcpy(country_code,lynq_cmd_ret);
2155 printf("---country code %s\n", country_code);
2156
2157 int ret=pclose(fp);
2158 if(ret==-1)
2159 {
2160 perror("close file faild");
2161 }
2162
2163 return 0;
2164}
2165
2166int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2167// const char * cmd_str = "GET country";
2168// CHECK_IDX(idx, CTRL_AP);
2169// CHECK_WPA_CTRL(CTRL_STA);
2170
2171// DO_REQUEST(cmd_str);
2172// printf("result %s\n", cmd_reply);
2173
2174 if (country_code == NULL || *country_code == '\0') {
2175 printf("bad country code\n");
2176 return -1;
2177 }
2178
2179 char lynq_country_cmd[MAX_CMD];
2180 sprintf(lynq_country_cmd, "wl country %s", country_code);
2181 if (system(lynq_country_cmd) == 0) {
2182 return 0;
2183 }
2184
2185 return -1;
2186}
2187
2188int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2189{
2190 if (mac == NULL) {
2191 return -1;
2192 }
2193
2194 CHECK_IDX(idx, CTRL_STA);
2195 ap_info_s ap;
2196 ap.ap_mac[0] = '\0';
2197
2198 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2199 return -1;
2200 }
2201 strcpy(mac, ap.ap_mac);
2202
2203 return 0;
2204}
2205
2206int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2207{
2208 struct ifaddrs *ifaddr, *ifa;
2209 int family, s;
2210 char host[NI_MAXHOST];
2211 const char * ifaName = "wlan0";
you.chenf58b3c92022-06-21 16:53:48 +08002212 if (ip == NULL)
2213 {
2214 return -1;
2215 }
2216
you.chen35020192022-05-06 11:30:57 +08002217 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002218 ifaName = "tether";
you.chen35020192022-05-06 11:30:57 +08002219 }
2220 else if (idx != 0) {
2221 return -1;
2222 }
2223
2224 if (getifaddrs(&ifaddr) == -1)
2225 {
2226 perror("getifaddrs");
2227 return -1;
2228 //exit(EXIT_FAILURE);
2229 }
2230
2231
2232 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2233 {
2234 if (ifa->ifa_addr == NULL)
2235 continue;
2236 host[0] = '\0';
2237 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2238
2239 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2240 if((strcmp(ifa->ifa_name,ifaName)==0))
2241 {
2242 if (s != 0)
2243 {
2244 // printf("getnameinfo() failed: %s", gai_strerror(s));
2245 //exit(EXIT_FAILURE);
2246 }
2247 freeifaddrs(ifaddr);
2248 strcpy(ip, host);
2249 printf("ip %s\n", ip);
2250 return 0;
2251 //printf(" Interface : <%s>",ifa->ifa_name );
2252 //printf(" Address : <%s>", host);
2253 }
2254 }
2255 return -1;
2256}
2257
2258int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2259{
2260 int count;
2261 size_t i;
2262 char *split_words[128] = {0};
2263 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2264
2265 CHECK_WPA_CTRL(idx);
2266
2267 DO_REQUEST(lynq_get_mac_cmd);
2268
2269 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2270 return -1;
2271 }
2272
2273 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2274
2275 if (count < 2) {
2276 return -1;
2277 }
2278
2279 for (i=0; i < strlen(split_words[1]); i++ ) {
2280 if (split_words[1][i] != ' ') {
2281 break;
2282 }
2283 }
2284
2285 strcpy(mac, split_words[1] + i);
2286
2287 return 0;
2288}
2289
2290int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2291{
2292// int count;
2293// char *split_words[128] = {0};
2294// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2295
2296// if (rssi == NULL) {
2297// return -1;
2298// }
2299
2300// CHECK_IDX(idx, CTRL_STA);
2301
2302// CHECK_WPA_CTRL(CTRL_STA);
2303
2304// DO_REQUEST(lynq_get_rssi_cmd);
2305
2306// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2307// return -1;
2308// }
2309
2310// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2311
2312// if (count < 2) {
2313// return -1;
2314// }
2315
2316// *rssi = atoi(split_words[1]) * -1;
2317
2318 FILE *fp;
2319 size_t i = 0;
2320 char lynq_cmd_ret[MAX_RET]={0};
2321
2322// CHECK_IDX(idx, CTRL_AP);
2323
you.chen9f17e4d2022-06-06 17:18:18 +08002324 if((fp=popen("wl rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002325 {
2326 perror("popen error!");
2327 return -1;
2328 }
2329 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2330 {
2331 perror("fread fail!");
2332 return -1;
2333 }
you.chen9f17e4d2022-06-06 17:18:18 +08002334 *rssi = atoi(lynq_cmd_ret) * -1;
you.chen35020192022-05-06 11:30:57 +08002335
2336 return 0;
2337}
2338
2339int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2340{
2341 if (band == NULL) {
2342 return -1;
2343 }
2344
2345 CHECK_IDX(idx, CTRL_STA);
2346 ap_info_s ap;
2347 ap.band = -1;
2348
2349 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2350 return -1;
2351 }
2352 *band = ap.band;
2353
2354 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002355}
you.chenf58b3c92022-06-21 16:53:48 +08002356
2357int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2358{
2359 int index;
2360 int ip_count = 0;
2361 char bssid[1024] = {0};
2362 char *mac_list[128] = {0};
2363 char *ip_list[128] = {0};
2364
2365 if (ip == NULL)
2366 {
2367 printf("invalid param");
2368 return -1;
2369 }
2370
2371 CHECK_IDX(idx, CTRL_STA);
2372
2373 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2374 {
2375 return -1;
2376 }
2377
2378 ip_count = get_ip_mac_list(mac_list, ip_list);
2379
2380 for (index=0; index < ip_count; index++)
2381 {
2382 if (strcmp(bssid, mac_list[index]) == 0)
2383 {
2384 strcpy(ip, ip_list[index]);
2385 return 0;
2386 }
2387 }
2388
2389 printf("not found\n");
2390 return -1;
2391}
2392
2393