blob: 3672fc7651b73e592728a2dc010a57ef1d41167c [file] [log] [blame]
qs.xiong799dab02022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chenebcb8022022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong799dab02022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chenebcb8022022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050013#include <string.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chenebcb8022022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050024
qs.xiong799dab02022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chenebcb8022022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongd189c542022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chenebcb8022022-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;
41
42pthread_t g_sta_watcher_pid = 0;
43volatile int g_sta_watcher_stop_flag = 0;
44volatile int g_sta_scan_finish_flag = 0;
45
46void * g_ap_callback_priv = NULL;
47AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
48void * g_sta_callback_priv = NULL;
49STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
50
51//const char * CTRL_PATH="/var/run/wpa_supplicant";
52const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
53//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
54const char * cmd_list_networks = "LIST_NETWORKS";
55const char * cmd_save_config = "SAVE_CONFIG";
you.chen5f55bc42022-05-16 17:55:28 +080056const char * cmd_disconnect = "DISCONNECT";
57const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chenebcb8022022-05-06 11:30:57 +080058const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
59
60static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
61
62
63typedef struct __curr_status_info {
64 ap_info_s *ap;
65 char * state;
66 int net_no;
67}curr_status_info;
qs.xiong99b48d62022-04-07 05:41:29 -040068
69#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong99b48d62022-04-07 05:41:29 -040070{\
you.chenebcb8022022-05-06 11:30:57 +080071 perror((str));\
72 return (value);\
qs.xiong99b48d62022-04-07 05:41:29 -040073}
74
you.chenebcb8022022-05-06 11:30:57 +080075#define CHECK_IDX(idx, type) do { \
76 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
77 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
78 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
79 return -1; \
80 } \
81 }while (0)
82
83#define CHECK_WPA_CTRL(index) int ret = 0;\
84 size_t reply_len = MAX_RET; \
85 char cmd_reply[MAX_RET]={0}; \
86 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
87 do{ \
88 if (NULL == g_lynq_wpa_ctrl[index]) { \
89 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
90 if (NULL == g_lynq_wpa_ctrl[index]) { \
91 printf("wpa_ctrl_open fail\n"); \
92 return -1; \
93 } \
94 } \
95 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
96 }while(0)
97
98#define DO_REQUEST(cmd_str) do { \
99 reply_len = MAX_RET;\
100 cmd_reply[0] = '\0'; \
101 printf("to call [%s]\n", cmd_str); \
102 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
103 if (ret != 0) { \
you.chen5f55bc42022-05-16 17:55:28 +0800104 printf("call "#cmd_str" fail %d\n", ret); \
you.chenebcb8022022-05-06 11:30:57 +0800105 return ret; \
106 } \
107 cmd_reply[reply_len+1] = '\0'; \
108 printf("cmd replay [ %s ]\n", cmd_reply); \
109 }while(0)
110
111#define DO_OK_FAIL_REQUEST(cmd_str) do { \
112 DO_REQUEST(cmd_str); \
113 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chen5f55bc42022-05-16 17:55:28 +0800114 printf("cmd "#cmd_str" return FAIL\n"); \
you.chenebcb8022022-05-06 11:30:57 +0800115 return -1; \
116 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chen5f55bc42022-05-16 17:55:28 +0800117 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chenebcb8022022-05-06 11:30:57 +0800118 return -1; \
119 } \
120 }while (0)
121
122
123
124static void APWatcherThreadProc() {
125 size_t len = MAX_RET;
126 char msg_notify[MAX_RET];
127
you.chen5f55bc42022-05-16 17:55:28 +0800128 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chenebcb8022022-05-06 11:30:57 +0800129
130 while (g_ap_watcher_stop_flag == 0) {
you.chen5f55bc42022-05-16 17:55:28 +0800131 if (lynq_wpa_ctrl == NULL) {
132 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
133 if (lynq_wpa_ctrl == NULL) {
134 usleep(100*1000);
135 continue;
136 }
137
138 wpa_ctrl_attach(lynq_wpa_ctrl);
139 }
140
you.chenebcb8022022-05-06 11:30:57 +0800141 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
142 usleep(100*1000);
143 continue;
144 }
you.chen5f55bc42022-05-16 17:55:28 +0800145 memset(msg_notify, 0, MAX_RET);
146 len = MAX_RET;
you.chenebcb8022022-05-06 11:30:57 +0800147 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
148 msg_notify[len+1] = '\0';
149 printf("ap------> %s\n", msg_notify);
150 if (g_ap_callback_func == NULL) {
151 continue;
152 }
153 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
154 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
155 }
156 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
157 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
158 }
159 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
160 } // end while (g_ap_watcher_stop_flag == 0)
161 wpa_ctrl_detach(lynq_wpa_ctrl);
162 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiong99b48d62022-04-07 05:41:29 -0400163}
164
you.chenebcb8022022-05-06 11:30:57 +0800165static void STAWatcherThreadProc() {
166 size_t len = MAX_RET;
167 char msg_notify[MAX_RET];
168 char *pReason;
169 error_number_s error;
qs.xiongd189c542022-03-31 00:58:23 -0400170
you.chen5f55bc42022-05-16 17:55:28 +0800171 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chenebcb8022022-05-06 11:30:57 +0800172
173 while (g_sta_watcher_stop_flag == 0) {
you.chen5f55bc42022-05-16 17:55:28 +0800174 if (lynq_wpa_ctrl == NULL) {
175 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
176 if (lynq_wpa_ctrl == NULL) {
177 usleep(100*1000);
178 continue;
179 }
180
181 wpa_ctrl_attach(lynq_wpa_ctrl);
182 }
183
you.chenebcb8022022-05-06 11:30:57 +0800184 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
185 usleep(100*1000);
186 continue;
187 }
you.chen5f55bc42022-05-16 17:55:28 +0800188 memset(msg_notify, 0, MAX_RET);
189 len = MAX_RET;
you.chenebcb8022022-05-06 11:30:57 +0800190 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
191 msg_notify[len+1] = '\0';
192 printf("sta ------> %s\n", msg_notify);
193 if (strstr(msg_notify, state_scan_result) != NULL) {
194 g_sta_scan_finish_flag = 1;
195 }
196
197 if (g_sta_callback_func == NULL) {
198 continue;
199 }
200 error = -1;
201 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
202 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
203 }
204 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
205 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
206 }
207 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
208 pReason = strstr(msg_notify, "reason=");
209 if (pReason != NULL) {
210 pReason += strlen("reason=");
211 if (memcpy(pReason, "CONN_FAILED", 11) == 0) {
212 error = LYNQ_TIME_OUT;
213 }
214 else if (memcpy(pReason, "WRONG_KEY", 9) == 0) {
215 error = LYNQ_PSW_ERROR;
216 }
217 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
218 }
219 }
220 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
221 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
222 }
223 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
224 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
225 }
226 }
227 }
228 wpa_ctrl_detach(lynq_wpa_ctrl);
229 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiongd189c542022-03-31 00:58:23 -0400230}
231
qs.xiong799dab02022-03-14 09:12:12 -0400232int lynq_wifi_enable(void)
233{
you.chenebcb8022022-05-06 11:30:57 +0800234 int ret = 0;
you.chen5f55bc42022-05-16 17:55:28 +0800235 int i;
you.chenebcb8022022-05-06 11:30:57 +0800236 const char * cmd_check_service =
237 "state=`systemctl is-active wg870_drv_insmod.service`\n"
238 "[ \"\"$state == \"active\" ] && exit 0\n"
239 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
240// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
241// return 0;
242// }
qs.xiong799dab02022-03-14 09:12:12 -0400243
you.chenebcb8022022-05-06 11:30:57 +0800244 ret = system(cmd_check_service);
245 if (ret != 0) {
you.chen5f55bc42022-05-16 17:55:28 +0800246 printf("service state %d\n", ret);
you.chenebcb8022022-05-06 11:30:57 +0800247 return -1;
248 }
249
you.chen5f55bc42022-05-16 17:55:28 +0800250 for (i=0; i<10; i++) {
251 if (system("connmanctl technologies | grep \"/net/connman/technology/wifi\"") == 0) {
252 break;
253 }
254 usleep(300*1000);
255 }
256
257 if (i >= 10) {
258 return -1;
259 }
260
261 if (0 != system("ifconfig | grep ap0")) {
262 printf("enable wifi\n");
263 system("connmanctl enable wifi");
264 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
265 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
266 }
267
you.chenebcb8022022-05-06 11:30:57 +0800268 if (g_ap_watcher_pid == 0 ) {
269 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
270 if(ret<0){
271 return -1;
272 }
273 }
274
275 if (g_sta_watcher_pid == 0 ) {
276 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
277 if(ret<0){
278 return -1;
279 }
280 }
281
282 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -0500283}
284
qs.xiong799dab02022-03-14 09:12:12 -0400285int lynq_wifi_disable(void)
286{
you.chenebcb8022022-05-06 11:30:57 +0800287 g_ap_watcher_stop_flag = 1;
288 g_sta_watcher_stop_flag = 1;
289 if (g_ap_watcher_pid != 0)
290 pthread_join(g_ap_watcher_pid, NULL);
291 if (g_sta_watcher_pid != 0)
292 pthread_join(g_sta_watcher_pid, NULL);
293 if (g_lynq_wpa_ctrl[0] != NULL)
294 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
295 if (g_lynq_wpa_ctrl[1] != NULL)
296 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
297 g_ap_watcher_pid = 0;
298 g_sta_watcher_pid = 0;
299 g_lynq_wpa_ctrl[0] = NULL;
300 g_lynq_wpa_ctrl[1] = NULL;
301 system("systemctl stop wg870_drv_insmod.service");
qs.xiong8d42bb92022-03-02 09:43:11 -0500302 return 0;
303}
qs.xiong799dab02022-03-14 09:12:12 -0400304
you.chenebcb8022022-05-06 11:30:57 +0800305static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
306
307 char lynq_cmd_get[128]={0};
308
309 if (out_put == NULL) {
310 printf("output ptr is null\n");
311 return -1;
312 }
313 if (param_name == NULL) {
314 printf("param ptr is null");
315 return -1;
316 }
317 if (param_name[0] == '\0') {
318 printf("param is empty");
319 return -1;
320 }
321
322 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
323
324 CHECK_WPA_CTRL(interface);
325
326 DO_REQUEST(lynq_cmd_get);
327
328 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
329 return -1;
330 }
331
332 printf("reply len %d, %08x\n", reply_len, (int)out_put);
333 memcpy(out_put, cmd_reply, reply_len + 1);
334 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500335}
qs.xiong799dab02022-03-14 09:12:12 -0400336
you.chenebcb8022022-05-06 11:30:57 +0800337static int lynq_split(char * str, int len, char delimiter, char * results[]) {
338 int ret = 0;
339 char * end = str + len - 1;
340 results[ret++] = str;
341 while(str < end) {
342 if (*str == delimiter) {
343 *str++ = '\0';
344 results[ret++] = str;
345 continue;
346 }
347 str++;
348 }
349 if (*str == delimiter) {
350 *str = '\0';
351 }
qs.xiong799dab02022-03-14 09:12:12 -0400352
you.chenebcb8022022-05-06 11:30:57 +0800353 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -0500354}
355
you.chenebcb8022022-05-06 11:30:57 +0800356static void trim_space(char * p, int count) {
357 char * begin = p;
358 p += count;
359 printf("%C-%C||\n", *begin, *p);
360 while (p >= begin ) {
361 if (*p == ' ') {
362 *p-- = '\0';
363 }
364 else {
365 break;
366 }
367 }
368}
369
370static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
371 FILE * fp;
372 int len, ret;
373 int count, count_words, index;
374 int mac_start, mac_end;
375 int ip_start, ip_end;
376 char *split_lines[128] = {0};
377 char *buff;
378 const char * ip_header = "IP address";
379 const char * mac_header = "HW address";
380 const char * zero_mac = "00:00:00:00:00:00";
381
382 fp = fopen("/proc/net/arp", "rb");
383 if (NULL == fp) {
384 printf("open file fail\n");
385 return -1;
386 }
387
388 buff = alloca(MAX_RET);
389 fseek(fp, 0, SEEK_SET);
390 len = fread(buff, 1, MAX_RET, fp);
391 fclose(fp);
392 if (len <= 0) {
393 printf("read file fail\n");
394 return -1;
395 }
396 printf("file : %s\n", buff);
397
398 count = lynq_split(buff, len, '\n', split_lines);
399 printf("----- %s\n", split_lines[0]);
400
401 mac_end = 0;
402 count_words = strlen(split_lines[0]);
403 if (strstr(split_lines[0], mac_header) != NULL) {
404 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
405 mac_end = mac_start + strlen(mac_header) + 1;
406 while (mac_end < count_words) {
407 if (split_lines[0][mac_end] != ' ') {
408 break;
409 }
410 mac_end++;
411 }
412 }
413
414 ip_end = 0;
415 if (strstr(split_lines[0], ip_header) != NULL) {
416 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
417 ip_end = ip_start + strlen(ip_header) + 1;
418 while (ip_end < count_words) {
419 if (split_lines[0][ip_end] != ' ') {
420 break;
421 }
422 ip_end++;
423 }
424 }
425
426 if (mac_end == 0 || ip_end == 0) {
427 return 0;
428 }
429
430 ret = 0;
431 for(index = 1;index < count; index++) {
432 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
433 continue;
434 }
435 mac_list[ret] = malloc(mac_end - mac_start + 1);
436 ip_list[ret] = malloc(ip_end - ip_start + 1);
437 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
438 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
439 trim_space(mac_list[ret], mac_end - mac_start - 1);
440 trim_space(ip_list[ret], ip_end - ip_start - 1);
441 ret++;
442 }
443
444 return ret;
445}
446
447static int get_hostname_by_ip(char *ip, char *hostname) {
448 struct in_addr addr ={0};
449 struct hostent *ht;
450
451 if (ip == NULL || *ip == '\0' || hostname == NULL) {
452 return -1;
453 }
454
455 if (inet_aton(ip, &addr) == 0) {
456 printf("---inet_aton fail\n");
457 return -1;
458 }
459
460 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
461
462 if (ht == NULL) {
463 printf("---gethostbyaddr fail\n");
464 herror(NULL);
465 return -1;
466 }
467
468 strcpy(hostname, ht->h_name);
469
470 return 0;
471}
472
473static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
474{
475 int count, index, words_count;
476 char * split_lines[128]= {0};
477 char * split_words[128] = {0};
478 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
479
480 CHECK_WPA_CTRL(ap_sta);
481
482 DO_REQUEST(lynq_wifi_list_networks);
483
484 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
485
486 //@todo check ssid field to compatible
487
488 ret = 0;
489 for(index=1; index < count; index++) {
490 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
491 if (words_count > 2) {
492 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
493 net_no_list[ret++] = atoi(split_words[0]);
494 }
495 }
496 }
497
498 return ret;
499}
500
501static int lynq_add_network(int ap_sta) {
you.chen5f55bc42022-05-16 17:55:28 +0800502 size_t i=0;
you.chenebcb8022022-05-06 11:30:57 +0800503 CHECK_WPA_CTRL(ap_sta);
504 const char *lynq_wifi_add_network = "ADD_NETWORK";
505
506 DO_REQUEST(lynq_wifi_add_network);
you.chen5f55bc42022-05-16 17:55:28 +0800507 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chenebcb8022022-05-06 11:30:57 +0800508 return -1;
509 }
510
you.chen5f55bc42022-05-16 17:55:28 +0800511 for(i=0;i<reply_len;i++) {
you.chenebcb8022022-05-06 11:30:57 +0800512 if(cmd_reply[i] == '\n') {
513 cmd_reply[i] = '\0';
514 break;
515 }
516 }
517 return atoi(cmd_reply);
518}
you.chen5f55bc42022-05-16 17:55:28 +0800519
you.chenebcb8022022-05-06 11:30:57 +0800520static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
521{
522 int count, index;
523 int net_no_list[128];
524
525
526 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
527 for (index=0; index < count; index++) {
528 if (net_no_list[index] == net_no) {
529 return 0;
530 }
531 }
532
533 if (count >= 1)
534 index = net_no_list[count - 1];
535 else
536 index = -1;
537
538 while (index < net_no ) {
539 index = lynq_add_network(ap_sta);
540 if (index >= net_no) { // required network no created
541 return 0;
542 }
you.chen5f55bc42022-05-16 17:55:28 +0800543 else if( index < 0) {
544 printf("add network fail\n");
545 return -1;
546 }
you.chenebcb8022022-05-06 11:30:57 +0800547 }
548
549 if (index < 0)
550 return -1;
551
552 return 0;
553}
554
555static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
556 if (freq > 5000 && freq < 6000) {
557 return LYNQ_WIFI_5G_band;
558 }
559 else if (freq > 2000 && freq < 3000) {
560 return LYNQ_WIFI_2G_band;
561 }
562 return LYNQ_WIFI_2_and_5G_band;
563}
564
565static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
566 if (key_mgmt != NULL) {
567 if (memcmp( key_mgmt, "NONE", 4) == 0) {
568 return LYNQ_WIFI_AUTH_OPEN;
569 }
570 else if (memcmp( key_mgmt, "WEP", 3) == 0){
571 return LYNQ_WIFI_AUTH_WEP;
572 }
573 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
574 return LYNQ_WIFI_AUTH_WPA_PSK;
575 }
576 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
577 return LYNQ_WIFI_AUTH_WPA2_PSK;
578 }
579 }
580
581 return -1;
582}
583
584static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
585 if (flag != NULL) {
586 if (strstr( flag, "WPA2-PSK") != NULL){
587 return LYNQ_WIFI_AUTH_WPA2_PSK;
588 }
589 else if (strstr( flag, "WPA-PSK") != NULL){
590 return LYNQ_WIFI_AUTH_WPA_PSK;
591 }
592 else if (strstr( flag, "WEP") != NULL){
593 return LYNQ_WIFI_AUTH_WEP;
594 }
595 else if (strstr( flag, "NONE") != NULL) {
596 return LYNQ_WIFI_AUTH_OPEN;
597 }
598 }
599
600 return -1;
601}
602
603static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
604 switch (bw) {
605 case 10:
606 return LYNQ_WIFI_BANDWIDTH_HT10;
607 break;
608 case 20:
609 return LYNQ_WIFI_BANDWIDTH_HT20;
610 break;
611 case 40:
612 return LYNQ_WIFI_BANDWIDTH_HT40;
613 break;
614 case 80:
615 return LYNQ_WIFI_BANDWIDTH_HT80;
616 break;
617 default:
618 break;
619 }
620
621 return -1;
622}
623
624static int inner_get_status_info(int interface, curr_status_info *curr_state) {
625 int i, count;
626 char *p;
627 const char *lynq_status_cmd = "STATUS";
628 const char * FLAG_SSID = "ssid=";
629 const char * FLAG_SBSID = "bssid=";
630 const char * FLAG_KEY_MGMT = "key_mgmt=";
631 const char * FLAG_FREQ = "freq=";
632 const char * FLAG_STATE = "wpa_state=";
633 const char * FLAG_ID = "id=";
634 char *split_lines[128] = {0};
635
636 CHECK_WPA_CTRL(interface);
637
638 if (curr_state == NULL) {
639 return -1;
640 }
641
642 DO_REQUEST(lynq_status_cmd);
643
644 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
645
646 curr_state->net_no = -1;
647 ret = -1;
648 for(i=0; i < count; i++) {
649 if (curr_state->ap != NULL) {
650 p = strstr(split_lines[i], FLAG_SSID);
651 if (p != NULL) {
652 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
653 ret = 0;
654 continue;
655 }
656 p = strstr(split_lines[i], FLAG_SBSID);
657 if (p != NULL) {
658 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
659 ret = 0;
660 continue;
661 }
662 p = strstr(split_lines[i], FLAG_KEY_MGMT);
663 if (p != NULL) {
664 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
665 ret = 0;
666 continue;
667 }
668 p = strstr(split_lines[i], FLAG_FREQ);
669 if (p != NULL) {
670 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
671 ret = 0;
672 continue;
673 }
674 } // end if (ap != NULL)
675 if (curr_state->state != NULL) {
676 p = strstr(split_lines[i], FLAG_STATE);
677 if (p != NULL) {
678 strcpy(curr_state->state, p + strlen(FLAG_STATE));
679 ret = 0;
680 continue;
681 }
682
683 } //end else if (state != NULL)
you.chen5f55bc42022-05-16 17:55:28 +0800684 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chenebcb8022022-05-06 11:30:57 +0800685 ret = 0;
686 curr_state->net_no = atoi(p);
687 printf("net_no %d, -- %s\n", curr_state->net_no, p);
688 }
689 }
690
691 return ret;
692}
693
694
qs.xiongd189c542022-03-31 00:58:23 -0400695int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -0500696{
you.chenebcb8022022-05-06 11:30:57 +0800697 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong8d42bb92022-03-02 09:43:11 -0500698
you.chenebcb8022022-05-06 11:30:57 +0800699 if (ap_ssid == NULL) {
700 printf("ap_ssid is null\n");
701 return -1;
702 }
703 else {
704 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
705 }
qs.xiong799dab02022-03-14 09:12:12 -0400706
you.chenebcb8022022-05-06 11:30:57 +0800707 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
708 return -1;
709 }
qs.xiong799dab02022-03-14 09:12:12 -0400710
you.chenebcb8022022-05-06 11:30:57 +0800711 CHECK_IDX(idx, CTRL_AP);
712
713 CHECK_WPA_CTRL(CTRL_AP);
714
715 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
716
717 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
718 DO_OK_FAIL_REQUEST(cmd_save_config);
719
qs.xiong8d42bb92022-03-02 09:43:11 -0500720 return 0;
you.chenebcb8022022-05-06 11:30:57 +0800721
qs.xiong8d42bb92022-03-02 09:43:11 -0500722}
723
you.chenebcb8022022-05-06 11:30:57 +0800724int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -0500725{
you.chen5f55bc42022-05-16 17:55:28 +0800726 int len;
you.chenebcb8022022-05-06 11:30:57 +0800727 CHECK_IDX(idx, CTRL_AP);
you.chen5f55bc42022-05-16 17:55:28 +0800728 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
729 return -1;
730 len = strlen(ap_ssid);
731 if (ap_ssid[0] == '\"') {
732 memmove(ap_ssid, ap_ssid + 1, len - 1);
733 len -= 1;
734 }
735 if (len > 0 && ap_ssid[len-1] == '\"') {
736 ap_ssid[len-1] = '\0';
737 }
738 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500739}
740
qs.xiongd189c542022-03-31 00:58:23 -0400741int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -0500742{
you.chenebcb8022022-05-06 11:30:57 +0800743 char lynq_wifi_frequency_cmd[128]={0};
744 char lynq_cmd_mode[128]={0};
745 char lynq_cmd_slect[128]={0};
qs.xiong8d42bb92022-03-02 09:43:11 -0500746
you.chenebcb8022022-05-06 11:30:57 +0800747 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
748 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
749 }
qs.xiong99b48d62022-04-07 05:41:29 -0400750
you.chenebcb8022022-05-06 11:30:57 +0800751 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
752 return -1;
753 }
qs.xiong799dab02022-03-14 09:12:12 -0400754
you.chenebcb8022022-05-06 11:30:57 +0800755 CHECK_IDX(idx, CTRL_AP);
756
757 CHECK_WPA_CTRL(CTRL_AP);
758
759 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
760 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
761 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
762
you.chen5f55bc42022-05-16 17:55:28 +0800763 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chenebcb8022022-05-06 11:30:57 +0800764 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
765 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
766 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -0500767
qs.xiong799dab02022-03-14 09:12:12 -0400768 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500769}
770
qs.xiongd189c542022-03-31 00:58:23 -0400771int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -0500772{
you.chenebcb8022022-05-06 11:30:57 +0800773 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -0400774
you.chenebcb8022022-05-06 11:30:57 +0800775 CHECK_IDX(idx, CTRL_AP);
qs.xiongd189c542022-03-31 00:58:23 -0400776
you.chenebcb8022022-05-06 11:30:57 +0800777 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
778 return -1;
779 }
780 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongd189c542022-03-31 00:58:23 -0400781
782 return 0;
783}
784
qs.xiongd189c542022-03-31 00:58:23 -0400785int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
786{
you.chenebcb8022022-05-06 11:30:57 +0800787 CHECK_IDX(idx, CTRL_AP);
788 switch(bandwidth){
789 case LYNQ_WIFI_BANDWIDTH_HT10:
790 {
791 printf("bandwith [%d] not support now\n", bandwidth);
792 return -1;
793 }
794 case LYNQ_WIFI_BANDWIDTH_HT20:
795 {
796 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
797 system("wl down");
798 if (system(lynq_cmd_bandwith) != 0 ){
799 return -1;
800 }
801 system("wl up");
802 break;
803 }
804 case LYNQ_WIFI_BANDWIDTH_HT40:
805 {
806 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
807 sprintf(lynq_cmd_bandwith, "wl chanspec ");
808 system("wl down");
809 if (system(lynq_cmd_bandwith) != 0 ){
810 return -1;
811 }
812 system("wl up");
813 break;
814 }
815 case LYNQ_WIFI_BANDWIDTH_HT80:
816 {
817 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
818 system("wl down");
819 if (system(lynq_cmd_bandwith) != 0 ){
820 return -1;
821 }
822 system("wl up");
823 break;
824 }
825 default:
826 {
827 printf("auth type [%d] not support now\n", bandwidth);
828 return -1;
829 }
830 }
qs.xiongd189c542022-03-31 00:58:23 -0400831
832
you.chenebcb8022022-05-06 11:30:57 +0800833 return 0;
qs.xiongd189c542022-03-31 00:58:23 -0400834}
you.chenebcb8022022-05-06 11:30:57 +0800835
qs.xiongd189c542022-03-31 00:58:23 -0400836int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
837{
you.chenebcb8022022-05-06 11:30:57 +0800838 int count = 0;
839 int index = 0;
840 char *split_words[128] = {0};
841 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongd189c542022-03-31 00:58:23 -0400842
you.chenebcb8022022-05-06 11:30:57 +0800843 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -0500844
you.chenebcb8022022-05-06 11:30:57 +0800845 CHECK_WPA_CTRL(CTRL_AP);
846
847 DO_REQUEST(lynq_chanspec_cmd);
848
849 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
850 for(;index < count; index++) {
851 if (strncmp(split_words[index], "bw", 2) != 0) {
852 continue;
853 }
854
855 index++;
856 if (index >= count) {
857 return -1;
858 }
859
860 printf("bw %s\n", split_words[index]);
861 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
862 return 0;
863 }
864
865 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -0500866}
qs.xiongd2689cd2022-04-13 20:46:56 -0400867
qs.xiongd189c542022-03-31 00:58:23 -0400868int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong8d42bb92022-03-02 09:43:11 -0500869{
you.chenebcb8022022-05-06 11:30:57 +0800870 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong799dab02022-03-14 09:12:12 -0400871
you.chenebcb8022022-05-06 11:30:57 +0800872 CHECK_IDX(idx, CTRL_AP);
873
874 sprintf(lynq_cmd_channel, "wl channel %d", channel);
875
876 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
877 return -1;
878 }
879
880 system("wl down");
881 if (system(lynq_cmd_channel) != 0 ){
882 return -1;
883 }
884 system("wl up");
885 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -0500886}
qs.xiongd2689cd2022-04-13 20:46:56 -0400887
qs.xiongd189c542022-03-31 00:58:23 -0400888int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong8d42bb92022-03-02 09:43:11 -0500889{
you.chenebcb8022022-05-06 11:30:57 +0800890 int count = 0;
891 int index = 0;
892 char *split_words[128] = {0};
893 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongd189c542022-03-31 00:58:23 -0400894
you.chenebcb8022022-05-06 11:30:57 +0800895 CHECK_IDX(idx, CTRL_AP);
qs.xiong799dab02022-03-14 09:12:12 -0400896
you.chenebcb8022022-05-06 11:30:57 +0800897 CHECK_WPA_CTRL(CTRL_AP);
898
899 DO_REQUEST(lynq_chanspec_cmd);
900
901 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
902 for(;index < count; index++) {
903 printf("---- %s\n",split_words[index]);
904 if (strncmp(split_words[index], "channel", 2) != 0) {
905 continue;
906 }
907
908 index++;
909 if (index >= count) {
910 return -1;
911 }
912
913 *channel = atoi(split_words[index]);
914 return 0;
915 }
916
917 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -0500918}
919
920
you.chenebcb8022022-05-06 11:30:57 +0800921int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong8d42bb92022-03-02 09:43:11 -0500922{
you.chen5f55bc42022-05-16 17:55:28 +0800923 char ssid[MAX_CMD] = {0};
924 int freq = 0;
925 char lynq_auth_cmd[64]={0};
926 char lynq_auth_alg_cmd[64]={0};
927 char lynq_psk_cmd[64]={0};
928 char lynq_pairwise_cmd[64]={0};
929 lynq_wifi_auth_s org_auth;
you.chenebcb8022022-05-06 11:30:57 +0800930 CHECK_IDX(idx, CTRL_AP);
931
you.chen5f55bc42022-05-16 17:55:28 +0800932 CHECK_WPA_CTRL(CTRL_AP);
933
you.chenebcb8022022-05-06 11:30:57 +0800934 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
935 return -1;
936 }
937
you.chen5f55bc42022-05-16 17:55:28 +0800938 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth)) {
939 if (org_auth == auth) {
940 return 0;
941 }
942 else {
943 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
944 ssid[0] = '\0';
945 }
946 lynq_wifi_ap_frequency_get(idx, &freq);
947
948 DO_OK_FAIL_REQUEST(cmd_disconnect);
949 DO_OK_FAIL_REQUEST(cmd_remove_all);
950 if (ssid[0] != '\0') {
951 lynq_wifi_ap_ssid_set(idx, ssid);
952 }
953 if (freq != 0) {
954 lynq_wifi_ap_frequency_set(idx, freq);
955 }
956 }
957 }
you.chenebcb8022022-05-06 11:30:57 +0800958
qs.xiong8d42bb92022-03-02 09:43:11 -0500959 switch(auth){
qs.xiongd189c542022-03-31 00:58:23 -0400960 case LYNQ_WIFI_AUTH_OPEN:
you.chen5f55bc42022-05-16 17:55:28 +0800961 {
you.chenebcb8022022-05-06 11:30:57 +0800962 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
qs.xiong8d42bb92022-03-02 09:43:11 -0500963
you.chenebcb8022022-05-06 11:30:57 +0800964 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -0500965 break;
966 }
you.chen5f55bc42022-05-16 17:55:28 +0800967 case LYNQ_WIFI_AUTH_WEP:
968 {
969 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
970 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
971
972 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
973 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
974 break;
975 }
qs.xiongd189c542022-03-31 00:58:23 -0400976 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chenebcb8022022-05-06 11:30:57 +0800977 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong8d42bb92022-03-02 09:43:11 -0500978 {
you.chenebcb8022022-05-06 11:30:57 +0800979 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
980 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
981 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
982 }
983 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen5f55bc42022-05-16 17:55:28 +0800984 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
985 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chenebcb8022022-05-06 11:30:57 +0800986 }
987// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
988// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
989 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong8d42bb92022-03-02 09:43:11 -0500990
you.chenebcb8022022-05-06 11:30:57 +0800991 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
992 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
993 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -0500994 break;
you.chenebcb8022022-05-06 11:30:57 +0800995 }
qs.xiong8d42bb92022-03-02 09:43:11 -0500996 default:
you.chenebcb8022022-05-06 11:30:57 +0800997 {
998 printf("auth type [%d] not support now\n", auth);
qs.xiong8d42bb92022-03-02 09:43:11 -0500999 return -1;
you.chenebcb8022022-05-06 11:30:57 +08001000 }
1001 }
you.chen5f55bc42022-05-16 17:55:28 +08001002 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -05001003
qs.xiong8d42bb92022-03-02 09:43:11 -05001004 return 0;
1005}
1006
you.chenebcb8022022-05-06 11:30:57 +08001007int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001008{
you.chenebcb8022022-05-06 11:30:57 +08001009 char lynq_auth_str[MAX_RET] = {0};
you.chen5f55bc42022-05-16 17:55:28 +08001010 char lynq_auth_alg_str[MAX_RET] = {0};
1011 char lynq_proto_str[MAX_RET] = {0};
you.chenebcb8022022-05-06 11:30:57 +08001012
1013 CHECK_IDX(idx, CTRL_AP);
1014
1015 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1016 return -1;
1017 }
1018
1019 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen5f55bc42022-05-16 17:55:28 +08001020 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1021 *auth = LYNQ_WIFI_AUTH_OPEN;
1022 }
1023 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1024 *auth = LYNQ_WIFI_AUTH_WEP;
1025 }
1026 else {
1027 *auth = LYNQ_WIFI_AUTH_OPEN;
1028 }
you.chenebcb8022022-05-06 11:30:57 +08001029 }
1030 else {
you.chen5f55bc42022-05-16 17:55:28 +08001031 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_proto_str) != 0) {
1032 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1033 }
1034 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1035 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1036 }
1037 else {
1038 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1039 }
you.chenebcb8022022-05-06 11:30:57 +08001040 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001041
you.chen5f55bc42022-05-16 17:55:28 +08001042 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001043}
qs.xiong799dab02022-03-14 09:12:12 -04001044
qs.xiong799dab02022-03-14 09:12:12 -04001045
qs.xiong8d42bb92022-03-02 09:43:11 -05001046
qs.xiongd189c542022-03-31 00:58:23 -04001047int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001048{
you.chenebcb8022022-05-06 11:30:57 +08001049 char LYNQ_WIFI_CMD[128]={0};
1050 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1051 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongd189c542022-03-31 00:58:23 -04001052
you.chenebcb8022022-05-06 11:30:57 +08001053 CHECK_IDX(idx, CTRL_AP);
1054
1055 CHECK_WPA_CTRL(CTRL_AP);
1056
1057// system("connmanctl enable wifi");
1058// system("connmanctl tether wifi on cy-test 12345678");
1059// system("ifconfig wlan0 down");
1060// system("ifconfig wlan0 up");
1061// system("ifconfig wlan0 up");
1062
1063 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1064 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1065
1066 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1067 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1068
qs.xiong8d42bb92022-03-02 09:43:11 -05001069 return 0;
1070}
1071
qs.xiongd189c542022-03-31 00:58:23 -04001072int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001073{
you.chenebcb8022022-05-06 11:30:57 +08001074 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong8d42bb92022-03-02 09:43:11 -05001075}
1076
qs.xiongd189c542022-03-31 00:58:23 -04001077int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001078{
you.chenebcb8022022-05-06 11:30:57 +08001079 char LYNQ_WIFI_CMD[128]={0};
qs.xiong799dab02022-03-14 09:12:12 -04001080
you.chenebcb8022022-05-06 11:30:57 +08001081 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001082
you.chenebcb8022022-05-06 11:30:57 +08001083 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001084
you.chenebcb8022022-05-06 11:30:57 +08001085 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1086
1087 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1088
you.chenc00d5582022-05-06 17:50:16 +08001089// system("connmanctl tether wifi off");
you.chenebcb8022022-05-06 11:30:57 +08001090
qs.xiong8d42bb92022-03-02 09:43:11 -05001091 return 0;
1092}
qs.xiong799dab02022-03-14 09:12:12 -04001093
qs.xiongd189c542022-03-31 00:58:23 -04001094int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001095{
you.chenebcb8022022-05-06 11:30:57 +08001096 char lynq_disable_cmd[128] = {0};
1097 char lynq_select_cmd[128] = {0};
1098 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong99b48d62022-04-07 05:41:29 -04001099
you.chenebcb8022022-05-06 11:30:57 +08001100 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001101
you.chenebcb8022022-05-06 11:30:57 +08001102 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001103
you.chenebcb8022022-05-06 11:30:57 +08001104 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1105 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1106
1107 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1108 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1109 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong799dab02022-03-14 09:12:12 -04001110
1111 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001112}
1113
qs.xiongd189c542022-03-31 00:58:23 -04001114int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001115{
you.chenebcb8022022-05-06 11:30:57 +08001116 char lynq_disable_cmd[128] = {0};
1117 char lynq_select_cmd[128] = {0};
1118 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongd189c542022-03-31 00:58:23 -04001119
you.chenebcb8022022-05-06 11:30:57 +08001120 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001121
you.chenebcb8022022-05-06 11:30:57 +08001122 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001123
you.chenebcb8022022-05-06 11:30:57 +08001124 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1125 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1126
1127 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1128 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1129 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -05001130
1131 return 0;
1132}
qs.xiongd189c542022-03-31 00:58:23 -04001133
you.chenebcb8022022-05-06 11:30:57 +08001134int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05001135{
qs.xiongd189c542022-03-31 00:58:23 -04001136 int pass_len;
you.chen5f55bc42022-05-16 17:55:28 +08001137 char lynq_tmp_cmd[MAX_CMD] = {0};
1138 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongd189c542022-03-31 00:58:23 -04001139 pass_len=strlen(password);
you.chen5f55bc42022-05-16 17:55:28 +08001140 lynq_wifi_auth_s auth = -1;
you.chenebcb8022022-05-06 11:30:57 +08001141 if(pass_len < 8 || pass_len >= 64){
qs.xiongd189c542022-03-31 00:58:23 -04001142 return -1;
you.chenebcb8022022-05-06 11:30:57 +08001143 }
qs.xiongd189c542022-03-31 00:58:23 -04001144
you.chenebcb8022022-05-06 11:30:57 +08001145 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001146
you.chen5f55bc42022-05-16 17:55:28 +08001147 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1148 return -1;
1149 }
1150 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1151 return -1;
1152 }
1153
you.chenebcb8022022-05-06 11:30:57 +08001154 CHECK_WPA_CTRL(CTRL_AP);
1155
you.chen5f55bc42022-05-16 17:55:28 +08001156 if (auth == LYNQ_WIFI_AUTH_WEP) {
1157 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1158 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1159 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1160 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1161 }
1162 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1163 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1164 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1165 }
1166 else {
1167 return -1;
1168 }
you.chenebcb8022022-05-06 11:30:57 +08001169
you.chenebcb8022022-05-06 11:30:57 +08001170 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong99b48d62022-04-07 05:41:29 -04001171
qs.xiongd189c542022-03-31 00:58:23 -04001172 return 0;
1173}
1174
you.chenebcb8022022-05-06 11:30:57 +08001175int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongd189c542022-03-31 00:58:23 -04001176{
you.chenebcb8022022-05-06 11:30:57 +08001177 FILE * fp;
1178 int len, ret;
1179 int count, index;
1180 char *split_lines[128] = {0};
1181 char *buff, *p;
qs.xiong99b48d62022-04-07 05:41:29 -04001182
you.chenebcb8022022-05-06 11:30:57 +08001183 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001184
you.chenebcb8022022-05-06 11:30:57 +08001185 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1186// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1187 if (NULL == fp) {
1188 printf("open file fail\n");
1189 return -1;
1190 }
qs.xiong99b48d62022-04-07 05:41:29 -04001191
you.chenebcb8022022-05-06 11:30:57 +08001192 buff = alloca(MAX_RET);
1193 fseek(fp, 0, SEEK_SET);
1194 len = fread(buff, 1, MAX_RET, fp);
1195 fclose(fp);
qs.xiong99b48d62022-04-07 05:41:29 -04001196
you.chenebcb8022022-05-06 11:30:57 +08001197 for(index=0; index < len; index ++) {
1198 if (memcmp(buff + index, "network={", 9) != 0) {
1199 continue;
1200 }
1201 p = buff + index + 9;
1202 for (; index < len; index ++ ) {
1203 if (buff[index] != '}') {
1204 continue;
1205 }
1206 buff[index] = '\0';
1207 break;
1208 }
1209 len = buff + index - p;
1210 }
1211
1212 count = lynq_split(p, len, '\n', split_lines);
1213
1214 ret = -1;
1215 for(index=0; index < count; index++) {
1216 p = strstr(split_lines[index], "psk=");
you.chen5f55bc42022-05-16 17:55:28 +08001217 if (p != NULL) {
1218 p += 4;
1219 if (*p == '\"') {
1220 p++;
1221 }
you.chenebcb8022022-05-06 11:30:57 +08001222 }
you.chen5f55bc42022-05-16 17:55:28 +08001223 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1224 p += 9;
1225 if (*p == '\"') {
1226 p++;
1227 }
1228 }
1229 else {
1230 continue;
you.chenebcb8022022-05-06 11:30:57 +08001231 }
1232
1233 strcpy(password, p);
1234
1235 while(*password != '\0') {
1236 if (*password == '\"') {
1237 *password = '\0';
1238 break;
1239 }
1240 password++;
1241 }
1242 ret = 0;
1243 break;
1244 } //end for(index=0; index < count; index++)
1245
1246 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -05001247}
1248
you.chenebcb8022022-05-06 11:30:57 +08001249static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1250 char lynq_auth_str[MAX_RET] = {0};
you.chen5f55bc42022-05-16 17:55:28 +08001251 char lynq_proto_str[MAX_RET] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04001252
you.chenebcb8022022-05-06 11:30:57 +08001253 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1254 return -1;
1255 }
1256
1257 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chen5f55bc42022-05-16 17:55:28 +08001258
1259 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1260 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1261 if (strcmp(lynq_proto_str, "RSN") == 0) {
1262 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1263 }
1264 }
1265 }
you.chenebcb8022022-05-06 11:30:57 +08001266 return 0;
1267}
1268
1269int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05001270{
you.chenebcb8022022-05-06 11:30:57 +08001271 int pass_len, net_no, count, index;
1272 char lynq_tmp_cmd[300]={0};
1273 int net_no_list[128];
1274 lynq_wifi_auth_s net_auth;
1275 pass_len=strlen(password);
1276 if(pass_len < 8 || pass_len >= 64){
1277 return -1;
1278 }
1279
1280 CHECK_IDX(idx, CTRL_STA);
1281
1282 net_no = -1;
1283 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1284
1285 for (index=0; index < count; index++) {
1286 net_auth = -1;
1287 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1288 net_no = net_no_list[index];
1289 break;
1290 }
1291 }
1292
1293 if (net_no < 0) {
1294 return -1;
1295 }
1296
1297 CHECK_WPA_CTRL(CTRL_STA);
1298
1299 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1300
1301 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1302 DO_OK_FAIL_REQUEST(cmd_save_config);
1303
1304 return 0;
1305}
1306
1307int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1308
1309 FILE * fp;
1310 int len, ret;
1311 int count, index;
1312 char *split_lines[128] = {0};
1313 char *buff, *p;
1314
1315 CHECK_IDX(idx, CTRL_STA);
1316
1317 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1318 if (NULL == fp) {
1319 printf("open file fail\n");
1320 return -1;
1321 }
1322
1323 buff = alloca(MAX_RET);
1324 fseek(fp, 0, SEEK_SET);
1325 len = fread(buff, 1, MAX_RET, fp);
1326 fclose(fp);
1327
1328 for(index=0; index < len; index ++) {
1329 for(; index < len; index ++) {
1330 if (memcmp(buff + index, "network={", 9) != 0) {
1331 continue;
1332 }
1333 p = buff + index + 9;
1334 for (; index < len; index ++ ) {
1335 if (buff[index] != '}') {
1336 continue;
1337 }
1338 buff[index] = '\0';
1339 break;
1340 }
1341 len = buff + index - p;
1342 }
1343
1344 if (strstr(p, ap->ap_ssid) != NULL) {
1345 break;
1346 }
1347 }
1348
1349 if (index >= len) {
1350 return -1;
1351 }
1352
1353 count = lynq_split(p, len, '\n', split_lines);
1354
1355 ret = -1;
1356 for(index=0; index < count; index++) {
1357 p = strstr(split_lines[index], "psk=");
you.chen5f55bc42022-05-16 17:55:28 +08001358 if (p != NULL) {
1359 p += 4;
1360 if (*p == '\"') {
1361 p++;
1362 }
you.chenebcb8022022-05-06 11:30:57 +08001363 }
you.chen5f55bc42022-05-16 17:55:28 +08001364 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1365 p += 9;
1366 if (*p == '\"') {
1367 p++;
1368 }
1369 }
1370 else {
1371 continue;
you.chenebcb8022022-05-06 11:30:57 +08001372 }
1373
1374 strcpy(password, p);
1375
1376 while(*password != '\0') {
1377 if (*password == '\"') {
1378 *password = '\0';
1379 break;
1380 }
1381 password++;
1382 }
1383 ret = 0;
1384 break;
1385 } //end for(index=0; index < count; index++)
1386
1387 return ret;
1388}
1389
1390static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1391{
qs.xiong99b48d62022-04-07 05:41:29 -04001392 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongd189c542022-03-31 00:58:23 -04001393
you.chenebcb8022022-05-06 11:30:57 +08001394 if (sta_ssid == NULL) {
1395 printf("sta_ssid is null\n");
qs.xiongd189c542022-03-31 00:58:23 -04001396 return -1;
you.chenebcb8022022-05-06 11:30:57 +08001397 }
1398
1399 CHECK_WPA_CTRL(CTRL_STA);
1400
1401 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1402
1403 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1404// DO_OK_FAIL_REQUEST(cmd_save_config);
1405
qs.xiong8d42bb92022-03-02 09:43:11 -05001406 return 0;
1407
1408}
1409
you.chenebcb8022022-05-06 11:30:57 +08001410static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong8d42bb92022-03-02 09:43:11 -05001411{
you.chenebcb8022022-05-06 11:30:57 +08001412 char lynq_disable_cmd[128]={0};
1413 char lynq_select_cmd[128]={0};
1414
1415 CHECK_WPA_CTRL(CTRL_STA);
1416
1417 if (save != 0) {
1418 DO_OK_FAIL_REQUEST(cmd_save_config);
1419 }
1420
1421 if (start_flag == 0) {
you.chen5f55bc42022-05-16 17:55:28 +08001422// sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1423 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chenebcb8022022-05-06 11:30:57 +08001424 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1425 }
1426 else {
1427 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1428 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1429 }
1430
1431 return 0;
1432}
1433
1434int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1435{
1436 CHECK_IDX(idx, CTRL_STA);
1437
you.chen5f55bc42022-05-16 17:55:28 +08001438 curr_status_info curr_state;
1439 ap_info_s ap_info;
1440 curr_state.ap = &ap_info;
1441 curr_state.state = NULL;
1442
1443 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1444 strcpy(sta_ssid, ap_info.ap_ssid);
1445 return 0;
1446 }
1447
1448 return -1;
you.chenebcb8022022-05-06 11:30:57 +08001449}
1450
1451int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1452{
1453 scan_info_s *scan_list;
1454 saved_ap_info_s *save_list;
1455 int scan_len=0;
1456 int save_len=0;
1457 int best_index = -1;
1458 int best_scan_index = -1;
1459 int best_rssi = 0;
1460 int i, j;
1461
1462 CHECK_IDX(idx, CTRL_STA);
1463 if (info == NULL) {
1464 return -1;
1465 }
1466
1467 curr_status_info curr_state;
1468 ap_info_s ap_info;
1469 curr_state.ap = &ap_info;
1470 curr_state.state = NULL;
1471
1472 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1473 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1474 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1475 lynq_get_connect_ap_rssi(idx, &info->rssi);
1476 return 0;
1477 }
1478
1479 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1480 return -1;
1481 }
1482
1483 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1484 return -1;
1485 }
1486
1487 for (i=0; i < save_len; i++) {
1488 for (j=0; j < scan_len; j++) {
1489 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1490 && save_list[i].base_info.auth == scan_list[j].auth) {
1491 if (best_rssi == 0) {
1492 best_rssi = scan_list[j].rssi;
1493 }
1494 else if (best_rssi > scan_list[j].rssi) {
1495 best_index = i;
1496 best_scan_index = j;
1497 best_rssi = scan_list[j].rssi;
1498 }
1499 break;
1500 }
1501 }
1502 }
1503
1504 if (best_index >= 0) {
1505 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1506 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1507 info->rssi = best_rssi;
1508 return 0;
1509 }
1510
1511 return -1;
1512}
1513
1514static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1515{
1516 char lynq_auth_cmd[64]={0};
1517 char lynq_ket_mgmt_cmd[64]={0};
1518 char lynq_pairwise_cmd[64]={0};
1519 char lynq_psk_cmd[64]={0};
1520
1521 CHECK_WPA_CTRL(CTRL_STA);
1522
qs.xiong799dab02022-03-14 09:12:12 -04001523 switch(auth){
you.chenebcb8022022-05-06 11:30:57 +08001524 case LYNQ_WIFI_AUTH_OPEN:
1525 {
1526 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04001527
you.chenebcb8022022-05-06 11:30:57 +08001528 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1529// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong99b48d62022-04-07 05:41:29 -04001530 break;
1531 }
you.chenebcb8022022-05-06 11:30:57 +08001532 case LYNQ_WIFI_AUTH_WPA_PSK:
1533 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongd189c542022-03-31 00:58:23 -04001534 {
you.chenebcb8022022-05-06 11:30:57 +08001535 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1536 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1537 }
1538 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen5f55bc42022-05-16 17:55:28 +08001539 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chenebcb8022022-05-06 11:30:57 +08001540 }
1541 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1542 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04001543
you.chenebcb8022022-05-06 11:30:57 +08001544 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1545 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1546 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong99b48d62022-04-07 05:41:29 -04001547
you.chenebcb8022022-05-06 11:30:57 +08001548 if (password != NULL) {
1549 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1550 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1551 }
qs.xiong99b48d62022-04-07 05:41:29 -04001552
you.chenebcb8022022-05-06 11:30:57 +08001553// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongd189c542022-03-31 00:58:23 -04001554 break;
you.chenebcb8022022-05-06 11:30:57 +08001555 }
qs.xiongd189c542022-03-31 00:58:23 -04001556 default:
1557 return -1;
you.chenebcb8022022-05-06 11:30:57 +08001558 }
qs.xiong99b48d62022-04-07 05:41:29 -04001559
qs.xiongd189c542022-03-31 00:58:23 -04001560 return 0;
qs.xiong799dab02022-03-14 09:12:12 -04001561}
qs.xiong8d42bb92022-03-02 09:43:11 -05001562
you.chenebcb8022022-05-06 11:30:57 +08001563static int inner_get_curr_net_no(int interface) {
1564 curr_status_info curr_state;
1565 curr_state.ap = NULL;
1566 curr_state.state = NULL;
1567
1568 if (0 != inner_get_status_info(interface, &curr_state)) {
1569 return -1;
1570 }
1571
1572 return curr_state.net_no;
1573}
1574
1575int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001576{
you.chenebcb8022022-05-06 11:30:57 +08001577 int net_no;
1578 CHECK_IDX(idx, CTRL_STA);
qs.xiongd189c542022-03-31 00:58:23 -04001579
you.chenebcb8022022-05-06 11:30:57 +08001580 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong8d42bb92022-03-02 09:43:11 -05001581
you.chenebcb8022022-05-06 11:30:57 +08001582 if (net_no < 0) {
1583 return -1;
1584 }
1585
1586 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong8d42bb92022-03-02 09:43:11 -05001587}
1588
you.chenebcb8022022-05-06 11:30:57 +08001589int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong8d42bb92022-03-02 09:43:11 -05001590{
you.chenebcb8022022-05-06 11:30:57 +08001591 int count, net_no, index;
1592 int net_no_list[128];
1593 lynq_wifi_auth_s net_auth;
qs.xiongd189c542022-03-31 00:58:23 -04001594
you.chenebcb8022022-05-06 11:30:57 +08001595 if (ssid == NULL || *ssid == '\0') {
1596 printf("bad ssid\n");
1597 return -1;
1598 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001599
you.chenebcb8022022-05-06 11:30:57 +08001600 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1601 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1602 printf("bad password\n");
1603 return -1;
1604 }
1605 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001606
you.chenebcb8022022-05-06 11:30:57 +08001607 CHECK_IDX(idx, CTRL_STA);
1608
1609 net_no = -1;
1610 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1611
1612 for (index=0; index < count; index++) {
1613 net_auth = -1;
1614 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1615 net_no = net_no_list[index];
1616 break;
1617 }
1618 }
1619
1620 if (net_no < 0) {
1621 net_no = lynq_add_network(CTRL_STA);
1622 if (net_no == -1) {
1623 return -1;
1624 }
1625
1626 printf("net no is %d\n", net_no);
1627 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1628 return -1;
1629 }
1630 }
1631
1632 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1633 return -1;
1634 }
1635
1636 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong8d42bb92022-03-02 09:43:11 -05001637}
1638
you.chenebcb8022022-05-06 11:30:57 +08001639int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -05001640{
you.chenebcb8022022-05-06 11:30:57 +08001641 ap_info_s ap;
1642 curr_status_info curr_state;
1643 ap.ap_ssid[0] = '\0';
qs.xiong99b48d62022-04-07 05:41:29 -04001644
you.chenebcb8022022-05-06 11:30:57 +08001645 if (ssid == NULL || *ssid == '\0') {
1646 return -1;
1647 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001648
you.chenebcb8022022-05-06 11:30:57 +08001649 CHECK_IDX(idx, CTRL_STA);
qs.xiong99b48d62022-04-07 05:41:29 -04001650
you.chenebcb8022022-05-06 11:30:57 +08001651 curr_state.ap = &ap;
you.chenc00d5582022-05-06 17:50:16 +08001652 curr_state.state = NULL;
1653
you.chenebcb8022022-05-06 11:30:57 +08001654 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1655 return 0;
1656 }
qs.xiong799dab02022-03-14 09:12:12 -04001657
you.chenebcb8022022-05-06 11:30:57 +08001658 if (strcmp(ap.ap_ssid, ssid) != 0) {
1659 return 0;
1660 }
1661
1662 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong8d42bb92022-03-02 09:43:11 -05001663}
qs.xiong99b48d62022-04-07 05:41:29 -04001664
you.chen5f55bc42022-05-16 17:55:28 +08001665int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1666{
1667// const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong8d42bb92022-03-02 09:43:11 -05001668
you.chenebcb8022022-05-06 11:30:57 +08001669 CHECK_IDX(idx, CTRL_STA);
you.chen5f55bc42022-05-16 17:55:28 +08001670 CHECK_WPA_CTRL(CTRL_STA);
1671
1672 system("connmanctl enable wifi");
1673
you.chenebcb8022022-05-06 11:30:57 +08001674 if (system("ifconfig | grep wlan0") != 0) {
1675 return -1;
1676 }
qs.xionge5164a82022-03-15 08:03:26 -04001677
you.chen5f55bc42022-05-16 17:55:28 +08001678// DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
1679
1680 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001681}
1682
you.chenebcb8022022-05-06 11:30:57 +08001683int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong99b48d62022-04-07 05:41:29 -04001684{
you.chen5f55bc42022-05-16 17:55:28 +08001685 char lynq_disable_network_cmd[MAX_CMD];
1686 curr_status_info curr_state;
1687 ap_info_s ap_info;
you.chenebcb8022022-05-06 11:30:57 +08001688
you.chen5f55bc42022-05-16 17:55:28 +08001689 CHECK_IDX(idx, CTRL_STA);
1690 CHECK_WPA_CTRL(CTRL_STA);
1691
1692 curr_state.ap = &ap_info;
1693 curr_state.state = NULL;
1694
1695 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1696 return 0;
1697 }
1698
1699 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1700 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1701
1702 DO_OK_FAIL_REQUEST(cmd_save_config);
1703
1704 return 0;
1705// return system("connmanctl disable wifi");
qs.xiongd189c542022-03-31 00:58:23 -04001706}
qs.xiong8d42bb92022-03-02 09:43:11 -05001707
you.chenebcb8022022-05-06 11:30:57 +08001708//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1709// int i, count;
1710// char *p;
1711// const char * FLAG_SSID = "ssid=";
1712// const char * FLAG_SBSID = "bssid=";
1713// const char * FLAG_KEY_MGMT = "key_mgmt=";
1714// const char * FLAG_FREQ = "freq=";
1715// char lynq_sta_cmd[MAX_CMD];
1716// char *split_lines[128] = {0};
1717
1718// CHECK_WPA_CTRL(CTRL_AP);
1719
1720// sprintf(lynq_sta_cmd, "STA %s", bssid);
1721
1722// DO_REQUEST(lynq_sta_cmd);
1723
1724// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1725
1726// for(i=0; i < count; i++) {
1727// p = strstr(split_lines[i], FLAG_SSID);
1728// if (p != NULL) {
1729// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1730// continue;
1731// }
1732// }
1733
1734// lynq_get_interface_ip(idx, ap->ap_ip);
1735// lynq_ap_password_set(idx, ap->psw);
1736
1737// return 0;
1738//}
1739
1740static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1741 curr_status_info curr_state;
1742 curr_state.ap = ap;
1743 curr_state.state = NULL;
1744 return inner_get_status_info(interface, &curr_state);
1745}
1746
1747int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04001748{
you.chenebcb8022022-05-06 11:30:57 +08001749 int ip_count, index, i, line_count;
1750 const char *lynq_first_sta_cmd = "STA-FIRST";
1751 char lynq_next_sta_cmd[MAX_CMD];
1752 char *bssid[1024] = {0};
1753 char *mac_list[128] = {0};
1754 char *ip_list[128] = {0};
1755 char *split_lines[128] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04001756
you.chenebcb8022022-05-06 11:30:57 +08001757 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001758
you.chenebcb8022022-05-06 11:30:57 +08001759 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001760
you.chenebcb8022022-05-06 11:30:57 +08001761// ap_info_s * tmp_ap;
1762// device_info_s * tmp_list;
1763 if (ap == NULL || list == NULL || len == NULL) {
1764 printf("bad input param");
1765 return -1;
1766 }
1767
1768// ap = &tmp_ap;
1769// list = &tmp_list;
1770 *ap = malloc(sizeof (ap_info_s));
1771
1772 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1773 return -1;
1774 }
1775
1776 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1777 lynq_ap_password_get(idx, (*ap)->psw);
1778
1779 ip_count = get_ip_mac_list(mac_list, ip_list);
1780 printf("get count %d\n", ip_count);
1781
1782 DO_REQUEST(lynq_first_sta_cmd);
1783
1784 index = 0;
1785 while (reply_len > 0) {
1786 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1787 break;
1788 }
1789 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1790 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1791 strcpy(bssid[index], split_lines[0]);
1792 index++;
1793 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1794 reply_len = MAX_RET;
1795 cmd_reply[0] = '\0';
1796 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1797 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1798 break;
1799 }
1800 }
1801
1802 *len = index;
1803
1804 *list = malloc(sizeof(device_info_s) * (*len));
1805 for (index=0; index < *len; index++) {
1806 strcpy((*list)[index].sta_mac, bssid[index]);
1807 for(i=0;i < ip_count; i++ ) {
1808 if (strcmp(bssid[index], mac_list[i]) == 0) {
1809 strcpy((*list)[index].sta_ip, ip_list[i]);
1810 break;
1811 }
1812 }
1813 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1814 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1815 free(bssid[index]);
1816 }
1817
1818 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04001819}
1820
you.chenebcb8022022-05-06 11:30:57 +08001821int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04001822{
you.chenebcb8022022-05-06 11:30:57 +08001823 int i, count, index, count_words;
1824 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1825 char *split_lines[128] = {0};
1826 char *split_words[128] = {0};
1827 scan_info_s * p;
qs.xiong99b48d62022-04-07 05:41:29 -04001828
you.chenebcb8022022-05-06 11:30:57 +08001829 if (list == NULL || len == NULL) {
1830 return -1;
1831 }
qs.xiong99b48d62022-04-07 05:41:29 -04001832
you.chenebcb8022022-05-06 11:30:57 +08001833 CHECK_IDX(idx, CTRL_STA);
1834
1835 CHECK_WPA_CTRL(CTRL_STA);
1836
1837 DO_REQUEST(lynq_scan_result_cmd);
1838
1839 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1840 *len = count - 1;
1841 *list = malloc(sizeof (scan_info_s) * *len);
1842
1843 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1844 for (index=0; index <count_words; index++) {
1845 printf("----header: %s\n", split_words[index]);
1846 }
1847
1848 for(index = 1;index < count; index++) {
1849 printf("---- %s\n",split_lines[index]);
1850 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1851 if (count_words < 4)
1852 continue;
1853 printf("count: %d, %s\n", count_words, split_words[0]);
1854 //bssid / frequency / signal level / flags / ssid
1855 p = (*list) + index - 1;
1856 strcpy(p->mac, split_words[0]);
1857 p->band = convert_band_from_freq(atoi(split_words[1]));
1858 p->rssi = -1 * atoi( split_words[2]);
1859 p->auth = convert_max_auth_from_flag(split_words[3]);
1860 strcpy(p->ssid, split_words[4]);
1861 }
1862
1863 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04001864}
qs.xiong99b48d62022-04-07 05:41:29 -04001865
you.chenebcb8022022-05-06 11:30:57 +08001866int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1867{
1868 int count, net_no, index;
1869 int net_no_list[128];
1870 lynq_wifi_auth_s net_auth;
1871 char lynq_remove_cmd[MAX_CMD];
1872
1873 if (ssid == NULL || *ssid == '\0') {
1874 printf("bad ssid\n");
1875 return -1;
1876 }
1877
1878 CHECK_IDX(idx, CTRL_STA);
1879
1880 CHECK_WPA_CTRL(CTRL_STA);
1881
1882 net_no = -1;
1883 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1884
1885 for (index=0; index < count; index++) {
1886 net_auth = -1;
1887 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1888 net_no = net_no_list[index];
1889 break;
1890 }
1891 }
1892
1893 if (net_no < 0) {
1894 return 0;
1895 }
1896
1897 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1898
1899 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1900 DO_OK_FAIL_REQUEST(cmd_save_config);
1901
1902 return 0;
1903}
1904
1905int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1906{
1907 int count, index;
1908 int net_no_list[128];
1909 char freq[16];
1910
1911 if (list == NULL || len == NULL) {
1912 printf("bad param\n");
1913 return -1;
1914 }
1915
1916 CHECK_IDX(idx, CTRL_STA);
1917
1918// CHECK_WPA_CTRL(CTRL_STA);
1919
1920 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1921 printf("count is %d\n", count);
1922
1923 *list = malloc(sizeof (saved_ap_info_s) * count);
1924 *len = count;
1925
1926 for (index=0; index < count; index++) {
1927 printf("to get ssid %d\n", index);
1928 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1929 printf("to get bssid\n");
1930 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1931 printf("to get inner_get_network_auth\n");
1932 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1933 printf("to get frequency\n");
1934 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1935 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1936 }
1937 else {
1938 (*list)[index].base_info.band = -1;
1939 }
1940
1941 }
1942
1943 return 0;
1944}
1945
1946int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1947{
1948 const char *lynq_scan_cmd = "SCAN";
1949
1950 CHECK_IDX(idx, CTRL_STA);
1951
1952 CHECK_WPA_CTRL(CTRL_STA);
1953
1954 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1955
1956 return 0;
1957}
1958
1959int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1960 if (cb == NULL) {
1961 return -1;
1962 }
1963
1964 g_ap_callback_priv = priv;
1965 g_ap_callback_func = cb;
1966
1967 return 0;
1968}
1969
1970int lynq_unreg_ap_event_callback(void * priv) {
1971 if (g_ap_callback_priv == priv) {
1972 g_ap_callback_func = NULL;
1973 g_ap_callback_priv = NULL;
1974 return 0;
1975 }
1976 return -1;
1977}
1978
1979int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
1980 if (cb == NULL) {
1981 return -1;
1982 }
1983
1984 g_sta_callback_priv = priv;
1985 g_sta_callback_func = cb;
1986
1987 return 0;
1988}
1989
1990int lynq_unreg_sta_event_callback(void * priv) {
1991 if (g_sta_callback_priv == priv) {
1992 g_sta_callback_func = NULL;
1993 g_sta_callback_priv = NULL;
1994 return 0;
1995 }
1996 return -1;
1997}
1998
1999
2000static int inner_get_status_info_state (int interface, char *state) {
2001 curr_status_info curr_state;
2002 curr_state.ap = NULL;
2003 curr_state.state = state;
2004 return inner_get_status_info(interface, &curr_state);
2005}
2006
2007int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2008{
2009 char state[MAX_CMD];
2010 const char * STATE_COMPLETED = "COMPLETED";
2011 CHECK_IDX(idx, CTRL_AP);
2012
2013 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2014 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2015 return 0;
2016 }
2017
2018 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2019 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2020 }
2021 else {
2022 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2023 }
2024
2025 return 0;
2026}
2027
2028int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2029 char state[MAX_CMD];
2030 const char * STATE_COMPLETED = "COMPLETED";
2031 CHECK_IDX(idx, CTRL_STA);
2032
2033 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2034 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2035 return 0;
2036 }
2037
2038 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2039 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2040 }
2041 else {
2042 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2043 }
2044
2045 return 0;
2046}
2047
2048int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2049// CHECK_IDX(idx, CTRL_AP);
2050// int ret = 0;
2051// size_t reply_len = MAX_RET;
2052// char cmd_reply[MAX_RET]={0};
2053// const char * cmd_str = "GET country";
2054// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2055// do{
2056// if (NULL == s_lynq_wpa_ctrl) {
2057// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2058// if (NULL == s_lynq_wpa_ctrl ) {
2059// printf("wpa_ctrl_open fail\n");
2060// return -1;
2061// }
2062// }
2063// }while(0);
2064
2065// do {
2066// reply_len = MAX_RET;
2067// cmd_reply[0] = '\0';
2068// printf("to call [%s]\n", cmd_str);
2069// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
2070// if (ret != 0) {
2071// printf("call ##cmd_str fail %d\n", ret);
2072// return ret;
2073// }
2074// cmd_reply[reply_len+1] = '\0';
2075// printf("cmd replay [ %s ]\n", cmd_reply);
2076// }while(0);
2077
2078 FILE *fp;
2079 size_t i = 0;
2080 char lynq_cmd_ret[MAX_RET]={0};
2081
2082// CHECK_IDX(idx, CTRL_AP);
2083
2084 if((fp=popen("wl country","r"))==NULL)
2085 {
2086 perror("popen error!");
2087 return -1;
2088 }
2089 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2090 {
2091 perror("fread fail!");
2092 return -1;
2093 }
2094
2095 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2096 if (lynq_cmd_ret[i] == ' ') {
2097 lynq_cmd_ret[i] = '\0';
2098 break;
2099 }
2100 }
2101
2102 strcpy(country_code,lynq_cmd_ret);
2103 printf("---country code %s\n", country_code);
2104
2105 int ret=pclose(fp);
2106 if(ret==-1)
2107 {
2108 perror("close file faild");
2109 }
2110
2111 return 0;
2112}
2113
2114int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2115// const char * cmd_str = "GET country";
2116// CHECK_IDX(idx, CTRL_AP);
2117// CHECK_WPA_CTRL(CTRL_STA);
2118
2119// DO_REQUEST(cmd_str);
2120// printf("result %s\n", cmd_reply);
2121
2122 if (country_code == NULL || *country_code == '\0') {
2123 printf("bad country code\n");
2124 return -1;
2125 }
2126
2127 char lynq_country_cmd[MAX_CMD];
2128 sprintf(lynq_country_cmd, "wl country %s", country_code);
2129 if (system(lynq_country_cmd) == 0) {
2130 return 0;
2131 }
2132
2133 return -1;
2134}
2135
2136int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2137{
2138 if (mac == NULL) {
2139 return -1;
2140 }
2141
2142 CHECK_IDX(idx, CTRL_STA);
2143 ap_info_s ap;
2144 ap.ap_mac[0] = '\0';
2145
2146 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2147 return -1;
2148 }
2149 strcpy(mac, ap.ap_mac);
2150
2151 return 0;
2152}
2153
2154int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2155{
2156 struct ifaddrs *ifaddr, *ifa;
2157 int family, s;
2158 char host[NI_MAXHOST];
2159 const char * ifaName = "wlan0";
2160 if (idx == 1) {
you.chen5f55bc42022-05-16 17:55:28 +08002161 ifaName = "tether";
you.chenebcb8022022-05-06 11:30:57 +08002162 }
2163 else if (idx != 0) {
2164 return -1;
2165 }
2166
2167 if (getifaddrs(&ifaddr) == -1)
2168 {
2169 perror("getifaddrs");
2170 return -1;
2171 //exit(EXIT_FAILURE);
2172 }
2173
2174
2175 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2176 {
2177 if (ifa->ifa_addr == NULL)
2178 continue;
2179 host[0] = '\0';
2180 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2181
2182 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2183 if((strcmp(ifa->ifa_name,ifaName)==0))
2184 {
2185 if (s != 0)
2186 {
2187 // printf("getnameinfo() failed: %s", gai_strerror(s));
2188 //exit(EXIT_FAILURE);
2189 }
2190 freeifaddrs(ifaddr);
2191 strcpy(ip, host);
2192 printf("ip %s\n", ip);
2193 return 0;
2194 //printf(" Interface : <%s>",ifa->ifa_name );
2195 //printf(" Address : <%s>", host);
2196 }
2197 }
2198 return -1;
2199}
2200
2201int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2202{
2203 int count;
2204 size_t i;
2205 char *split_words[128] = {0};
2206 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2207
2208 CHECK_WPA_CTRL(idx);
2209
2210 DO_REQUEST(lynq_get_mac_cmd);
2211
2212 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2213 return -1;
2214 }
2215
2216 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2217
2218 if (count < 2) {
2219 return -1;
2220 }
2221
2222 for (i=0; i < strlen(split_words[1]); i++ ) {
2223 if (split_words[1][i] != ' ') {
2224 break;
2225 }
2226 }
2227
2228 strcpy(mac, split_words[1] + i);
2229
2230 return 0;
2231}
2232
2233int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2234{
2235// int count;
2236// char *split_words[128] = {0};
2237// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2238
2239// if (rssi == NULL) {
2240// return -1;
2241// }
2242
2243// CHECK_IDX(idx, CTRL_STA);
2244
2245// CHECK_WPA_CTRL(CTRL_STA);
2246
2247// DO_REQUEST(lynq_get_rssi_cmd);
2248
2249// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2250// return -1;
2251// }
2252
2253// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2254
2255// if (count < 2) {
2256// return -1;
2257// }
2258
2259// *rssi = atoi(split_words[1]) * -1;
2260
2261 FILE *fp;
2262 size_t i = 0;
2263 char lynq_cmd_ret[MAX_RET]={0};
2264
2265// CHECK_IDX(idx, CTRL_AP);
2266
2267 if((fp=popen("wl country","r"))==NULL)
2268 {
2269 perror("popen error!");
2270 return -1;
2271 }
2272 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2273 {
2274 perror("fread fail!");
2275 return -1;
2276 }
2277 *rssi = atoi(lynq_cmd_ret);
2278
2279 return 0;
2280}
2281
2282int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2283{
2284 if (band == NULL) {
2285 return -1;
2286 }
2287
2288 CHECK_IDX(idx, CTRL_STA);
2289 ap_info_s ap;
2290 ap.band = -1;
2291
2292 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2293 return -1;
2294 }
2295 *band = ap.band;
2296
2297 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04002298}