blob: 5ea342a0f425c8831bddb93b9fa464ea008286f6 [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;
you.chen9ac66392022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
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";
you.chen9ac66392022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen35020192022-05-06 11:30:57 +080062
qs.xiongfdbc58e2022-09-29 11:37:32 +080063static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-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.xiong97fa59b2022-04-07 05:41:29 -040071
72#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040073{\
you.chen35020192022-05-06 11:30:57 +080074 perror((str));\
75 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040076}
77
you.chen35020192022-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.xiongfdbc58e2022-09-29 11:37:32 +080089 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +080090 do{ \
qs.xiongfdbc58e2022-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.chen35020192022-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.xiongfdbc58e2022-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.chen35020192022-05-06 11:30:57 +0800106 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800107 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-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.chena6cd55a2022-05-08 12:20:18 +0800117 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800118 return -1; \
119 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800120 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-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.chen6c2dd9c2022-05-16 17:55:28 +0800131 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800132 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800133
134 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-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.chena6fa5b22022-05-18 10:28:19 +0800143 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800144 }
145
you.chen35020192022-05-06 11:30:57 +0800146 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
147 usleep(100*1000);
148 continue;
149 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800150 memset(msg_notify, 0, MAX_RET);
151 len = MAX_RET;
you.chen35020192022-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.chen92fd5d32022-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.xiong97fa59b2022-04-07 05:41:29 -0400170}
171
you.chen35020192022-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.xiongf1b525b2022-03-31 00:58:23 -0400177
you.chen6c2dd9c2022-05-16 17:55:28 +0800178 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800179 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800180
181 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-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.chena6fa5b22022-05-18 10:28:19 +0800190 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800191 }
192
you.chen35020192022-05-06 11:30:57 +0800193 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
194 usleep(100*1000);
195 continue;
196 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800197 memset(msg_notify, 0, MAX_RET);
198 len = MAX_RET;
you.chen35020192022-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.chen92fd5d32022-05-25 10:09:47 +0800220 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800221 error = LYNQ_TIME_OUT;
222 }
you.chen92fd5d32022-05-25 10:09:47 +0800223 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-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.chen92fd5d32022-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.xiongf1b525b2022-03-31 00:58:23 -0400241}
242
qs.xiong1af5daf2022-03-14 09:12:12 -0400243int lynq_wifi_enable(void)
244{
you.chen35020192022-05-06 11:30:57 +0800245 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800246 int i;
you.chen35020192022-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.xiong1af5daf2022-03-14 09:12:12 -0400254
you.chen35020192022-05-06 11:30:57 +0800255 ret = system(cmd_check_service);
256 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800257 printf("service state %d\n", ret);
qs.xiongfdbc58e2022-09-29 11:37:32 +0800258 return -1;
you.chen35020192022-05-06 11:30:57 +0800259 }
lhfe8da902022-10-11 18:55:36 +0800260
you.chen6c2dd9c2022-05-16 17:55:28 +0800261 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800262 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800263 break;
264 }
265 usleep(300*1000);
266 }
267
268 if (i >= 10) {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800269 return -1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800270 }
271
you.chen9f17e4d2022-06-06 17:18:18 +0800272 //@todo delete add temp check for socket avilable start (20220606)
273 for (i=0; i<60; i++)
274 {
275 if (system("netstat -an | grep -q DGRAM") == 0) {
276 break;
277 }
278 sleep(1);
279 }
280
281 if (i >= 60)
282 {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800283 return -1;
you.chen9f17e4d2022-06-06 17:18:18 +0800284 }
285 //@todo delete add temp check for socket avilable end (20220606)
286
you.chena6fa5b22022-05-18 10:28:19 +0800287 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800288 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800289 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800290 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800291 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800292 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800293 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800294 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800295 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800296 }
297
you.chen35020192022-05-06 11:30:57 +0800298 if (g_ap_watcher_pid == 0 ) {
299 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
300 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800301 return -1;
you.chen35020192022-05-06 11:30:57 +0800302 }
303 }
304
305 if (g_sta_watcher_pid == 0 ) {
306 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
307 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800308 return -1;
you.chen35020192022-05-06 11:30:57 +0800309 }
310 }
311
you.chena6fa5b22022-05-18 10:28:19 +0800312 for (i=0; i<10; i++) {
313 usleep(300*1000);
314 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
315 break;
316 }
317 }
318
you.chen35020192022-05-06 11:30:57 +0800319 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500320}
321
qs.xiong1af5daf2022-03-14 09:12:12 -0400322int lynq_wifi_disable(void)
323{
you.chen35020192022-05-06 11:30:57 +0800324 g_ap_watcher_stop_flag = 1;
325 g_sta_watcher_stop_flag = 1;
326 if (g_ap_watcher_pid != 0)
327 pthread_join(g_ap_watcher_pid, NULL);
328 if (g_sta_watcher_pid != 0)
329 pthread_join(g_sta_watcher_pid, NULL);
330 if (g_lynq_wpa_ctrl[0] != NULL)
331 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
332 if (g_lynq_wpa_ctrl[1] != NULL)
333 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
334 g_ap_watcher_pid = 0;
335 g_sta_watcher_pid = 0;
336 g_lynq_wpa_ctrl[0] = NULL;
337 g_lynq_wpa_ctrl[1] = NULL;
338 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500339 return 0;
340}
qs.xiong1af5daf2022-03-14 09:12:12 -0400341
you.chen35020192022-05-06 11:30:57 +0800342static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
343
344 char lynq_cmd_get[128]={0};
345
346 if (out_put == NULL) {
347 printf("output ptr is null\n");
348 return -1;
349 }
350 if (param_name == NULL) {
351 printf("param ptr is null");
352 return -1;
353 }
354 if (param_name[0] == '\0') {
355 printf("param is empty");
356 return -1;
357 }
358
359 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
360
361 CHECK_WPA_CTRL(interface);
362
363 DO_REQUEST(lynq_cmd_get);
364
365 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
366 return -1;
367 }
368
you.chena6fa5b22022-05-18 10:28:19 +0800369// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800370 memcpy(out_put, cmd_reply, reply_len + 1);
371 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500372}
qs.xiong1af5daf2022-03-14 09:12:12 -0400373
you.chen35020192022-05-06 11:30:57 +0800374static int lynq_split(char * str, int len, char delimiter, char * results[]) {
375 int ret = 0;
376 char * end = str + len - 1;
377 results[ret++] = str;
378 while(str < end) {
379 if (*str == delimiter) {
380 *str++ = '\0';
381 results[ret++] = str;
382 continue;
383 }
384 str++;
385 }
386 if (*str == delimiter) {
387 *str = '\0';
388 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400389
you.chen35020192022-05-06 11:30:57 +0800390 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500391}
392
you.chen35020192022-05-06 11:30:57 +0800393static void trim_space(char * p, int count) {
394 char * begin = p;
395 p += count;
396 printf("%C-%C||\n", *begin, *p);
397 while (p >= begin ) {
398 if (*p == ' ') {
399 *p-- = '\0';
400 }
401 else {
402 break;
403 }
404 }
405}
406
407static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
408 FILE * fp;
409 int len, ret;
410 int count, count_words, index;
411 int mac_start, mac_end;
412 int ip_start, ip_end;
413 char *split_lines[128] = {0};
414 char *buff;
415 const char * ip_header = "IP address";
416 const char * mac_header = "HW address";
417 const char * zero_mac = "00:00:00:00:00:00";
418
419 fp = fopen("/proc/net/arp", "rb");
420 if (NULL == fp) {
421 printf("open file fail\n");
422 return -1;
423 }
424
425 buff = alloca(MAX_RET);
426 fseek(fp, 0, SEEK_SET);
427 len = fread(buff, 1, MAX_RET, fp);
428 fclose(fp);
429 if (len <= 0) {
430 printf("read file fail\n");
431 return -1;
432 }
433 printf("file : %s\n", buff);
434
435 count = lynq_split(buff, len, '\n', split_lines);
436 printf("----- %s\n", split_lines[0]);
437
438 mac_end = 0;
439 count_words = strlen(split_lines[0]);
440 if (strstr(split_lines[0], mac_header) != NULL) {
441 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
442 mac_end = mac_start + strlen(mac_header) + 1;
443 while (mac_end < count_words) {
444 if (split_lines[0][mac_end] != ' ') {
445 break;
446 }
447 mac_end++;
448 }
449 }
450
451 ip_end = 0;
452 if (strstr(split_lines[0], ip_header) != NULL) {
453 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
454 ip_end = ip_start + strlen(ip_header) + 1;
455 while (ip_end < count_words) {
456 if (split_lines[0][ip_end] != ' ') {
457 break;
458 }
459 ip_end++;
460 }
461 }
462
463 if (mac_end == 0 || ip_end == 0) {
464 return 0;
465 }
466
467 ret = 0;
468 for(index = 1;index < count; index++) {
469 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
470 continue;
471 }
472 mac_list[ret] = malloc(mac_end - mac_start + 1);
473 ip_list[ret] = malloc(ip_end - ip_start + 1);
474 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
475 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
476 trim_space(mac_list[ret], mac_end - mac_start - 1);
477 trim_space(ip_list[ret], ip_end - ip_start - 1);
478 ret++;
479 }
480
481 return ret;
482}
483
you.chen9ac66392022-08-06 17:01:16 +0800484static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
485{
486 int i;
487 if (mac_list != NULL && mac_cnt > 0) {
488 for(i = 0; i< mac_cnt; i++)
489 {
490 if (NULL != mac_list[i])
491 {
492 free(mac_list[i]);
493 mac_list[i] = NULL;
494 }
495 }
496 }
497 if (ip_list != NULL && ip_cnt > 0) {
498 for(i = 0; i< mac_cnt; i++)
499 {
500 if (NULL != ip_list[i])
501 {
502 free(ip_list[i]);
503 ip_list[i] = NULL;
504 }
505 }
506 }
507}
508
you.chen35020192022-05-06 11:30:57 +0800509static int get_hostname_by_ip(char *ip, char *hostname) {
510 struct in_addr addr ={0};
511 struct hostent *ht;
512
513 if (ip == NULL || *ip == '\0' || hostname == NULL) {
514 return -1;
515 }
516
517 if (inet_aton(ip, &addr) == 0) {
518 printf("---inet_aton fail\n");
519 return -1;
520 }
521
522 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
523
524 if (ht == NULL) {
525 printf("---gethostbyaddr fail\n");
526 herror(NULL);
527 return -1;
528 }
529
530 strcpy(hostname, ht->h_name);
531
532 return 0;
533}
534
535static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
536{
537 int count, index, words_count;
538 char * split_lines[128]= {0};
539 char * split_words[128] = {0};
540 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
541
542 CHECK_WPA_CTRL(ap_sta);
543
544 DO_REQUEST(lynq_wifi_list_networks);
545
546 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
547
548 //@todo check ssid field to compatible
549
550 ret = 0;
551 for(index=1; index < count; index++) {
552 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
553 if (words_count > 2) {
554 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
555 net_no_list[ret++] = atoi(split_words[0]);
556 }
557 }
558 }
559
560 return ret;
561}
562
563static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800564 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800565 CHECK_WPA_CTRL(ap_sta);
566 const char *lynq_wifi_add_network = "ADD_NETWORK";
567
568 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800569 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800570 return -1;
571 }
572
you.chen6c2dd9c2022-05-16 17:55:28 +0800573 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800574 if(cmd_reply[i] == '\n') {
575 cmd_reply[i] = '\0';
576 break;
577 }
578 }
579 return atoi(cmd_reply);
580}
you.chena6cd55a2022-05-08 12:20:18 +0800581
you.chen35020192022-05-06 11:30:57 +0800582static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
583{
584 int count, index;
585 int net_no_list[128];
586
587
588 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
589 for (index=0; index < count; index++) {
590 if (net_no_list[index] == net_no) {
591 return 0;
592 }
593 }
594
595 if (count >= 1)
596 index = net_no_list[count - 1];
597 else
598 index = -1;
599
600 while (index < net_no ) {
601 index = lynq_add_network(ap_sta);
602 if (index >= net_no) { // required network no created
603 return 0;
604 }
you.chena6cd55a2022-05-08 12:20:18 +0800605 else if( index < 0) {
606 printf("add network fail\n");
607 return -1;
608 }
you.chen35020192022-05-06 11:30:57 +0800609 }
610
611 if (index < 0)
612 return -1;
613
614 return 0;
615}
616
617static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
618 if (freq > 5000 && freq < 6000) {
619 return LYNQ_WIFI_5G_band;
620 }
621 else if (freq > 2000 && freq < 3000) {
622 return LYNQ_WIFI_2G_band;
623 }
624 return LYNQ_WIFI_2_and_5G_band;
625}
626
627static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
628 if (key_mgmt != NULL) {
629 if (memcmp( key_mgmt, "NONE", 4) == 0) {
630 return LYNQ_WIFI_AUTH_OPEN;
631 }
632 else if (memcmp( key_mgmt, "WEP", 3) == 0){
633 return LYNQ_WIFI_AUTH_WEP;
634 }
635 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
636 return LYNQ_WIFI_AUTH_WPA_PSK;
637 }
638 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
639 return LYNQ_WIFI_AUTH_WPA2_PSK;
640 }
641 }
642
643 return -1;
644}
645
646static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
647 if (flag != NULL) {
648 if (strstr( flag, "WPA2-PSK") != NULL){
649 return LYNQ_WIFI_AUTH_WPA2_PSK;
650 }
651 else if (strstr( flag, "WPA-PSK") != NULL){
652 return LYNQ_WIFI_AUTH_WPA_PSK;
653 }
654 else if (strstr( flag, "WEP") != NULL){
655 return LYNQ_WIFI_AUTH_WEP;
656 }
657 else if (strstr( flag, "NONE") != NULL) {
658 return LYNQ_WIFI_AUTH_OPEN;
659 }
660 }
661
662 return -1;
663}
664
665static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
666 switch (bw) {
667 case 10:
668 return LYNQ_WIFI_BANDWIDTH_HT10;
669 break;
670 case 20:
671 return LYNQ_WIFI_BANDWIDTH_HT20;
672 break;
673 case 40:
674 return LYNQ_WIFI_BANDWIDTH_HT40;
675 break;
676 case 80:
677 return LYNQ_WIFI_BANDWIDTH_HT80;
678 break;
679 default:
680 break;
681 }
682
683 return -1;
684}
685
686static int inner_get_status_info(int interface, curr_status_info *curr_state) {
687 int i, count;
688 char *p;
689 const char *lynq_status_cmd = "STATUS";
690 const char * FLAG_SSID = "ssid=";
691 const char * FLAG_SBSID = "bssid=";
692 const char * FLAG_KEY_MGMT = "key_mgmt=";
693 const char * FLAG_FREQ = "freq=";
694 const char * FLAG_STATE = "wpa_state=";
695 const char * FLAG_ID = "id=";
696 char *split_lines[128] = {0};
697
698 CHECK_WPA_CTRL(interface);
699
700 if (curr_state == NULL) {
701 return -1;
702 }
703
704 DO_REQUEST(lynq_status_cmd);
705
706 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
707
708 curr_state->net_no = -1;
709 ret = -1;
710 for(i=0; i < count; i++) {
711 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800712 p = strstr(split_lines[i], FLAG_SBSID);
713 if (p != NULL) {
714 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
715 ret = 0;
716 continue;
717 }
you.chenf58b3c92022-06-21 16:53:48 +0800718 p = strstr(split_lines[i], FLAG_SSID);
719 if (p != NULL) {
720 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
721 ret = 0;
722 continue;
723 }
you.chen35020192022-05-06 11:30:57 +0800724 p = strstr(split_lines[i], FLAG_KEY_MGMT);
725 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800726 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
727 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800728 ret = 0;
729 continue;
730 }
731 p = strstr(split_lines[i], FLAG_FREQ);
732 if (p != NULL) {
733 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
734 ret = 0;
735 continue;
736 }
737 } // end if (ap != NULL)
738 if (curr_state->state != NULL) {
739 p = strstr(split_lines[i], FLAG_STATE);
740 if (p != NULL) {
741 strcpy(curr_state->state, p + strlen(FLAG_STATE));
742 ret = 0;
743 continue;
744 }
745
746 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800747 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800748 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800749 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800750 printf("net_no %d, -- %s\n", curr_state->net_no, p);
751 }
752 }
753
754 return ret;
755}
756
757
qs.xiongf1b525b2022-03-31 00:58:23 -0400758int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500759{
you.chen35020192022-05-06 11:30:57 +0800760 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500761
you.chen35020192022-05-06 11:30:57 +0800762 if (ap_ssid == NULL) {
763 printf("ap_ssid is null\n");
764 return -1;
765 }
766 else {
767 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
768 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400769
you.chen35020192022-05-06 11:30:57 +0800770 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
771 return -1;
772 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400773
you.chen35020192022-05-06 11:30:57 +0800774 CHECK_IDX(idx, CTRL_AP);
775
776 CHECK_WPA_CTRL(CTRL_AP);
777
778 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
779
780 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
781 DO_OK_FAIL_REQUEST(cmd_save_config);
782
qs.xiong7a105ce2022-03-02 09:43:11 -0500783 return 0;
you.chen35020192022-05-06 11:30:57 +0800784
qs.xiong7a105ce2022-03-02 09:43:11 -0500785}
786
you.chen35020192022-05-06 11:30:57 +0800787int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500788{
you.chen6c2dd9c2022-05-16 17:55:28 +0800789 int len;
you.chen35020192022-05-06 11:30:57 +0800790 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800791 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
792 return -1;
793 len = strlen(ap_ssid);
794 if (ap_ssid[0] == '\"') {
795 memmove(ap_ssid, ap_ssid + 1, len - 1);
796 len -= 1;
797 }
798 if (len > 0 && ap_ssid[len-1] == '\"') {
799 ap_ssid[len-1] = '\0';
800 }
801 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500802}
803
qs.xiongc9c79f72022-10-17 15:27:18 +0800804/*****
805 *frequency <------>channel
806 *
807 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
808 *
809 *
810 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
811 *
812 *
813 * */
814static int lynq_check_set_frequency(int input_frequency){
815 static int legitimate_frequency[21]={2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825};
816 int i = 0;
817 int flag_check = 0;
818 for(i=0 ;i<= 21; i++){
819 if(input_frequency == legitimate_frequency[i]){
820 flag_check = 1;
821 break;
822 }
823 }
824 if(flag_check == 1){
825 printf("input frequency in range\n");
826 }else{
827 printf("input frequency is eero--->%d,please check it\n",input_frequency);
828 return -1;
829 }
830 return 0;
831}
qs.xiongf1b525b2022-03-31 00:58:23 -0400832int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500833{
qs.xiongc9c79f72022-10-17 15:27:18 +0800834 char lynq_wifi_frequency_cmd[128]={0};
835 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800836 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500837
qs.xiongc9c79f72022-10-17 15:27:18 +0800838 //@do check input frequency
839 if((lynq_check_set_frequency(lynq_wifi_frequency)) != 0){
840 return -1;
you.chen35020192022-05-06 11:30:57 +0800841 }
you.chen35020192022-05-06 11:30:57 +0800842 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
843 return -1;
844 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400845
you.chen35020192022-05-06 11:30:57 +0800846 CHECK_IDX(idx, CTRL_AP);
847
848 CHECK_WPA_CTRL(CTRL_AP);
849
850 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
851 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
852 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
853
you.chen6c2dd9c2022-05-16 17:55:28 +0800854 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800855 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
856 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
857 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500858
qs.xiong1af5daf2022-03-14 09:12:12 -0400859 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500860}
861
qs.xiongf1b525b2022-03-31 00:58:23 -0400862int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500863{
you.chen35020192022-05-06 11:30:57 +0800864 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400865
you.chen35020192022-05-06 11:30:57 +0800866 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400867
you.chen35020192022-05-06 11:30:57 +0800868 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
869 return -1;
870 }
871 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400872
873 return 0;
874}
875
qs.xiongf1b525b2022-03-31 00:58:23 -0400876int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
877{
you.chen35020192022-05-06 11:30:57 +0800878 CHECK_IDX(idx, CTRL_AP);
879 switch(bandwidth){
880 case LYNQ_WIFI_BANDWIDTH_HT10:
881 {
882 printf("bandwith [%d] not support now\n", bandwidth);
883 return -1;
884 }
885 case LYNQ_WIFI_BANDWIDTH_HT20:
886 {
887 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
888 system("wl down");
889 if (system(lynq_cmd_bandwith) != 0 ){
890 return -1;
891 }
892 system("wl up");
893 break;
894 }
895 case LYNQ_WIFI_BANDWIDTH_HT40:
896 {
897 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
898 sprintf(lynq_cmd_bandwith, "wl chanspec ");
899 system("wl down");
900 if (system(lynq_cmd_bandwith) != 0 ){
901 return -1;
902 }
903 system("wl up");
904 break;
905 }
906 case LYNQ_WIFI_BANDWIDTH_HT80:
907 {
908 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
909 system("wl down");
910 if (system(lynq_cmd_bandwith) != 0 ){
911 return -1;
912 }
913 system("wl up");
914 break;
915 }
916 default:
917 {
918 printf("auth type [%d] not support now\n", bandwidth);
919 return -1;
920 }
921 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400922
923
you.chen35020192022-05-06 11:30:57 +0800924 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400925}
you.chen35020192022-05-06 11:30:57 +0800926
qs.xiongf1b525b2022-03-31 00:58:23 -0400927int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
928{
you.chen35020192022-05-06 11:30:57 +0800929 int count = 0;
930 int index = 0;
931 char *split_words[128] = {0};
932 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400933
you.chen35020192022-05-06 11:30:57 +0800934 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500935
you.chen35020192022-05-06 11:30:57 +0800936 CHECK_WPA_CTRL(CTRL_AP);
937
938 DO_REQUEST(lynq_chanspec_cmd);
939
940 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
941 for(;index < count; index++) {
942 if (strncmp(split_words[index], "bw", 2) != 0) {
943 continue;
944 }
945
946 index++;
947 if (index >= count) {
948 return -1;
949 }
950
951 printf("bw %s\n", split_words[index]);
952 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
953 return 0;
954 }
955
956 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500957}
qs.xiong0fb469a2022-04-14 03:50:45 -0400958
qs.xiongf1b525b2022-03-31 00:58:23 -0400959int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500960{
you.chen35020192022-05-06 11:30:57 +0800961 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400962
you.chen35020192022-05-06 11:30:57 +0800963 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400964
you.chen35020192022-05-06 11:30:57 +0800965 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400966
you.chen35020192022-05-06 11:30:57 +0800967 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
968 return -1;
969 }
970
971 system("wl down");
972 if (system(lynq_cmd_channel) != 0 ){
973 return -1;
974 }
975 system("wl up");
976 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500977}
qs.xiong0fb469a2022-04-14 03:50:45 -0400978
qs.xiongf1b525b2022-03-31 00:58:23 -0400979int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500980{
you.chen35020192022-05-06 11:30:57 +0800981 int count = 0;
982 int index = 0;
983 char *split_words[128] = {0};
984 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400985
you.chen35020192022-05-06 11:30:57 +0800986 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400987
you.chen35020192022-05-06 11:30:57 +0800988 CHECK_WPA_CTRL(CTRL_AP);
989
990 DO_REQUEST(lynq_chanspec_cmd);
991
992 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
993 for(;index < count; index++) {
994 printf("---- %s\n",split_words[index]);
995 if (strncmp(split_words[index], "channel", 2) != 0) {
996 continue;
997 }
998
999 index++;
1000 if (index >= count) {
1001 return -1;
1002 }
1003
1004 *channel = atoi(split_words[index]);
1005 return 0;
1006 }
1007
1008 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001009}
1010
1011
you.chen35020192022-05-06 11:30:57 +08001012int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001013{
you.chen6c2dd9c2022-05-16 17:55:28 +08001014 char ssid[MAX_CMD] = {0};
1015 int freq = 0;
1016 char lynq_auth_cmd[64]={0};
1017 char lynq_auth_alg_cmd[64]={0};
1018 char lynq_psk_cmd[64]={0};
1019 char lynq_pairwise_cmd[64]={0};
1020 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001021 CHECK_IDX(idx, CTRL_AP);
1022
you.chen6c2dd9c2022-05-16 17:55:28 +08001023 CHECK_WPA_CTRL(CTRL_AP);
1024
you.chen35020192022-05-06 11:30:57 +08001025 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1026 return -1;
1027 }
1028
you.chen92fd5d32022-05-25 10:09:47 +08001029 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001030 if (org_auth == auth) {
1031 return 0;
1032 }
1033 else {
1034 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1035 ssid[0] = '\0';
1036 }
1037 lynq_wifi_ap_frequency_get(idx, &freq);
1038
1039 DO_OK_FAIL_REQUEST(cmd_disconnect);
1040 DO_OK_FAIL_REQUEST(cmd_remove_all);
1041 if (ssid[0] != '\0') {
1042 lynq_wifi_ap_ssid_set(idx, ssid);
1043 }
1044 if (freq != 0) {
1045 lynq_wifi_ap_frequency_set(idx, freq);
1046 }
1047 }
1048 }
you.chen35020192022-05-06 11:30:57 +08001049
qs.xiong7a105ce2022-03-02 09:43:11 -05001050 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001051 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001052 {
you.chen35020192022-05-06 11:30:57 +08001053 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001054 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001055
you.chen35020192022-05-06 11:30:57 +08001056 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001057 break;
1058 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001059 case LYNQ_WIFI_AUTH_WEP:
1060 {
1061 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001062 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001063 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1064
1065 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1066 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1067 break;
1068 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001069 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001070 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001071 {
you.chen35020192022-05-06 11:30:57 +08001072 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1073 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1074 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1075 }
1076 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001077 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001078 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001079 }
1080// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1081// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1082 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001083
you.chen35020192022-05-06 11:30:57 +08001084 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1085 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1086 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001087 break;
you.chen35020192022-05-06 11:30:57 +08001088 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001089 default:
you.chen35020192022-05-06 11:30:57 +08001090 {
1091 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001092 return -1;
you.chen35020192022-05-06 11:30:57 +08001093 }
1094 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001095 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001096
qs.xiong7a105ce2022-03-02 09:43:11 -05001097 return 0;
1098}
1099
you.chen35020192022-05-06 11:30:57 +08001100int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001101{
you.chen35020192022-05-06 11:30:57 +08001102 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001103 char lynq_auth_alg_str[MAX_RET] = {0};
1104 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001105
1106 CHECK_IDX(idx, CTRL_AP);
1107
1108 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1109 return -1;
1110 }
1111
1112 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001113 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1114 *auth = LYNQ_WIFI_AUTH_OPEN;
1115 }
1116 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1117 *auth = LYNQ_WIFI_AUTH_WEP;
1118 }
1119 else {
1120 *auth = LYNQ_WIFI_AUTH_OPEN;
1121 }
you.chen35020192022-05-06 11:30:57 +08001122 }
you.chen92fd5d32022-05-25 10:09:47 +08001123 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001124 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001125 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001126 }
1127 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1128 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1129 }
1130 else {
1131 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1132 }
you.chen35020192022-05-06 11:30:57 +08001133 }
you.chen92fd5d32022-05-25 10:09:47 +08001134 else {
1135 *auth = -1;
1136 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001137
you.chen6c2dd9c2022-05-16 17:55:28 +08001138 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001139}
qs.xiong1af5daf2022-03-14 09:12:12 -04001140
qs.xiong1af5daf2022-03-14 09:12:12 -04001141
qs.xiongf1b525b2022-03-31 00:58:23 -04001142int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001143{
you.chen35020192022-05-06 11:30:57 +08001144 char LYNQ_WIFI_CMD[128]={0};
1145 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1146 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001147
you.chen35020192022-05-06 11:30:57 +08001148 CHECK_IDX(idx, CTRL_AP);
1149
1150 CHECK_WPA_CTRL(CTRL_AP);
1151
1152// system("connmanctl enable wifi");
1153// system("connmanctl tether wifi on cy-test 12345678");
1154// system("ifconfig wlan0 down");
1155// system("ifconfig wlan0 up");
1156// system("ifconfig wlan0 up");
1157
1158 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1159 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1160
1161 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1162 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1163
qs.xiong7a105ce2022-03-02 09:43:11 -05001164 return 0;
1165}
1166
qs.xiongf1b525b2022-03-31 00:58:23 -04001167int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001168{
you.chen35020192022-05-06 11:30:57 +08001169 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001170}
1171
qs.xiongf1b525b2022-03-31 00:58:23 -04001172int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001173{
you.chen35020192022-05-06 11:30:57 +08001174 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001175
you.chen35020192022-05-06 11:30:57 +08001176 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001177
you.chen35020192022-05-06 11:30:57 +08001178 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001179
you.chen35020192022-05-06 11:30:57 +08001180 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1181
1182 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1183
you.chenb4b121c2022-05-06 17:50:16 +08001184// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001185
qs.xiong7a105ce2022-03-02 09:43:11 -05001186 return 0;
1187}
qs.xiong1af5daf2022-03-14 09:12:12 -04001188
qs.xiongf1b525b2022-03-31 00:58:23 -04001189int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001190{
you.chen35020192022-05-06 11:30:57 +08001191 char lynq_disable_cmd[128] = {0};
1192 char lynq_select_cmd[128] = {0};
1193 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001194
you.chen35020192022-05-06 11:30:57 +08001195 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001196
you.chen35020192022-05-06 11:30:57 +08001197 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001198
you.chen35020192022-05-06 11:30:57 +08001199 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1200 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1201
1202 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1203 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1204 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001205
1206 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001207}
1208
qs.xiongf1b525b2022-03-31 00:58:23 -04001209int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001210{
you.chen35020192022-05-06 11:30:57 +08001211 char lynq_disable_cmd[128] = {0};
1212 char lynq_select_cmd[128] = {0};
1213 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001214
you.chen35020192022-05-06 11:30:57 +08001215 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001216
you.chen35020192022-05-06 11:30:57 +08001217 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001218
you.chen35020192022-05-06 11:30:57 +08001219 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1220 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1221
1222 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1223 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1224 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001225
1226 return 0;
1227}
qs.xiongf1b525b2022-03-31 00:58:23 -04001228
you.chen35020192022-05-06 11:30:57 +08001229int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001230{
qs.xiongf1b525b2022-03-31 00:58:23 -04001231 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001232 char lynq_tmp_cmd[MAX_CMD] = {0};
1233 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001234 if( password == NULL ){
1235 return -1;
1236 }
1237 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001238 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001239 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001240 return -1;
you.chen35020192022-05-06 11:30:57 +08001241 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001242
you.chen35020192022-05-06 11:30:57 +08001243 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001244
you.chen6c2dd9c2022-05-16 17:55:28 +08001245 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1246 return -1;
1247 }
1248 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1249 return -1;
1250 }
1251
you.chen35020192022-05-06 11:30:57 +08001252 CHECK_WPA_CTRL(CTRL_AP);
1253
you.chen6c2dd9c2022-05-16 17:55:28 +08001254 if (auth == LYNQ_WIFI_AUTH_WEP) {
1255 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1256 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1257 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1258 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1259 }
1260 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1261 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1262 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1263 }
1264 else {
1265 return -1;
1266 }
you.chen35020192022-05-06 11:30:57 +08001267
you.chen35020192022-05-06 11:30:57 +08001268 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001269
qs.xiongf1b525b2022-03-31 00:58:23 -04001270 return 0;
1271}
1272
you.chen35020192022-05-06 11:30:57 +08001273int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001274{
you.chen35020192022-05-06 11:30:57 +08001275 FILE * fp;
1276 int len, ret;
1277 int count, index;
1278 char *split_lines[128] = {0};
1279 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001280
you.chen35020192022-05-06 11:30:57 +08001281 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001282
you.chen35020192022-05-06 11:30:57 +08001283 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1284// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1285 if (NULL == fp) {
1286 printf("open file fail\n");
1287 return -1;
1288 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001289
you.chen35020192022-05-06 11:30:57 +08001290 buff = alloca(MAX_RET);
1291 fseek(fp, 0, SEEK_SET);
1292 len = fread(buff, 1, MAX_RET, fp);
1293 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001294
you.chen35020192022-05-06 11:30:57 +08001295 for(index=0; index < len; index ++) {
1296 if (memcmp(buff + index, "network={", 9) != 0) {
1297 continue;
1298 }
1299 p = buff + index + 9;
1300 for (; index < len; index ++ ) {
1301 if (buff[index] != '}') {
1302 continue;
1303 }
1304 buff[index] = '\0';
1305 break;
1306 }
1307 len = buff + index - p;
1308 }
1309
1310 count = lynq_split(p, len, '\n', split_lines);
1311
1312 ret = -1;
1313 for(index=0; index < count; index++) {
1314 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001315 if (p != NULL) {
1316 p += 4;
1317 if (*p == '\"') {
1318 p++;
1319 }
you.chen35020192022-05-06 11:30:57 +08001320 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001321 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1322 p += 9;
1323 if (*p == '\"') {
1324 p++;
1325 }
1326 }
1327 else {
1328 continue;
you.chen35020192022-05-06 11:30:57 +08001329 }
1330
1331 strcpy(password, p);
1332
1333 while(*password != '\0') {
1334 if (*password == '\"') {
1335 *password = '\0';
1336 break;
1337 }
1338 password++;
1339 }
1340 ret = 0;
1341 break;
1342 } //end for(index=0; index < count; index++)
1343
1344 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001345}
1346
you.chen35020192022-05-06 11:30:57 +08001347static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1348 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001349 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001350
you.chen35020192022-05-06 11:30:57 +08001351 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1352 return -1;
1353 }
1354
1355 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001356
1357 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1358 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1359 if (strcmp(lynq_proto_str, "RSN") == 0) {
1360 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1361 }
1362 }
1363 }
you.chen35020192022-05-06 11:30:57 +08001364 return 0;
1365}
1366
1367int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001368{
you.chen35020192022-05-06 11:30:57 +08001369 int pass_len, net_no, count, index;
1370 char lynq_tmp_cmd[300]={0};
1371 int net_no_list[128];
1372 lynq_wifi_auth_s net_auth;
1373 pass_len=strlen(password);
1374 if(pass_len < 8 || pass_len >= 64){
1375 return -1;
1376 }
1377
1378 CHECK_IDX(idx, CTRL_STA);
1379
1380 net_no = -1;
1381 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1382
1383 for (index=0; index < count; index++) {
1384 net_auth = -1;
1385 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1386 net_no = net_no_list[index];
1387 break;
1388 }
1389 }
1390
1391 if (net_no < 0) {
1392 return -1;
1393 }
1394
1395 CHECK_WPA_CTRL(CTRL_STA);
1396
1397 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1398
1399 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1400 DO_OK_FAIL_REQUEST(cmd_save_config);
1401
1402 return 0;
1403}
1404
1405int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1406
1407 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001408 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001409 int count, index;
1410 char *split_lines[128] = {0};
1411 char *buff, *p;
1412
you.chen755332b2022-08-06 16:59:10 +08001413 network_len = 0;
1414 p = NULL;
1415
you.chen35020192022-05-06 11:30:57 +08001416 CHECK_IDX(idx, CTRL_STA);
1417
you.chen755332b2022-08-06 16:59:10 +08001418 if (NULL == password) {
1419 printf("bad param\n");
1420 return -1;
1421 }
1422
you.chen35020192022-05-06 11:30:57 +08001423 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1424 if (NULL == fp) {
1425 printf("open file fail\n");
1426 return -1;
1427 }
1428
1429 buff = alloca(MAX_RET);
1430 fseek(fp, 0, SEEK_SET);
1431 len = fread(buff, 1, MAX_RET, fp);
1432 fclose(fp);
1433
1434 for(index=0; index < len; index ++) {
1435 for(; index < len; index ++) {
1436 if (memcmp(buff + index, "network={", 9) != 0) {
1437 continue;
1438 }
1439 p = buff + index + 9;
1440 for (; index < len; index ++ ) {
1441 if (buff[index] != '}') {
1442 continue;
1443 }
1444 buff[index] = '\0';
1445 break;
1446 }
you.chen755332b2022-08-06 16:59:10 +08001447 network_len = buff + index - p;
1448 break;
you.chen35020192022-05-06 11:30:57 +08001449 }
1450
1451 if (strstr(p, ap->ap_ssid) != NULL) {
1452 break;
1453 }
1454 }
1455
you.chen755332b2022-08-06 16:59:10 +08001456 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001457 return -1;
1458 }
1459
you.chen755332b2022-08-06 16:59:10 +08001460 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001461
1462 ret = -1;
1463 for(index=0; index < count; index++) {
1464 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001465 if (p != NULL) {
1466 p += 4;
1467 if (*p == '\"') {
1468 p++;
1469 }
you.chen35020192022-05-06 11:30:57 +08001470 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001471 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1472 p += 9;
1473 if (*p == '\"') {
1474 p++;
1475 }
1476 }
1477 else {
1478 continue;
you.chen35020192022-05-06 11:30:57 +08001479 }
1480
1481 strcpy(password, p);
1482
1483 while(*password != '\0') {
1484 if (*password == '\"') {
1485 *password = '\0';
1486 break;
1487 }
1488 password++;
1489 }
1490 ret = 0;
1491 break;
1492 } //end for(index=0; index < count; index++)
1493
1494 return ret;
1495}
1496
1497static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1498{
qs.xiong97fa59b2022-04-07 05:41:29 -04001499 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001500
you.chen35020192022-05-06 11:30:57 +08001501 if (sta_ssid == NULL) {
1502 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001503 return -1;
you.chen35020192022-05-06 11:30:57 +08001504 }
1505
1506 CHECK_WPA_CTRL(CTRL_STA);
1507
1508 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1509
1510 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1511// DO_OK_FAIL_REQUEST(cmd_save_config);
1512
qs.xiong7a105ce2022-03-02 09:43:11 -05001513 return 0;
1514
1515}
1516
you.chen35020192022-05-06 11:30:57 +08001517static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001518{
you.chen35020192022-05-06 11:30:57 +08001519 char lynq_disable_cmd[128]={0};
1520 char lynq_select_cmd[128]={0};
1521
1522 CHECK_WPA_CTRL(CTRL_STA);
1523
1524 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001525 if (start_flag != 0)
1526 {
1527 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1528 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1529 }
1530 else
1531 {
1532 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1533 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1534 }
you.chen35020192022-05-06 11:30:57 +08001535 DO_OK_FAIL_REQUEST(cmd_save_config);
1536 }
1537
1538 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001539 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001540 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1541 }
1542 else {
1543 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1544 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1545 }
1546
1547 return 0;
1548}
1549
1550int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1551{
1552 CHECK_IDX(idx, CTRL_STA);
1553
you.chen6c2dd9c2022-05-16 17:55:28 +08001554 curr_status_info curr_state;
1555 ap_info_s ap_info;
1556 curr_state.ap = &ap_info;
1557 curr_state.state = NULL;
1558
1559 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1560 strcpy(sta_ssid, ap_info.ap_ssid);
1561 return 0;
1562 }
1563
1564 return -1;
you.chen35020192022-05-06 11:30:57 +08001565}
1566
1567int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1568{
you.chen9ac66392022-08-06 17:01:16 +08001569 scan_info_s *scan_list = NULL;
1570 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001571 int scan_len=0;
1572 int save_len=0;
1573 int best_index = -1;
1574 int best_scan_index = -1;
1575 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001576 int i, j, ret;
1577
1578 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001579
1580 CHECK_IDX(idx, CTRL_STA);
1581 if (info == NULL) {
1582 return -1;
1583 }
1584
1585 curr_status_info curr_state;
1586 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001587 char status[64];
you.chen35020192022-05-06 11:30:57 +08001588
you.chen9ac66392022-08-06 17:01:16 +08001589 memset(&ap_info, 0, sizeof (ap_info));
1590 memset(status, 0, sizeof (status));
1591
1592 curr_state.ap = &ap_info;
1593 curr_state.state = status;
1594
1595 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001596 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001597 if (strcmp(status, STATE_COMPLETED) == 0)
1598 {
1599 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1600 }
1601 else
1602 {
1603 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1604 }
you.chen35020192022-05-06 11:30:57 +08001605 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001606 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001607 return 0;
1608 }
1609
you.chen9ac66392022-08-06 17:01:16 +08001610 lynq_wifi_sta_start_scan(idx);
1611
you.chen35020192022-05-06 11:30:57 +08001612 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001613 if (NULL != scan_list)
1614 {
1615 free(scan_list);
1616 }
you.chen35020192022-05-06 11:30:57 +08001617 return -1;
1618 }
1619
1620 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001621 if (NULL != scan_list)
1622 {
1623 free(scan_list);
1624 }
1625 if (NULL != save_list)
1626 {
1627 free(save_list);
1628 }
you.chen35020192022-05-06 11:30:57 +08001629 return -1;
1630 }
1631
1632 for (i=0; i < save_len; i++) {
1633 for (j=0; j < scan_len; j++) {
1634 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1635 && save_list[i].base_info.auth == scan_list[j].auth) {
1636 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001637 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001638 best_rssi = scan_list[j].rssi;
1639 }
1640 else if (best_rssi > scan_list[j].rssi) {
1641 best_index = i;
1642 best_scan_index = j;
1643 best_rssi = scan_list[j].rssi;
1644 }
1645 break;
1646 }
1647 }
1648 }
1649
1650 if (best_index >= 0) {
1651 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1652 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1653 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001654 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001655 }
1656
you.chen9ac66392022-08-06 17:01:16 +08001657 if (NULL != scan_list)
1658 {
1659 free(scan_list);
1660 }
1661 if (NULL != save_list)
1662 {
1663 free(save_list);
1664 }
1665
1666 return ret;
you.chen35020192022-05-06 11:30:57 +08001667}
1668
1669static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1670{
1671 char lynq_auth_cmd[64]={0};
1672 char lynq_ket_mgmt_cmd[64]={0};
1673 char lynq_pairwise_cmd[64]={0};
1674 char lynq_psk_cmd[64]={0};
1675
1676 CHECK_WPA_CTRL(CTRL_STA);
1677
qs.xiong1af5daf2022-03-14 09:12:12 -04001678 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001679 case LYNQ_WIFI_AUTH_OPEN:
1680 {
1681 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001682
you.chen35020192022-05-06 11:30:57 +08001683 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1684// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001685 break;
1686 }
you.chen35020192022-05-06 11:30:57 +08001687 case LYNQ_WIFI_AUTH_WPA_PSK:
1688 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001689 {
you.chen35020192022-05-06 11:30:57 +08001690 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1691 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1692 }
1693 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001694 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001695 }
1696 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1697 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001698
you.chen35020192022-05-06 11:30:57 +08001699 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1700 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1701 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001702
you.chen35020192022-05-06 11:30:57 +08001703 if (password != NULL) {
1704 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1705 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1706 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001707
you.chen35020192022-05-06 11:30:57 +08001708// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001709 break;
you.chen35020192022-05-06 11:30:57 +08001710 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001711 default:
1712 return -1;
you.chen35020192022-05-06 11:30:57 +08001713 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001714
qs.xiongf1b525b2022-03-31 00:58:23 -04001715 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001716}
qs.xiong7a105ce2022-03-02 09:43:11 -05001717
you.chen35020192022-05-06 11:30:57 +08001718static int inner_get_curr_net_no(int interface) {
1719 curr_status_info curr_state;
1720 curr_state.ap = NULL;
1721 curr_state.state = NULL;
1722
1723 if (0 != inner_get_status_info(interface, &curr_state)) {
1724 return -1;
1725 }
1726
1727 return curr_state.net_no;
1728}
1729
1730int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001731{
you.chen35020192022-05-06 11:30:57 +08001732 int net_no;
1733 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001734
you.chen35020192022-05-06 11:30:57 +08001735 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001736
you.chen35020192022-05-06 11:30:57 +08001737 if (net_no < 0) {
1738 return -1;
1739 }
1740
1741 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001742}
1743
you.chen35020192022-05-06 11:30:57 +08001744int 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 -05001745{
you.chen35020192022-05-06 11:30:57 +08001746 int count, net_no, index;
1747 int net_no_list[128];
1748 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001749
you.chen35020192022-05-06 11:30:57 +08001750 if (ssid == NULL || *ssid == '\0') {
1751 printf("bad ssid\n");
1752 return -1;
1753 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001754
you.chen35020192022-05-06 11:30:57 +08001755 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1756 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1757 printf("bad password\n");
1758 return -1;
1759 }
1760 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001761
you.chen35020192022-05-06 11:30:57 +08001762 CHECK_IDX(idx, CTRL_STA);
1763
1764 net_no = -1;
1765 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1766
1767 for (index=0; index < count; index++) {
1768 net_auth = -1;
1769 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1770 net_no = net_no_list[index];
1771 break;
1772 }
1773 }
1774
1775 if (net_no < 0) {
1776 net_no = lynq_add_network(CTRL_STA);
1777 if (net_no == -1) {
1778 return -1;
1779 }
1780
1781 printf("net no is %d\n", net_no);
1782 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1783 return -1;
1784 }
1785 }
1786
1787 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1788 return -1;
1789 }
1790
1791 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001792}
1793
you.chen35020192022-05-06 11:30:57 +08001794int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001795{
you.chen35020192022-05-06 11:30:57 +08001796 ap_info_s ap;
1797 curr_status_info curr_state;
1798 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001799
you.chen35020192022-05-06 11:30:57 +08001800 if (ssid == NULL || *ssid == '\0') {
1801 return -1;
1802 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001803
you.chen35020192022-05-06 11:30:57 +08001804 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001805
you.chen35020192022-05-06 11:30:57 +08001806 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001807 curr_state.state = NULL;
1808
you.chen35020192022-05-06 11:30:57 +08001809 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1810 return 0;
1811 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001812
you.chen35020192022-05-06 11:30:57 +08001813 if (strcmp(ap.ap_ssid, ssid) != 0) {
1814 return 0;
1815 }
1816
1817 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001818}
qs.xiong97fa59b2022-04-07 05:41:29 -04001819
you.chena6cd55a2022-05-08 12:20:18 +08001820int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1821{
you.chenc29444e2022-06-07 18:01:16 +08001822 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 +08001823 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001824
you.chen35020192022-05-06 11:30:57 +08001825 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001826 CHECK_WPA_CTRL(CTRL_STA);
1827
1828 system("connmanctl enable wifi");
1829
you.chena6fa5b22022-05-18 10:28:19 +08001830 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001831 return -1;
1832 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001833
you.chen01bfac32022-06-07 10:36:00 +08001834 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001835 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001836 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001837
1838 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001839}
1840
you.chen35020192022-05-06 11:30:57 +08001841int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001842{
you.chena6cd55a2022-05-08 12:20:18 +08001843 char lynq_disable_network_cmd[MAX_CMD];
1844 curr_status_info curr_state;
1845 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001846
you.chena6cd55a2022-05-08 12:20:18 +08001847 CHECK_IDX(idx, CTRL_STA);
1848 CHECK_WPA_CTRL(CTRL_STA);
1849
1850 curr_state.ap = &ap_info;
1851 curr_state.state = NULL;
1852
1853 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1854 return 0;
1855 }
1856
1857 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1858 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1859
1860 DO_OK_FAIL_REQUEST(cmd_save_config);
1861
1862 return 0;
1863// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001864}
qs.xiong7a105ce2022-03-02 09:43:11 -05001865
you.chen35020192022-05-06 11:30:57 +08001866//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1867// int i, count;
1868// char *p;
1869// const char * FLAG_SSID = "ssid=";
1870// const char * FLAG_SBSID = "bssid=";
1871// const char * FLAG_KEY_MGMT = "key_mgmt=";
1872// const char * FLAG_FREQ = "freq=";
1873// char lynq_sta_cmd[MAX_CMD];
1874// char *split_lines[128] = {0};
1875
1876// CHECK_WPA_CTRL(CTRL_AP);
1877
1878// sprintf(lynq_sta_cmd, "STA %s", bssid);
1879
1880// DO_REQUEST(lynq_sta_cmd);
1881
1882// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1883
1884// for(i=0; i < count; i++) {
1885// p = strstr(split_lines[i], FLAG_SSID);
1886// if (p != NULL) {
1887// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1888// continue;
1889// }
1890// }
1891
1892// lynq_get_interface_ip(idx, ap->ap_ip);
1893// lynq_ap_password_set(idx, ap->psw);
1894
1895// return 0;
1896//}
1897
1898static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1899 curr_status_info curr_state;
1900 curr_state.ap = ap;
1901 curr_state.state = NULL;
1902 return inner_get_status_info(interface, &curr_state);
1903}
1904
1905int 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 -04001906{
you.chen35020192022-05-06 11:30:57 +08001907 int ip_count, index, i, line_count;
1908 const char *lynq_first_sta_cmd = "STA-FIRST";
1909 char lynq_next_sta_cmd[MAX_CMD];
1910 char *bssid[1024] = {0};
1911 char *mac_list[128] = {0};
1912 char *ip_list[128] = {0};
1913 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001914
you.chen35020192022-05-06 11:30:57 +08001915 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001916
you.chen35020192022-05-06 11:30:57 +08001917 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001918
you.chen35020192022-05-06 11:30:57 +08001919// ap_info_s * tmp_ap;
1920// device_info_s * tmp_list;
1921 if (ap == NULL || list == NULL || len == NULL) {
1922 printf("bad input param");
1923 return -1;
1924 }
1925
1926// ap = &tmp_ap;
1927// list = &tmp_list;
1928 *ap = malloc(sizeof (ap_info_s));
1929
1930 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1931 return -1;
1932 }
1933
1934 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1935 lynq_ap_password_get(idx, (*ap)->psw);
1936
1937 ip_count = get_ip_mac_list(mac_list, ip_list);
1938 printf("get count %d\n", ip_count);
1939
1940 DO_REQUEST(lynq_first_sta_cmd);
1941
1942 index = 0;
1943 while (reply_len > 0) {
1944 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1945 break;
1946 }
1947 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1948 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1949 strcpy(bssid[index], split_lines[0]);
1950 index++;
1951 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1952 reply_len = MAX_RET;
1953 cmd_reply[0] = '\0';
qs.xiongfdbc58e2022-09-29 11:37:32 +08001954 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen9ac66392022-08-06 17:01:16 +08001955 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1956 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001957 break;
1958 }
1959 }
1960
1961 *len = index;
1962
1963 *list = malloc(sizeof(device_info_s) * (*len));
1964 for (index=0; index < *len; index++) {
1965 strcpy((*list)[index].sta_mac, bssid[index]);
1966 for(i=0;i < ip_count; i++ ) {
1967 if (strcmp(bssid[index], mac_list[i]) == 0) {
1968 strcpy((*list)[index].sta_ip, ip_list[i]);
1969 break;
1970 }
1971 }
1972 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1973 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1974 free(bssid[index]);
1975 }
1976
you.chen9ac66392022-08-06 17:01:16 +08001977 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
1978
you.chen35020192022-05-06 11:30:57 +08001979 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001980}
1981
you.chen35020192022-05-06 11:30:57 +08001982int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001983{
you.chen35020192022-05-06 11:30:57 +08001984 int i, count, index, count_words;
1985 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1986 char *split_lines[128] = {0};
1987 char *split_words[128] = {0};
1988 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001989
you.chen35020192022-05-06 11:30:57 +08001990 if (list == NULL || len == NULL) {
1991 return -1;
1992 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001993
you.chen9ac66392022-08-06 17:01:16 +08001994 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
1995 {
1996 usleep(100 * 1000);
1997 }
1998
you.chen35020192022-05-06 11:30:57 +08001999 CHECK_IDX(idx, CTRL_STA);
2000
2001 CHECK_WPA_CTRL(CTRL_STA);
2002
2003 DO_REQUEST(lynq_scan_result_cmd);
2004
2005 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2006 *len = count - 1;
2007 *list = malloc(sizeof (scan_info_s) * *len);
2008
2009 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2010 for (index=0; index <count_words; index++) {
2011 printf("----header: %s\n", split_words[index]);
2012 }
2013
2014 for(index = 1;index < count; index++) {
2015 printf("---- %s\n",split_lines[index]);
2016 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2017 if (count_words < 4)
2018 continue;
2019 printf("count: %d, %s\n", count_words, split_words[0]);
2020 //bssid / frequency / signal level / flags / ssid
2021 p = (*list) + index - 1;
2022 strcpy(p->mac, split_words[0]);
2023 p->band = convert_band_from_freq(atoi(split_words[1]));
2024 p->rssi = -1 * atoi( split_words[2]);
2025 p->auth = convert_max_auth_from_flag(split_words[3]);
2026 strcpy(p->ssid, split_words[4]);
2027 }
2028
2029 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002030}
qs.xiong97fa59b2022-04-07 05:41:29 -04002031
you.chen35020192022-05-06 11:30:57 +08002032int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2033{
2034 int count, net_no, index;
2035 int net_no_list[128];
2036 lynq_wifi_auth_s net_auth;
2037 char lynq_remove_cmd[MAX_CMD];
2038
2039 if (ssid == NULL || *ssid == '\0') {
2040 printf("bad ssid\n");
2041 return -1;
2042 }
2043
2044 CHECK_IDX(idx, CTRL_STA);
2045
2046 CHECK_WPA_CTRL(CTRL_STA);
2047
2048 net_no = -1;
2049 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2050
2051 for (index=0; index < count; index++) {
2052 net_auth = -1;
2053 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2054 net_no = net_no_list[index];
2055 break;
2056 }
2057 }
2058
2059 if (net_no < 0) {
2060 return 0;
2061 }
2062
2063 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2064
2065 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2066 DO_OK_FAIL_REQUEST(cmd_save_config);
2067
2068 return 0;
2069}
2070
2071int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2072{
you.chen755332b2022-08-06 16:59:10 +08002073 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002074 int net_no_list[128];
2075 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002076 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002077
2078 if (list == NULL || len == NULL) {
2079 printf("bad param\n");
2080 return -1;
2081 }
2082
2083 CHECK_IDX(idx, CTRL_STA);
2084
2085// CHECK_WPA_CTRL(CTRL_STA);
2086
2087 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2088 printf("count is %d\n", count);
2089
2090 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002091 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002092 *len = count;
2093
2094 for (index=0; index < count; index++) {
2095 printf("to get ssid %d\n", index);
2096 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002097
2098 ssid_ptr = (*list)[index].base_info.ap_ssid;
2099 ssid_len = strlen(ssid_ptr);
2100 if (ssid_ptr[0] == '\"') {
2101 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2102 ssid_len -= 1;
2103 }
2104 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2105 ssid_ptr[ssid_len - 1] = '\0';
2106 }
2107
you.chen35020192022-05-06 11:30:57 +08002108 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002109 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002110 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2111 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2112 }
2113 else {
2114 (*list)[index].base_info.band = -1;
2115 }
2116
you.chen755332b2022-08-06 16:59:10 +08002117 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002118 }
2119
2120 return 0;
2121}
2122
2123int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2124{
2125 const char *lynq_scan_cmd = "SCAN";
2126
2127 CHECK_IDX(idx, CTRL_STA);
2128
2129 CHECK_WPA_CTRL(CTRL_STA);
2130
2131 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002132 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002133
2134 return 0;
2135}
2136
2137int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2138 if (cb == NULL) {
2139 return -1;
2140 }
2141
2142 g_ap_callback_priv = priv;
2143 g_ap_callback_func = cb;
2144
2145 return 0;
2146}
2147
2148int lynq_unreg_ap_event_callback(void * priv) {
2149 if (g_ap_callback_priv == priv) {
2150 g_ap_callback_func = NULL;
2151 g_ap_callback_priv = NULL;
2152 return 0;
2153 }
2154 return -1;
2155}
2156
2157int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2158 if (cb == NULL) {
2159 return -1;
2160 }
2161
2162 g_sta_callback_priv = priv;
2163 g_sta_callback_func = cb;
2164
2165 return 0;
2166}
2167
2168int lynq_unreg_sta_event_callback(void * priv) {
2169 if (g_sta_callback_priv == priv) {
2170 g_sta_callback_func = NULL;
2171 g_sta_callback_priv = NULL;
2172 return 0;
2173 }
2174 return -1;
2175}
2176
2177
2178static int inner_get_status_info_state (int interface, char *state) {
2179 curr_status_info curr_state;
2180 curr_state.ap = NULL;
2181 curr_state.state = state;
2182 return inner_get_status_info(interface, &curr_state);
2183}
2184
2185int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2186{
2187 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002188 CHECK_IDX(idx, CTRL_AP);
2189
2190 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2191 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2192 return 0;
2193 }
2194
2195 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2196 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2197 }
2198 else {
2199 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2200 }
2201
2202 return 0;
2203}
2204
2205int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2206 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002207 CHECK_IDX(idx, CTRL_STA);
2208
2209 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2210 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2211 return 0;
2212 }
2213
2214 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2215 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2216 }
2217 else {
2218 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2219 }
2220
2221 return 0;
2222}
2223
2224int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2225// CHECK_IDX(idx, CTRL_AP);
2226// int ret = 0;
2227// size_t reply_len = MAX_RET;
2228// char cmd_reply[MAX_RET]={0};
2229// const char * cmd_str = "GET country";
2230// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2231// do{
2232// if (NULL == s_lynq_wpa_ctrl) {
2233// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2234// if (NULL == s_lynq_wpa_ctrl ) {
2235// printf("wpa_ctrl_open fail\n");
2236// return -1;
2237// }
2238// }
2239// }while(0);
2240
2241// do {
2242// reply_len = MAX_RET;
2243// cmd_reply[0] = '\0';
2244// printf("to call [%s]\n", cmd_str);
qs.xiongfdbc58e2022-09-29 11:37:32 +08002245// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen35020192022-05-06 11:30:57 +08002246// if (ret != 0) {
2247// printf("call ##cmd_str fail %d\n", ret);
2248// return ret;
2249// }
2250// cmd_reply[reply_len+1] = '\0';
2251// printf("cmd replay [ %s ]\n", cmd_reply);
2252// }while(0);
2253
2254 FILE *fp;
2255 size_t i = 0;
2256 char lynq_cmd_ret[MAX_RET]={0};
2257
2258// CHECK_IDX(idx, CTRL_AP);
2259
2260 if((fp=popen("wl country","r"))==NULL)
2261 {
2262 perror("popen error!");
2263 return -1;
2264 }
2265 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2266 {
2267 perror("fread fail!");
2268 return -1;
2269 }
2270
2271 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2272 if (lynq_cmd_ret[i] == ' ') {
2273 lynq_cmd_ret[i] = '\0';
2274 break;
2275 }
2276 }
2277
2278 strcpy(country_code,lynq_cmd_ret);
2279 printf("---country code %s\n", country_code);
2280
2281 int ret=pclose(fp);
2282 if(ret==-1)
2283 {
2284 perror("close file faild");
2285 }
2286
2287 return 0;
2288}
2289
2290int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2291// const char * cmd_str = "GET country";
2292// CHECK_IDX(idx, CTRL_AP);
2293// CHECK_WPA_CTRL(CTRL_STA);
2294
2295// DO_REQUEST(cmd_str);
2296// printf("result %s\n", cmd_reply);
2297
2298 if (country_code == NULL || *country_code == '\0') {
2299 printf("bad country code\n");
2300 return -1;
2301 }
2302
2303 char lynq_country_cmd[MAX_CMD];
2304 sprintf(lynq_country_cmd, "wl country %s", country_code);
2305 if (system(lynq_country_cmd) == 0) {
2306 return 0;
2307 }
2308
2309 return -1;
2310}
2311
2312int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2313{
2314 if (mac == NULL) {
2315 return -1;
2316 }
2317
2318 CHECK_IDX(idx, CTRL_STA);
2319 ap_info_s ap;
2320 ap.ap_mac[0] = '\0';
2321
2322 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2323 return -1;
2324 }
2325 strcpy(mac, ap.ap_mac);
2326
2327 return 0;
2328}
2329
2330int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2331{
you.chen9ac66392022-08-06 17:01:16 +08002332 struct ifaddrs *ifaddr_header, *ifaddr;
2333 struct in_addr * ifa;
2334 const char * ifaName = "wlan0";
2335 if (ip == NULL)
2336 {
you.chenf58b3c92022-06-21 16:53:48 +08002337 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002338 }
you.chenf58b3c92022-06-21 16:53:48 +08002339
you.chen9ac66392022-08-06 17:01:16 +08002340 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002341 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002342 }
2343 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002344 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002345 }
you.chen35020192022-05-06 11:30:57 +08002346
you.chen9ac66392022-08-06 17:01:16 +08002347 if (getifaddrs(&ifaddr_header) == -1)
2348 {
you.chen35020192022-05-06 11:30:57 +08002349 perror("getifaddrs");
2350 return -1;
2351 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002352 }
you.chen35020192022-05-06 11:30:57 +08002353
2354
you.chen9ac66392022-08-06 17:01:16 +08002355 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2356 {
2357 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002358 continue;
you.chen9ac66392022-08-06 17:01:16 +08002359 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2360 {
2361 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2362 {
2363 // is a valid IP4 Address
2364 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2365 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2366 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2367 freeifaddrs(ifaddr_header);
2368 printf("ip %s\n", ip);
2369 return 0;
2370 }
2371 }
2372 }
2373 return -1;
you.chen35020192022-05-06 11:30:57 +08002374}
2375
2376int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2377{
2378 int count;
2379 size_t i;
2380 char *split_words[128] = {0};
2381 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2382
2383 CHECK_WPA_CTRL(idx);
2384
2385 DO_REQUEST(lynq_get_mac_cmd);
2386
2387 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2388 return -1;
2389 }
2390
2391 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2392
2393 if (count < 2) {
2394 return -1;
2395 }
2396
2397 for (i=0; i < strlen(split_words[1]); i++ ) {
2398 if (split_words[1][i] != ' ') {
2399 break;
2400 }
2401 }
2402
2403 strcpy(mac, split_words[1] + i);
2404
2405 return 0;
2406}
2407
2408int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2409{
2410// int count;
2411// char *split_words[128] = {0};
2412// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2413
2414// if (rssi == NULL) {
2415// return -1;
2416// }
2417
2418// CHECK_IDX(idx, CTRL_STA);
2419
2420// CHECK_WPA_CTRL(CTRL_STA);
2421
2422// DO_REQUEST(lynq_get_rssi_cmd);
2423
2424// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2425// return -1;
2426// }
2427
2428// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2429
2430// if (count < 2) {
2431// return -1;
2432// }
2433
2434// *rssi = atoi(split_words[1]) * -1;
2435
2436 FILE *fp;
2437 size_t i = 0;
2438 char lynq_cmd_ret[MAX_RET]={0};
2439
2440// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002441/*******change other cmd to get rssi*******
2442 *
2443 *wl rssi ---> wl -i wlan0 rssi
2444 *
2445 ***** change by qs.xiong 20221011*******/
2446 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002447 {
2448 perror("popen error!");
2449 return -1;
2450 }
2451 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2452 {
2453 perror("fread fail!");
2454 return -1;
2455 }
you.chen9f17e4d2022-06-06 17:18:18 +08002456 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002457/****** if got rssi is 0,means sta didn't connected any device****/
2458 if(*rssi == 0)
2459 {
2460 printf("sta didn't connected any ap device,please check connection\n");
2461 }
you.chen35020192022-05-06 11:30:57 +08002462
2463 return 0;
2464}
2465
2466int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2467{
2468 if (band == NULL) {
2469 return -1;
2470 }
2471
2472 CHECK_IDX(idx, CTRL_STA);
2473 ap_info_s ap;
2474 ap.band = -1;
2475
2476 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2477 return -1;
2478 }
2479 *band = ap.band;
2480
2481 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002482}
you.chenf58b3c92022-06-21 16:53:48 +08002483
2484int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2485{
2486 int index;
2487 int ip_count = 0;
2488 char bssid[1024] = {0};
2489 char *mac_list[128] = {0};
2490 char *ip_list[128] = {0};
2491
2492 if (ip == NULL)
2493 {
2494 printf("invalid param");
2495 return -1;
2496 }
2497
2498 CHECK_IDX(idx, CTRL_STA);
2499
2500 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2501 {
2502 return -1;
2503 }
2504
2505 ip_count = get_ip_mac_list(mac_list, ip_list);
2506
2507 for (index=0; index < ip_count; index++)
2508 {
2509 if (strcmp(bssid, mac_list[index]) == 0)
2510 {
2511 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002512
2513 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2514
you.chenf58b3c92022-06-21 16:53:48 +08002515 return 0;
2516 }
2517 }
2518
2519 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002520 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2521
you.chenf58b3c92022-06-21 16:53:48 +08002522 return -1;
2523}
2524
qs.xiong026c5c72022-10-17 11:15:45 +08002525int lynq_ap_connect_num(int sta_number)
2526{
2527 char lynq_limit_cmd[32]={0};
2528 int ret;
2529 if((sta_number < 1 ) && (sta_number > 15)){
2530 printf("sta_number: not in range\n",sta_number);
2531 return -1;
2532 }
2533 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2534 ret = system(lynq_limit_cmd);
2535 if(ret != 0){
2536 printf("cmd of limit ap devices number error\n");
2537 }
2538 return 0;
2539}
you.chenf58b3c92022-06-21 16:53:48 +08002540
qs.xiong77905552022-10-17 11:19:57 +08002541int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2542{
2543
2544 char lynq_wifi_acs_cmd[128]={0};
2545 char lynq_cmd_mode[128]={0};
2546 char lynq_cmd_slect[128]={0};
2547
2548 if((acs_mode != 2) && (acs_mode != 5)){
2549 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2550 }
2551
2552 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2553 return -1;
2554 }
2555
2556 CHECK_IDX(idx, CTRL_AP);
2557
2558 CHECK_WPA_CTRL(CTRL_AP);
2559
2560 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2561 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2562 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2563
2564 DO_OK_FAIL_REQUEST(cmd_disconnect);
2565 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2566 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2567 DO_OK_FAIL_REQUEST(cmd_save_config);
2568 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2569
2570 return 0;
2571}