blob: 8867126f1312b2c4d13c339ccfedc29aca1a8b19 [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.xiongf1b525b2022-03-31 00:58:23 -0400804int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500805{
you.chen35020192022-05-06 11:30:57 +0800806 char lynq_wifi_frequency_cmd[128]={0};
807 char lynq_cmd_mode[128]={0};
808 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500809
you.chen35020192022-05-06 11:30:57 +0800810 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
811 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
812 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400813
you.chen35020192022-05-06 11:30:57 +0800814 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
815 return -1;
816 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400817
you.chen35020192022-05-06 11:30:57 +0800818 CHECK_IDX(idx, CTRL_AP);
819
820 CHECK_WPA_CTRL(CTRL_AP);
821
822 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
823 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
824 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
825
you.chen6c2dd9c2022-05-16 17:55:28 +0800826 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800827 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
828 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
829 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500830
qs.xiong1af5daf2022-03-14 09:12:12 -0400831 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500832}
833
qs.xiongf1b525b2022-03-31 00:58:23 -0400834int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500835{
you.chen35020192022-05-06 11:30:57 +0800836 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400837
you.chen35020192022-05-06 11:30:57 +0800838 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400839
you.chen35020192022-05-06 11:30:57 +0800840 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
841 return -1;
842 }
843 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400844
845 return 0;
846}
847
qs.xiongf1b525b2022-03-31 00:58:23 -0400848int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
849{
you.chen35020192022-05-06 11:30:57 +0800850 CHECK_IDX(idx, CTRL_AP);
851 switch(bandwidth){
852 case LYNQ_WIFI_BANDWIDTH_HT10:
853 {
854 printf("bandwith [%d] not support now\n", bandwidth);
855 return -1;
856 }
857 case LYNQ_WIFI_BANDWIDTH_HT20:
858 {
859 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
860 system("wl down");
861 if (system(lynq_cmd_bandwith) != 0 ){
862 return -1;
863 }
864 system("wl up");
865 break;
866 }
867 case LYNQ_WIFI_BANDWIDTH_HT40:
868 {
869 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
870 sprintf(lynq_cmd_bandwith, "wl chanspec ");
871 system("wl down");
872 if (system(lynq_cmd_bandwith) != 0 ){
873 return -1;
874 }
875 system("wl up");
876 break;
877 }
878 case LYNQ_WIFI_BANDWIDTH_HT80:
879 {
880 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
881 system("wl down");
882 if (system(lynq_cmd_bandwith) != 0 ){
883 return -1;
884 }
885 system("wl up");
886 break;
887 }
888 default:
889 {
890 printf("auth type [%d] not support now\n", bandwidth);
891 return -1;
892 }
893 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400894
895
you.chen35020192022-05-06 11:30:57 +0800896 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400897}
you.chen35020192022-05-06 11:30:57 +0800898
qs.xiongf1b525b2022-03-31 00:58:23 -0400899int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
900{
you.chen35020192022-05-06 11:30:57 +0800901 int count = 0;
902 int index = 0;
903 char *split_words[128] = {0};
904 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400905
you.chen35020192022-05-06 11:30:57 +0800906 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500907
you.chen35020192022-05-06 11:30:57 +0800908 CHECK_WPA_CTRL(CTRL_AP);
909
910 DO_REQUEST(lynq_chanspec_cmd);
911
912 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
913 for(;index < count; index++) {
914 if (strncmp(split_words[index], "bw", 2) != 0) {
915 continue;
916 }
917
918 index++;
919 if (index >= count) {
920 return -1;
921 }
922
923 printf("bw %s\n", split_words[index]);
924 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
925 return 0;
926 }
927
928 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500929}
qs.xiong0fb469a2022-04-14 03:50:45 -0400930
qs.xiongf1b525b2022-03-31 00:58:23 -0400931int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500932{
you.chen35020192022-05-06 11:30:57 +0800933 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400934
you.chen35020192022-05-06 11:30:57 +0800935 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400936
you.chen35020192022-05-06 11:30:57 +0800937 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400938
you.chen35020192022-05-06 11:30:57 +0800939 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
940 return -1;
941 }
942
943 system("wl down");
944 if (system(lynq_cmd_channel) != 0 ){
945 return -1;
946 }
947 system("wl up");
948 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500949}
qs.xiong0fb469a2022-04-14 03:50:45 -0400950
qs.xiongf1b525b2022-03-31 00:58:23 -0400951int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500952{
you.chen35020192022-05-06 11:30:57 +0800953 int count = 0;
954 int index = 0;
955 char *split_words[128] = {0};
956 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400957
you.chen35020192022-05-06 11:30:57 +0800958 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400959
you.chen35020192022-05-06 11:30:57 +0800960 CHECK_WPA_CTRL(CTRL_AP);
961
962 DO_REQUEST(lynq_chanspec_cmd);
963
964 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
965 for(;index < count; index++) {
966 printf("---- %s\n",split_words[index]);
967 if (strncmp(split_words[index], "channel", 2) != 0) {
968 continue;
969 }
970
971 index++;
972 if (index >= count) {
973 return -1;
974 }
975
976 *channel = atoi(split_words[index]);
977 return 0;
978 }
979
980 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500981}
982
983
you.chen35020192022-05-06 11:30:57 +0800984int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500985{
you.chen6c2dd9c2022-05-16 17:55:28 +0800986 char ssid[MAX_CMD] = {0};
987 int freq = 0;
988 char lynq_auth_cmd[64]={0};
989 char lynq_auth_alg_cmd[64]={0};
990 char lynq_psk_cmd[64]={0};
991 char lynq_pairwise_cmd[64]={0};
992 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +0800993 CHECK_IDX(idx, CTRL_AP);
994
you.chen6c2dd9c2022-05-16 17:55:28 +0800995 CHECK_WPA_CTRL(CTRL_AP);
996
you.chen35020192022-05-06 11:30:57 +0800997 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
998 return -1;
999 }
1000
you.chen92fd5d32022-05-25 10:09:47 +08001001 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001002 if (org_auth == auth) {
1003 return 0;
1004 }
1005 else {
1006 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1007 ssid[0] = '\0';
1008 }
1009 lynq_wifi_ap_frequency_get(idx, &freq);
1010
1011 DO_OK_FAIL_REQUEST(cmd_disconnect);
1012 DO_OK_FAIL_REQUEST(cmd_remove_all);
1013 if (ssid[0] != '\0') {
1014 lynq_wifi_ap_ssid_set(idx, ssid);
1015 }
1016 if (freq != 0) {
1017 lynq_wifi_ap_frequency_set(idx, freq);
1018 }
1019 }
1020 }
you.chen35020192022-05-06 11:30:57 +08001021
qs.xiong7a105ce2022-03-02 09:43:11 -05001022 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001023 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001024 {
you.chen35020192022-05-06 11:30:57 +08001025 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001026 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001027
you.chen35020192022-05-06 11:30:57 +08001028 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001029 break;
1030 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001031 case LYNQ_WIFI_AUTH_WEP:
1032 {
1033 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001034 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001035 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1036
1037 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1038 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1039 break;
1040 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001041 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001042 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001043 {
you.chen35020192022-05-06 11:30:57 +08001044 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1045 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1046 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1047 }
1048 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001049 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001050 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001051 }
1052// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1053// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1054 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", 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);
1057 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1058 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001059 break;
you.chen35020192022-05-06 11:30:57 +08001060 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001061 default:
you.chen35020192022-05-06 11:30:57 +08001062 {
1063 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001064 return -1;
you.chen35020192022-05-06 11:30:57 +08001065 }
1066 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001067 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001068
qs.xiong7a105ce2022-03-02 09:43:11 -05001069 return 0;
1070}
1071
you.chen35020192022-05-06 11:30:57 +08001072int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001073{
you.chen35020192022-05-06 11:30:57 +08001074 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001075 char lynq_auth_alg_str[MAX_RET] = {0};
1076 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001077
1078 CHECK_IDX(idx, CTRL_AP);
1079
1080 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1081 return -1;
1082 }
1083
1084 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001085 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1086 *auth = LYNQ_WIFI_AUTH_OPEN;
1087 }
1088 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1089 *auth = LYNQ_WIFI_AUTH_WEP;
1090 }
1091 else {
1092 *auth = LYNQ_WIFI_AUTH_OPEN;
1093 }
you.chen35020192022-05-06 11:30:57 +08001094 }
you.chen92fd5d32022-05-25 10:09:47 +08001095 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001096 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001097 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001098 }
1099 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1100 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1101 }
1102 else {
1103 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1104 }
you.chen35020192022-05-06 11:30:57 +08001105 }
you.chen92fd5d32022-05-25 10:09:47 +08001106 else {
1107 *auth = -1;
1108 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001109
you.chen6c2dd9c2022-05-16 17:55:28 +08001110 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001111}
qs.xiong1af5daf2022-03-14 09:12:12 -04001112
qs.xiong1af5daf2022-03-14 09:12:12 -04001113
qs.xiongf1b525b2022-03-31 00:58:23 -04001114int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001115{
you.chen35020192022-05-06 11:30:57 +08001116 char LYNQ_WIFI_CMD[128]={0};
1117 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1118 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001119
you.chen35020192022-05-06 11:30:57 +08001120 CHECK_IDX(idx, CTRL_AP);
1121
1122 CHECK_WPA_CTRL(CTRL_AP);
1123
1124// system("connmanctl enable wifi");
1125// system("connmanctl tether wifi on cy-test 12345678");
1126// system("ifconfig wlan0 down");
1127// system("ifconfig wlan0 up");
1128// system("ifconfig wlan0 up");
1129
1130 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1131 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1132
1133 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1134 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1135
qs.xiong7a105ce2022-03-02 09:43:11 -05001136 return 0;
1137}
1138
qs.xiongf1b525b2022-03-31 00:58:23 -04001139int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001140{
you.chen35020192022-05-06 11:30:57 +08001141 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001142}
1143
qs.xiongf1b525b2022-03-31 00:58:23 -04001144int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001145{
you.chen35020192022-05-06 11:30:57 +08001146 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001147
you.chen35020192022-05-06 11:30:57 +08001148 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001149
you.chen35020192022-05-06 11:30:57 +08001150 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001151
you.chen35020192022-05-06 11:30:57 +08001152 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1153
1154 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1155
you.chenb4b121c2022-05-06 17:50:16 +08001156// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001157
qs.xiong7a105ce2022-03-02 09:43:11 -05001158 return 0;
1159}
qs.xiong1af5daf2022-03-14 09:12:12 -04001160
qs.xiongf1b525b2022-03-31 00:58:23 -04001161int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001162{
you.chen35020192022-05-06 11:30:57 +08001163 char lynq_disable_cmd[128] = {0};
1164 char lynq_select_cmd[128] = {0};
1165 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001166
you.chen35020192022-05-06 11:30:57 +08001167 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001168
you.chen35020192022-05-06 11:30:57 +08001169 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001170
you.chen35020192022-05-06 11:30:57 +08001171 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1172 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1173
1174 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1175 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1176 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001177
1178 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001179}
1180
qs.xiongf1b525b2022-03-31 00:58:23 -04001181int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001182{
you.chen35020192022-05-06 11:30:57 +08001183 char lynq_disable_cmd[128] = {0};
1184 char lynq_select_cmd[128] = {0};
1185 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001186
you.chen35020192022-05-06 11:30:57 +08001187 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001188
you.chen35020192022-05-06 11:30:57 +08001189 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001190
you.chen35020192022-05-06 11:30:57 +08001191 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1192 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1193
1194 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1195 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1196 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001197
1198 return 0;
1199}
qs.xiongf1b525b2022-03-31 00:58:23 -04001200
you.chen35020192022-05-06 11:30:57 +08001201int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001202{
qs.xiongf1b525b2022-03-31 00:58:23 -04001203 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001204 char lynq_tmp_cmd[MAX_CMD] = {0};
1205 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001206 if( password == NULL ){
1207 return -1;
1208 }
1209 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001210 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001211 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001212 return -1;
you.chen35020192022-05-06 11:30:57 +08001213 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001214
you.chen35020192022-05-06 11:30:57 +08001215 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001216
you.chen6c2dd9c2022-05-16 17:55:28 +08001217 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1218 return -1;
1219 }
1220 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1221 return -1;
1222 }
1223
you.chen35020192022-05-06 11:30:57 +08001224 CHECK_WPA_CTRL(CTRL_AP);
1225
you.chen6c2dd9c2022-05-16 17:55:28 +08001226 if (auth == LYNQ_WIFI_AUTH_WEP) {
1227 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1228 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1229 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1230 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1231 }
1232 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1233 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1234 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1235 }
1236 else {
1237 return -1;
1238 }
you.chen35020192022-05-06 11:30:57 +08001239
you.chen35020192022-05-06 11:30:57 +08001240 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001241
qs.xiongf1b525b2022-03-31 00:58:23 -04001242 return 0;
1243}
1244
you.chen35020192022-05-06 11:30:57 +08001245int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001246{
you.chen35020192022-05-06 11:30:57 +08001247 FILE * fp;
1248 int len, ret;
1249 int count, index;
1250 char *split_lines[128] = {0};
1251 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001252
you.chen35020192022-05-06 11:30:57 +08001253 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001254
you.chen35020192022-05-06 11:30:57 +08001255 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1256// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1257 if (NULL == fp) {
1258 printf("open file fail\n");
1259 return -1;
1260 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001261
you.chen35020192022-05-06 11:30:57 +08001262 buff = alloca(MAX_RET);
1263 fseek(fp, 0, SEEK_SET);
1264 len = fread(buff, 1, MAX_RET, fp);
1265 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001266
you.chen35020192022-05-06 11:30:57 +08001267 for(index=0; index < len; index ++) {
1268 if (memcmp(buff + index, "network={", 9) != 0) {
1269 continue;
1270 }
1271 p = buff + index + 9;
1272 for (; index < len; index ++ ) {
1273 if (buff[index] != '}') {
1274 continue;
1275 }
1276 buff[index] = '\0';
1277 break;
1278 }
1279 len = buff + index - p;
1280 }
1281
1282 count = lynq_split(p, len, '\n', split_lines);
1283
1284 ret = -1;
1285 for(index=0; index < count; index++) {
1286 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001287 if (p != NULL) {
1288 p += 4;
1289 if (*p == '\"') {
1290 p++;
1291 }
you.chen35020192022-05-06 11:30:57 +08001292 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001293 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1294 p += 9;
1295 if (*p == '\"') {
1296 p++;
1297 }
1298 }
1299 else {
1300 continue;
you.chen35020192022-05-06 11:30:57 +08001301 }
1302
1303 strcpy(password, p);
1304
1305 while(*password != '\0') {
1306 if (*password == '\"') {
1307 *password = '\0';
1308 break;
1309 }
1310 password++;
1311 }
1312 ret = 0;
1313 break;
1314 } //end for(index=0; index < count; index++)
1315
1316 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001317}
1318
you.chen35020192022-05-06 11:30:57 +08001319static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1320 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001321 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001322
you.chen35020192022-05-06 11:30:57 +08001323 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1324 return -1;
1325 }
1326
1327 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001328
1329 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1330 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1331 if (strcmp(lynq_proto_str, "RSN") == 0) {
1332 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1333 }
1334 }
1335 }
you.chen35020192022-05-06 11:30:57 +08001336 return 0;
1337}
1338
1339int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001340{
you.chen35020192022-05-06 11:30:57 +08001341 int pass_len, net_no, count, index;
1342 char lynq_tmp_cmd[300]={0};
1343 int net_no_list[128];
1344 lynq_wifi_auth_s net_auth;
1345 pass_len=strlen(password);
1346 if(pass_len < 8 || pass_len >= 64){
1347 return -1;
1348 }
1349
1350 CHECK_IDX(idx, CTRL_STA);
1351
1352 net_no = -1;
1353 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1354
1355 for (index=0; index < count; index++) {
1356 net_auth = -1;
1357 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1358 net_no = net_no_list[index];
1359 break;
1360 }
1361 }
1362
1363 if (net_no < 0) {
1364 return -1;
1365 }
1366
1367 CHECK_WPA_CTRL(CTRL_STA);
1368
1369 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1370
1371 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1372 DO_OK_FAIL_REQUEST(cmd_save_config);
1373
1374 return 0;
1375}
1376
1377int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1378
1379 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001380 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001381 int count, index;
1382 char *split_lines[128] = {0};
1383 char *buff, *p;
1384
you.chen755332b2022-08-06 16:59:10 +08001385 network_len = 0;
1386 p = NULL;
1387
you.chen35020192022-05-06 11:30:57 +08001388 CHECK_IDX(idx, CTRL_STA);
1389
you.chen755332b2022-08-06 16:59:10 +08001390 if (NULL == password) {
1391 printf("bad param\n");
1392 return -1;
1393 }
1394
you.chen35020192022-05-06 11:30:57 +08001395 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1396 if (NULL == fp) {
1397 printf("open file fail\n");
1398 return -1;
1399 }
1400
1401 buff = alloca(MAX_RET);
1402 fseek(fp, 0, SEEK_SET);
1403 len = fread(buff, 1, MAX_RET, fp);
1404 fclose(fp);
1405
1406 for(index=0; index < len; index ++) {
1407 for(; index < len; index ++) {
1408 if (memcmp(buff + index, "network={", 9) != 0) {
1409 continue;
1410 }
1411 p = buff + index + 9;
1412 for (; index < len; index ++ ) {
1413 if (buff[index] != '}') {
1414 continue;
1415 }
1416 buff[index] = '\0';
1417 break;
1418 }
you.chen755332b2022-08-06 16:59:10 +08001419 network_len = buff + index - p;
1420 break;
you.chen35020192022-05-06 11:30:57 +08001421 }
1422
1423 if (strstr(p, ap->ap_ssid) != NULL) {
1424 break;
1425 }
1426 }
1427
you.chen755332b2022-08-06 16:59:10 +08001428 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001429 return -1;
1430 }
1431
you.chen755332b2022-08-06 16:59:10 +08001432 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001433
1434 ret = -1;
1435 for(index=0; index < count; index++) {
1436 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001437 if (p != NULL) {
1438 p += 4;
1439 if (*p == '\"') {
1440 p++;
1441 }
you.chen35020192022-05-06 11:30:57 +08001442 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001443 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1444 p += 9;
1445 if (*p == '\"') {
1446 p++;
1447 }
1448 }
1449 else {
1450 continue;
you.chen35020192022-05-06 11:30:57 +08001451 }
1452
1453 strcpy(password, p);
1454
1455 while(*password != '\0') {
1456 if (*password == '\"') {
1457 *password = '\0';
1458 break;
1459 }
1460 password++;
1461 }
1462 ret = 0;
1463 break;
1464 } //end for(index=0; index < count; index++)
1465
1466 return ret;
1467}
1468
1469static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1470{
qs.xiong97fa59b2022-04-07 05:41:29 -04001471 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001472
you.chen35020192022-05-06 11:30:57 +08001473 if (sta_ssid == NULL) {
1474 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001475 return -1;
you.chen35020192022-05-06 11:30:57 +08001476 }
1477
1478 CHECK_WPA_CTRL(CTRL_STA);
1479
1480 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1481
1482 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1483// DO_OK_FAIL_REQUEST(cmd_save_config);
1484
qs.xiong7a105ce2022-03-02 09:43:11 -05001485 return 0;
1486
1487}
1488
you.chen35020192022-05-06 11:30:57 +08001489static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001490{
you.chen35020192022-05-06 11:30:57 +08001491 char lynq_disable_cmd[128]={0};
1492 char lynq_select_cmd[128]={0};
1493
1494 CHECK_WPA_CTRL(CTRL_STA);
1495
1496 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001497 if (start_flag != 0)
1498 {
1499 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1500 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1501 }
1502 else
1503 {
1504 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1505 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1506 }
you.chen35020192022-05-06 11:30:57 +08001507 DO_OK_FAIL_REQUEST(cmd_save_config);
1508 }
1509
1510 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001511 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001512 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1513 }
1514 else {
1515 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1516 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1517 }
1518
1519 return 0;
1520}
1521
1522int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1523{
1524 CHECK_IDX(idx, CTRL_STA);
1525
you.chen6c2dd9c2022-05-16 17:55:28 +08001526 curr_status_info curr_state;
1527 ap_info_s ap_info;
1528 curr_state.ap = &ap_info;
1529 curr_state.state = NULL;
1530
1531 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1532 strcpy(sta_ssid, ap_info.ap_ssid);
1533 return 0;
1534 }
1535
1536 return -1;
you.chen35020192022-05-06 11:30:57 +08001537}
1538
1539int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1540{
you.chen9ac66392022-08-06 17:01:16 +08001541 scan_info_s *scan_list = NULL;
1542 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001543 int scan_len=0;
1544 int save_len=0;
1545 int best_index = -1;
1546 int best_scan_index = -1;
1547 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001548 int i, j, ret;
1549
1550 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001551
1552 CHECK_IDX(idx, CTRL_STA);
1553 if (info == NULL) {
1554 return -1;
1555 }
1556
1557 curr_status_info curr_state;
1558 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001559 char status[64];
you.chen35020192022-05-06 11:30:57 +08001560
you.chen9ac66392022-08-06 17:01:16 +08001561 memset(&ap_info, 0, sizeof (ap_info));
1562 memset(status, 0, sizeof (status));
1563
1564 curr_state.ap = &ap_info;
1565 curr_state.state = status;
1566
1567 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001568 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001569 if (strcmp(status, STATE_COMPLETED) == 0)
1570 {
1571 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1572 }
1573 else
1574 {
1575 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1576 }
you.chen35020192022-05-06 11:30:57 +08001577 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001578 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001579 return 0;
1580 }
1581
you.chen9ac66392022-08-06 17:01:16 +08001582 lynq_wifi_sta_start_scan(idx);
1583
you.chen35020192022-05-06 11:30:57 +08001584 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001585 if (NULL != scan_list)
1586 {
1587 free(scan_list);
1588 }
you.chen35020192022-05-06 11:30:57 +08001589 return -1;
1590 }
1591
1592 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001593 if (NULL != scan_list)
1594 {
1595 free(scan_list);
1596 }
1597 if (NULL != save_list)
1598 {
1599 free(save_list);
1600 }
you.chen35020192022-05-06 11:30:57 +08001601 return -1;
1602 }
1603
1604 for (i=0; i < save_len; i++) {
1605 for (j=0; j < scan_len; j++) {
1606 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1607 && save_list[i].base_info.auth == scan_list[j].auth) {
1608 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001609 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001610 best_rssi = scan_list[j].rssi;
1611 }
1612 else if (best_rssi > scan_list[j].rssi) {
1613 best_index = i;
1614 best_scan_index = j;
1615 best_rssi = scan_list[j].rssi;
1616 }
1617 break;
1618 }
1619 }
1620 }
1621
1622 if (best_index >= 0) {
1623 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1624 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1625 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001626 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001627 }
1628
you.chen9ac66392022-08-06 17:01:16 +08001629 if (NULL != scan_list)
1630 {
1631 free(scan_list);
1632 }
1633 if (NULL != save_list)
1634 {
1635 free(save_list);
1636 }
1637
1638 return ret;
you.chen35020192022-05-06 11:30:57 +08001639}
1640
1641static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1642{
1643 char lynq_auth_cmd[64]={0};
1644 char lynq_ket_mgmt_cmd[64]={0};
1645 char lynq_pairwise_cmd[64]={0};
1646 char lynq_psk_cmd[64]={0};
1647
1648 CHECK_WPA_CTRL(CTRL_STA);
1649
qs.xiong1af5daf2022-03-14 09:12:12 -04001650 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001651 case LYNQ_WIFI_AUTH_OPEN:
1652 {
1653 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001654
you.chen35020192022-05-06 11:30:57 +08001655 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1656// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001657 break;
1658 }
you.chen35020192022-05-06 11:30:57 +08001659 case LYNQ_WIFI_AUTH_WPA_PSK:
1660 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001661 {
you.chen35020192022-05-06 11:30:57 +08001662 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1663 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1664 }
1665 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001666 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001667 }
1668 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1669 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001670
you.chen35020192022-05-06 11:30:57 +08001671 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1672 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1673 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001674
you.chen35020192022-05-06 11:30:57 +08001675 if (password != NULL) {
1676 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1677 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1678 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001679
you.chen35020192022-05-06 11:30:57 +08001680// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001681 break;
you.chen35020192022-05-06 11:30:57 +08001682 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001683 default:
1684 return -1;
you.chen35020192022-05-06 11:30:57 +08001685 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001686
qs.xiongf1b525b2022-03-31 00:58:23 -04001687 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001688}
qs.xiong7a105ce2022-03-02 09:43:11 -05001689
you.chen35020192022-05-06 11:30:57 +08001690static int inner_get_curr_net_no(int interface) {
1691 curr_status_info curr_state;
1692 curr_state.ap = NULL;
1693 curr_state.state = NULL;
1694
1695 if (0 != inner_get_status_info(interface, &curr_state)) {
1696 return -1;
1697 }
1698
1699 return curr_state.net_no;
1700}
1701
1702int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001703{
you.chen35020192022-05-06 11:30:57 +08001704 int net_no;
1705 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001706
you.chen35020192022-05-06 11:30:57 +08001707 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001708
you.chen35020192022-05-06 11:30:57 +08001709 if (net_no < 0) {
1710 return -1;
1711 }
1712
1713 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001714}
1715
you.chen35020192022-05-06 11:30:57 +08001716int 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 -05001717{
you.chen35020192022-05-06 11:30:57 +08001718 int count, net_no, index;
1719 int net_no_list[128];
1720 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001721
you.chen35020192022-05-06 11:30:57 +08001722 if (ssid == NULL || *ssid == '\0') {
1723 printf("bad ssid\n");
1724 return -1;
1725 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001726
you.chen35020192022-05-06 11:30:57 +08001727 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1728 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1729 printf("bad password\n");
1730 return -1;
1731 }
1732 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001733
you.chen35020192022-05-06 11:30:57 +08001734 CHECK_IDX(idx, CTRL_STA);
1735
1736 net_no = -1;
1737 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1738
1739 for (index=0; index < count; index++) {
1740 net_auth = -1;
1741 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1742 net_no = net_no_list[index];
1743 break;
1744 }
1745 }
1746
1747 if (net_no < 0) {
1748 net_no = lynq_add_network(CTRL_STA);
1749 if (net_no == -1) {
1750 return -1;
1751 }
1752
1753 printf("net no is %d\n", net_no);
1754 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1755 return -1;
1756 }
1757 }
1758
1759 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1760 return -1;
1761 }
1762
1763 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001764}
1765
you.chen35020192022-05-06 11:30:57 +08001766int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001767{
you.chen35020192022-05-06 11:30:57 +08001768 ap_info_s ap;
1769 curr_status_info curr_state;
1770 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001771
you.chen35020192022-05-06 11:30:57 +08001772 if (ssid == NULL || *ssid == '\0') {
1773 return -1;
1774 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001775
you.chen35020192022-05-06 11:30:57 +08001776 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001777
you.chen35020192022-05-06 11:30:57 +08001778 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001779 curr_state.state = NULL;
1780
you.chen35020192022-05-06 11:30:57 +08001781 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1782 return 0;
1783 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001784
you.chen35020192022-05-06 11:30:57 +08001785 if (strcmp(ap.ap_ssid, ssid) != 0) {
1786 return 0;
1787 }
1788
1789 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001790}
qs.xiong97fa59b2022-04-07 05:41:29 -04001791
you.chena6cd55a2022-05-08 12:20:18 +08001792int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1793{
you.chenc29444e2022-06-07 18:01:16 +08001794 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 +08001795 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001796
you.chen35020192022-05-06 11:30:57 +08001797 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001798 CHECK_WPA_CTRL(CTRL_STA);
1799
1800 system("connmanctl enable wifi");
1801
you.chena6fa5b22022-05-18 10:28:19 +08001802 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001803 return -1;
1804 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001805
you.chen01bfac32022-06-07 10:36:00 +08001806 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001807 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001808 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001809
1810 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001811}
1812
you.chen35020192022-05-06 11:30:57 +08001813int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001814{
you.chena6cd55a2022-05-08 12:20:18 +08001815 char lynq_disable_network_cmd[MAX_CMD];
1816 curr_status_info curr_state;
1817 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001818
you.chena6cd55a2022-05-08 12:20:18 +08001819 CHECK_IDX(idx, CTRL_STA);
1820 CHECK_WPA_CTRL(CTRL_STA);
1821
1822 curr_state.ap = &ap_info;
1823 curr_state.state = NULL;
1824
1825 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1826 return 0;
1827 }
1828
1829 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1830 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1831
1832 DO_OK_FAIL_REQUEST(cmd_save_config);
1833
1834 return 0;
1835// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001836}
qs.xiong7a105ce2022-03-02 09:43:11 -05001837
you.chen35020192022-05-06 11:30:57 +08001838//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1839// int i, count;
1840// char *p;
1841// const char * FLAG_SSID = "ssid=";
1842// const char * FLAG_SBSID = "bssid=";
1843// const char * FLAG_KEY_MGMT = "key_mgmt=";
1844// const char * FLAG_FREQ = "freq=";
1845// char lynq_sta_cmd[MAX_CMD];
1846// char *split_lines[128] = {0};
1847
1848// CHECK_WPA_CTRL(CTRL_AP);
1849
1850// sprintf(lynq_sta_cmd, "STA %s", bssid);
1851
1852// DO_REQUEST(lynq_sta_cmd);
1853
1854// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1855
1856// for(i=0; i < count; i++) {
1857// p = strstr(split_lines[i], FLAG_SSID);
1858// if (p != NULL) {
1859// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1860// continue;
1861// }
1862// }
1863
1864// lynq_get_interface_ip(idx, ap->ap_ip);
1865// lynq_ap_password_set(idx, ap->psw);
1866
1867// return 0;
1868//}
1869
1870static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1871 curr_status_info curr_state;
1872 curr_state.ap = ap;
1873 curr_state.state = NULL;
1874 return inner_get_status_info(interface, &curr_state);
1875}
1876
1877int 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 -04001878{
you.chen35020192022-05-06 11:30:57 +08001879 int ip_count, index, i, line_count;
1880 const char *lynq_first_sta_cmd = "STA-FIRST";
1881 char lynq_next_sta_cmd[MAX_CMD];
1882 char *bssid[1024] = {0};
1883 char *mac_list[128] = {0};
1884 char *ip_list[128] = {0};
1885 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001886
you.chen35020192022-05-06 11:30:57 +08001887 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001888
you.chen35020192022-05-06 11:30:57 +08001889 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001890
you.chen35020192022-05-06 11:30:57 +08001891// ap_info_s * tmp_ap;
1892// device_info_s * tmp_list;
1893 if (ap == NULL || list == NULL || len == NULL) {
1894 printf("bad input param");
1895 return -1;
1896 }
1897
1898// ap = &tmp_ap;
1899// list = &tmp_list;
1900 *ap = malloc(sizeof (ap_info_s));
1901
1902 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1903 return -1;
1904 }
1905
1906 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1907 lynq_ap_password_get(idx, (*ap)->psw);
1908
1909 ip_count = get_ip_mac_list(mac_list, ip_list);
1910 printf("get count %d\n", ip_count);
1911
1912 DO_REQUEST(lynq_first_sta_cmd);
1913
1914 index = 0;
1915 while (reply_len > 0) {
1916 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1917 break;
1918 }
1919 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1920 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1921 strcpy(bssid[index], split_lines[0]);
1922 index++;
1923 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1924 reply_len = MAX_RET;
1925 cmd_reply[0] = '\0';
qs.xiongfdbc58e2022-09-29 11:37:32 +08001926 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 +08001927 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1928 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001929 break;
1930 }
1931 }
1932
1933 *len = index;
1934
1935 *list = malloc(sizeof(device_info_s) * (*len));
1936 for (index=0; index < *len; index++) {
1937 strcpy((*list)[index].sta_mac, bssid[index]);
1938 for(i=0;i < ip_count; i++ ) {
1939 if (strcmp(bssid[index], mac_list[i]) == 0) {
1940 strcpy((*list)[index].sta_ip, ip_list[i]);
1941 break;
1942 }
1943 }
1944 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1945 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1946 free(bssid[index]);
1947 }
1948
you.chen9ac66392022-08-06 17:01:16 +08001949 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
1950
you.chen35020192022-05-06 11:30:57 +08001951 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001952}
1953
you.chen35020192022-05-06 11:30:57 +08001954int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001955{
you.chen35020192022-05-06 11:30:57 +08001956 int i, count, index, count_words;
1957 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1958 char *split_lines[128] = {0};
1959 char *split_words[128] = {0};
1960 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001961
you.chen35020192022-05-06 11:30:57 +08001962 if (list == NULL || len == NULL) {
1963 return -1;
1964 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001965
you.chen9ac66392022-08-06 17:01:16 +08001966 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
1967 {
1968 usleep(100 * 1000);
1969 }
1970
you.chen35020192022-05-06 11:30:57 +08001971 CHECK_IDX(idx, CTRL_STA);
1972
1973 CHECK_WPA_CTRL(CTRL_STA);
1974
1975 DO_REQUEST(lynq_scan_result_cmd);
1976
1977 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1978 *len = count - 1;
1979 *list = malloc(sizeof (scan_info_s) * *len);
1980
1981 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1982 for (index=0; index <count_words; index++) {
1983 printf("----header: %s\n", split_words[index]);
1984 }
1985
1986 for(index = 1;index < count; index++) {
1987 printf("---- %s\n",split_lines[index]);
1988 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1989 if (count_words < 4)
1990 continue;
1991 printf("count: %d, %s\n", count_words, split_words[0]);
1992 //bssid / frequency / signal level / flags / ssid
1993 p = (*list) + index - 1;
1994 strcpy(p->mac, split_words[0]);
1995 p->band = convert_band_from_freq(atoi(split_words[1]));
1996 p->rssi = -1 * atoi( split_words[2]);
1997 p->auth = convert_max_auth_from_flag(split_words[3]);
1998 strcpy(p->ssid, split_words[4]);
1999 }
2000
2001 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002002}
qs.xiong97fa59b2022-04-07 05:41:29 -04002003
you.chen35020192022-05-06 11:30:57 +08002004int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2005{
2006 int count, net_no, index;
2007 int net_no_list[128];
2008 lynq_wifi_auth_s net_auth;
2009 char lynq_remove_cmd[MAX_CMD];
2010
2011 if (ssid == NULL || *ssid == '\0') {
2012 printf("bad ssid\n");
2013 return -1;
2014 }
2015
2016 CHECK_IDX(idx, CTRL_STA);
2017
2018 CHECK_WPA_CTRL(CTRL_STA);
2019
2020 net_no = -1;
2021 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2022
2023 for (index=0; index < count; index++) {
2024 net_auth = -1;
2025 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2026 net_no = net_no_list[index];
2027 break;
2028 }
2029 }
2030
2031 if (net_no < 0) {
2032 return 0;
2033 }
2034
2035 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2036
2037 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2038 DO_OK_FAIL_REQUEST(cmd_save_config);
2039
2040 return 0;
2041}
2042
2043int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2044{
you.chen755332b2022-08-06 16:59:10 +08002045 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002046 int net_no_list[128];
2047 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002048 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002049
2050 if (list == NULL || len == NULL) {
2051 printf("bad param\n");
2052 return -1;
2053 }
2054
2055 CHECK_IDX(idx, CTRL_STA);
2056
2057// CHECK_WPA_CTRL(CTRL_STA);
2058
2059 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2060 printf("count is %d\n", count);
2061
2062 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002063 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002064 *len = count;
2065
2066 for (index=0; index < count; index++) {
2067 printf("to get ssid %d\n", index);
2068 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002069
2070 ssid_ptr = (*list)[index].base_info.ap_ssid;
2071 ssid_len = strlen(ssid_ptr);
2072 if (ssid_ptr[0] == '\"') {
2073 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2074 ssid_len -= 1;
2075 }
2076 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2077 ssid_ptr[ssid_len - 1] = '\0';
2078 }
2079
you.chen35020192022-05-06 11:30:57 +08002080 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002081 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002082 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2083 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2084 }
2085 else {
2086 (*list)[index].base_info.band = -1;
2087 }
2088
you.chen755332b2022-08-06 16:59:10 +08002089 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002090 }
2091
2092 return 0;
2093}
2094
2095int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2096{
2097 const char *lynq_scan_cmd = "SCAN";
2098
2099 CHECK_IDX(idx, CTRL_STA);
2100
2101 CHECK_WPA_CTRL(CTRL_STA);
2102
2103 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002104 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002105
2106 return 0;
2107}
2108
2109int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2110 if (cb == NULL) {
2111 return -1;
2112 }
2113
2114 g_ap_callback_priv = priv;
2115 g_ap_callback_func = cb;
2116
2117 return 0;
2118}
2119
2120int lynq_unreg_ap_event_callback(void * priv) {
2121 if (g_ap_callback_priv == priv) {
2122 g_ap_callback_func = NULL;
2123 g_ap_callback_priv = NULL;
2124 return 0;
2125 }
2126 return -1;
2127}
2128
2129int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2130 if (cb == NULL) {
2131 return -1;
2132 }
2133
2134 g_sta_callback_priv = priv;
2135 g_sta_callback_func = cb;
2136
2137 return 0;
2138}
2139
2140int lynq_unreg_sta_event_callback(void * priv) {
2141 if (g_sta_callback_priv == priv) {
2142 g_sta_callback_func = NULL;
2143 g_sta_callback_priv = NULL;
2144 return 0;
2145 }
2146 return -1;
2147}
2148
2149
2150static int inner_get_status_info_state (int interface, char *state) {
2151 curr_status_info curr_state;
2152 curr_state.ap = NULL;
2153 curr_state.state = state;
2154 return inner_get_status_info(interface, &curr_state);
2155}
2156
2157int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2158{
2159 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002160 CHECK_IDX(idx, CTRL_AP);
2161
2162 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2163 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2164 return 0;
2165 }
2166
2167 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2168 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2169 }
2170 else {
2171 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2172 }
2173
2174 return 0;
2175}
2176
2177int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2178 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002179 CHECK_IDX(idx, CTRL_STA);
2180
2181 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2182 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2183 return 0;
2184 }
2185
2186 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2187 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2188 }
2189 else {
2190 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2191 }
2192
2193 return 0;
2194}
2195
2196int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2197// CHECK_IDX(idx, CTRL_AP);
2198// int ret = 0;
2199// size_t reply_len = MAX_RET;
2200// char cmd_reply[MAX_RET]={0};
2201// const char * cmd_str = "GET country";
2202// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2203// do{
2204// if (NULL == s_lynq_wpa_ctrl) {
2205// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2206// if (NULL == s_lynq_wpa_ctrl ) {
2207// printf("wpa_ctrl_open fail\n");
2208// return -1;
2209// }
2210// }
2211// }while(0);
2212
2213// do {
2214// reply_len = MAX_RET;
2215// cmd_reply[0] = '\0';
2216// printf("to call [%s]\n", cmd_str);
qs.xiongfdbc58e2022-09-29 11:37:32 +08002217// 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 +08002218// if (ret != 0) {
2219// printf("call ##cmd_str fail %d\n", ret);
2220// return ret;
2221// }
2222// cmd_reply[reply_len+1] = '\0';
2223// printf("cmd replay [ %s ]\n", cmd_reply);
2224// }while(0);
2225
2226 FILE *fp;
2227 size_t i = 0;
2228 char lynq_cmd_ret[MAX_RET]={0};
2229
2230// CHECK_IDX(idx, CTRL_AP);
2231
2232 if((fp=popen("wl country","r"))==NULL)
2233 {
2234 perror("popen error!");
2235 return -1;
2236 }
2237 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2238 {
2239 perror("fread fail!");
2240 return -1;
2241 }
2242
2243 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2244 if (lynq_cmd_ret[i] == ' ') {
2245 lynq_cmd_ret[i] = '\0';
2246 break;
2247 }
2248 }
2249
2250 strcpy(country_code,lynq_cmd_ret);
2251 printf("---country code %s\n", country_code);
2252
2253 int ret=pclose(fp);
2254 if(ret==-1)
2255 {
2256 perror("close file faild");
2257 }
2258
2259 return 0;
2260}
2261
2262int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2263// const char * cmd_str = "GET country";
2264// CHECK_IDX(idx, CTRL_AP);
2265// CHECK_WPA_CTRL(CTRL_STA);
2266
2267// DO_REQUEST(cmd_str);
2268// printf("result %s\n", cmd_reply);
2269
2270 if (country_code == NULL || *country_code == '\0') {
2271 printf("bad country code\n");
2272 return -1;
2273 }
2274
2275 char lynq_country_cmd[MAX_CMD];
2276 sprintf(lynq_country_cmd, "wl country %s", country_code);
2277 if (system(lynq_country_cmd) == 0) {
2278 return 0;
2279 }
2280
2281 return -1;
2282}
2283
2284int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2285{
2286 if (mac == NULL) {
2287 return -1;
2288 }
2289
2290 CHECK_IDX(idx, CTRL_STA);
2291 ap_info_s ap;
2292 ap.ap_mac[0] = '\0';
2293
2294 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2295 return -1;
2296 }
2297 strcpy(mac, ap.ap_mac);
2298
2299 return 0;
2300}
2301
2302int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2303{
you.chen9ac66392022-08-06 17:01:16 +08002304 struct ifaddrs *ifaddr_header, *ifaddr;
2305 struct in_addr * ifa;
2306 const char * ifaName = "wlan0";
2307 if (ip == NULL)
2308 {
you.chenf58b3c92022-06-21 16:53:48 +08002309 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002310 }
you.chenf58b3c92022-06-21 16:53:48 +08002311
you.chen9ac66392022-08-06 17:01:16 +08002312 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002313 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002314 }
2315 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002316 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002317 }
you.chen35020192022-05-06 11:30:57 +08002318
you.chen9ac66392022-08-06 17:01:16 +08002319 if (getifaddrs(&ifaddr_header) == -1)
2320 {
you.chen35020192022-05-06 11:30:57 +08002321 perror("getifaddrs");
2322 return -1;
2323 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002324 }
you.chen35020192022-05-06 11:30:57 +08002325
2326
you.chen9ac66392022-08-06 17:01:16 +08002327 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2328 {
2329 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002330 continue;
you.chen9ac66392022-08-06 17:01:16 +08002331 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2332 {
2333 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2334 {
2335 // is a valid IP4 Address
2336 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2337 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2338 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2339 freeifaddrs(ifaddr_header);
2340 printf("ip %s\n", ip);
2341 return 0;
2342 }
2343 }
2344 }
2345 return -1;
you.chen35020192022-05-06 11:30:57 +08002346}
2347
2348int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2349{
2350 int count;
2351 size_t i;
2352 char *split_words[128] = {0};
2353 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2354
2355 CHECK_WPA_CTRL(idx);
2356
2357 DO_REQUEST(lynq_get_mac_cmd);
2358
2359 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2360 return -1;
2361 }
2362
2363 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2364
2365 if (count < 2) {
2366 return -1;
2367 }
2368
2369 for (i=0; i < strlen(split_words[1]); i++ ) {
2370 if (split_words[1][i] != ' ') {
2371 break;
2372 }
2373 }
2374
2375 strcpy(mac, split_words[1] + i);
2376
2377 return 0;
2378}
2379
2380int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2381{
2382// int count;
2383// char *split_words[128] = {0};
2384// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2385
2386// if (rssi == NULL) {
2387// return -1;
2388// }
2389
2390// CHECK_IDX(idx, CTRL_STA);
2391
2392// CHECK_WPA_CTRL(CTRL_STA);
2393
2394// DO_REQUEST(lynq_get_rssi_cmd);
2395
2396// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2397// return -1;
2398// }
2399
2400// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2401
2402// if (count < 2) {
2403// return -1;
2404// }
2405
2406// *rssi = atoi(split_words[1]) * -1;
2407
2408 FILE *fp;
2409 size_t i = 0;
2410 char lynq_cmd_ret[MAX_RET]={0};
2411
2412// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002413/*******change other cmd to get rssi*******
2414 *
2415 *wl rssi ---> wl -i wlan0 rssi
2416 *
2417 ***** change by qs.xiong 20221011*******/
2418 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002419 {
2420 perror("popen error!");
2421 return -1;
2422 }
2423 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2424 {
2425 perror("fread fail!");
2426 return -1;
2427 }
you.chen9f17e4d2022-06-06 17:18:18 +08002428 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002429/****** if got rssi is 0,means sta didn't connected any device****/
2430 if(*rssi == 0)
2431 {
2432 printf("sta didn't connected any ap device,please check connection\n");
2433 }
you.chen35020192022-05-06 11:30:57 +08002434
2435 return 0;
2436}
2437
2438int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2439{
2440 if (band == NULL) {
2441 return -1;
2442 }
2443
2444 CHECK_IDX(idx, CTRL_STA);
2445 ap_info_s ap;
2446 ap.band = -1;
2447
2448 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2449 return -1;
2450 }
2451 *band = ap.band;
2452
2453 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002454}
you.chenf58b3c92022-06-21 16:53:48 +08002455
2456int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2457{
2458 int index;
2459 int ip_count = 0;
2460 char bssid[1024] = {0};
2461 char *mac_list[128] = {0};
2462 char *ip_list[128] = {0};
2463
2464 if (ip == NULL)
2465 {
2466 printf("invalid param");
2467 return -1;
2468 }
2469
2470 CHECK_IDX(idx, CTRL_STA);
2471
2472 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2473 {
2474 return -1;
2475 }
2476
2477 ip_count = get_ip_mac_list(mac_list, ip_list);
2478
2479 for (index=0; index < ip_count; index++)
2480 {
2481 if (strcmp(bssid, mac_list[index]) == 0)
2482 {
2483 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002484
2485 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2486
you.chenf58b3c92022-06-21 16:53:48 +08002487 return 0;
2488 }
2489 }
2490
2491 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002492 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2493
you.chenf58b3c92022-06-21 16:53:48 +08002494 return -1;
2495}
2496
qs.xiong026c5c72022-10-17 11:15:45 +08002497int lynq_ap_connect_num(int sta_number)
2498{
2499 char lynq_limit_cmd[32]={0};
2500 int ret;
2501 if((sta_number < 1 ) && (sta_number > 15)){
2502 printf("sta_number: not in range\n",sta_number);
2503 return -1;
2504 }
2505 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2506 ret = system(lynq_limit_cmd);
2507 if(ret != 0){
2508 printf("cmd of limit ap devices number error\n");
2509 }
2510 return 0;
2511}
you.chenf58b3c92022-06-21 16:53:48 +08002512