blob: 102d1309172038513522f453fd486cddfa07ecda [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
45volatile int g_sta_scan_finish_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
61
62static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
63
64
65typedef struct __curr_status_info {
66 ap_info_s *ap;
67 char * state;
68 int net_no;
69}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040070
71#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040072{\
you.chen35020192022-05-06 11:30:57 +080073 perror((str));\
74 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040075}
76
you.chen35020192022-05-06 11:30:57 +080077#define CHECK_IDX(idx, type) do { \
78 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
79 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
80 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
81 return -1; \
82 } \
83 }while (0)
84
85#define CHECK_WPA_CTRL(index) int ret = 0;\
86 size_t reply_len = MAX_RET; \
87 char cmd_reply[MAX_RET]={0}; \
88 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
89 do{ \
90 if (NULL == g_lynq_wpa_ctrl[index]) { \
91 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
92 if (NULL == g_lynq_wpa_ctrl[index]) { \
93 printf("wpa_ctrl_open fail\n"); \
94 return -1; \
95 } \
96 } \
97 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
98 }while(0)
99
100#define DO_REQUEST(cmd_str) do { \
101 reply_len = MAX_RET;\
102 cmd_reply[0] = '\0'; \
103 printf("to call [%s]\n", cmd_str); \
104 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
105 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800106 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800107 return ret; \
108 } \
109 cmd_reply[reply_len+1] = '\0'; \
110 printf("cmd replay [ %s ]\n", cmd_reply); \
111 }while(0)
112
113#define DO_OK_FAIL_REQUEST(cmd_str) do { \
114 DO_REQUEST(cmd_str); \
115 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800116 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800117 return -1; \
118 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800119 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800120 return -1; \
121 } \
122 }while (0)
123
124
125
126static void APWatcherThreadProc() {
127 size_t len = MAX_RET;
128 char msg_notify[MAX_RET];
129
you.chen6c2dd9c2022-05-16 17:55:28 +0800130 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen35020192022-05-06 11:30:57 +0800131
132 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800133 if (lynq_wpa_ctrl == NULL) {
134 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
135 if (lynq_wpa_ctrl == NULL) {
136 usleep(100*1000);
137 continue;
138 }
139
140 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800141 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800142 }
143
you.chen35020192022-05-06 11:30:57 +0800144 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
145 usleep(100*1000);
146 continue;
147 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800148 memset(msg_notify, 0, MAX_RET);
149 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800150 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
151 msg_notify[len+1] = '\0';
152 printf("ap------> %s\n", msg_notify);
153 if (g_ap_callback_func == NULL) {
154 continue;
155 }
156 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
157 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
158 }
159 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
160 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
161 }
162 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
163 } // end while (g_ap_watcher_stop_flag == 0)
164 wpa_ctrl_detach(lynq_wpa_ctrl);
165 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiong97fa59b2022-04-07 05:41:29 -0400166}
167
you.chen35020192022-05-06 11:30:57 +0800168static void STAWatcherThreadProc() {
169 size_t len = MAX_RET;
170 char msg_notify[MAX_RET];
171 char *pReason;
172 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400173
you.chen6c2dd9c2022-05-16 17:55:28 +0800174 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen35020192022-05-06 11:30:57 +0800175
176 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800177 if (lynq_wpa_ctrl == NULL) {
178 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
179 if (lynq_wpa_ctrl == NULL) {
180 usleep(100*1000);
181 continue;
182 }
183
184 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800185 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800186 }
187
you.chen35020192022-05-06 11:30:57 +0800188 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
189 usleep(100*1000);
190 continue;
191 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800192 memset(msg_notify, 0, MAX_RET);
193 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800194 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
195 msg_notify[len+1] = '\0';
196 printf("sta ------> %s\n", msg_notify);
197 if (strstr(msg_notify, state_scan_result) != NULL) {
198 g_sta_scan_finish_flag = 1;
199 }
200
201 if (g_sta_callback_func == NULL) {
202 continue;
203 }
204 error = -1;
205 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
206 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
207 }
208 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
209 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
210 }
211 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
212 pReason = strstr(msg_notify, "reason=");
213 if (pReason != NULL) {
214 pReason += strlen("reason=");
215 if (memcpy(pReason, "CONN_FAILED", 11) == 0) {
216 error = LYNQ_TIME_OUT;
217 }
218 else if (memcpy(pReason, "WRONG_KEY", 9) == 0) {
219 error = LYNQ_PSW_ERROR;
220 }
221 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
222 }
223 }
224 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
225 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
226 }
227 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
228 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
229 }
230 }
231 }
232 wpa_ctrl_detach(lynq_wpa_ctrl);
233 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiongf1b525b2022-03-31 00:58:23 -0400234}
235
qs.xiong1af5daf2022-03-14 09:12:12 -0400236int lynq_wifi_enable(void)
237{
you.chen35020192022-05-06 11:30:57 +0800238 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800239 int i;
you.chen35020192022-05-06 11:30:57 +0800240 const char * cmd_check_service =
241 "state=`systemctl is-active wg870_drv_insmod.service`\n"
242 "[ \"\"$state == \"active\" ] && exit 0\n"
243 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
244// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
245// return 0;
246// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400247
you.chen35020192022-05-06 11:30:57 +0800248 ret = system(cmd_check_service);
249 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800250 printf("service state %d\n", ret);
you.chen35020192022-05-06 11:30:57 +0800251 return -1;
252 }
253
you.chen6c2dd9c2022-05-16 17:55:28 +0800254 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800255 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800256 break;
257 }
258 usleep(300*1000);
259 }
260
261 if (i >= 10) {
262 return -1;
263 }
264
you.chena6fa5b22022-05-18 10:28:19 +0800265 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800266 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800267 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800268 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800269 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800270 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800271 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800272 }
273
you.chen35020192022-05-06 11:30:57 +0800274 if (g_ap_watcher_pid == 0 ) {
275 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
276 if(ret<0){
277 return -1;
278 }
279 }
280
281 if (g_sta_watcher_pid == 0 ) {
282 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
283 if(ret<0){
284 return -1;
285 }
286 }
287
you.chena6fa5b22022-05-18 10:28:19 +0800288 for (i=0; i<10; i++) {
289 usleep(300*1000);
290 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
291 break;
292 }
293 }
294
you.chen35020192022-05-06 11:30:57 +0800295 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500296}
297
qs.xiong1af5daf2022-03-14 09:12:12 -0400298int lynq_wifi_disable(void)
299{
you.chen35020192022-05-06 11:30:57 +0800300 g_ap_watcher_stop_flag = 1;
301 g_sta_watcher_stop_flag = 1;
302 if (g_ap_watcher_pid != 0)
303 pthread_join(g_ap_watcher_pid, NULL);
304 if (g_sta_watcher_pid != 0)
305 pthread_join(g_sta_watcher_pid, NULL);
306 if (g_lynq_wpa_ctrl[0] != NULL)
307 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
308 if (g_lynq_wpa_ctrl[1] != NULL)
309 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
310 g_ap_watcher_pid = 0;
311 g_sta_watcher_pid = 0;
312 g_lynq_wpa_ctrl[0] = NULL;
313 g_lynq_wpa_ctrl[1] = NULL;
314 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500315 return 0;
316}
qs.xiong1af5daf2022-03-14 09:12:12 -0400317
you.chen35020192022-05-06 11:30:57 +0800318static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
319
320 char lynq_cmd_get[128]={0};
321
322 if (out_put == NULL) {
323 printf("output ptr is null\n");
324 return -1;
325 }
326 if (param_name == NULL) {
327 printf("param ptr is null");
328 return -1;
329 }
330 if (param_name[0] == '\0') {
331 printf("param is empty");
332 return -1;
333 }
334
335 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
336
337 CHECK_WPA_CTRL(interface);
338
339 DO_REQUEST(lynq_cmd_get);
340
341 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
342 return -1;
343 }
344
you.chena6fa5b22022-05-18 10:28:19 +0800345// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800346 memcpy(out_put, cmd_reply, reply_len + 1);
347 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500348}
qs.xiong1af5daf2022-03-14 09:12:12 -0400349
you.chen35020192022-05-06 11:30:57 +0800350static int lynq_split(char * str, int len, char delimiter, char * results[]) {
351 int ret = 0;
352 char * end = str + len - 1;
353 results[ret++] = str;
354 while(str < end) {
355 if (*str == delimiter) {
356 *str++ = '\0';
357 results[ret++] = str;
358 continue;
359 }
360 str++;
361 }
362 if (*str == delimiter) {
363 *str = '\0';
364 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400365
you.chen35020192022-05-06 11:30:57 +0800366 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500367}
368
you.chen35020192022-05-06 11:30:57 +0800369static void trim_space(char * p, int count) {
370 char * begin = p;
371 p += count;
372 printf("%C-%C||\n", *begin, *p);
373 while (p >= begin ) {
374 if (*p == ' ') {
375 *p-- = '\0';
376 }
377 else {
378 break;
379 }
380 }
381}
382
383static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
384 FILE * fp;
385 int len, ret;
386 int count, count_words, index;
387 int mac_start, mac_end;
388 int ip_start, ip_end;
389 char *split_lines[128] = {0};
390 char *buff;
391 const char * ip_header = "IP address";
392 const char * mac_header = "HW address";
393 const char * zero_mac = "00:00:00:00:00:00";
394
395 fp = fopen("/proc/net/arp", "rb");
396 if (NULL == fp) {
397 printf("open file fail\n");
398 return -1;
399 }
400
401 buff = alloca(MAX_RET);
402 fseek(fp, 0, SEEK_SET);
403 len = fread(buff, 1, MAX_RET, fp);
404 fclose(fp);
405 if (len <= 0) {
406 printf("read file fail\n");
407 return -1;
408 }
409 printf("file : %s\n", buff);
410
411 count = lynq_split(buff, len, '\n', split_lines);
412 printf("----- %s\n", split_lines[0]);
413
414 mac_end = 0;
415 count_words = strlen(split_lines[0]);
416 if (strstr(split_lines[0], mac_header) != NULL) {
417 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
418 mac_end = mac_start + strlen(mac_header) + 1;
419 while (mac_end < count_words) {
420 if (split_lines[0][mac_end] != ' ') {
421 break;
422 }
423 mac_end++;
424 }
425 }
426
427 ip_end = 0;
428 if (strstr(split_lines[0], ip_header) != NULL) {
429 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
430 ip_end = ip_start + strlen(ip_header) + 1;
431 while (ip_end < count_words) {
432 if (split_lines[0][ip_end] != ' ') {
433 break;
434 }
435 ip_end++;
436 }
437 }
438
439 if (mac_end == 0 || ip_end == 0) {
440 return 0;
441 }
442
443 ret = 0;
444 for(index = 1;index < count; index++) {
445 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
446 continue;
447 }
448 mac_list[ret] = malloc(mac_end - mac_start + 1);
449 ip_list[ret] = malloc(ip_end - ip_start + 1);
450 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
451 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
452 trim_space(mac_list[ret], mac_end - mac_start - 1);
453 trim_space(ip_list[ret], ip_end - ip_start - 1);
454 ret++;
455 }
456
457 return ret;
458}
459
460static int get_hostname_by_ip(char *ip, char *hostname) {
461 struct in_addr addr ={0};
462 struct hostent *ht;
463
464 if (ip == NULL || *ip == '\0' || hostname == NULL) {
465 return -1;
466 }
467
468 if (inet_aton(ip, &addr) == 0) {
469 printf("---inet_aton fail\n");
470 return -1;
471 }
472
473 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
474
475 if (ht == NULL) {
476 printf("---gethostbyaddr fail\n");
477 herror(NULL);
478 return -1;
479 }
480
481 strcpy(hostname, ht->h_name);
482
483 return 0;
484}
485
486static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
487{
488 int count, index, words_count;
489 char * split_lines[128]= {0};
490 char * split_words[128] = {0};
491 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
492
493 CHECK_WPA_CTRL(ap_sta);
494
495 DO_REQUEST(lynq_wifi_list_networks);
496
497 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
498
499 //@todo check ssid field to compatible
500
501 ret = 0;
502 for(index=1; index < count; index++) {
503 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
504 if (words_count > 2) {
505 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
506 net_no_list[ret++] = atoi(split_words[0]);
507 }
508 }
509 }
510
511 return ret;
512}
513
514static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800515 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800516 CHECK_WPA_CTRL(ap_sta);
517 const char *lynq_wifi_add_network = "ADD_NETWORK";
518
519 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800520 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800521 return -1;
522 }
523
you.chen6c2dd9c2022-05-16 17:55:28 +0800524 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800525 if(cmd_reply[i] == '\n') {
526 cmd_reply[i] = '\0';
527 break;
528 }
529 }
530 return atoi(cmd_reply);
531}
you.chena6cd55a2022-05-08 12:20:18 +0800532
you.chen35020192022-05-06 11:30:57 +0800533static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
534{
535 int count, index;
536 int net_no_list[128];
537
538
539 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
540 for (index=0; index < count; index++) {
541 if (net_no_list[index] == net_no) {
542 return 0;
543 }
544 }
545
546 if (count >= 1)
547 index = net_no_list[count - 1];
548 else
549 index = -1;
550
551 while (index < net_no ) {
552 index = lynq_add_network(ap_sta);
553 if (index >= net_no) { // required network no created
554 return 0;
555 }
you.chena6cd55a2022-05-08 12:20:18 +0800556 else if( index < 0) {
557 printf("add network fail\n");
558 return -1;
559 }
you.chen35020192022-05-06 11:30:57 +0800560 }
561
562 if (index < 0)
563 return -1;
564
565 return 0;
566}
567
568static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
569 if (freq > 5000 && freq < 6000) {
570 return LYNQ_WIFI_5G_band;
571 }
572 else if (freq > 2000 && freq < 3000) {
573 return LYNQ_WIFI_2G_band;
574 }
575 return LYNQ_WIFI_2_and_5G_band;
576}
577
578static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
579 if (key_mgmt != NULL) {
580 if (memcmp( key_mgmt, "NONE", 4) == 0) {
581 return LYNQ_WIFI_AUTH_OPEN;
582 }
583 else if (memcmp( key_mgmt, "WEP", 3) == 0){
584 return LYNQ_WIFI_AUTH_WEP;
585 }
586 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
587 return LYNQ_WIFI_AUTH_WPA_PSK;
588 }
589 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
590 return LYNQ_WIFI_AUTH_WPA2_PSK;
591 }
592 }
593
594 return -1;
595}
596
597static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
598 if (flag != NULL) {
599 if (strstr( flag, "WPA2-PSK") != NULL){
600 return LYNQ_WIFI_AUTH_WPA2_PSK;
601 }
602 else if (strstr( flag, "WPA-PSK") != NULL){
603 return LYNQ_WIFI_AUTH_WPA_PSK;
604 }
605 else if (strstr( flag, "WEP") != NULL){
606 return LYNQ_WIFI_AUTH_WEP;
607 }
608 else if (strstr( flag, "NONE") != NULL) {
609 return LYNQ_WIFI_AUTH_OPEN;
610 }
611 }
612
613 return -1;
614}
615
616static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
617 switch (bw) {
618 case 10:
619 return LYNQ_WIFI_BANDWIDTH_HT10;
620 break;
621 case 20:
622 return LYNQ_WIFI_BANDWIDTH_HT20;
623 break;
624 case 40:
625 return LYNQ_WIFI_BANDWIDTH_HT40;
626 break;
627 case 80:
628 return LYNQ_WIFI_BANDWIDTH_HT80;
629 break;
630 default:
631 break;
632 }
633
634 return -1;
635}
636
637static int inner_get_status_info(int interface, curr_status_info *curr_state) {
638 int i, count;
639 char *p;
640 const char *lynq_status_cmd = "STATUS";
641 const char * FLAG_SSID = "ssid=";
642 const char * FLAG_SBSID = "bssid=";
643 const char * FLAG_KEY_MGMT = "key_mgmt=";
644 const char * FLAG_FREQ = "freq=";
645 const char * FLAG_STATE = "wpa_state=";
646 const char * FLAG_ID = "id=";
647 char *split_lines[128] = {0};
648
649 CHECK_WPA_CTRL(interface);
650
651 if (curr_state == NULL) {
652 return -1;
653 }
654
655 DO_REQUEST(lynq_status_cmd);
656
657 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
658
659 curr_state->net_no = -1;
660 ret = -1;
661 for(i=0; i < count; i++) {
662 if (curr_state->ap != NULL) {
663 p = strstr(split_lines[i], FLAG_SSID);
664 if (p != NULL) {
665 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
666 ret = 0;
667 continue;
668 }
669 p = strstr(split_lines[i], FLAG_SBSID);
670 if (p != NULL) {
671 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
672 ret = 0;
673 continue;
674 }
675 p = strstr(split_lines[i], FLAG_KEY_MGMT);
676 if (p != NULL) {
677 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
678 ret = 0;
679 continue;
680 }
681 p = strstr(split_lines[i], FLAG_FREQ);
682 if (p != NULL) {
683 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
684 ret = 0;
685 continue;
686 }
687 } // end if (ap != NULL)
688 if (curr_state->state != NULL) {
689 p = strstr(split_lines[i], FLAG_STATE);
690 if (p != NULL) {
691 strcpy(curr_state->state, p + strlen(FLAG_STATE));
692 ret = 0;
693 continue;
694 }
695
696 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800697 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800698 ret = 0;
699 curr_state->net_no = atoi(p);
700 printf("net_no %d, -- %s\n", curr_state->net_no, p);
701 }
702 }
703
704 return ret;
705}
706
707
qs.xiongf1b525b2022-03-31 00:58:23 -0400708int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500709{
you.chen35020192022-05-06 11:30:57 +0800710 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500711
you.chen35020192022-05-06 11:30:57 +0800712 if (ap_ssid == NULL) {
713 printf("ap_ssid is null\n");
714 return -1;
715 }
716 else {
717 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
718 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400719
you.chen35020192022-05-06 11:30:57 +0800720 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
721 return -1;
722 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400723
you.chen35020192022-05-06 11:30:57 +0800724 CHECK_IDX(idx, CTRL_AP);
725
726 CHECK_WPA_CTRL(CTRL_AP);
727
728 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
729
730 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
731 DO_OK_FAIL_REQUEST(cmd_save_config);
732
qs.xiong7a105ce2022-03-02 09:43:11 -0500733 return 0;
you.chen35020192022-05-06 11:30:57 +0800734
qs.xiong7a105ce2022-03-02 09:43:11 -0500735}
736
you.chen35020192022-05-06 11:30:57 +0800737int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500738{
you.chen6c2dd9c2022-05-16 17:55:28 +0800739 int len;
you.chen35020192022-05-06 11:30:57 +0800740 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800741 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
742 return -1;
743 len = strlen(ap_ssid);
744 if (ap_ssid[0] == '\"') {
745 memmove(ap_ssid, ap_ssid + 1, len - 1);
746 len -= 1;
747 }
748 if (len > 0 && ap_ssid[len-1] == '\"') {
749 ap_ssid[len-1] = '\0';
750 }
751 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500752}
753
qs.xiongf1b525b2022-03-31 00:58:23 -0400754int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500755{
you.chen35020192022-05-06 11:30:57 +0800756 char lynq_wifi_frequency_cmd[128]={0};
757 char lynq_cmd_mode[128]={0};
758 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500759
you.chen35020192022-05-06 11:30:57 +0800760 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
761 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
762 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400763
you.chen35020192022-05-06 11:30:57 +0800764 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
765 return -1;
766 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400767
you.chen35020192022-05-06 11:30:57 +0800768 CHECK_IDX(idx, CTRL_AP);
769
770 CHECK_WPA_CTRL(CTRL_AP);
771
772 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
773 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
774 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
775
you.chen6c2dd9c2022-05-16 17:55:28 +0800776 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800777 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
778 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
779 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500780
qs.xiong1af5daf2022-03-14 09:12:12 -0400781 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500782}
783
qs.xiongf1b525b2022-03-31 00:58:23 -0400784int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500785{
you.chen35020192022-05-06 11:30:57 +0800786 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400787
you.chen35020192022-05-06 11:30:57 +0800788 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400789
you.chen35020192022-05-06 11:30:57 +0800790 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
791 return -1;
792 }
793 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400794
795 return 0;
796}
797
qs.xiongf1b525b2022-03-31 00:58:23 -0400798int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
799{
you.chen35020192022-05-06 11:30:57 +0800800 CHECK_IDX(idx, CTRL_AP);
801 switch(bandwidth){
802 case LYNQ_WIFI_BANDWIDTH_HT10:
803 {
804 printf("bandwith [%d] not support now\n", bandwidth);
805 return -1;
806 }
807 case LYNQ_WIFI_BANDWIDTH_HT20:
808 {
809 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
810 system("wl down");
811 if (system(lynq_cmd_bandwith) != 0 ){
812 return -1;
813 }
814 system("wl up");
815 break;
816 }
817 case LYNQ_WIFI_BANDWIDTH_HT40:
818 {
819 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
820 sprintf(lynq_cmd_bandwith, "wl chanspec ");
821 system("wl down");
822 if (system(lynq_cmd_bandwith) != 0 ){
823 return -1;
824 }
825 system("wl up");
826 break;
827 }
828 case LYNQ_WIFI_BANDWIDTH_HT80:
829 {
830 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
831 system("wl down");
832 if (system(lynq_cmd_bandwith) != 0 ){
833 return -1;
834 }
835 system("wl up");
836 break;
837 }
838 default:
839 {
840 printf("auth type [%d] not support now\n", bandwidth);
841 return -1;
842 }
843 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400844
845
you.chen35020192022-05-06 11:30:57 +0800846 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400847}
you.chen35020192022-05-06 11:30:57 +0800848
qs.xiongf1b525b2022-03-31 00:58:23 -0400849int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
850{
you.chen35020192022-05-06 11:30:57 +0800851 int count = 0;
852 int index = 0;
853 char *split_words[128] = {0};
854 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400855
you.chen35020192022-05-06 11:30:57 +0800856 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500857
you.chen35020192022-05-06 11:30:57 +0800858 CHECK_WPA_CTRL(CTRL_AP);
859
860 DO_REQUEST(lynq_chanspec_cmd);
861
862 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
863 for(;index < count; index++) {
864 if (strncmp(split_words[index], "bw", 2) != 0) {
865 continue;
866 }
867
868 index++;
869 if (index >= count) {
870 return -1;
871 }
872
873 printf("bw %s\n", split_words[index]);
874 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
875 return 0;
876 }
877
878 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500879}
qs.xiong0fb469a2022-04-14 03:50:45 -0400880
qs.xiongf1b525b2022-03-31 00:58:23 -0400881int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500882{
you.chen35020192022-05-06 11:30:57 +0800883 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400884
you.chen35020192022-05-06 11:30:57 +0800885 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400886
you.chen35020192022-05-06 11:30:57 +0800887 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400888
you.chen35020192022-05-06 11:30:57 +0800889 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
890 return -1;
891 }
892
893 system("wl down");
894 if (system(lynq_cmd_channel) != 0 ){
895 return -1;
896 }
897 system("wl up");
898 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500899}
qs.xiong0fb469a2022-04-14 03:50:45 -0400900
qs.xiongf1b525b2022-03-31 00:58:23 -0400901int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500902{
you.chen35020192022-05-06 11:30:57 +0800903 int count = 0;
904 int index = 0;
905 char *split_words[128] = {0};
906 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400907
you.chen35020192022-05-06 11:30:57 +0800908 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400909
you.chen35020192022-05-06 11:30:57 +0800910 CHECK_WPA_CTRL(CTRL_AP);
911
912 DO_REQUEST(lynq_chanspec_cmd);
913
914 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
915 for(;index < count; index++) {
916 printf("---- %s\n",split_words[index]);
917 if (strncmp(split_words[index], "channel", 2) != 0) {
918 continue;
919 }
920
921 index++;
922 if (index >= count) {
923 return -1;
924 }
925
926 *channel = atoi(split_words[index]);
927 return 0;
928 }
929
930 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500931}
932
933
you.chen35020192022-05-06 11:30:57 +0800934int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500935{
you.chen6c2dd9c2022-05-16 17:55:28 +0800936 char ssid[MAX_CMD] = {0};
937 int freq = 0;
938 char lynq_auth_cmd[64]={0};
939 char lynq_auth_alg_cmd[64]={0};
940 char lynq_psk_cmd[64]={0};
941 char lynq_pairwise_cmd[64]={0};
942 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +0800943 CHECK_IDX(idx, CTRL_AP);
944
you.chen6c2dd9c2022-05-16 17:55:28 +0800945 CHECK_WPA_CTRL(CTRL_AP);
946
you.chen35020192022-05-06 11:30:57 +0800947 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
948 return -1;
949 }
950
you.chen6c2dd9c2022-05-16 17:55:28 +0800951 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth)) {
952 if (org_auth == auth) {
953 return 0;
954 }
955 else {
956 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
957 ssid[0] = '\0';
958 }
959 lynq_wifi_ap_frequency_get(idx, &freq);
960
961 DO_OK_FAIL_REQUEST(cmd_disconnect);
962 DO_OK_FAIL_REQUEST(cmd_remove_all);
963 if (ssid[0] != '\0') {
964 lynq_wifi_ap_ssid_set(idx, ssid);
965 }
966 if (freq != 0) {
967 lynq_wifi_ap_frequency_set(idx, freq);
968 }
969 }
970 }
you.chen35020192022-05-06 11:30:57 +0800971
qs.xiong7a105ce2022-03-02 09:43:11 -0500972 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400973 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +0800974 {
you.chen35020192022-05-06 11:30:57 +0800975 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500976
you.chen35020192022-05-06 11:30:57 +0800977 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -0500978 break;
979 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800980 case LYNQ_WIFI_AUTH_WEP:
981 {
982 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
983 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
984
985 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
986 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
987 break;
988 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400989 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +0800990 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -0500991 {
you.chen35020192022-05-06 11:30:57 +0800992 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
993 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
994 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
995 }
996 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800997 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +0800998 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +0800999 }
1000// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1001// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1002 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001003
you.chen35020192022-05-06 11:30:57 +08001004 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1005 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1006 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001007 break;
you.chen35020192022-05-06 11:30:57 +08001008 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001009 default:
you.chen35020192022-05-06 11:30:57 +08001010 {
1011 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001012 return -1;
you.chen35020192022-05-06 11:30:57 +08001013 }
1014 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001015 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001016
qs.xiong7a105ce2022-03-02 09:43:11 -05001017 return 0;
1018}
1019
you.chen35020192022-05-06 11:30:57 +08001020int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001021{
you.chen35020192022-05-06 11:30:57 +08001022 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001023 char lynq_auth_alg_str[MAX_RET] = {0};
1024 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001025
1026 CHECK_IDX(idx, CTRL_AP);
1027
1028 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1029 return -1;
1030 }
1031
1032 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001033 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1034 *auth = LYNQ_WIFI_AUTH_OPEN;
1035 }
1036 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1037 *auth = LYNQ_WIFI_AUTH_WEP;
1038 }
1039 else {
1040 *auth = LYNQ_WIFI_AUTH_OPEN;
1041 }
you.chen35020192022-05-06 11:30:57 +08001042 }
1043 else {
you.chena6fa5b22022-05-18 10:28:19 +08001044 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001045 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1046 }
1047 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1048 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1049 }
1050 else {
1051 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1052 }
you.chen35020192022-05-06 11:30:57 +08001053 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001054
you.chen6c2dd9c2022-05-16 17:55:28 +08001055 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001056}
qs.xiong1af5daf2022-03-14 09:12:12 -04001057
qs.xiong1af5daf2022-03-14 09:12:12 -04001058
qs.xiongf1b525b2022-03-31 00:58:23 -04001059int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001060{
you.chen35020192022-05-06 11:30:57 +08001061 char LYNQ_WIFI_CMD[128]={0};
1062 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1063 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001064
you.chen35020192022-05-06 11:30:57 +08001065 CHECK_IDX(idx, CTRL_AP);
1066
1067 CHECK_WPA_CTRL(CTRL_AP);
1068
1069// system("connmanctl enable wifi");
1070// system("connmanctl tether wifi on cy-test 12345678");
1071// system("ifconfig wlan0 down");
1072// system("ifconfig wlan0 up");
1073// system("ifconfig wlan0 up");
1074
1075 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1076 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1077
1078 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1079 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1080
qs.xiong7a105ce2022-03-02 09:43:11 -05001081 return 0;
1082}
1083
qs.xiongf1b525b2022-03-31 00:58:23 -04001084int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001085{
you.chen35020192022-05-06 11:30:57 +08001086 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001087}
1088
qs.xiongf1b525b2022-03-31 00:58:23 -04001089int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001090{
you.chen35020192022-05-06 11:30:57 +08001091 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001092
you.chen35020192022-05-06 11:30:57 +08001093 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001094
you.chen35020192022-05-06 11:30:57 +08001095 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001096
you.chen35020192022-05-06 11:30:57 +08001097 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1098
1099 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1100
you.chenb4b121c2022-05-06 17:50:16 +08001101// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001102
qs.xiong7a105ce2022-03-02 09:43:11 -05001103 return 0;
1104}
qs.xiong1af5daf2022-03-14 09:12:12 -04001105
qs.xiongf1b525b2022-03-31 00:58:23 -04001106int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001107{
you.chen35020192022-05-06 11:30:57 +08001108 char lynq_disable_cmd[128] = {0};
1109 char lynq_select_cmd[128] = {0};
1110 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001111
you.chen35020192022-05-06 11:30:57 +08001112 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001113
you.chen35020192022-05-06 11:30:57 +08001114 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001115
you.chen35020192022-05-06 11:30:57 +08001116 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1117 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1118
1119 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1120 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1121 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001122
1123 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001124}
1125
qs.xiongf1b525b2022-03-31 00:58:23 -04001126int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001127{
you.chen35020192022-05-06 11:30:57 +08001128 char lynq_disable_cmd[128] = {0};
1129 char lynq_select_cmd[128] = {0};
1130 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001131
you.chen35020192022-05-06 11:30:57 +08001132 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001133
you.chen35020192022-05-06 11:30:57 +08001134 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001135
you.chen35020192022-05-06 11:30:57 +08001136 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1137 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1138
1139 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1140 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1141 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001142
1143 return 0;
1144}
qs.xiongf1b525b2022-03-31 00:58:23 -04001145
you.chen35020192022-05-06 11:30:57 +08001146int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001147{
qs.xiongf1b525b2022-03-31 00:58:23 -04001148 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001149 char lynq_tmp_cmd[MAX_CMD] = {0};
1150 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001151 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001152 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001153 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001154 return -1;
you.chen35020192022-05-06 11:30:57 +08001155 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001156
you.chen35020192022-05-06 11:30:57 +08001157 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001158
you.chen6c2dd9c2022-05-16 17:55:28 +08001159 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1160 return -1;
1161 }
1162 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1163 return -1;
1164 }
1165
you.chen35020192022-05-06 11:30:57 +08001166 CHECK_WPA_CTRL(CTRL_AP);
1167
you.chen6c2dd9c2022-05-16 17:55:28 +08001168 if (auth == LYNQ_WIFI_AUTH_WEP) {
1169 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1170 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1171 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1172 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1173 }
1174 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1175 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1176 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1177 }
1178 else {
1179 return -1;
1180 }
you.chen35020192022-05-06 11:30:57 +08001181
you.chen35020192022-05-06 11:30:57 +08001182 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001183
qs.xiongf1b525b2022-03-31 00:58:23 -04001184 return 0;
1185}
1186
you.chen35020192022-05-06 11:30:57 +08001187int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001188{
you.chen35020192022-05-06 11:30:57 +08001189 FILE * fp;
1190 int len, ret;
1191 int count, index;
1192 char *split_lines[128] = {0};
1193 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001194
you.chen35020192022-05-06 11:30:57 +08001195 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001196
you.chen35020192022-05-06 11:30:57 +08001197 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1198// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1199 if (NULL == fp) {
1200 printf("open file fail\n");
1201 return -1;
1202 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001203
you.chen35020192022-05-06 11:30:57 +08001204 buff = alloca(MAX_RET);
1205 fseek(fp, 0, SEEK_SET);
1206 len = fread(buff, 1, MAX_RET, fp);
1207 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001208
you.chen35020192022-05-06 11:30:57 +08001209 for(index=0; index < len; index ++) {
1210 if (memcmp(buff + index, "network={", 9) != 0) {
1211 continue;
1212 }
1213 p = buff + index + 9;
1214 for (; index < len; index ++ ) {
1215 if (buff[index] != '}') {
1216 continue;
1217 }
1218 buff[index] = '\0';
1219 break;
1220 }
1221 len = buff + index - p;
1222 }
1223
1224 count = lynq_split(p, len, '\n', split_lines);
1225
1226 ret = -1;
1227 for(index=0; index < count; index++) {
1228 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001229 if (p != NULL) {
1230 p += 4;
1231 if (*p == '\"') {
1232 p++;
1233 }
you.chen35020192022-05-06 11:30:57 +08001234 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001235 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1236 p += 9;
1237 if (*p == '\"') {
1238 p++;
1239 }
1240 }
1241 else {
1242 continue;
you.chen35020192022-05-06 11:30:57 +08001243 }
1244
1245 strcpy(password, p);
1246
1247 while(*password != '\0') {
1248 if (*password == '\"') {
1249 *password = '\0';
1250 break;
1251 }
1252 password++;
1253 }
1254 ret = 0;
1255 break;
1256 } //end for(index=0; index < count; index++)
1257
1258 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001259}
1260
you.chen35020192022-05-06 11:30:57 +08001261static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1262 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001263 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001264
you.chen35020192022-05-06 11:30:57 +08001265 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1266 return -1;
1267 }
1268
1269 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001270
1271 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1272 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1273 if (strcmp(lynq_proto_str, "RSN") == 0) {
1274 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1275 }
1276 }
1277 }
you.chen35020192022-05-06 11:30:57 +08001278 return 0;
1279}
1280
1281int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001282{
you.chen35020192022-05-06 11:30:57 +08001283 int pass_len, net_no, count, index;
1284 char lynq_tmp_cmd[300]={0};
1285 int net_no_list[128];
1286 lynq_wifi_auth_s net_auth;
1287 pass_len=strlen(password);
1288 if(pass_len < 8 || pass_len >= 64){
1289 return -1;
1290 }
1291
1292 CHECK_IDX(idx, CTRL_STA);
1293
1294 net_no = -1;
1295 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1296
1297 for (index=0; index < count; index++) {
1298 net_auth = -1;
1299 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1300 net_no = net_no_list[index];
1301 break;
1302 }
1303 }
1304
1305 if (net_no < 0) {
1306 return -1;
1307 }
1308
1309 CHECK_WPA_CTRL(CTRL_STA);
1310
1311 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1312
1313 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1314 DO_OK_FAIL_REQUEST(cmd_save_config);
1315
1316 return 0;
1317}
1318
1319int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1320
1321 FILE * fp;
1322 int len, ret;
1323 int count, index;
1324 char *split_lines[128] = {0};
1325 char *buff, *p;
1326
1327 CHECK_IDX(idx, CTRL_STA);
1328
1329 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1330 if (NULL == fp) {
1331 printf("open file fail\n");
1332 return -1;
1333 }
1334
1335 buff = alloca(MAX_RET);
1336 fseek(fp, 0, SEEK_SET);
1337 len = fread(buff, 1, MAX_RET, fp);
1338 fclose(fp);
1339
1340 for(index=0; index < len; index ++) {
1341 for(; index < len; index ++) {
1342 if (memcmp(buff + index, "network={", 9) != 0) {
1343 continue;
1344 }
1345 p = buff + index + 9;
1346 for (; index < len; index ++ ) {
1347 if (buff[index] != '}') {
1348 continue;
1349 }
1350 buff[index] = '\0';
1351 break;
1352 }
1353 len = buff + index - p;
1354 }
1355
1356 if (strstr(p, ap->ap_ssid) != NULL) {
1357 break;
1358 }
1359 }
1360
1361 if (index >= len) {
1362 return -1;
1363 }
1364
1365 count = lynq_split(p, len, '\n', split_lines);
1366
1367 ret = -1;
1368 for(index=0; index < count; index++) {
1369 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001370 if (p != NULL) {
1371 p += 4;
1372 if (*p == '\"') {
1373 p++;
1374 }
you.chen35020192022-05-06 11:30:57 +08001375 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001376 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1377 p += 9;
1378 if (*p == '\"') {
1379 p++;
1380 }
1381 }
1382 else {
1383 continue;
you.chen35020192022-05-06 11:30:57 +08001384 }
1385
1386 strcpy(password, p);
1387
1388 while(*password != '\0') {
1389 if (*password == '\"') {
1390 *password = '\0';
1391 break;
1392 }
1393 password++;
1394 }
1395 ret = 0;
1396 break;
1397 } //end for(index=0; index < count; index++)
1398
1399 return ret;
1400}
1401
1402static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1403{
qs.xiong97fa59b2022-04-07 05:41:29 -04001404 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001405
you.chen35020192022-05-06 11:30:57 +08001406 if (sta_ssid == NULL) {
1407 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001408 return -1;
you.chen35020192022-05-06 11:30:57 +08001409 }
1410
1411 CHECK_WPA_CTRL(CTRL_STA);
1412
1413 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1414
1415 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1416// DO_OK_FAIL_REQUEST(cmd_save_config);
1417
qs.xiong7a105ce2022-03-02 09:43:11 -05001418 return 0;
1419
1420}
1421
you.chen35020192022-05-06 11:30:57 +08001422static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001423{
you.chen35020192022-05-06 11:30:57 +08001424 char lynq_disable_cmd[128]={0};
1425 char lynq_select_cmd[128]={0};
1426
1427 CHECK_WPA_CTRL(CTRL_STA);
1428
1429 if (save != 0) {
1430 DO_OK_FAIL_REQUEST(cmd_save_config);
1431 }
1432
1433 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001434// sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1435 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001436 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1437 }
1438 else {
1439 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1440 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1441 }
1442
1443 return 0;
1444}
1445
1446int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1447{
1448 CHECK_IDX(idx, CTRL_STA);
1449
you.chen6c2dd9c2022-05-16 17:55:28 +08001450 curr_status_info curr_state;
1451 ap_info_s ap_info;
1452 curr_state.ap = &ap_info;
1453 curr_state.state = NULL;
1454
1455 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1456 strcpy(sta_ssid, ap_info.ap_ssid);
1457 return 0;
1458 }
1459
1460 return -1;
you.chen35020192022-05-06 11:30:57 +08001461}
1462
1463int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1464{
1465 scan_info_s *scan_list;
1466 saved_ap_info_s *save_list;
1467 int scan_len=0;
1468 int save_len=0;
1469 int best_index = -1;
1470 int best_scan_index = -1;
1471 int best_rssi = 0;
1472 int i, j;
1473
1474 CHECK_IDX(idx, CTRL_STA);
1475 if (info == NULL) {
1476 return -1;
1477 }
1478
1479 curr_status_info curr_state;
1480 ap_info_s ap_info;
1481 curr_state.ap = &ap_info;
1482 curr_state.state = NULL;
1483
1484 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1485 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1486 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1487 lynq_get_connect_ap_rssi(idx, &info->rssi);
1488 return 0;
1489 }
1490
1491 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1492 return -1;
1493 }
1494
1495 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1496 return -1;
1497 }
1498
1499 for (i=0; i < save_len; i++) {
1500 for (j=0; j < scan_len; j++) {
1501 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1502 && save_list[i].base_info.auth == scan_list[j].auth) {
1503 if (best_rssi == 0) {
1504 best_rssi = scan_list[j].rssi;
1505 }
1506 else if (best_rssi > scan_list[j].rssi) {
1507 best_index = i;
1508 best_scan_index = j;
1509 best_rssi = scan_list[j].rssi;
1510 }
1511 break;
1512 }
1513 }
1514 }
1515
1516 if (best_index >= 0) {
1517 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1518 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1519 info->rssi = best_rssi;
1520 return 0;
1521 }
1522
1523 return -1;
1524}
1525
1526static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1527{
1528 char lynq_auth_cmd[64]={0};
1529 char lynq_ket_mgmt_cmd[64]={0};
1530 char lynq_pairwise_cmd[64]={0};
1531 char lynq_psk_cmd[64]={0};
1532
1533 CHECK_WPA_CTRL(CTRL_STA);
1534
qs.xiong1af5daf2022-03-14 09:12:12 -04001535 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001536 case LYNQ_WIFI_AUTH_OPEN:
1537 {
1538 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001539
you.chen35020192022-05-06 11:30:57 +08001540 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1541// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001542 break;
1543 }
you.chen35020192022-05-06 11:30:57 +08001544 case LYNQ_WIFI_AUTH_WPA_PSK:
1545 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001546 {
you.chen35020192022-05-06 11:30:57 +08001547 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1548 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1549 }
1550 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001551 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001552 }
1553 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1554 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001555
you.chen35020192022-05-06 11:30:57 +08001556 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1557 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1558 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001559
you.chen35020192022-05-06 11:30:57 +08001560 if (password != NULL) {
1561 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1562 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1563 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001564
you.chen35020192022-05-06 11:30:57 +08001565// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001566 break;
you.chen35020192022-05-06 11:30:57 +08001567 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001568 default:
1569 return -1;
you.chen35020192022-05-06 11:30:57 +08001570 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001571
qs.xiongf1b525b2022-03-31 00:58:23 -04001572 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001573}
qs.xiong7a105ce2022-03-02 09:43:11 -05001574
you.chen35020192022-05-06 11:30:57 +08001575static int inner_get_curr_net_no(int interface) {
1576 curr_status_info curr_state;
1577 curr_state.ap = NULL;
1578 curr_state.state = NULL;
1579
1580 if (0 != inner_get_status_info(interface, &curr_state)) {
1581 return -1;
1582 }
1583
1584 return curr_state.net_no;
1585}
1586
1587int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001588{
you.chen35020192022-05-06 11:30:57 +08001589 int net_no;
1590 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001591
you.chen35020192022-05-06 11:30:57 +08001592 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001593
you.chen35020192022-05-06 11:30:57 +08001594 if (net_no < 0) {
1595 return -1;
1596 }
1597
1598 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001599}
1600
you.chen35020192022-05-06 11:30:57 +08001601int 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 -05001602{
you.chen35020192022-05-06 11:30:57 +08001603 int count, net_no, index;
1604 int net_no_list[128];
1605 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001606
you.chen35020192022-05-06 11:30:57 +08001607 if (ssid == NULL || *ssid == '\0') {
1608 printf("bad ssid\n");
1609 return -1;
1610 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001611
you.chen35020192022-05-06 11:30:57 +08001612 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1613 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1614 printf("bad password\n");
1615 return -1;
1616 }
1617 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001618
you.chen35020192022-05-06 11:30:57 +08001619 CHECK_IDX(idx, CTRL_STA);
1620
1621 net_no = -1;
1622 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1623
1624 for (index=0; index < count; index++) {
1625 net_auth = -1;
1626 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1627 net_no = net_no_list[index];
1628 break;
1629 }
1630 }
1631
1632 if (net_no < 0) {
1633 net_no = lynq_add_network(CTRL_STA);
1634 if (net_no == -1) {
1635 return -1;
1636 }
1637
1638 printf("net no is %d\n", net_no);
1639 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1640 return -1;
1641 }
1642 }
1643
1644 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1645 return -1;
1646 }
1647
1648 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001649}
1650
you.chen35020192022-05-06 11:30:57 +08001651int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001652{
you.chen35020192022-05-06 11:30:57 +08001653 ap_info_s ap;
1654 curr_status_info curr_state;
1655 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001656
you.chen35020192022-05-06 11:30:57 +08001657 if (ssid == NULL || *ssid == '\0') {
1658 return -1;
1659 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001660
you.chen35020192022-05-06 11:30:57 +08001661 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001662
you.chen35020192022-05-06 11:30:57 +08001663 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001664 curr_state.state = NULL;
1665
you.chen35020192022-05-06 11:30:57 +08001666 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1667 return 0;
1668 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001669
you.chen35020192022-05-06 11:30:57 +08001670 if (strcmp(ap.ap_ssid, ssid) != 0) {
1671 return 0;
1672 }
1673
1674 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001675}
qs.xiong97fa59b2022-04-07 05:41:29 -04001676
you.chena6cd55a2022-05-08 12:20:18 +08001677int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1678{
1679// const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong7a105ce2022-03-02 09:43:11 -05001680
you.chen35020192022-05-06 11:30:57 +08001681 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001682 CHECK_WPA_CTRL(CTRL_STA);
1683
1684 system("connmanctl enable wifi");
1685
you.chena6fa5b22022-05-18 10:28:19 +08001686 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001687 return -1;
1688 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001689
you.chena6cd55a2022-05-08 12:20:18 +08001690// DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
1691
1692 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001693}
1694
you.chen35020192022-05-06 11:30:57 +08001695int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001696{
you.chena6cd55a2022-05-08 12:20:18 +08001697 char lynq_disable_network_cmd[MAX_CMD];
1698 curr_status_info curr_state;
1699 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001700
you.chena6cd55a2022-05-08 12:20:18 +08001701 CHECK_IDX(idx, CTRL_STA);
1702 CHECK_WPA_CTRL(CTRL_STA);
1703
1704 curr_state.ap = &ap_info;
1705 curr_state.state = NULL;
1706
1707 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1708 return 0;
1709 }
1710
1711 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1712 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1713
1714 DO_OK_FAIL_REQUEST(cmd_save_config);
1715
1716 return 0;
1717// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001718}
qs.xiong7a105ce2022-03-02 09:43:11 -05001719
you.chen35020192022-05-06 11:30:57 +08001720//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1721// int i, count;
1722// char *p;
1723// const char * FLAG_SSID = "ssid=";
1724// const char * FLAG_SBSID = "bssid=";
1725// const char * FLAG_KEY_MGMT = "key_mgmt=";
1726// const char * FLAG_FREQ = "freq=";
1727// char lynq_sta_cmd[MAX_CMD];
1728// char *split_lines[128] = {0};
1729
1730// CHECK_WPA_CTRL(CTRL_AP);
1731
1732// sprintf(lynq_sta_cmd, "STA %s", bssid);
1733
1734// DO_REQUEST(lynq_sta_cmd);
1735
1736// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1737
1738// for(i=0; i < count; i++) {
1739// p = strstr(split_lines[i], FLAG_SSID);
1740// if (p != NULL) {
1741// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1742// continue;
1743// }
1744// }
1745
1746// lynq_get_interface_ip(idx, ap->ap_ip);
1747// lynq_ap_password_set(idx, ap->psw);
1748
1749// return 0;
1750//}
1751
1752static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1753 curr_status_info curr_state;
1754 curr_state.ap = ap;
1755 curr_state.state = NULL;
1756 return inner_get_status_info(interface, &curr_state);
1757}
1758
1759int 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 -04001760{
you.chen35020192022-05-06 11:30:57 +08001761 int ip_count, index, i, line_count;
1762 const char *lynq_first_sta_cmd = "STA-FIRST";
1763 char lynq_next_sta_cmd[MAX_CMD];
1764 char *bssid[1024] = {0};
1765 char *mac_list[128] = {0};
1766 char *ip_list[128] = {0};
1767 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001768
you.chen35020192022-05-06 11:30:57 +08001769 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001770
you.chen35020192022-05-06 11:30:57 +08001771 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001772
you.chen35020192022-05-06 11:30:57 +08001773// ap_info_s * tmp_ap;
1774// device_info_s * tmp_list;
1775 if (ap == NULL || list == NULL || len == NULL) {
1776 printf("bad input param");
1777 return -1;
1778 }
1779
1780// ap = &tmp_ap;
1781// list = &tmp_list;
1782 *ap = malloc(sizeof (ap_info_s));
1783
1784 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1785 return -1;
1786 }
1787
1788 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1789 lynq_ap_password_get(idx, (*ap)->psw);
1790
1791 ip_count = get_ip_mac_list(mac_list, ip_list);
1792 printf("get count %d\n", ip_count);
1793
1794 DO_REQUEST(lynq_first_sta_cmd);
1795
1796 index = 0;
1797 while (reply_len > 0) {
1798 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1799 break;
1800 }
1801 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1802 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1803 strcpy(bssid[index], split_lines[0]);
1804 index++;
1805 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1806 reply_len = MAX_RET;
1807 cmd_reply[0] = '\0';
1808 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1809 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1810 break;
1811 }
1812 }
1813
1814 *len = index;
1815
1816 *list = malloc(sizeof(device_info_s) * (*len));
1817 for (index=0; index < *len; index++) {
1818 strcpy((*list)[index].sta_mac, bssid[index]);
1819 for(i=0;i < ip_count; i++ ) {
1820 if (strcmp(bssid[index], mac_list[i]) == 0) {
1821 strcpy((*list)[index].sta_ip, ip_list[i]);
1822 break;
1823 }
1824 }
1825 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1826 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1827 free(bssid[index]);
1828 }
1829
1830 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001831}
1832
you.chen35020192022-05-06 11:30:57 +08001833int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001834{
you.chen35020192022-05-06 11:30:57 +08001835 int i, count, index, count_words;
1836 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1837 char *split_lines[128] = {0};
1838 char *split_words[128] = {0};
1839 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001840
you.chen35020192022-05-06 11:30:57 +08001841 if (list == NULL || len == NULL) {
1842 return -1;
1843 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001844
you.chen35020192022-05-06 11:30:57 +08001845 CHECK_IDX(idx, CTRL_STA);
1846
1847 CHECK_WPA_CTRL(CTRL_STA);
1848
1849 DO_REQUEST(lynq_scan_result_cmd);
1850
1851 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1852 *len = count - 1;
1853 *list = malloc(sizeof (scan_info_s) * *len);
1854
1855 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1856 for (index=0; index <count_words; index++) {
1857 printf("----header: %s\n", split_words[index]);
1858 }
1859
1860 for(index = 1;index < count; index++) {
1861 printf("---- %s\n",split_lines[index]);
1862 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1863 if (count_words < 4)
1864 continue;
1865 printf("count: %d, %s\n", count_words, split_words[0]);
1866 //bssid / frequency / signal level / flags / ssid
1867 p = (*list) + index - 1;
1868 strcpy(p->mac, split_words[0]);
1869 p->band = convert_band_from_freq(atoi(split_words[1]));
1870 p->rssi = -1 * atoi( split_words[2]);
1871 p->auth = convert_max_auth_from_flag(split_words[3]);
1872 strcpy(p->ssid, split_words[4]);
1873 }
1874
1875 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001876}
qs.xiong97fa59b2022-04-07 05:41:29 -04001877
you.chen35020192022-05-06 11:30:57 +08001878int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1879{
1880 int count, net_no, index;
1881 int net_no_list[128];
1882 lynq_wifi_auth_s net_auth;
1883 char lynq_remove_cmd[MAX_CMD];
1884
1885 if (ssid == NULL || *ssid == '\0') {
1886 printf("bad ssid\n");
1887 return -1;
1888 }
1889
1890 CHECK_IDX(idx, CTRL_STA);
1891
1892 CHECK_WPA_CTRL(CTRL_STA);
1893
1894 net_no = -1;
1895 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1896
1897 for (index=0; index < count; index++) {
1898 net_auth = -1;
1899 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1900 net_no = net_no_list[index];
1901 break;
1902 }
1903 }
1904
1905 if (net_no < 0) {
1906 return 0;
1907 }
1908
1909 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1910
1911 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1912 DO_OK_FAIL_REQUEST(cmd_save_config);
1913
1914 return 0;
1915}
1916
1917int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1918{
1919 int count, index;
1920 int net_no_list[128];
1921 char freq[16];
1922
1923 if (list == NULL || len == NULL) {
1924 printf("bad param\n");
1925 return -1;
1926 }
1927
1928 CHECK_IDX(idx, CTRL_STA);
1929
1930// CHECK_WPA_CTRL(CTRL_STA);
1931
1932 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1933 printf("count is %d\n", count);
1934
1935 *list = malloc(sizeof (saved_ap_info_s) * count);
1936 *len = count;
1937
1938 for (index=0; index < count; index++) {
1939 printf("to get ssid %d\n", index);
1940 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1941 printf("to get bssid\n");
1942 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1943 printf("to get inner_get_network_auth\n");
1944 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1945 printf("to get frequency\n");
1946 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1947 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1948 }
1949 else {
1950 (*list)[index].base_info.band = -1;
1951 }
1952
1953 }
1954
1955 return 0;
1956}
1957
1958int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1959{
1960 const char *lynq_scan_cmd = "SCAN";
1961
1962 CHECK_IDX(idx, CTRL_STA);
1963
1964 CHECK_WPA_CTRL(CTRL_STA);
1965
1966 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1967
1968 return 0;
1969}
1970
1971int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1972 if (cb == NULL) {
1973 return -1;
1974 }
1975
1976 g_ap_callback_priv = priv;
1977 g_ap_callback_func = cb;
1978
1979 return 0;
1980}
1981
1982int lynq_unreg_ap_event_callback(void * priv) {
1983 if (g_ap_callback_priv == priv) {
1984 g_ap_callback_func = NULL;
1985 g_ap_callback_priv = NULL;
1986 return 0;
1987 }
1988 return -1;
1989}
1990
1991int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
1992 if (cb == NULL) {
1993 return -1;
1994 }
1995
1996 g_sta_callback_priv = priv;
1997 g_sta_callback_func = cb;
1998
1999 return 0;
2000}
2001
2002int lynq_unreg_sta_event_callback(void * priv) {
2003 if (g_sta_callback_priv == priv) {
2004 g_sta_callback_func = NULL;
2005 g_sta_callback_priv = NULL;
2006 return 0;
2007 }
2008 return -1;
2009}
2010
2011
2012static int inner_get_status_info_state (int interface, char *state) {
2013 curr_status_info curr_state;
2014 curr_state.ap = NULL;
2015 curr_state.state = state;
2016 return inner_get_status_info(interface, &curr_state);
2017}
2018
2019int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2020{
2021 char state[MAX_CMD];
2022 const char * STATE_COMPLETED = "COMPLETED";
2023 CHECK_IDX(idx, CTRL_AP);
2024
2025 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2026 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2027 return 0;
2028 }
2029
2030 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2031 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2032 }
2033 else {
2034 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2035 }
2036
2037 return 0;
2038}
2039
2040int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2041 char state[MAX_CMD];
2042 const char * STATE_COMPLETED = "COMPLETED";
2043 CHECK_IDX(idx, CTRL_STA);
2044
2045 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2046 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2047 return 0;
2048 }
2049
2050 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2051 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2052 }
2053 else {
2054 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2055 }
2056
2057 return 0;
2058}
2059
2060int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2061// CHECK_IDX(idx, CTRL_AP);
2062// int ret = 0;
2063// size_t reply_len = MAX_RET;
2064// char cmd_reply[MAX_RET]={0};
2065// const char * cmd_str = "GET country";
2066// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2067// do{
2068// if (NULL == s_lynq_wpa_ctrl) {
2069// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2070// if (NULL == s_lynq_wpa_ctrl ) {
2071// printf("wpa_ctrl_open fail\n");
2072// return -1;
2073// }
2074// }
2075// }while(0);
2076
2077// do {
2078// reply_len = MAX_RET;
2079// cmd_reply[0] = '\0';
2080// printf("to call [%s]\n", cmd_str);
2081// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
2082// if (ret != 0) {
2083// printf("call ##cmd_str fail %d\n", ret);
2084// return ret;
2085// }
2086// cmd_reply[reply_len+1] = '\0';
2087// printf("cmd replay [ %s ]\n", cmd_reply);
2088// }while(0);
2089
2090 FILE *fp;
2091 size_t i = 0;
2092 char lynq_cmd_ret[MAX_RET]={0};
2093
2094// CHECK_IDX(idx, CTRL_AP);
2095
2096 if((fp=popen("wl country","r"))==NULL)
2097 {
2098 perror("popen error!");
2099 return -1;
2100 }
2101 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2102 {
2103 perror("fread fail!");
2104 return -1;
2105 }
2106
2107 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2108 if (lynq_cmd_ret[i] == ' ') {
2109 lynq_cmd_ret[i] = '\0';
2110 break;
2111 }
2112 }
2113
2114 strcpy(country_code,lynq_cmd_ret);
2115 printf("---country code %s\n", country_code);
2116
2117 int ret=pclose(fp);
2118 if(ret==-1)
2119 {
2120 perror("close file faild");
2121 }
2122
2123 return 0;
2124}
2125
2126int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2127// const char * cmd_str = "GET country";
2128// CHECK_IDX(idx, CTRL_AP);
2129// CHECK_WPA_CTRL(CTRL_STA);
2130
2131// DO_REQUEST(cmd_str);
2132// printf("result %s\n", cmd_reply);
2133
2134 if (country_code == NULL || *country_code == '\0') {
2135 printf("bad country code\n");
2136 return -1;
2137 }
2138
2139 char lynq_country_cmd[MAX_CMD];
2140 sprintf(lynq_country_cmd, "wl country %s", country_code);
2141 if (system(lynq_country_cmd) == 0) {
2142 return 0;
2143 }
2144
2145 return -1;
2146}
2147
2148int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2149{
2150 if (mac == NULL) {
2151 return -1;
2152 }
2153
2154 CHECK_IDX(idx, CTRL_STA);
2155 ap_info_s ap;
2156 ap.ap_mac[0] = '\0';
2157
2158 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2159 return -1;
2160 }
2161 strcpy(mac, ap.ap_mac);
2162
2163 return 0;
2164}
2165
2166int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2167{
2168 struct ifaddrs *ifaddr, *ifa;
2169 int family, s;
2170 char host[NI_MAXHOST];
2171 const char * ifaName = "wlan0";
2172 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002173 ifaName = "tether";
you.chen35020192022-05-06 11:30:57 +08002174 }
2175 else if (idx != 0) {
2176 return -1;
2177 }
2178
2179 if (getifaddrs(&ifaddr) == -1)
2180 {
2181 perror("getifaddrs");
2182 return -1;
2183 //exit(EXIT_FAILURE);
2184 }
2185
2186
2187 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2188 {
2189 if (ifa->ifa_addr == NULL)
2190 continue;
2191 host[0] = '\0';
2192 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2193
2194 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2195 if((strcmp(ifa->ifa_name,ifaName)==0))
2196 {
2197 if (s != 0)
2198 {
2199 // printf("getnameinfo() failed: %s", gai_strerror(s));
2200 //exit(EXIT_FAILURE);
2201 }
2202 freeifaddrs(ifaddr);
2203 strcpy(ip, host);
2204 printf("ip %s\n", ip);
2205 return 0;
2206 //printf(" Interface : <%s>",ifa->ifa_name );
2207 //printf(" Address : <%s>", host);
2208 }
2209 }
2210 return -1;
2211}
2212
2213int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2214{
2215 int count;
2216 size_t i;
2217 char *split_words[128] = {0};
2218 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2219
2220 CHECK_WPA_CTRL(idx);
2221
2222 DO_REQUEST(lynq_get_mac_cmd);
2223
2224 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2225 return -1;
2226 }
2227
2228 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2229
2230 if (count < 2) {
2231 return -1;
2232 }
2233
2234 for (i=0; i < strlen(split_words[1]); i++ ) {
2235 if (split_words[1][i] != ' ') {
2236 break;
2237 }
2238 }
2239
2240 strcpy(mac, split_words[1] + i);
2241
2242 return 0;
2243}
2244
2245int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2246{
2247// int count;
2248// char *split_words[128] = {0};
2249// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2250
2251// if (rssi == NULL) {
2252// return -1;
2253// }
2254
2255// CHECK_IDX(idx, CTRL_STA);
2256
2257// CHECK_WPA_CTRL(CTRL_STA);
2258
2259// DO_REQUEST(lynq_get_rssi_cmd);
2260
2261// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2262// return -1;
2263// }
2264
2265// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2266
2267// if (count < 2) {
2268// return -1;
2269// }
2270
2271// *rssi = atoi(split_words[1]) * -1;
2272
2273 FILE *fp;
2274 size_t i = 0;
2275 char lynq_cmd_ret[MAX_RET]={0};
2276
2277// CHECK_IDX(idx, CTRL_AP);
2278
2279 if((fp=popen("wl country","r"))==NULL)
2280 {
2281 perror("popen error!");
2282 return -1;
2283 }
2284 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2285 {
2286 perror("fread fail!");
2287 return -1;
2288 }
2289 *rssi = atoi(lynq_cmd_ret);
2290
2291 return 0;
2292}
2293
2294int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2295{
2296 if (band == NULL) {
2297 return -1;
2298 }
2299
2300 CHECK_IDX(idx, CTRL_STA);
2301 ap_info_s ap;
2302 ap.band = -1;
2303
2304 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2305 return -1;
2306 }
2307 *band = ap.band;
2308
2309 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002310}