blob: 07beaf748c90eb5f9e45e87a313ca6e6cf491c47 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
45volatile int g_sta_scan_finish_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
61
62static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
63
64
65typedef struct __curr_status_info {
66 ap_info_s *ap;
67 char * state;
68 int net_no;
69}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040070
71#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040072{\
you.chen35020192022-05-06 11:30:57 +080073 perror((str));\
74 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040075}
76
you.chen35020192022-05-06 11:30:57 +080077#define CHECK_IDX(idx, type) do { \
78 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
79 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
80 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
81 return -1; \
82 } \
83 }while (0)
84
85#define CHECK_WPA_CTRL(index) int ret = 0;\
86 size_t reply_len = MAX_RET; \
87 char cmd_reply[MAX_RET]={0}; \
88 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
89 do{ \
90 if (NULL == g_lynq_wpa_ctrl[index]) { \
91 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
92 if (NULL == g_lynq_wpa_ctrl[index]) { \
93 printf("wpa_ctrl_open fail\n"); \
94 return -1; \
95 } \
96 } \
97 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
98 }while(0)
99
100#define DO_REQUEST(cmd_str) do { \
101 reply_len = MAX_RET;\
102 cmd_reply[0] = '\0'; \
103 printf("to call [%s]\n", cmd_str); \
104 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
105 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800106 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800107 return ret; \
108 } \
109 cmd_reply[reply_len+1] = '\0'; \
110 printf("cmd replay [ %s ]\n", cmd_reply); \
111 }while(0)
112
113#define DO_OK_FAIL_REQUEST(cmd_str) do { \
114 DO_REQUEST(cmd_str); \
115 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800116 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800117 return -1; \
118 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800119 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800120 return -1; \
121 } \
122 }while (0)
123
124
125
126static void APWatcherThreadProc() {
127 size_t len = MAX_RET;
128 char msg_notify[MAX_RET];
129
you.chen6c2dd9c2022-05-16 17:55:28 +0800130 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800131 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800132
133 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800134 if (lynq_wpa_ctrl == NULL) {
135 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
136 if (lynq_wpa_ctrl == NULL) {
137 usleep(100*1000);
138 continue;
139 }
140
141 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800142 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800143 }
144
you.chen35020192022-05-06 11:30:57 +0800145 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
146 usleep(100*1000);
147 continue;
148 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800149 memset(msg_notify, 0, MAX_RET);
150 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800151 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
152 msg_notify[len+1] = '\0';
153 printf("ap------> %s\n", msg_notify);
154 if (g_ap_callback_func == NULL) {
155 continue;
156 }
157 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
158 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
159 }
160 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
161 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
162 }
163 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
164 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800165 if (lynq_wpa_ctrl != NULL) {
166 wpa_ctrl_detach(lynq_wpa_ctrl);
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400169}
170
you.chen35020192022-05-06 11:30:57 +0800171static void STAWatcherThreadProc() {
172 size_t len = MAX_RET;
173 char msg_notify[MAX_RET];
174 char *pReason;
175 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400176
you.chen6c2dd9c2022-05-16 17:55:28 +0800177 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800178 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800179
180 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800181 if (lynq_wpa_ctrl == NULL) {
182 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
183 if (lynq_wpa_ctrl == NULL) {
184 usleep(100*1000);
185 continue;
186 }
187
188 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800189 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800190 }
191
you.chen35020192022-05-06 11:30:57 +0800192 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
193 usleep(100*1000);
194 continue;
195 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800196 memset(msg_notify, 0, MAX_RET);
197 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800198 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
199 msg_notify[len+1] = '\0';
200 printf("sta ------> %s\n", msg_notify);
201 if (strstr(msg_notify, state_scan_result) != NULL) {
202 g_sta_scan_finish_flag = 1;
203 }
204
205 if (g_sta_callback_func == NULL) {
206 continue;
207 }
208 error = -1;
209 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
210 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
211 }
212 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
213 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
214 }
215 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
216 pReason = strstr(msg_notify, "reason=");
217 if (pReason != NULL) {
218 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800219 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800220 error = LYNQ_TIME_OUT;
221 }
you.chen92fd5d32022-05-25 10:09:47 +0800222 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800223 error = LYNQ_PSW_ERROR;
224 }
225 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
226 }
227 }
228 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
229 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
230 }
231 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
232 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
233 }
234 }
235 }
you.chen92fd5d32022-05-25 10:09:47 +0800236 if (lynq_wpa_ctrl != NULL) {
237 wpa_ctrl_detach(lynq_wpa_ctrl);
238 wpa_ctrl_close(lynq_wpa_ctrl);
239 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400240}
241
qs.xiong1af5daf2022-03-14 09:12:12 -0400242int lynq_wifi_enable(void)
243{
you.chen35020192022-05-06 11:30:57 +0800244 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800245 int i;
you.chen35020192022-05-06 11:30:57 +0800246 const char * cmd_check_service =
247 "state=`systemctl is-active wg870_drv_insmod.service`\n"
248 "[ \"\"$state == \"active\" ] && exit 0\n"
249 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
250// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
251// return 0;
252// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400253
you.chen35020192022-05-06 11:30:57 +0800254 ret = system(cmd_check_service);
255 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800256 printf("service state %d\n", ret);
you.chen35020192022-05-06 11:30:57 +0800257 return -1;
258 }
259
you.chen6c2dd9c2022-05-16 17:55:28 +0800260 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800261 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800262 break;
263 }
264 usleep(300*1000);
265 }
266
267 if (i >= 10) {
268 return -1;
269 }
270
you.chena6fa5b22022-05-18 10:28:19 +0800271 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800272 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800273 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800274 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800275 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800276 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800277 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800278 }
279
you.chen35020192022-05-06 11:30:57 +0800280 if (g_ap_watcher_pid == 0 ) {
281 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
282 if(ret<0){
283 return -1;
284 }
285 }
286
287 if (g_sta_watcher_pid == 0 ) {
288 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
289 if(ret<0){
290 return -1;
291 }
292 }
293
you.chena6fa5b22022-05-18 10:28:19 +0800294 for (i=0; i<10; i++) {
295 usleep(300*1000);
296 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
297 break;
298 }
299 }
300
you.chen35020192022-05-06 11:30:57 +0800301 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500302}
303
qs.xiong1af5daf2022-03-14 09:12:12 -0400304int lynq_wifi_disable(void)
305{
you.chen35020192022-05-06 11:30:57 +0800306 g_ap_watcher_stop_flag = 1;
307 g_sta_watcher_stop_flag = 1;
308 if (g_ap_watcher_pid != 0)
309 pthread_join(g_ap_watcher_pid, NULL);
310 if (g_sta_watcher_pid != 0)
311 pthread_join(g_sta_watcher_pid, NULL);
312 if (g_lynq_wpa_ctrl[0] != NULL)
313 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
314 if (g_lynq_wpa_ctrl[1] != NULL)
315 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
316 g_ap_watcher_pid = 0;
317 g_sta_watcher_pid = 0;
318 g_lynq_wpa_ctrl[0] = NULL;
319 g_lynq_wpa_ctrl[1] = NULL;
320 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500321 return 0;
322}
qs.xiong1af5daf2022-03-14 09:12:12 -0400323
you.chen35020192022-05-06 11:30:57 +0800324static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
325
326 char lynq_cmd_get[128]={0};
327
328 if (out_put == NULL) {
329 printf("output ptr is null\n");
330 return -1;
331 }
332 if (param_name == NULL) {
333 printf("param ptr is null");
334 return -1;
335 }
336 if (param_name[0] == '\0') {
337 printf("param is empty");
338 return -1;
339 }
340
341 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
342
343 CHECK_WPA_CTRL(interface);
344
345 DO_REQUEST(lynq_cmd_get);
346
347 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
348 return -1;
349 }
350
you.chena6fa5b22022-05-18 10:28:19 +0800351// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800352 memcpy(out_put, cmd_reply, reply_len + 1);
353 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500354}
qs.xiong1af5daf2022-03-14 09:12:12 -0400355
you.chen35020192022-05-06 11:30:57 +0800356static int lynq_split(char * str, int len, char delimiter, char * results[]) {
357 int ret = 0;
358 char * end = str + len - 1;
359 results[ret++] = str;
360 while(str < end) {
361 if (*str == delimiter) {
362 *str++ = '\0';
363 results[ret++] = str;
364 continue;
365 }
366 str++;
367 }
368 if (*str == delimiter) {
369 *str = '\0';
370 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400371
you.chen35020192022-05-06 11:30:57 +0800372 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500373}
374
you.chen35020192022-05-06 11:30:57 +0800375static void trim_space(char * p, int count) {
376 char * begin = p;
377 p += count;
378 printf("%C-%C||\n", *begin, *p);
379 while (p >= begin ) {
380 if (*p == ' ') {
381 *p-- = '\0';
382 }
383 else {
384 break;
385 }
386 }
387}
388
389static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
390 FILE * fp;
391 int len, ret;
392 int count, count_words, index;
393 int mac_start, mac_end;
394 int ip_start, ip_end;
395 char *split_lines[128] = {0};
396 char *buff;
397 const char * ip_header = "IP address";
398 const char * mac_header = "HW address";
399 const char * zero_mac = "00:00:00:00:00:00";
400
401 fp = fopen("/proc/net/arp", "rb");
402 if (NULL == fp) {
403 printf("open file fail\n");
404 return -1;
405 }
406
407 buff = alloca(MAX_RET);
408 fseek(fp, 0, SEEK_SET);
409 len = fread(buff, 1, MAX_RET, fp);
410 fclose(fp);
411 if (len <= 0) {
412 printf("read file fail\n");
413 return -1;
414 }
415 printf("file : %s\n", buff);
416
417 count = lynq_split(buff, len, '\n', split_lines);
418 printf("----- %s\n", split_lines[0]);
419
420 mac_end = 0;
421 count_words = strlen(split_lines[0]);
422 if (strstr(split_lines[0], mac_header) != NULL) {
423 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
424 mac_end = mac_start + strlen(mac_header) + 1;
425 while (mac_end < count_words) {
426 if (split_lines[0][mac_end] != ' ') {
427 break;
428 }
429 mac_end++;
430 }
431 }
432
433 ip_end = 0;
434 if (strstr(split_lines[0], ip_header) != NULL) {
435 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
436 ip_end = ip_start + strlen(ip_header) + 1;
437 while (ip_end < count_words) {
438 if (split_lines[0][ip_end] != ' ') {
439 break;
440 }
441 ip_end++;
442 }
443 }
444
445 if (mac_end == 0 || ip_end == 0) {
446 return 0;
447 }
448
449 ret = 0;
450 for(index = 1;index < count; index++) {
451 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
452 continue;
453 }
454 mac_list[ret] = malloc(mac_end - mac_start + 1);
455 ip_list[ret] = malloc(ip_end - ip_start + 1);
456 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
457 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
458 trim_space(mac_list[ret], mac_end - mac_start - 1);
459 trim_space(ip_list[ret], ip_end - ip_start - 1);
460 ret++;
461 }
462
463 return ret;
464}
465
466static int get_hostname_by_ip(char *ip, char *hostname) {
467 struct in_addr addr ={0};
468 struct hostent *ht;
469
470 if (ip == NULL || *ip == '\0' || hostname == NULL) {
471 return -1;
472 }
473
474 if (inet_aton(ip, &addr) == 0) {
475 printf("---inet_aton fail\n");
476 return -1;
477 }
478
479 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
480
481 if (ht == NULL) {
482 printf("---gethostbyaddr fail\n");
483 herror(NULL);
484 return -1;
485 }
486
487 strcpy(hostname, ht->h_name);
488
489 return 0;
490}
491
492static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
493{
494 int count, index, words_count;
495 char * split_lines[128]= {0};
496 char * split_words[128] = {0};
497 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
498
499 CHECK_WPA_CTRL(ap_sta);
500
501 DO_REQUEST(lynq_wifi_list_networks);
502
503 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
504
505 //@todo check ssid field to compatible
506
507 ret = 0;
508 for(index=1; index < count; index++) {
509 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
510 if (words_count > 2) {
511 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
512 net_no_list[ret++] = atoi(split_words[0]);
513 }
514 }
515 }
516
517 return ret;
518}
519
520static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800521 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800522 CHECK_WPA_CTRL(ap_sta);
523 const char *lynq_wifi_add_network = "ADD_NETWORK";
524
525 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800526 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800527 return -1;
528 }
529
you.chen6c2dd9c2022-05-16 17:55:28 +0800530 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800531 if(cmd_reply[i] == '\n') {
532 cmd_reply[i] = '\0';
533 break;
534 }
535 }
536 return atoi(cmd_reply);
537}
you.chena6cd55a2022-05-08 12:20:18 +0800538
you.chen35020192022-05-06 11:30:57 +0800539static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
540{
541 int count, index;
542 int net_no_list[128];
543
544
545 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
546 for (index=0; index < count; index++) {
547 if (net_no_list[index] == net_no) {
548 return 0;
549 }
550 }
551
552 if (count >= 1)
553 index = net_no_list[count - 1];
554 else
555 index = -1;
556
557 while (index < net_no ) {
558 index = lynq_add_network(ap_sta);
559 if (index >= net_no) { // required network no created
560 return 0;
561 }
you.chena6cd55a2022-05-08 12:20:18 +0800562 else if( index < 0) {
563 printf("add network fail\n");
564 return -1;
565 }
you.chen35020192022-05-06 11:30:57 +0800566 }
567
568 if (index < 0)
569 return -1;
570
571 return 0;
572}
573
574static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
575 if (freq > 5000 && freq < 6000) {
576 return LYNQ_WIFI_5G_band;
577 }
578 else if (freq > 2000 && freq < 3000) {
579 return LYNQ_WIFI_2G_band;
580 }
581 return LYNQ_WIFI_2_and_5G_band;
582}
583
584static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
585 if (key_mgmt != NULL) {
586 if (memcmp( key_mgmt, "NONE", 4) == 0) {
587 return LYNQ_WIFI_AUTH_OPEN;
588 }
589 else if (memcmp( key_mgmt, "WEP", 3) == 0){
590 return LYNQ_WIFI_AUTH_WEP;
591 }
592 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
593 return LYNQ_WIFI_AUTH_WPA_PSK;
594 }
595 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
596 return LYNQ_WIFI_AUTH_WPA2_PSK;
597 }
598 }
599
600 return -1;
601}
602
603static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
604 if (flag != NULL) {
605 if (strstr( flag, "WPA2-PSK") != NULL){
606 return LYNQ_WIFI_AUTH_WPA2_PSK;
607 }
608 else if (strstr( flag, "WPA-PSK") != NULL){
609 return LYNQ_WIFI_AUTH_WPA_PSK;
610 }
611 else if (strstr( flag, "WEP") != NULL){
612 return LYNQ_WIFI_AUTH_WEP;
613 }
614 else if (strstr( flag, "NONE") != NULL) {
615 return LYNQ_WIFI_AUTH_OPEN;
616 }
617 }
618
619 return -1;
620}
621
622static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
623 switch (bw) {
624 case 10:
625 return LYNQ_WIFI_BANDWIDTH_HT10;
626 break;
627 case 20:
628 return LYNQ_WIFI_BANDWIDTH_HT20;
629 break;
630 case 40:
631 return LYNQ_WIFI_BANDWIDTH_HT40;
632 break;
633 case 80:
634 return LYNQ_WIFI_BANDWIDTH_HT80;
635 break;
636 default:
637 break;
638 }
639
640 return -1;
641}
642
643static int inner_get_status_info(int interface, curr_status_info *curr_state) {
644 int i, count;
645 char *p;
646 const char *lynq_status_cmd = "STATUS";
647 const char * FLAG_SSID = "ssid=";
648 const char * FLAG_SBSID = "bssid=";
649 const char * FLAG_KEY_MGMT = "key_mgmt=";
650 const char * FLAG_FREQ = "freq=";
651 const char * FLAG_STATE = "wpa_state=";
652 const char * FLAG_ID = "id=";
653 char *split_lines[128] = {0};
654
655 CHECK_WPA_CTRL(interface);
656
657 if (curr_state == NULL) {
658 return -1;
659 }
660
661 DO_REQUEST(lynq_status_cmd);
662
663 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
664
665 curr_state->net_no = -1;
666 ret = -1;
667 for(i=0; i < count; i++) {
668 if (curr_state->ap != NULL) {
669 p = strstr(split_lines[i], FLAG_SSID);
670 if (p != NULL) {
671 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
672 ret = 0;
673 continue;
674 }
675 p = strstr(split_lines[i], FLAG_SBSID);
676 if (p != NULL) {
677 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
678 ret = 0;
679 continue;
680 }
681 p = strstr(split_lines[i], FLAG_KEY_MGMT);
682 if (p != NULL) {
683 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
684 ret = 0;
685 continue;
686 }
687 p = strstr(split_lines[i], FLAG_FREQ);
688 if (p != NULL) {
689 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
690 ret = 0;
691 continue;
692 }
693 } // end if (ap != NULL)
694 if (curr_state->state != NULL) {
695 p = strstr(split_lines[i], FLAG_STATE);
696 if (p != NULL) {
697 strcpy(curr_state->state, p + strlen(FLAG_STATE));
698 ret = 0;
699 continue;
700 }
701
702 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800703 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800704 ret = 0;
705 curr_state->net_no = atoi(p);
706 printf("net_no %d, -- %s\n", curr_state->net_no, p);
707 }
708 }
709
710 return ret;
711}
712
713
qs.xiongf1b525b2022-03-31 00:58:23 -0400714int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500715{
you.chen35020192022-05-06 11:30:57 +0800716 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500717
you.chen35020192022-05-06 11:30:57 +0800718 if (ap_ssid == NULL) {
719 printf("ap_ssid is null\n");
720 return -1;
721 }
722 else {
723 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
724 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400725
you.chen35020192022-05-06 11:30:57 +0800726 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
727 return -1;
728 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400729
you.chen35020192022-05-06 11:30:57 +0800730 CHECK_IDX(idx, CTRL_AP);
731
732 CHECK_WPA_CTRL(CTRL_AP);
733
734 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
735
736 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
737 DO_OK_FAIL_REQUEST(cmd_save_config);
738
qs.xiong7a105ce2022-03-02 09:43:11 -0500739 return 0;
you.chen35020192022-05-06 11:30:57 +0800740
qs.xiong7a105ce2022-03-02 09:43:11 -0500741}
742
you.chen35020192022-05-06 11:30:57 +0800743int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500744{
you.chen6c2dd9c2022-05-16 17:55:28 +0800745 int len;
you.chen35020192022-05-06 11:30:57 +0800746 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800747 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
748 return -1;
749 len = strlen(ap_ssid);
750 if (ap_ssid[0] == '\"') {
751 memmove(ap_ssid, ap_ssid + 1, len - 1);
752 len -= 1;
753 }
754 if (len > 0 && ap_ssid[len-1] == '\"') {
755 ap_ssid[len-1] = '\0';
756 }
757 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500758}
759
qs.xiongf1b525b2022-03-31 00:58:23 -0400760int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500761{
you.chen35020192022-05-06 11:30:57 +0800762 char lynq_wifi_frequency_cmd[128]={0};
763 char lynq_cmd_mode[128]={0};
764 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500765
you.chen35020192022-05-06 11:30:57 +0800766 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
767 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
768 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400769
you.chen35020192022-05-06 11:30:57 +0800770 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
771 return -1;
772 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400773
you.chen35020192022-05-06 11:30:57 +0800774 CHECK_IDX(idx, CTRL_AP);
775
776 CHECK_WPA_CTRL(CTRL_AP);
777
778 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
779 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
780 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
781
you.chen6c2dd9c2022-05-16 17:55:28 +0800782 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800783 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
784 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
785 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500786
qs.xiong1af5daf2022-03-14 09:12:12 -0400787 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500788}
789
qs.xiongf1b525b2022-03-31 00:58:23 -0400790int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500791{
you.chen35020192022-05-06 11:30:57 +0800792 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400793
you.chen35020192022-05-06 11:30:57 +0800794 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400795
you.chen35020192022-05-06 11:30:57 +0800796 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
797 return -1;
798 }
799 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400800
801 return 0;
802}
803
qs.xiongf1b525b2022-03-31 00:58:23 -0400804int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
805{
you.chen35020192022-05-06 11:30:57 +0800806 CHECK_IDX(idx, CTRL_AP);
807 switch(bandwidth){
808 case LYNQ_WIFI_BANDWIDTH_HT10:
809 {
810 printf("bandwith [%d] not support now\n", bandwidth);
811 return -1;
812 }
813 case LYNQ_WIFI_BANDWIDTH_HT20:
814 {
815 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
816 system("wl down");
817 if (system(lynq_cmd_bandwith) != 0 ){
818 return -1;
819 }
820 system("wl up");
821 break;
822 }
823 case LYNQ_WIFI_BANDWIDTH_HT40:
824 {
825 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
826 sprintf(lynq_cmd_bandwith, "wl chanspec ");
827 system("wl down");
828 if (system(lynq_cmd_bandwith) != 0 ){
829 return -1;
830 }
831 system("wl up");
832 break;
833 }
834 case LYNQ_WIFI_BANDWIDTH_HT80:
835 {
836 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
837 system("wl down");
838 if (system(lynq_cmd_bandwith) != 0 ){
839 return -1;
840 }
841 system("wl up");
842 break;
843 }
844 default:
845 {
846 printf("auth type [%d] not support now\n", bandwidth);
847 return -1;
848 }
849 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400850
851
you.chen35020192022-05-06 11:30:57 +0800852 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400853}
you.chen35020192022-05-06 11:30:57 +0800854
qs.xiongf1b525b2022-03-31 00:58:23 -0400855int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
856{
you.chen35020192022-05-06 11:30:57 +0800857 int count = 0;
858 int index = 0;
859 char *split_words[128] = {0};
860 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400861
you.chen35020192022-05-06 11:30:57 +0800862 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500863
you.chen35020192022-05-06 11:30:57 +0800864 CHECK_WPA_CTRL(CTRL_AP);
865
866 DO_REQUEST(lynq_chanspec_cmd);
867
868 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
869 for(;index < count; index++) {
870 if (strncmp(split_words[index], "bw", 2) != 0) {
871 continue;
872 }
873
874 index++;
875 if (index >= count) {
876 return -1;
877 }
878
879 printf("bw %s\n", split_words[index]);
880 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
881 return 0;
882 }
883
884 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500885}
qs.xiong0fb469a2022-04-14 03:50:45 -0400886
qs.xiongf1b525b2022-03-31 00:58:23 -0400887int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500888{
you.chen35020192022-05-06 11:30:57 +0800889 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400890
you.chen35020192022-05-06 11:30:57 +0800891 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400892
you.chen35020192022-05-06 11:30:57 +0800893 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400894
you.chen35020192022-05-06 11:30:57 +0800895 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
896 return -1;
897 }
898
899 system("wl down");
900 if (system(lynq_cmd_channel) != 0 ){
901 return -1;
902 }
903 system("wl up");
904 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500905}
qs.xiong0fb469a2022-04-14 03:50:45 -0400906
qs.xiongf1b525b2022-03-31 00:58:23 -0400907int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500908{
you.chen35020192022-05-06 11:30:57 +0800909 int count = 0;
910 int index = 0;
911 char *split_words[128] = {0};
912 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400913
you.chen35020192022-05-06 11:30:57 +0800914 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400915
you.chen35020192022-05-06 11:30:57 +0800916 CHECK_WPA_CTRL(CTRL_AP);
917
918 DO_REQUEST(lynq_chanspec_cmd);
919
920 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
921 for(;index < count; index++) {
922 printf("---- %s\n",split_words[index]);
923 if (strncmp(split_words[index], "channel", 2) != 0) {
924 continue;
925 }
926
927 index++;
928 if (index >= count) {
929 return -1;
930 }
931
932 *channel = atoi(split_words[index]);
933 return 0;
934 }
935
936 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500937}
938
939
you.chen35020192022-05-06 11:30:57 +0800940int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500941{
you.chen6c2dd9c2022-05-16 17:55:28 +0800942 char ssid[MAX_CMD] = {0};
943 int freq = 0;
944 char lynq_auth_cmd[64]={0};
945 char lynq_auth_alg_cmd[64]={0};
946 char lynq_psk_cmd[64]={0};
947 char lynq_pairwise_cmd[64]={0};
948 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +0800949 CHECK_IDX(idx, CTRL_AP);
950
you.chen6c2dd9c2022-05-16 17:55:28 +0800951 CHECK_WPA_CTRL(CTRL_AP);
952
you.chen35020192022-05-06 11:30:57 +0800953 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
954 return -1;
955 }
956
you.chen92fd5d32022-05-25 10:09:47 +0800957 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800958 if (org_auth == auth) {
959 return 0;
960 }
961 else {
962 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
963 ssid[0] = '\0';
964 }
965 lynq_wifi_ap_frequency_get(idx, &freq);
966
967 DO_OK_FAIL_REQUEST(cmd_disconnect);
968 DO_OK_FAIL_REQUEST(cmd_remove_all);
969 if (ssid[0] != '\0') {
970 lynq_wifi_ap_ssid_set(idx, ssid);
971 }
972 if (freq != 0) {
973 lynq_wifi_ap_frequency_set(idx, freq);
974 }
975 }
976 }
you.chen35020192022-05-06 11:30:57 +0800977
qs.xiong7a105ce2022-03-02 09:43:11 -0500978 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400979 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +0800980 {
you.chen35020192022-05-06 11:30:57 +0800981 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +0800982 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500983
you.chen35020192022-05-06 11:30:57 +0800984 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -0500985 break;
986 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800987 case LYNQ_WIFI_AUTH_WEP:
988 {
989 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +0800990 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +0800991 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
992
993 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
994 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
995 break;
996 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400997 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +0800998 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -0500999 {
you.chen35020192022-05-06 11:30:57 +08001000 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1001 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1002 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1003 }
1004 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001005 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001006 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001007 }
1008// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1009// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1010 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001011
you.chen35020192022-05-06 11:30:57 +08001012 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1013 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1014 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001015 break;
you.chen35020192022-05-06 11:30:57 +08001016 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001017 default:
you.chen35020192022-05-06 11:30:57 +08001018 {
1019 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001020 return -1;
you.chen35020192022-05-06 11:30:57 +08001021 }
1022 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001023 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001024
qs.xiong7a105ce2022-03-02 09:43:11 -05001025 return 0;
1026}
1027
you.chen35020192022-05-06 11:30:57 +08001028int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001029{
you.chen35020192022-05-06 11:30:57 +08001030 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001031 char lynq_auth_alg_str[MAX_RET] = {0};
1032 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001033
1034 CHECK_IDX(idx, CTRL_AP);
1035
1036 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1037 return -1;
1038 }
1039
1040 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001041 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1042 *auth = LYNQ_WIFI_AUTH_OPEN;
1043 }
1044 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1045 *auth = LYNQ_WIFI_AUTH_WEP;
1046 }
1047 else {
1048 *auth = LYNQ_WIFI_AUTH_OPEN;
1049 }
you.chen35020192022-05-06 11:30:57 +08001050 }
you.chen92fd5d32022-05-25 10:09:47 +08001051 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001052 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001053 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001054 }
1055 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1056 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1057 }
1058 else {
1059 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1060 }
you.chen35020192022-05-06 11:30:57 +08001061 }
you.chen92fd5d32022-05-25 10:09:47 +08001062 else {
1063 *auth = -1;
1064 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001065
you.chen6c2dd9c2022-05-16 17:55:28 +08001066 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001067}
qs.xiong1af5daf2022-03-14 09:12:12 -04001068
qs.xiong1af5daf2022-03-14 09:12:12 -04001069
qs.xiongf1b525b2022-03-31 00:58:23 -04001070int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001071{
you.chen35020192022-05-06 11:30:57 +08001072 char LYNQ_WIFI_CMD[128]={0};
1073 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1074 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001075
you.chen35020192022-05-06 11:30:57 +08001076 CHECK_IDX(idx, CTRL_AP);
1077
1078 CHECK_WPA_CTRL(CTRL_AP);
1079
1080// system("connmanctl enable wifi");
1081// system("connmanctl tether wifi on cy-test 12345678");
1082// system("ifconfig wlan0 down");
1083// system("ifconfig wlan0 up");
1084// system("ifconfig wlan0 up");
1085
1086 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1087 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1088
1089 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1090 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1091
qs.xiong7a105ce2022-03-02 09:43:11 -05001092 return 0;
1093}
1094
qs.xiongf1b525b2022-03-31 00:58:23 -04001095int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001096{
you.chen35020192022-05-06 11:30:57 +08001097 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001098}
1099
qs.xiongf1b525b2022-03-31 00:58:23 -04001100int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001101{
you.chen35020192022-05-06 11:30:57 +08001102 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001103
you.chen35020192022-05-06 11:30:57 +08001104 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001105
you.chen35020192022-05-06 11:30:57 +08001106 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001107
you.chen35020192022-05-06 11:30:57 +08001108 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1109
1110 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1111
you.chenb4b121c2022-05-06 17:50:16 +08001112// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001113
qs.xiong7a105ce2022-03-02 09:43:11 -05001114 return 0;
1115}
qs.xiong1af5daf2022-03-14 09:12:12 -04001116
qs.xiongf1b525b2022-03-31 00:58:23 -04001117int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001118{
you.chen35020192022-05-06 11:30:57 +08001119 char lynq_disable_cmd[128] = {0};
1120 char lynq_select_cmd[128] = {0};
1121 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001122
you.chen35020192022-05-06 11:30:57 +08001123 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001124
you.chen35020192022-05-06 11:30:57 +08001125 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001126
you.chen35020192022-05-06 11:30:57 +08001127 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1128 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1129
1130 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1131 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1132 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001133
1134 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001135}
1136
qs.xiongf1b525b2022-03-31 00:58:23 -04001137int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001138{
you.chen35020192022-05-06 11:30:57 +08001139 char lynq_disable_cmd[128] = {0};
1140 char lynq_select_cmd[128] = {0};
1141 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001142
you.chen35020192022-05-06 11:30:57 +08001143 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001144
you.chen35020192022-05-06 11:30:57 +08001145 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001146
you.chen35020192022-05-06 11:30:57 +08001147 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1148 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1149
1150 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1151 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1152 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001153
1154 return 0;
1155}
qs.xiongf1b525b2022-03-31 00:58:23 -04001156
you.chen35020192022-05-06 11:30:57 +08001157int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001158{
qs.xiongf1b525b2022-03-31 00:58:23 -04001159 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001160 char lynq_tmp_cmd[MAX_CMD] = {0};
1161 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001162 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001163 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001164 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001165 return -1;
you.chen35020192022-05-06 11:30:57 +08001166 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001167
you.chen35020192022-05-06 11:30:57 +08001168 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001169
you.chen6c2dd9c2022-05-16 17:55:28 +08001170 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1171 return -1;
1172 }
1173 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1174 return -1;
1175 }
1176
you.chen35020192022-05-06 11:30:57 +08001177 CHECK_WPA_CTRL(CTRL_AP);
1178
you.chen6c2dd9c2022-05-16 17:55:28 +08001179 if (auth == LYNQ_WIFI_AUTH_WEP) {
1180 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1181 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1182 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1183 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1184 }
1185 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1186 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1187 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1188 }
1189 else {
1190 return -1;
1191 }
you.chen35020192022-05-06 11:30:57 +08001192
you.chen35020192022-05-06 11:30:57 +08001193 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001194
qs.xiongf1b525b2022-03-31 00:58:23 -04001195 return 0;
1196}
1197
you.chen35020192022-05-06 11:30:57 +08001198int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001199{
you.chen35020192022-05-06 11:30:57 +08001200 FILE * fp;
1201 int len, ret;
1202 int count, index;
1203 char *split_lines[128] = {0};
1204 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001205
you.chen35020192022-05-06 11:30:57 +08001206 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001207
you.chen35020192022-05-06 11:30:57 +08001208 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1209// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1210 if (NULL == fp) {
1211 printf("open file fail\n");
1212 return -1;
1213 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001214
you.chen35020192022-05-06 11:30:57 +08001215 buff = alloca(MAX_RET);
1216 fseek(fp, 0, SEEK_SET);
1217 len = fread(buff, 1, MAX_RET, fp);
1218 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001219
you.chen35020192022-05-06 11:30:57 +08001220 for(index=0; index < len; index ++) {
1221 if (memcmp(buff + index, "network={", 9) != 0) {
1222 continue;
1223 }
1224 p = buff + index + 9;
1225 for (; index < len; index ++ ) {
1226 if (buff[index] != '}') {
1227 continue;
1228 }
1229 buff[index] = '\0';
1230 break;
1231 }
1232 len = buff + index - p;
1233 }
1234
1235 count = lynq_split(p, len, '\n', split_lines);
1236
1237 ret = -1;
1238 for(index=0; index < count; index++) {
1239 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001240 if (p != NULL) {
1241 p += 4;
1242 if (*p == '\"') {
1243 p++;
1244 }
you.chen35020192022-05-06 11:30:57 +08001245 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001246 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1247 p += 9;
1248 if (*p == '\"') {
1249 p++;
1250 }
1251 }
1252 else {
1253 continue;
you.chen35020192022-05-06 11:30:57 +08001254 }
1255
1256 strcpy(password, p);
1257
1258 while(*password != '\0') {
1259 if (*password == '\"') {
1260 *password = '\0';
1261 break;
1262 }
1263 password++;
1264 }
1265 ret = 0;
1266 break;
1267 } //end for(index=0; index < count; index++)
1268
1269 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001270}
1271
you.chen35020192022-05-06 11:30:57 +08001272static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1273 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001274 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001275
you.chen35020192022-05-06 11:30:57 +08001276 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1277 return -1;
1278 }
1279
1280 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001281
1282 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1283 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1284 if (strcmp(lynq_proto_str, "RSN") == 0) {
1285 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1286 }
1287 }
1288 }
you.chen35020192022-05-06 11:30:57 +08001289 return 0;
1290}
1291
1292int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001293{
you.chen35020192022-05-06 11:30:57 +08001294 int pass_len, net_no, count, index;
1295 char lynq_tmp_cmd[300]={0};
1296 int net_no_list[128];
1297 lynq_wifi_auth_s net_auth;
1298 pass_len=strlen(password);
1299 if(pass_len < 8 || pass_len >= 64){
1300 return -1;
1301 }
1302
1303 CHECK_IDX(idx, CTRL_STA);
1304
1305 net_no = -1;
1306 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1307
1308 for (index=0; index < count; index++) {
1309 net_auth = -1;
1310 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1311 net_no = net_no_list[index];
1312 break;
1313 }
1314 }
1315
1316 if (net_no < 0) {
1317 return -1;
1318 }
1319
1320 CHECK_WPA_CTRL(CTRL_STA);
1321
1322 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1323
1324 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1325 DO_OK_FAIL_REQUEST(cmd_save_config);
1326
1327 return 0;
1328}
1329
1330int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1331
1332 FILE * fp;
1333 int len, ret;
1334 int count, index;
1335 char *split_lines[128] = {0};
1336 char *buff, *p;
1337
1338 CHECK_IDX(idx, CTRL_STA);
1339
1340 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1341 if (NULL == fp) {
1342 printf("open file fail\n");
1343 return -1;
1344 }
1345
1346 buff = alloca(MAX_RET);
1347 fseek(fp, 0, SEEK_SET);
1348 len = fread(buff, 1, MAX_RET, fp);
1349 fclose(fp);
1350
1351 for(index=0; index < len; index ++) {
1352 for(; index < len; index ++) {
1353 if (memcmp(buff + index, "network={", 9) != 0) {
1354 continue;
1355 }
1356 p = buff + index + 9;
1357 for (; index < len; index ++ ) {
1358 if (buff[index] != '}') {
1359 continue;
1360 }
1361 buff[index] = '\0';
1362 break;
1363 }
1364 len = buff + index - p;
1365 }
1366
1367 if (strstr(p, ap->ap_ssid) != NULL) {
1368 break;
1369 }
1370 }
1371
1372 if (index >= len) {
1373 return -1;
1374 }
1375
1376 count = lynq_split(p, len, '\n', split_lines);
1377
1378 ret = -1;
1379 for(index=0; index < count; index++) {
1380 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001381 if (p != NULL) {
1382 p += 4;
1383 if (*p == '\"') {
1384 p++;
1385 }
you.chen35020192022-05-06 11:30:57 +08001386 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001387 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1388 p += 9;
1389 if (*p == '\"') {
1390 p++;
1391 }
1392 }
1393 else {
1394 continue;
you.chen35020192022-05-06 11:30:57 +08001395 }
1396
1397 strcpy(password, p);
1398
1399 while(*password != '\0') {
1400 if (*password == '\"') {
1401 *password = '\0';
1402 break;
1403 }
1404 password++;
1405 }
1406 ret = 0;
1407 break;
1408 } //end for(index=0; index < count; index++)
1409
1410 return ret;
1411}
1412
1413static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1414{
qs.xiong97fa59b2022-04-07 05:41:29 -04001415 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001416
you.chen35020192022-05-06 11:30:57 +08001417 if (sta_ssid == NULL) {
1418 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001419 return -1;
you.chen35020192022-05-06 11:30:57 +08001420 }
1421
1422 CHECK_WPA_CTRL(CTRL_STA);
1423
1424 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1425
1426 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1427// DO_OK_FAIL_REQUEST(cmd_save_config);
1428
qs.xiong7a105ce2022-03-02 09:43:11 -05001429 return 0;
1430
1431}
1432
you.chen35020192022-05-06 11:30:57 +08001433static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001434{
you.chen35020192022-05-06 11:30:57 +08001435 char lynq_disable_cmd[128]={0};
1436 char lynq_select_cmd[128]={0};
1437
1438 CHECK_WPA_CTRL(CTRL_STA);
1439
1440 if (save != 0) {
1441 DO_OK_FAIL_REQUEST(cmd_save_config);
1442 }
1443
1444 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001445// sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1446 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001447 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1448 }
1449 else {
1450 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1451 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1452 }
1453
1454 return 0;
1455}
1456
1457int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1458{
1459 CHECK_IDX(idx, CTRL_STA);
1460
you.chen6c2dd9c2022-05-16 17:55:28 +08001461 curr_status_info curr_state;
1462 ap_info_s ap_info;
1463 curr_state.ap = &ap_info;
1464 curr_state.state = NULL;
1465
1466 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1467 strcpy(sta_ssid, ap_info.ap_ssid);
1468 return 0;
1469 }
1470
1471 return -1;
you.chen35020192022-05-06 11:30:57 +08001472}
1473
1474int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1475{
1476 scan_info_s *scan_list;
1477 saved_ap_info_s *save_list;
1478 int scan_len=0;
1479 int save_len=0;
1480 int best_index = -1;
1481 int best_scan_index = -1;
1482 int best_rssi = 0;
1483 int i, j;
1484
1485 CHECK_IDX(idx, CTRL_STA);
1486 if (info == NULL) {
1487 return -1;
1488 }
1489
1490 curr_status_info curr_state;
1491 ap_info_s ap_info;
1492 curr_state.ap = &ap_info;
1493 curr_state.state = NULL;
1494
1495 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1496 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1497 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1498 lynq_get_connect_ap_rssi(idx, &info->rssi);
1499 return 0;
1500 }
1501
1502 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1503 return -1;
1504 }
1505
1506 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1507 return -1;
1508 }
1509
1510 for (i=0; i < save_len; i++) {
1511 for (j=0; j < scan_len; j++) {
1512 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1513 && save_list[i].base_info.auth == scan_list[j].auth) {
1514 if (best_rssi == 0) {
1515 best_rssi = scan_list[j].rssi;
1516 }
1517 else if (best_rssi > scan_list[j].rssi) {
1518 best_index = i;
1519 best_scan_index = j;
1520 best_rssi = scan_list[j].rssi;
1521 }
1522 break;
1523 }
1524 }
1525 }
1526
1527 if (best_index >= 0) {
1528 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1529 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1530 info->rssi = best_rssi;
1531 return 0;
1532 }
1533
1534 return -1;
1535}
1536
1537static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1538{
1539 char lynq_auth_cmd[64]={0};
1540 char lynq_ket_mgmt_cmd[64]={0};
1541 char lynq_pairwise_cmd[64]={0};
1542 char lynq_psk_cmd[64]={0};
1543
1544 CHECK_WPA_CTRL(CTRL_STA);
1545
qs.xiong1af5daf2022-03-14 09:12:12 -04001546 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001547 case LYNQ_WIFI_AUTH_OPEN:
1548 {
1549 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001550
you.chen35020192022-05-06 11:30:57 +08001551 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1552// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001553 break;
1554 }
you.chen35020192022-05-06 11:30:57 +08001555 case LYNQ_WIFI_AUTH_WPA_PSK:
1556 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001557 {
you.chen35020192022-05-06 11:30:57 +08001558 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1559 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1560 }
1561 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001562 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001563 }
1564 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1565 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001566
you.chen35020192022-05-06 11:30:57 +08001567 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1568 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1569 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001570
you.chen35020192022-05-06 11:30:57 +08001571 if (password != NULL) {
1572 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1573 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1574 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001575
you.chen35020192022-05-06 11:30:57 +08001576// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001577 break;
you.chen35020192022-05-06 11:30:57 +08001578 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001579 default:
1580 return -1;
you.chen35020192022-05-06 11:30:57 +08001581 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001582
qs.xiongf1b525b2022-03-31 00:58:23 -04001583 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001584}
qs.xiong7a105ce2022-03-02 09:43:11 -05001585
you.chen35020192022-05-06 11:30:57 +08001586static int inner_get_curr_net_no(int interface) {
1587 curr_status_info curr_state;
1588 curr_state.ap = NULL;
1589 curr_state.state = NULL;
1590
1591 if (0 != inner_get_status_info(interface, &curr_state)) {
1592 return -1;
1593 }
1594
1595 return curr_state.net_no;
1596}
1597
1598int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001599{
you.chen35020192022-05-06 11:30:57 +08001600 int net_no;
1601 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001602
you.chen35020192022-05-06 11:30:57 +08001603 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001604
you.chen35020192022-05-06 11:30:57 +08001605 if (net_no < 0) {
1606 return -1;
1607 }
1608
1609 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001610}
1611
you.chen35020192022-05-06 11:30:57 +08001612int 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 -05001613{
you.chen35020192022-05-06 11:30:57 +08001614 int count, net_no, index;
1615 int net_no_list[128];
1616 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001617
you.chen35020192022-05-06 11:30:57 +08001618 if (ssid == NULL || *ssid == '\0') {
1619 printf("bad ssid\n");
1620 return -1;
1621 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001622
you.chen35020192022-05-06 11:30:57 +08001623 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1624 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1625 printf("bad password\n");
1626 return -1;
1627 }
1628 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001629
you.chen35020192022-05-06 11:30:57 +08001630 CHECK_IDX(idx, CTRL_STA);
1631
1632 net_no = -1;
1633 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1634
1635 for (index=0; index < count; index++) {
1636 net_auth = -1;
1637 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1638 net_no = net_no_list[index];
1639 break;
1640 }
1641 }
1642
1643 if (net_no < 0) {
1644 net_no = lynq_add_network(CTRL_STA);
1645 if (net_no == -1) {
1646 return -1;
1647 }
1648
1649 printf("net no is %d\n", net_no);
1650 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1651 return -1;
1652 }
1653 }
1654
1655 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1656 return -1;
1657 }
1658
1659 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001660}
1661
you.chen35020192022-05-06 11:30:57 +08001662int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001663{
you.chen35020192022-05-06 11:30:57 +08001664 ap_info_s ap;
1665 curr_status_info curr_state;
1666 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001667
you.chen35020192022-05-06 11:30:57 +08001668 if (ssid == NULL || *ssid == '\0') {
1669 return -1;
1670 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001671
you.chen35020192022-05-06 11:30:57 +08001672 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001673
you.chen35020192022-05-06 11:30:57 +08001674 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001675 curr_state.state = NULL;
1676
you.chen35020192022-05-06 11:30:57 +08001677 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1678 return 0;
1679 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001680
you.chen35020192022-05-06 11:30:57 +08001681 if (strcmp(ap.ap_ssid, ssid) != 0) {
1682 return 0;
1683 }
1684
1685 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001686}
qs.xiong97fa59b2022-04-07 05:41:29 -04001687
you.chena6cd55a2022-05-08 12:20:18 +08001688int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1689{
1690// const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong7a105ce2022-03-02 09:43:11 -05001691
you.chen35020192022-05-06 11:30:57 +08001692 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001693 CHECK_WPA_CTRL(CTRL_STA);
1694
1695 system("connmanctl enable wifi");
1696
you.chena6fa5b22022-05-18 10:28:19 +08001697 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001698 return -1;
1699 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001700
you.chena6cd55a2022-05-08 12:20:18 +08001701// DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
1702
1703 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001704}
1705
you.chen35020192022-05-06 11:30:57 +08001706int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001707{
you.chena6cd55a2022-05-08 12:20:18 +08001708 char lynq_disable_network_cmd[MAX_CMD];
1709 curr_status_info curr_state;
1710 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001711
you.chena6cd55a2022-05-08 12:20:18 +08001712 CHECK_IDX(idx, CTRL_STA);
1713 CHECK_WPA_CTRL(CTRL_STA);
1714
1715 curr_state.ap = &ap_info;
1716 curr_state.state = NULL;
1717
1718 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1719 return 0;
1720 }
1721
1722 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1723 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1724
1725 DO_OK_FAIL_REQUEST(cmd_save_config);
1726
1727 return 0;
1728// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001729}
qs.xiong7a105ce2022-03-02 09:43:11 -05001730
you.chen35020192022-05-06 11:30:57 +08001731//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1732// int i, count;
1733// char *p;
1734// const char * FLAG_SSID = "ssid=";
1735// const char * FLAG_SBSID = "bssid=";
1736// const char * FLAG_KEY_MGMT = "key_mgmt=";
1737// const char * FLAG_FREQ = "freq=";
1738// char lynq_sta_cmd[MAX_CMD];
1739// char *split_lines[128] = {0};
1740
1741// CHECK_WPA_CTRL(CTRL_AP);
1742
1743// sprintf(lynq_sta_cmd, "STA %s", bssid);
1744
1745// DO_REQUEST(lynq_sta_cmd);
1746
1747// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1748
1749// for(i=0; i < count; i++) {
1750// p = strstr(split_lines[i], FLAG_SSID);
1751// if (p != NULL) {
1752// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1753// continue;
1754// }
1755// }
1756
1757// lynq_get_interface_ip(idx, ap->ap_ip);
1758// lynq_ap_password_set(idx, ap->psw);
1759
1760// return 0;
1761//}
1762
1763static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1764 curr_status_info curr_state;
1765 curr_state.ap = ap;
1766 curr_state.state = NULL;
1767 return inner_get_status_info(interface, &curr_state);
1768}
1769
1770int 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 -04001771{
you.chen35020192022-05-06 11:30:57 +08001772 int ip_count, index, i, line_count;
1773 const char *lynq_first_sta_cmd = "STA-FIRST";
1774 char lynq_next_sta_cmd[MAX_CMD];
1775 char *bssid[1024] = {0};
1776 char *mac_list[128] = {0};
1777 char *ip_list[128] = {0};
1778 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001779
you.chen35020192022-05-06 11:30:57 +08001780 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001781
you.chen35020192022-05-06 11:30:57 +08001782 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001783
you.chen35020192022-05-06 11:30:57 +08001784// ap_info_s * tmp_ap;
1785// device_info_s * tmp_list;
1786 if (ap == NULL || list == NULL || len == NULL) {
1787 printf("bad input param");
1788 return -1;
1789 }
1790
1791// ap = &tmp_ap;
1792// list = &tmp_list;
1793 *ap = malloc(sizeof (ap_info_s));
1794
1795 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1796 return -1;
1797 }
1798
1799 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1800 lynq_ap_password_get(idx, (*ap)->psw);
1801
1802 ip_count = get_ip_mac_list(mac_list, ip_list);
1803 printf("get count %d\n", ip_count);
1804
1805 DO_REQUEST(lynq_first_sta_cmd);
1806
1807 index = 0;
1808 while (reply_len > 0) {
1809 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1810 break;
1811 }
1812 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1813 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1814 strcpy(bssid[index], split_lines[0]);
1815 index++;
1816 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1817 reply_len = MAX_RET;
1818 cmd_reply[0] = '\0';
1819 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1820 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1821 break;
1822 }
1823 }
1824
1825 *len = index;
1826
1827 *list = malloc(sizeof(device_info_s) * (*len));
1828 for (index=0; index < *len; index++) {
1829 strcpy((*list)[index].sta_mac, bssid[index]);
1830 for(i=0;i < ip_count; i++ ) {
1831 if (strcmp(bssid[index], mac_list[i]) == 0) {
1832 strcpy((*list)[index].sta_ip, ip_list[i]);
1833 break;
1834 }
1835 }
1836 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1837 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1838 free(bssid[index]);
1839 }
1840
1841 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001842}
1843
you.chen35020192022-05-06 11:30:57 +08001844int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001845{
you.chen35020192022-05-06 11:30:57 +08001846 int i, count, index, count_words;
1847 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1848 char *split_lines[128] = {0};
1849 char *split_words[128] = {0};
1850 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001851
you.chen35020192022-05-06 11:30:57 +08001852 if (list == NULL || len == NULL) {
1853 return -1;
1854 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001855
you.chen35020192022-05-06 11:30:57 +08001856 CHECK_IDX(idx, CTRL_STA);
1857
1858 CHECK_WPA_CTRL(CTRL_STA);
1859
1860 DO_REQUEST(lynq_scan_result_cmd);
1861
1862 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1863 *len = count - 1;
1864 *list = malloc(sizeof (scan_info_s) * *len);
1865
1866 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1867 for (index=0; index <count_words; index++) {
1868 printf("----header: %s\n", split_words[index]);
1869 }
1870
1871 for(index = 1;index < count; index++) {
1872 printf("---- %s\n",split_lines[index]);
1873 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1874 if (count_words < 4)
1875 continue;
1876 printf("count: %d, %s\n", count_words, split_words[0]);
1877 //bssid / frequency / signal level / flags / ssid
1878 p = (*list) + index - 1;
1879 strcpy(p->mac, split_words[0]);
1880 p->band = convert_band_from_freq(atoi(split_words[1]));
1881 p->rssi = -1 * atoi( split_words[2]);
1882 p->auth = convert_max_auth_from_flag(split_words[3]);
1883 strcpy(p->ssid, split_words[4]);
1884 }
1885
1886 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001887}
qs.xiong97fa59b2022-04-07 05:41:29 -04001888
you.chen35020192022-05-06 11:30:57 +08001889int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1890{
1891 int count, net_no, index;
1892 int net_no_list[128];
1893 lynq_wifi_auth_s net_auth;
1894 char lynq_remove_cmd[MAX_CMD];
1895
1896 if (ssid == NULL || *ssid == '\0') {
1897 printf("bad ssid\n");
1898 return -1;
1899 }
1900
1901 CHECK_IDX(idx, CTRL_STA);
1902
1903 CHECK_WPA_CTRL(CTRL_STA);
1904
1905 net_no = -1;
1906 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1907
1908 for (index=0; index < count; index++) {
1909 net_auth = -1;
1910 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1911 net_no = net_no_list[index];
1912 break;
1913 }
1914 }
1915
1916 if (net_no < 0) {
1917 return 0;
1918 }
1919
1920 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1921
1922 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1923 DO_OK_FAIL_REQUEST(cmd_save_config);
1924
1925 return 0;
1926}
1927
1928int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1929{
1930 int count, index;
1931 int net_no_list[128];
1932 char freq[16];
1933
1934 if (list == NULL || len == NULL) {
1935 printf("bad param\n");
1936 return -1;
1937 }
1938
1939 CHECK_IDX(idx, CTRL_STA);
1940
1941// CHECK_WPA_CTRL(CTRL_STA);
1942
1943 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1944 printf("count is %d\n", count);
1945
1946 *list = malloc(sizeof (saved_ap_info_s) * count);
1947 *len = count;
1948
1949 for (index=0; index < count; index++) {
1950 printf("to get ssid %d\n", index);
1951 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1952 printf("to get bssid\n");
1953 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1954 printf("to get inner_get_network_auth\n");
1955 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1956 printf("to get frequency\n");
1957 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1958 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1959 }
1960 else {
1961 (*list)[index].base_info.band = -1;
1962 }
1963
1964 }
1965
1966 return 0;
1967}
1968
1969int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1970{
1971 const char *lynq_scan_cmd = "SCAN";
1972
1973 CHECK_IDX(idx, CTRL_STA);
1974
1975 CHECK_WPA_CTRL(CTRL_STA);
1976
1977 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1978
1979 return 0;
1980}
1981
1982int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1983 if (cb == NULL) {
1984 return -1;
1985 }
1986
1987 g_ap_callback_priv = priv;
1988 g_ap_callback_func = cb;
1989
1990 return 0;
1991}
1992
1993int lynq_unreg_ap_event_callback(void * priv) {
1994 if (g_ap_callback_priv == priv) {
1995 g_ap_callback_func = NULL;
1996 g_ap_callback_priv = NULL;
1997 return 0;
1998 }
1999 return -1;
2000}
2001
2002int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2003 if (cb == NULL) {
2004 return -1;
2005 }
2006
2007 g_sta_callback_priv = priv;
2008 g_sta_callback_func = cb;
2009
2010 return 0;
2011}
2012
2013int lynq_unreg_sta_event_callback(void * priv) {
2014 if (g_sta_callback_priv == priv) {
2015 g_sta_callback_func = NULL;
2016 g_sta_callback_priv = NULL;
2017 return 0;
2018 }
2019 return -1;
2020}
2021
2022
2023static int inner_get_status_info_state (int interface, char *state) {
2024 curr_status_info curr_state;
2025 curr_state.ap = NULL;
2026 curr_state.state = state;
2027 return inner_get_status_info(interface, &curr_state);
2028}
2029
2030int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2031{
2032 char state[MAX_CMD];
2033 const char * STATE_COMPLETED = "COMPLETED";
2034 CHECK_IDX(idx, CTRL_AP);
2035
2036 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2037 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2038 return 0;
2039 }
2040
2041 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2042 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2043 }
2044 else {
2045 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2046 }
2047
2048 return 0;
2049}
2050
2051int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2052 char state[MAX_CMD];
2053 const char * STATE_COMPLETED = "COMPLETED";
2054 CHECK_IDX(idx, CTRL_STA);
2055
2056 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2057 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2058 return 0;
2059 }
2060
2061 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2062 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2063 }
2064 else {
2065 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2066 }
2067
2068 return 0;
2069}
2070
2071int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2072// CHECK_IDX(idx, CTRL_AP);
2073// int ret = 0;
2074// size_t reply_len = MAX_RET;
2075// char cmd_reply[MAX_RET]={0};
2076// const char * cmd_str = "GET country";
2077// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2078// do{
2079// if (NULL == s_lynq_wpa_ctrl) {
2080// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2081// if (NULL == s_lynq_wpa_ctrl ) {
2082// printf("wpa_ctrl_open fail\n");
2083// return -1;
2084// }
2085// }
2086// }while(0);
2087
2088// do {
2089// reply_len = MAX_RET;
2090// cmd_reply[0] = '\0';
2091// printf("to call [%s]\n", cmd_str);
2092// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
2093// if (ret != 0) {
2094// printf("call ##cmd_str fail %d\n", ret);
2095// return ret;
2096// }
2097// cmd_reply[reply_len+1] = '\0';
2098// printf("cmd replay [ %s ]\n", cmd_reply);
2099// }while(0);
2100
2101 FILE *fp;
2102 size_t i = 0;
2103 char lynq_cmd_ret[MAX_RET]={0};
2104
2105// CHECK_IDX(idx, CTRL_AP);
2106
2107 if((fp=popen("wl country","r"))==NULL)
2108 {
2109 perror("popen error!");
2110 return -1;
2111 }
2112 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2113 {
2114 perror("fread fail!");
2115 return -1;
2116 }
2117
2118 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2119 if (lynq_cmd_ret[i] == ' ') {
2120 lynq_cmd_ret[i] = '\0';
2121 break;
2122 }
2123 }
2124
2125 strcpy(country_code,lynq_cmd_ret);
2126 printf("---country code %s\n", country_code);
2127
2128 int ret=pclose(fp);
2129 if(ret==-1)
2130 {
2131 perror("close file faild");
2132 }
2133
2134 return 0;
2135}
2136
2137int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2138// const char * cmd_str = "GET country";
2139// CHECK_IDX(idx, CTRL_AP);
2140// CHECK_WPA_CTRL(CTRL_STA);
2141
2142// DO_REQUEST(cmd_str);
2143// printf("result %s\n", cmd_reply);
2144
2145 if (country_code == NULL || *country_code == '\0') {
2146 printf("bad country code\n");
2147 return -1;
2148 }
2149
2150 char lynq_country_cmd[MAX_CMD];
2151 sprintf(lynq_country_cmd, "wl country %s", country_code);
2152 if (system(lynq_country_cmd) == 0) {
2153 return 0;
2154 }
2155
2156 return -1;
2157}
2158
2159int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2160{
2161 if (mac == NULL) {
2162 return -1;
2163 }
2164
2165 CHECK_IDX(idx, CTRL_STA);
2166 ap_info_s ap;
2167 ap.ap_mac[0] = '\0';
2168
2169 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2170 return -1;
2171 }
2172 strcpy(mac, ap.ap_mac);
2173
2174 return 0;
2175}
2176
2177int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2178{
2179 struct ifaddrs *ifaddr, *ifa;
2180 int family, s;
2181 char host[NI_MAXHOST];
2182 const char * ifaName = "wlan0";
2183 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002184 ifaName = "tether";
you.chen35020192022-05-06 11:30:57 +08002185 }
2186 else if (idx != 0) {
2187 return -1;
2188 }
2189
2190 if (getifaddrs(&ifaddr) == -1)
2191 {
2192 perror("getifaddrs");
2193 return -1;
2194 //exit(EXIT_FAILURE);
2195 }
2196
2197
2198 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2199 {
2200 if (ifa->ifa_addr == NULL)
2201 continue;
2202 host[0] = '\0';
2203 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2204
2205 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2206 if((strcmp(ifa->ifa_name,ifaName)==0))
2207 {
2208 if (s != 0)
2209 {
2210 // printf("getnameinfo() failed: %s", gai_strerror(s));
2211 //exit(EXIT_FAILURE);
2212 }
2213 freeifaddrs(ifaddr);
2214 strcpy(ip, host);
2215 printf("ip %s\n", ip);
2216 return 0;
2217 //printf(" Interface : <%s>",ifa->ifa_name );
2218 //printf(" Address : <%s>", host);
2219 }
2220 }
2221 return -1;
2222}
2223
2224int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2225{
2226 int count;
2227 size_t i;
2228 char *split_words[128] = {0};
2229 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2230
2231 CHECK_WPA_CTRL(idx);
2232
2233 DO_REQUEST(lynq_get_mac_cmd);
2234
2235 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2236 return -1;
2237 }
2238
2239 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2240
2241 if (count < 2) {
2242 return -1;
2243 }
2244
2245 for (i=0; i < strlen(split_words[1]); i++ ) {
2246 if (split_words[1][i] != ' ') {
2247 break;
2248 }
2249 }
2250
2251 strcpy(mac, split_words[1] + i);
2252
2253 return 0;
2254}
2255
2256int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2257{
2258// int count;
2259// char *split_words[128] = {0};
2260// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2261
2262// if (rssi == NULL) {
2263// return -1;
2264// }
2265
2266// CHECK_IDX(idx, CTRL_STA);
2267
2268// CHECK_WPA_CTRL(CTRL_STA);
2269
2270// DO_REQUEST(lynq_get_rssi_cmd);
2271
2272// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2273// return -1;
2274// }
2275
2276// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2277
2278// if (count < 2) {
2279// return -1;
2280// }
2281
2282// *rssi = atoi(split_words[1]) * -1;
2283
2284 FILE *fp;
2285 size_t i = 0;
2286 char lynq_cmd_ret[MAX_RET]={0};
2287
2288// CHECK_IDX(idx, CTRL_AP);
2289
2290 if((fp=popen("wl country","r"))==NULL)
2291 {
2292 perror("popen error!");
2293 return -1;
2294 }
2295 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2296 {
2297 perror("fread fail!");
2298 return -1;
2299 }
2300 *rssi = atoi(lynq_cmd_ret);
2301
2302 return 0;
2303}
2304
2305int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2306{
2307 if (band == NULL) {
2308 return -1;
2309 }
2310
2311 CHECK_IDX(idx, CTRL_STA);
2312 ap_info_s ap;
2313 ap.band = -1;
2314
2315 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2316 return -1;
2317 }
2318 *band = ap.band;
2319
2320 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002321}