blob: 2d7290fc54a6f33e22e97c37e7105e751a55fc8d [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.chen9f17e4d2022-06-06 17:18:18 +0800271 //@todo delete add temp check for socket avilable start (20220606)
272 for (i=0; i<60; i++)
273 {
274 if (system("netstat -an | grep -q DGRAM") == 0) {
275 break;
276 }
277 sleep(1);
278 }
279
280 if (i >= 60)
281 {
282 return -1;
283 }
284 //@todo delete add temp check for socket avilable end (20220606)
285
you.chena6fa5b22022-05-18 10:28:19 +0800286 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800287 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800288 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800289 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800290 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800291 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800292 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800293 }
294
you.chen35020192022-05-06 11:30:57 +0800295 if (g_ap_watcher_pid == 0 ) {
296 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
297 if(ret<0){
298 return -1;
299 }
300 }
301
302 if (g_sta_watcher_pid == 0 ) {
303 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
304 if(ret<0){
305 return -1;
306 }
307 }
308
you.chena6fa5b22022-05-18 10:28:19 +0800309 for (i=0; i<10; i++) {
310 usleep(300*1000);
311 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
312 break;
313 }
314 }
315
you.chen35020192022-05-06 11:30:57 +0800316 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500317}
318
qs.xiong1af5daf2022-03-14 09:12:12 -0400319int lynq_wifi_disable(void)
320{
you.chen35020192022-05-06 11:30:57 +0800321 g_ap_watcher_stop_flag = 1;
322 g_sta_watcher_stop_flag = 1;
323 if (g_ap_watcher_pid != 0)
324 pthread_join(g_ap_watcher_pid, NULL);
325 if (g_sta_watcher_pid != 0)
326 pthread_join(g_sta_watcher_pid, NULL);
327 if (g_lynq_wpa_ctrl[0] != NULL)
328 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
329 if (g_lynq_wpa_ctrl[1] != NULL)
330 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
331 g_ap_watcher_pid = 0;
332 g_sta_watcher_pid = 0;
333 g_lynq_wpa_ctrl[0] = NULL;
334 g_lynq_wpa_ctrl[1] = NULL;
335 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500336 return 0;
337}
qs.xiong1af5daf2022-03-14 09:12:12 -0400338
you.chen35020192022-05-06 11:30:57 +0800339static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
340
341 char lynq_cmd_get[128]={0};
342
343 if (out_put == NULL) {
344 printf("output ptr is null\n");
345 return -1;
346 }
347 if (param_name == NULL) {
348 printf("param ptr is null");
349 return -1;
350 }
351 if (param_name[0] == '\0') {
352 printf("param is empty");
353 return -1;
354 }
355
356 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
357
358 CHECK_WPA_CTRL(interface);
359
360 DO_REQUEST(lynq_cmd_get);
361
362 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
363 return -1;
364 }
365
you.chena6fa5b22022-05-18 10:28:19 +0800366// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800367 memcpy(out_put, cmd_reply, reply_len + 1);
368 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500369}
qs.xiong1af5daf2022-03-14 09:12:12 -0400370
you.chen35020192022-05-06 11:30:57 +0800371static int lynq_split(char * str, int len, char delimiter, char * results[]) {
372 int ret = 0;
373 char * end = str + len - 1;
374 results[ret++] = str;
375 while(str < end) {
376 if (*str == delimiter) {
377 *str++ = '\0';
378 results[ret++] = str;
379 continue;
380 }
381 str++;
382 }
383 if (*str == delimiter) {
384 *str = '\0';
385 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400386
you.chen35020192022-05-06 11:30:57 +0800387 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500388}
389
you.chen35020192022-05-06 11:30:57 +0800390static void trim_space(char * p, int count) {
391 char * begin = p;
392 p += count;
393 printf("%C-%C||\n", *begin, *p);
394 while (p >= begin ) {
395 if (*p == ' ') {
396 *p-- = '\0';
397 }
398 else {
399 break;
400 }
401 }
402}
403
404static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
405 FILE * fp;
406 int len, ret;
407 int count, count_words, index;
408 int mac_start, mac_end;
409 int ip_start, ip_end;
410 char *split_lines[128] = {0};
411 char *buff;
412 const char * ip_header = "IP address";
413 const char * mac_header = "HW address";
414 const char * zero_mac = "00:00:00:00:00:00";
415
416 fp = fopen("/proc/net/arp", "rb");
417 if (NULL == fp) {
418 printf("open file fail\n");
419 return -1;
420 }
421
422 buff = alloca(MAX_RET);
423 fseek(fp, 0, SEEK_SET);
424 len = fread(buff, 1, MAX_RET, fp);
425 fclose(fp);
426 if (len <= 0) {
427 printf("read file fail\n");
428 return -1;
429 }
430 printf("file : %s\n", buff);
431
432 count = lynq_split(buff, len, '\n', split_lines);
433 printf("----- %s\n", split_lines[0]);
434
435 mac_end = 0;
436 count_words = strlen(split_lines[0]);
437 if (strstr(split_lines[0], mac_header) != NULL) {
438 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
439 mac_end = mac_start + strlen(mac_header) + 1;
440 while (mac_end < count_words) {
441 if (split_lines[0][mac_end] != ' ') {
442 break;
443 }
444 mac_end++;
445 }
446 }
447
448 ip_end = 0;
449 if (strstr(split_lines[0], ip_header) != NULL) {
450 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
451 ip_end = ip_start + strlen(ip_header) + 1;
452 while (ip_end < count_words) {
453 if (split_lines[0][ip_end] != ' ') {
454 break;
455 }
456 ip_end++;
457 }
458 }
459
460 if (mac_end == 0 || ip_end == 0) {
461 return 0;
462 }
463
464 ret = 0;
465 for(index = 1;index < count; index++) {
466 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
467 continue;
468 }
469 mac_list[ret] = malloc(mac_end - mac_start + 1);
470 ip_list[ret] = malloc(ip_end - ip_start + 1);
471 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
472 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
473 trim_space(mac_list[ret], mac_end - mac_start - 1);
474 trim_space(ip_list[ret], ip_end - ip_start - 1);
475 ret++;
476 }
477
478 return ret;
479}
480
481static int get_hostname_by_ip(char *ip, char *hostname) {
482 struct in_addr addr ={0};
483 struct hostent *ht;
484
485 if (ip == NULL || *ip == '\0' || hostname == NULL) {
486 return -1;
487 }
488
489 if (inet_aton(ip, &addr) == 0) {
490 printf("---inet_aton fail\n");
491 return -1;
492 }
493
494 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
495
496 if (ht == NULL) {
497 printf("---gethostbyaddr fail\n");
498 herror(NULL);
499 return -1;
500 }
501
502 strcpy(hostname, ht->h_name);
503
504 return 0;
505}
506
507static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
508{
509 int count, index, words_count;
510 char * split_lines[128]= {0};
511 char * split_words[128] = {0};
512 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
513
514 CHECK_WPA_CTRL(ap_sta);
515
516 DO_REQUEST(lynq_wifi_list_networks);
517
518 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
519
520 //@todo check ssid field to compatible
521
522 ret = 0;
523 for(index=1; index < count; index++) {
524 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
525 if (words_count > 2) {
526 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
527 net_no_list[ret++] = atoi(split_words[0]);
528 }
529 }
530 }
531
532 return ret;
533}
534
535static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800536 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800537 CHECK_WPA_CTRL(ap_sta);
538 const char *lynq_wifi_add_network = "ADD_NETWORK";
539
540 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800541 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800542 return -1;
543 }
544
you.chen6c2dd9c2022-05-16 17:55:28 +0800545 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800546 if(cmd_reply[i] == '\n') {
547 cmd_reply[i] = '\0';
548 break;
549 }
550 }
551 return atoi(cmd_reply);
552}
you.chena6cd55a2022-05-08 12:20:18 +0800553
you.chen35020192022-05-06 11:30:57 +0800554static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
555{
556 int count, index;
557 int net_no_list[128];
558
559
560 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
561 for (index=0; index < count; index++) {
562 if (net_no_list[index] == net_no) {
563 return 0;
564 }
565 }
566
567 if (count >= 1)
568 index = net_no_list[count - 1];
569 else
570 index = -1;
571
572 while (index < net_no ) {
573 index = lynq_add_network(ap_sta);
574 if (index >= net_no) { // required network no created
575 return 0;
576 }
you.chena6cd55a2022-05-08 12:20:18 +0800577 else if( index < 0) {
578 printf("add network fail\n");
579 return -1;
580 }
you.chen35020192022-05-06 11:30:57 +0800581 }
582
583 if (index < 0)
584 return -1;
585
586 return 0;
587}
588
589static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
590 if (freq > 5000 && freq < 6000) {
591 return LYNQ_WIFI_5G_band;
592 }
593 else if (freq > 2000 && freq < 3000) {
594 return LYNQ_WIFI_2G_band;
595 }
596 return LYNQ_WIFI_2_and_5G_band;
597}
598
599static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
600 if (key_mgmt != NULL) {
601 if (memcmp( key_mgmt, "NONE", 4) == 0) {
602 return LYNQ_WIFI_AUTH_OPEN;
603 }
604 else if (memcmp( key_mgmt, "WEP", 3) == 0){
605 return LYNQ_WIFI_AUTH_WEP;
606 }
607 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
608 return LYNQ_WIFI_AUTH_WPA_PSK;
609 }
610 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
611 return LYNQ_WIFI_AUTH_WPA2_PSK;
612 }
613 }
614
615 return -1;
616}
617
618static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
619 if (flag != NULL) {
620 if (strstr( flag, "WPA2-PSK") != NULL){
621 return LYNQ_WIFI_AUTH_WPA2_PSK;
622 }
623 else if (strstr( flag, "WPA-PSK") != NULL){
624 return LYNQ_WIFI_AUTH_WPA_PSK;
625 }
626 else if (strstr( flag, "WEP") != NULL){
627 return LYNQ_WIFI_AUTH_WEP;
628 }
629 else if (strstr( flag, "NONE") != NULL) {
630 return LYNQ_WIFI_AUTH_OPEN;
631 }
632 }
633
634 return -1;
635}
636
637static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
638 switch (bw) {
639 case 10:
640 return LYNQ_WIFI_BANDWIDTH_HT10;
641 break;
642 case 20:
643 return LYNQ_WIFI_BANDWIDTH_HT20;
644 break;
645 case 40:
646 return LYNQ_WIFI_BANDWIDTH_HT40;
647 break;
648 case 80:
649 return LYNQ_WIFI_BANDWIDTH_HT80;
650 break;
651 default:
652 break;
653 }
654
655 return -1;
656}
657
658static int inner_get_status_info(int interface, curr_status_info *curr_state) {
659 int i, count;
660 char *p;
661 const char *lynq_status_cmd = "STATUS";
662 const char * FLAG_SSID = "ssid=";
663 const char * FLAG_SBSID = "bssid=";
664 const char * FLAG_KEY_MGMT = "key_mgmt=";
665 const char * FLAG_FREQ = "freq=";
666 const char * FLAG_STATE = "wpa_state=";
667 const char * FLAG_ID = "id=";
668 char *split_lines[128] = {0};
669
670 CHECK_WPA_CTRL(interface);
671
672 if (curr_state == NULL) {
673 return -1;
674 }
675
676 DO_REQUEST(lynq_status_cmd);
677
678 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
679
680 curr_state->net_no = -1;
681 ret = -1;
682 for(i=0; i < count; i++) {
683 if (curr_state->ap != NULL) {
684 p = strstr(split_lines[i], FLAG_SSID);
685 if (p != NULL) {
686 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
687 ret = 0;
688 continue;
689 }
690 p = strstr(split_lines[i], FLAG_SBSID);
691 if (p != NULL) {
692 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
693 ret = 0;
694 continue;
695 }
696 p = strstr(split_lines[i], FLAG_KEY_MGMT);
697 if (p != NULL) {
698 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
699 ret = 0;
700 continue;
701 }
702 p = strstr(split_lines[i], FLAG_FREQ);
703 if (p != NULL) {
704 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
705 ret = 0;
706 continue;
707 }
708 } // end if (ap != NULL)
709 if (curr_state->state != NULL) {
710 p = strstr(split_lines[i], FLAG_STATE);
711 if (p != NULL) {
712 strcpy(curr_state->state, p + strlen(FLAG_STATE));
713 ret = 0;
714 continue;
715 }
716
717 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800718 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800719 ret = 0;
720 curr_state->net_no = atoi(p);
721 printf("net_no %d, -- %s\n", curr_state->net_no, p);
722 }
723 }
724
725 return ret;
726}
727
728
qs.xiongf1b525b2022-03-31 00:58:23 -0400729int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500730{
you.chen35020192022-05-06 11:30:57 +0800731 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500732
you.chen35020192022-05-06 11:30:57 +0800733 if (ap_ssid == NULL) {
734 printf("ap_ssid is null\n");
735 return -1;
736 }
737 else {
738 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
739 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400740
you.chen35020192022-05-06 11:30:57 +0800741 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
742 return -1;
743 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400744
you.chen35020192022-05-06 11:30:57 +0800745 CHECK_IDX(idx, CTRL_AP);
746
747 CHECK_WPA_CTRL(CTRL_AP);
748
749 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
750
751 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
752 DO_OK_FAIL_REQUEST(cmd_save_config);
753
qs.xiong7a105ce2022-03-02 09:43:11 -0500754 return 0;
you.chen35020192022-05-06 11:30:57 +0800755
qs.xiong7a105ce2022-03-02 09:43:11 -0500756}
757
you.chen35020192022-05-06 11:30:57 +0800758int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500759{
you.chen6c2dd9c2022-05-16 17:55:28 +0800760 int len;
you.chen35020192022-05-06 11:30:57 +0800761 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800762 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
763 return -1;
764 len = strlen(ap_ssid);
765 if (ap_ssid[0] == '\"') {
766 memmove(ap_ssid, ap_ssid + 1, len - 1);
767 len -= 1;
768 }
769 if (len > 0 && ap_ssid[len-1] == '\"') {
770 ap_ssid[len-1] = '\0';
771 }
772 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500773}
774
qs.xiongf1b525b2022-03-31 00:58:23 -0400775int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500776{
you.chen35020192022-05-06 11:30:57 +0800777 char lynq_wifi_frequency_cmd[128]={0};
778 char lynq_cmd_mode[128]={0};
779 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500780
you.chen35020192022-05-06 11:30:57 +0800781 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
782 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
783 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400784
you.chen35020192022-05-06 11:30:57 +0800785 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
786 return -1;
787 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400788
you.chen35020192022-05-06 11:30:57 +0800789 CHECK_IDX(idx, CTRL_AP);
790
791 CHECK_WPA_CTRL(CTRL_AP);
792
793 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
794 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
795 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
796
you.chen6c2dd9c2022-05-16 17:55:28 +0800797 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800798 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
799 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
800 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500801
qs.xiong1af5daf2022-03-14 09:12:12 -0400802 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500803}
804
qs.xiongf1b525b2022-03-31 00:58:23 -0400805int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500806{
you.chen35020192022-05-06 11:30:57 +0800807 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400808
you.chen35020192022-05-06 11:30:57 +0800809 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400810
you.chen35020192022-05-06 11:30:57 +0800811 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
812 return -1;
813 }
814 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400815
816 return 0;
817}
818
qs.xiongf1b525b2022-03-31 00:58:23 -0400819int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
820{
you.chen35020192022-05-06 11:30:57 +0800821 CHECK_IDX(idx, CTRL_AP);
822 switch(bandwidth){
823 case LYNQ_WIFI_BANDWIDTH_HT10:
824 {
825 printf("bandwith [%d] not support now\n", bandwidth);
826 return -1;
827 }
828 case LYNQ_WIFI_BANDWIDTH_HT20:
829 {
830 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
831 system("wl down");
832 if (system(lynq_cmd_bandwith) != 0 ){
833 return -1;
834 }
835 system("wl up");
836 break;
837 }
838 case LYNQ_WIFI_BANDWIDTH_HT40:
839 {
840 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
841 sprintf(lynq_cmd_bandwith, "wl chanspec ");
842 system("wl down");
843 if (system(lynq_cmd_bandwith) != 0 ){
844 return -1;
845 }
846 system("wl up");
847 break;
848 }
849 case LYNQ_WIFI_BANDWIDTH_HT80:
850 {
851 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
852 system("wl down");
853 if (system(lynq_cmd_bandwith) != 0 ){
854 return -1;
855 }
856 system("wl up");
857 break;
858 }
859 default:
860 {
861 printf("auth type [%d] not support now\n", bandwidth);
862 return -1;
863 }
864 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400865
866
you.chen35020192022-05-06 11:30:57 +0800867 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400868}
you.chen35020192022-05-06 11:30:57 +0800869
qs.xiongf1b525b2022-03-31 00:58:23 -0400870int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
871{
you.chen35020192022-05-06 11:30:57 +0800872 int count = 0;
873 int index = 0;
874 char *split_words[128] = {0};
875 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400876
you.chen35020192022-05-06 11:30:57 +0800877 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500878
you.chen35020192022-05-06 11:30:57 +0800879 CHECK_WPA_CTRL(CTRL_AP);
880
881 DO_REQUEST(lynq_chanspec_cmd);
882
883 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
884 for(;index < count; index++) {
885 if (strncmp(split_words[index], "bw", 2) != 0) {
886 continue;
887 }
888
889 index++;
890 if (index >= count) {
891 return -1;
892 }
893
894 printf("bw %s\n", split_words[index]);
895 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
896 return 0;
897 }
898
899 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500900}
qs.xiong0fb469a2022-04-14 03:50:45 -0400901
qs.xiongf1b525b2022-03-31 00:58:23 -0400902int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500903{
you.chen35020192022-05-06 11:30:57 +0800904 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400905
you.chen35020192022-05-06 11:30:57 +0800906 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400907
you.chen35020192022-05-06 11:30:57 +0800908 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400909
you.chen35020192022-05-06 11:30:57 +0800910 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
911 return -1;
912 }
913
914 system("wl down");
915 if (system(lynq_cmd_channel) != 0 ){
916 return -1;
917 }
918 system("wl up");
919 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500920}
qs.xiong0fb469a2022-04-14 03:50:45 -0400921
qs.xiongf1b525b2022-03-31 00:58:23 -0400922int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500923{
you.chen35020192022-05-06 11:30:57 +0800924 int count = 0;
925 int index = 0;
926 char *split_words[128] = {0};
927 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400928
you.chen35020192022-05-06 11:30:57 +0800929 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400930
you.chen35020192022-05-06 11:30:57 +0800931 CHECK_WPA_CTRL(CTRL_AP);
932
933 DO_REQUEST(lynq_chanspec_cmd);
934
935 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
936 for(;index < count; index++) {
937 printf("---- %s\n",split_words[index]);
938 if (strncmp(split_words[index], "channel", 2) != 0) {
939 continue;
940 }
941
942 index++;
943 if (index >= count) {
944 return -1;
945 }
946
947 *channel = atoi(split_words[index]);
948 return 0;
949 }
950
951 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500952}
953
954
you.chen35020192022-05-06 11:30:57 +0800955int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500956{
you.chen6c2dd9c2022-05-16 17:55:28 +0800957 char ssid[MAX_CMD] = {0};
958 int freq = 0;
959 char lynq_auth_cmd[64]={0};
960 char lynq_auth_alg_cmd[64]={0};
961 char lynq_psk_cmd[64]={0};
962 char lynq_pairwise_cmd[64]={0};
963 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +0800964 CHECK_IDX(idx, CTRL_AP);
965
you.chen6c2dd9c2022-05-16 17:55:28 +0800966 CHECK_WPA_CTRL(CTRL_AP);
967
you.chen35020192022-05-06 11:30:57 +0800968 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
969 return -1;
970 }
971
you.chen92fd5d32022-05-25 10:09:47 +0800972 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800973 if (org_auth == auth) {
974 return 0;
975 }
976 else {
977 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
978 ssid[0] = '\0';
979 }
980 lynq_wifi_ap_frequency_get(idx, &freq);
981
982 DO_OK_FAIL_REQUEST(cmd_disconnect);
983 DO_OK_FAIL_REQUEST(cmd_remove_all);
984 if (ssid[0] != '\0') {
985 lynq_wifi_ap_ssid_set(idx, ssid);
986 }
987 if (freq != 0) {
988 lynq_wifi_ap_frequency_set(idx, freq);
989 }
990 }
991 }
you.chen35020192022-05-06 11:30:57 +0800992
qs.xiong7a105ce2022-03-02 09:43:11 -0500993 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400994 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +0800995 {
you.chen35020192022-05-06 11:30:57 +0800996 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +0800997 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500998
you.chen35020192022-05-06 11:30:57 +0800999 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001000 break;
1001 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001002 case LYNQ_WIFI_AUTH_WEP:
1003 {
1004 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001005 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001006 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1007
1008 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1009 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1010 break;
1011 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001012 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001013 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001014 {
you.chen35020192022-05-06 11:30:57 +08001015 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1016 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1017 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1018 }
1019 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001020 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001021 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001022 }
1023// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1024// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1025 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001026
you.chen35020192022-05-06 11:30:57 +08001027 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1028 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1029 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001030 break;
you.chen35020192022-05-06 11:30:57 +08001031 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001032 default:
you.chen35020192022-05-06 11:30:57 +08001033 {
1034 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001035 return -1;
you.chen35020192022-05-06 11:30:57 +08001036 }
1037 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001038 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001039
qs.xiong7a105ce2022-03-02 09:43:11 -05001040 return 0;
1041}
1042
you.chen35020192022-05-06 11:30:57 +08001043int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001044{
you.chen35020192022-05-06 11:30:57 +08001045 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001046 char lynq_auth_alg_str[MAX_RET] = {0};
1047 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001048
1049 CHECK_IDX(idx, CTRL_AP);
1050
1051 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1052 return -1;
1053 }
1054
1055 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001056 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1057 *auth = LYNQ_WIFI_AUTH_OPEN;
1058 }
1059 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1060 *auth = LYNQ_WIFI_AUTH_WEP;
1061 }
1062 else {
1063 *auth = LYNQ_WIFI_AUTH_OPEN;
1064 }
you.chen35020192022-05-06 11:30:57 +08001065 }
you.chen92fd5d32022-05-25 10:09:47 +08001066 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001067 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001068 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001069 }
1070 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1071 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1072 }
1073 else {
1074 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1075 }
you.chen35020192022-05-06 11:30:57 +08001076 }
you.chen92fd5d32022-05-25 10:09:47 +08001077 else {
1078 *auth = -1;
1079 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001080
you.chen6c2dd9c2022-05-16 17:55:28 +08001081 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001082}
qs.xiong1af5daf2022-03-14 09:12:12 -04001083
qs.xiong1af5daf2022-03-14 09:12:12 -04001084
qs.xiongf1b525b2022-03-31 00:58:23 -04001085int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001086{
you.chen35020192022-05-06 11:30:57 +08001087 char LYNQ_WIFI_CMD[128]={0};
1088 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1089 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001090
you.chen35020192022-05-06 11:30:57 +08001091 CHECK_IDX(idx, CTRL_AP);
1092
1093 CHECK_WPA_CTRL(CTRL_AP);
1094
1095// system("connmanctl enable wifi");
1096// system("connmanctl tether wifi on cy-test 12345678");
1097// system("ifconfig wlan0 down");
1098// system("ifconfig wlan0 up");
1099// system("ifconfig wlan0 up");
1100
1101 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1102 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1103
1104 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1105 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1106
qs.xiong7a105ce2022-03-02 09:43:11 -05001107 return 0;
1108}
1109
qs.xiongf1b525b2022-03-31 00:58:23 -04001110int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001111{
you.chen35020192022-05-06 11:30:57 +08001112 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001113}
1114
qs.xiongf1b525b2022-03-31 00:58:23 -04001115int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001116{
you.chen35020192022-05-06 11:30:57 +08001117 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001118
you.chen35020192022-05-06 11:30:57 +08001119 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001120
you.chen35020192022-05-06 11:30:57 +08001121 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001122
you.chen35020192022-05-06 11:30:57 +08001123 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1124
1125 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1126
you.chenb4b121c2022-05-06 17:50:16 +08001127// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001128
qs.xiong7a105ce2022-03-02 09:43:11 -05001129 return 0;
1130}
qs.xiong1af5daf2022-03-14 09:12:12 -04001131
qs.xiongf1b525b2022-03-31 00:58:23 -04001132int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001133{
you.chen35020192022-05-06 11:30:57 +08001134 char lynq_disable_cmd[128] = {0};
1135 char lynq_select_cmd[128] = {0};
1136 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001137
you.chen35020192022-05-06 11:30:57 +08001138 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001139
you.chen35020192022-05-06 11:30:57 +08001140 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001141
you.chen35020192022-05-06 11:30:57 +08001142 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1143 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1144
1145 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1146 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1147 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001148
1149 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001150}
1151
qs.xiongf1b525b2022-03-31 00:58:23 -04001152int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001153{
you.chen35020192022-05-06 11:30:57 +08001154 char lynq_disable_cmd[128] = {0};
1155 char lynq_select_cmd[128] = {0};
1156 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001157
you.chen35020192022-05-06 11:30:57 +08001158 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001159
you.chen35020192022-05-06 11:30:57 +08001160 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001161
you.chen35020192022-05-06 11:30:57 +08001162 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1163 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1164
1165 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1166 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1167 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001168
1169 return 0;
1170}
qs.xiongf1b525b2022-03-31 00:58:23 -04001171
you.chen35020192022-05-06 11:30:57 +08001172int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001173{
qs.xiongf1b525b2022-03-31 00:58:23 -04001174 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001175 char lynq_tmp_cmd[MAX_CMD] = {0};
1176 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001177 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001178 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001179 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001180 return -1;
you.chen35020192022-05-06 11:30:57 +08001181 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001182
you.chen35020192022-05-06 11:30:57 +08001183 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001184
you.chen6c2dd9c2022-05-16 17:55:28 +08001185 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1186 return -1;
1187 }
1188 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1189 return -1;
1190 }
1191
you.chen35020192022-05-06 11:30:57 +08001192 CHECK_WPA_CTRL(CTRL_AP);
1193
you.chen6c2dd9c2022-05-16 17:55:28 +08001194 if (auth == LYNQ_WIFI_AUTH_WEP) {
1195 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1196 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1197 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1198 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1199 }
1200 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1201 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1202 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1203 }
1204 else {
1205 return -1;
1206 }
you.chen35020192022-05-06 11:30:57 +08001207
you.chen35020192022-05-06 11:30:57 +08001208 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001209
qs.xiongf1b525b2022-03-31 00:58:23 -04001210 return 0;
1211}
1212
you.chen35020192022-05-06 11:30:57 +08001213int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001214{
you.chen35020192022-05-06 11:30:57 +08001215 FILE * fp;
1216 int len, ret;
1217 int count, index;
1218 char *split_lines[128] = {0};
1219 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001220
you.chen35020192022-05-06 11:30:57 +08001221 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001222
you.chen35020192022-05-06 11:30:57 +08001223 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1224// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1225 if (NULL == fp) {
1226 printf("open file fail\n");
1227 return -1;
1228 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001229
you.chen35020192022-05-06 11:30:57 +08001230 buff = alloca(MAX_RET);
1231 fseek(fp, 0, SEEK_SET);
1232 len = fread(buff, 1, MAX_RET, fp);
1233 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001234
you.chen35020192022-05-06 11:30:57 +08001235 for(index=0; index < len; index ++) {
1236 if (memcmp(buff + index, "network={", 9) != 0) {
1237 continue;
1238 }
1239 p = buff + index + 9;
1240 for (; index < len; index ++ ) {
1241 if (buff[index] != '}') {
1242 continue;
1243 }
1244 buff[index] = '\0';
1245 break;
1246 }
1247 len = buff + index - p;
1248 }
1249
1250 count = lynq_split(p, len, '\n', split_lines);
1251
1252 ret = -1;
1253 for(index=0; index < count; index++) {
1254 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001255 if (p != NULL) {
1256 p += 4;
1257 if (*p == '\"') {
1258 p++;
1259 }
you.chen35020192022-05-06 11:30:57 +08001260 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001261 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1262 p += 9;
1263 if (*p == '\"') {
1264 p++;
1265 }
1266 }
1267 else {
1268 continue;
you.chen35020192022-05-06 11:30:57 +08001269 }
1270
1271 strcpy(password, p);
1272
1273 while(*password != '\0') {
1274 if (*password == '\"') {
1275 *password = '\0';
1276 break;
1277 }
1278 password++;
1279 }
1280 ret = 0;
1281 break;
1282 } //end for(index=0; index < count; index++)
1283
1284 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001285}
1286
you.chen35020192022-05-06 11:30:57 +08001287static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1288 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001289 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001290
you.chen35020192022-05-06 11:30:57 +08001291 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1292 return -1;
1293 }
1294
1295 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001296
1297 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1298 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1299 if (strcmp(lynq_proto_str, "RSN") == 0) {
1300 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1301 }
1302 }
1303 }
you.chen35020192022-05-06 11:30:57 +08001304 return 0;
1305}
1306
1307int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001308{
you.chen35020192022-05-06 11:30:57 +08001309 int pass_len, net_no, count, index;
1310 char lynq_tmp_cmd[300]={0};
1311 int net_no_list[128];
1312 lynq_wifi_auth_s net_auth;
1313 pass_len=strlen(password);
1314 if(pass_len < 8 || pass_len >= 64){
1315 return -1;
1316 }
1317
1318 CHECK_IDX(idx, CTRL_STA);
1319
1320 net_no = -1;
1321 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1322
1323 for (index=0; index < count; index++) {
1324 net_auth = -1;
1325 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1326 net_no = net_no_list[index];
1327 break;
1328 }
1329 }
1330
1331 if (net_no < 0) {
1332 return -1;
1333 }
1334
1335 CHECK_WPA_CTRL(CTRL_STA);
1336
1337 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1338
1339 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1340 DO_OK_FAIL_REQUEST(cmd_save_config);
1341
1342 return 0;
1343}
1344
1345int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1346
1347 FILE * fp;
1348 int len, ret;
1349 int count, index;
1350 char *split_lines[128] = {0};
1351 char *buff, *p;
1352
1353 CHECK_IDX(idx, CTRL_STA);
1354
1355 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1356 if (NULL == fp) {
1357 printf("open file fail\n");
1358 return -1;
1359 }
1360
1361 buff = alloca(MAX_RET);
1362 fseek(fp, 0, SEEK_SET);
1363 len = fread(buff, 1, MAX_RET, fp);
1364 fclose(fp);
1365
1366 for(index=0; index < len; index ++) {
1367 for(; index < len; index ++) {
1368 if (memcmp(buff + index, "network={", 9) != 0) {
1369 continue;
1370 }
1371 p = buff + index + 9;
1372 for (; index < len; index ++ ) {
1373 if (buff[index] != '}') {
1374 continue;
1375 }
1376 buff[index] = '\0';
1377 break;
1378 }
1379 len = buff + index - p;
1380 }
1381
1382 if (strstr(p, ap->ap_ssid) != NULL) {
1383 break;
1384 }
1385 }
1386
1387 if (index >= len) {
1388 return -1;
1389 }
1390
1391 count = lynq_split(p, len, '\n', split_lines);
1392
1393 ret = -1;
1394 for(index=0; index < count; index++) {
1395 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001396 if (p != NULL) {
1397 p += 4;
1398 if (*p == '\"') {
1399 p++;
1400 }
you.chen35020192022-05-06 11:30:57 +08001401 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001402 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1403 p += 9;
1404 if (*p == '\"') {
1405 p++;
1406 }
1407 }
1408 else {
1409 continue;
you.chen35020192022-05-06 11:30:57 +08001410 }
1411
1412 strcpy(password, p);
1413
1414 while(*password != '\0') {
1415 if (*password == '\"') {
1416 *password = '\0';
1417 break;
1418 }
1419 password++;
1420 }
1421 ret = 0;
1422 break;
1423 } //end for(index=0; index < count; index++)
1424
1425 return ret;
1426}
1427
1428static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1429{
qs.xiong97fa59b2022-04-07 05:41:29 -04001430 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001431
you.chen35020192022-05-06 11:30:57 +08001432 if (sta_ssid == NULL) {
1433 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001434 return -1;
you.chen35020192022-05-06 11:30:57 +08001435 }
1436
1437 CHECK_WPA_CTRL(CTRL_STA);
1438
1439 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1440
1441 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1442// DO_OK_FAIL_REQUEST(cmd_save_config);
1443
qs.xiong7a105ce2022-03-02 09:43:11 -05001444 return 0;
1445
1446}
1447
you.chen35020192022-05-06 11:30:57 +08001448static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001449{
you.chen35020192022-05-06 11:30:57 +08001450 char lynq_disable_cmd[128]={0};
1451 char lynq_select_cmd[128]={0};
1452
1453 CHECK_WPA_CTRL(CTRL_STA);
1454
1455 if (save != 0) {
1456 DO_OK_FAIL_REQUEST(cmd_save_config);
1457 }
1458
1459 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001460// sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1461 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001462 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1463 }
1464 else {
1465 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1466 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1467 }
1468
1469 return 0;
1470}
1471
1472int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1473{
1474 CHECK_IDX(idx, CTRL_STA);
1475
you.chen6c2dd9c2022-05-16 17:55:28 +08001476 curr_status_info curr_state;
1477 ap_info_s ap_info;
1478 curr_state.ap = &ap_info;
1479 curr_state.state = NULL;
1480
1481 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1482 strcpy(sta_ssid, ap_info.ap_ssid);
1483 return 0;
1484 }
1485
1486 return -1;
you.chen35020192022-05-06 11:30:57 +08001487}
1488
1489int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1490{
1491 scan_info_s *scan_list;
1492 saved_ap_info_s *save_list;
1493 int scan_len=0;
1494 int save_len=0;
1495 int best_index = -1;
1496 int best_scan_index = -1;
1497 int best_rssi = 0;
1498 int i, j;
1499
1500 CHECK_IDX(idx, CTRL_STA);
1501 if (info == NULL) {
1502 return -1;
1503 }
1504
1505 curr_status_info curr_state;
1506 ap_info_s ap_info;
1507 curr_state.ap = &ap_info;
1508 curr_state.state = NULL;
1509
1510 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1511 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1512 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1513 lynq_get_connect_ap_rssi(idx, &info->rssi);
1514 return 0;
1515 }
1516
1517 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1518 return -1;
1519 }
1520
1521 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1522 return -1;
1523 }
1524
1525 for (i=0; i < save_len; i++) {
1526 for (j=0; j < scan_len; j++) {
1527 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1528 && save_list[i].base_info.auth == scan_list[j].auth) {
1529 if (best_rssi == 0) {
1530 best_rssi = scan_list[j].rssi;
1531 }
1532 else if (best_rssi > scan_list[j].rssi) {
1533 best_index = i;
1534 best_scan_index = j;
1535 best_rssi = scan_list[j].rssi;
1536 }
1537 break;
1538 }
1539 }
1540 }
1541
1542 if (best_index >= 0) {
1543 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1544 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1545 info->rssi = best_rssi;
1546 return 0;
1547 }
1548
1549 return -1;
1550}
1551
1552static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1553{
1554 char lynq_auth_cmd[64]={0};
1555 char lynq_ket_mgmt_cmd[64]={0};
1556 char lynq_pairwise_cmd[64]={0};
1557 char lynq_psk_cmd[64]={0};
1558
1559 CHECK_WPA_CTRL(CTRL_STA);
1560
qs.xiong1af5daf2022-03-14 09:12:12 -04001561 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001562 case LYNQ_WIFI_AUTH_OPEN:
1563 {
1564 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001565
you.chen35020192022-05-06 11:30:57 +08001566 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1567// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001568 break;
1569 }
you.chen35020192022-05-06 11:30:57 +08001570 case LYNQ_WIFI_AUTH_WPA_PSK:
1571 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001572 {
you.chen35020192022-05-06 11:30:57 +08001573 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1574 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1575 }
1576 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001577 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001578 }
1579 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1580 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001581
you.chen35020192022-05-06 11:30:57 +08001582 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1583 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1584 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001585
you.chen35020192022-05-06 11:30:57 +08001586 if (password != NULL) {
1587 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1588 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1589 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001590
you.chen35020192022-05-06 11:30:57 +08001591// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001592 break;
you.chen35020192022-05-06 11:30:57 +08001593 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001594 default:
1595 return -1;
you.chen35020192022-05-06 11:30:57 +08001596 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001597
qs.xiongf1b525b2022-03-31 00:58:23 -04001598 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001599}
qs.xiong7a105ce2022-03-02 09:43:11 -05001600
you.chen35020192022-05-06 11:30:57 +08001601static int inner_get_curr_net_no(int interface) {
1602 curr_status_info curr_state;
1603 curr_state.ap = NULL;
1604 curr_state.state = NULL;
1605
1606 if (0 != inner_get_status_info(interface, &curr_state)) {
1607 return -1;
1608 }
1609
1610 return curr_state.net_no;
1611}
1612
1613int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001614{
you.chen35020192022-05-06 11:30:57 +08001615 int net_no;
1616 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001617
you.chen35020192022-05-06 11:30:57 +08001618 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001619
you.chen35020192022-05-06 11:30:57 +08001620 if (net_no < 0) {
1621 return -1;
1622 }
1623
1624 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001625}
1626
you.chen35020192022-05-06 11:30:57 +08001627int 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 -05001628{
you.chen35020192022-05-06 11:30:57 +08001629 int count, net_no, index;
1630 int net_no_list[128];
1631 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001632
you.chen35020192022-05-06 11:30:57 +08001633 if (ssid == NULL || *ssid == '\0') {
1634 printf("bad ssid\n");
1635 return -1;
1636 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001637
you.chen35020192022-05-06 11:30:57 +08001638 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1639 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1640 printf("bad password\n");
1641 return -1;
1642 }
1643 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001644
you.chen35020192022-05-06 11:30:57 +08001645 CHECK_IDX(idx, CTRL_STA);
1646
1647 net_no = -1;
1648 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1649
1650 for (index=0; index < count; index++) {
1651 net_auth = -1;
1652 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1653 net_no = net_no_list[index];
1654 break;
1655 }
1656 }
1657
1658 if (net_no < 0) {
1659 net_no = lynq_add_network(CTRL_STA);
1660 if (net_no == -1) {
1661 return -1;
1662 }
1663
1664 printf("net no is %d\n", net_no);
1665 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1666 return -1;
1667 }
1668 }
1669
1670 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1671 return -1;
1672 }
1673
1674 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001675}
1676
you.chen35020192022-05-06 11:30:57 +08001677int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001678{
you.chen35020192022-05-06 11:30:57 +08001679 ap_info_s ap;
1680 curr_status_info curr_state;
1681 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001682
you.chen35020192022-05-06 11:30:57 +08001683 if (ssid == NULL || *ssid == '\0') {
1684 return -1;
1685 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001686
you.chen35020192022-05-06 11:30:57 +08001687 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001688
you.chen35020192022-05-06 11:30:57 +08001689 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001690 curr_state.state = NULL;
1691
you.chen35020192022-05-06 11:30:57 +08001692 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1693 return 0;
1694 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001695
you.chen35020192022-05-06 11:30:57 +08001696 if (strcmp(ap.ap_ssid, ssid) != 0) {
1697 return 0;
1698 }
1699
1700 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001701}
qs.xiong97fa59b2022-04-07 05:41:29 -04001702
you.chena6cd55a2022-05-08 12:20:18 +08001703int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1704{
1705// const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong7a105ce2022-03-02 09:43:11 -05001706
you.chen35020192022-05-06 11:30:57 +08001707 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001708 CHECK_WPA_CTRL(CTRL_STA);
1709
1710 system("connmanctl enable wifi");
1711
you.chena6fa5b22022-05-18 10:28:19 +08001712 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001713 return -1;
1714 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001715
you.chena6cd55a2022-05-08 12:20:18 +08001716// DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
1717
1718 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001719}
1720
you.chen35020192022-05-06 11:30:57 +08001721int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001722{
you.chena6cd55a2022-05-08 12:20:18 +08001723 char lynq_disable_network_cmd[MAX_CMD];
1724 curr_status_info curr_state;
1725 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001726
you.chena6cd55a2022-05-08 12:20:18 +08001727 CHECK_IDX(idx, CTRL_STA);
1728 CHECK_WPA_CTRL(CTRL_STA);
1729
1730 curr_state.ap = &ap_info;
1731 curr_state.state = NULL;
1732
1733 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1734 return 0;
1735 }
1736
1737 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1738 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1739
1740 DO_OK_FAIL_REQUEST(cmd_save_config);
1741
1742 return 0;
1743// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001744}
qs.xiong7a105ce2022-03-02 09:43:11 -05001745
you.chen35020192022-05-06 11:30:57 +08001746//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1747// int i, count;
1748// char *p;
1749// const char * FLAG_SSID = "ssid=";
1750// const char * FLAG_SBSID = "bssid=";
1751// const char * FLAG_KEY_MGMT = "key_mgmt=";
1752// const char * FLAG_FREQ = "freq=";
1753// char lynq_sta_cmd[MAX_CMD];
1754// char *split_lines[128] = {0};
1755
1756// CHECK_WPA_CTRL(CTRL_AP);
1757
1758// sprintf(lynq_sta_cmd, "STA %s", bssid);
1759
1760// DO_REQUEST(lynq_sta_cmd);
1761
1762// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1763
1764// for(i=0; i < count; i++) {
1765// p = strstr(split_lines[i], FLAG_SSID);
1766// if (p != NULL) {
1767// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1768// continue;
1769// }
1770// }
1771
1772// lynq_get_interface_ip(idx, ap->ap_ip);
1773// lynq_ap_password_set(idx, ap->psw);
1774
1775// return 0;
1776//}
1777
1778static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1779 curr_status_info curr_state;
1780 curr_state.ap = ap;
1781 curr_state.state = NULL;
1782 return inner_get_status_info(interface, &curr_state);
1783}
1784
1785int 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 -04001786{
you.chen35020192022-05-06 11:30:57 +08001787 int ip_count, index, i, line_count;
1788 const char *lynq_first_sta_cmd = "STA-FIRST";
1789 char lynq_next_sta_cmd[MAX_CMD];
1790 char *bssid[1024] = {0};
1791 char *mac_list[128] = {0};
1792 char *ip_list[128] = {0};
1793 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001794
you.chen35020192022-05-06 11:30:57 +08001795 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001796
you.chen35020192022-05-06 11:30:57 +08001797 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001798
you.chen35020192022-05-06 11:30:57 +08001799// ap_info_s * tmp_ap;
1800// device_info_s * tmp_list;
1801 if (ap == NULL || list == NULL || len == NULL) {
1802 printf("bad input param");
1803 return -1;
1804 }
1805
1806// ap = &tmp_ap;
1807// list = &tmp_list;
1808 *ap = malloc(sizeof (ap_info_s));
1809
1810 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1811 return -1;
1812 }
1813
1814 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1815 lynq_ap_password_get(idx, (*ap)->psw);
1816
1817 ip_count = get_ip_mac_list(mac_list, ip_list);
1818 printf("get count %d\n", ip_count);
1819
1820 DO_REQUEST(lynq_first_sta_cmd);
1821
1822 index = 0;
1823 while (reply_len > 0) {
1824 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1825 break;
1826 }
1827 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1828 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1829 strcpy(bssid[index], split_lines[0]);
1830 index++;
1831 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1832 reply_len = MAX_RET;
1833 cmd_reply[0] = '\0';
1834 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1835 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1836 break;
1837 }
1838 }
1839
1840 *len = index;
1841
1842 *list = malloc(sizeof(device_info_s) * (*len));
1843 for (index=0; index < *len; index++) {
1844 strcpy((*list)[index].sta_mac, bssid[index]);
1845 for(i=0;i < ip_count; i++ ) {
1846 if (strcmp(bssid[index], mac_list[i]) == 0) {
1847 strcpy((*list)[index].sta_ip, ip_list[i]);
1848 break;
1849 }
1850 }
1851 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1852 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1853 free(bssid[index]);
1854 }
1855
1856 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001857}
1858
you.chen35020192022-05-06 11:30:57 +08001859int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001860{
you.chen35020192022-05-06 11:30:57 +08001861 int i, count, index, count_words;
1862 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1863 char *split_lines[128] = {0};
1864 char *split_words[128] = {0};
1865 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001866
you.chen35020192022-05-06 11:30:57 +08001867 if (list == NULL || len == NULL) {
1868 return -1;
1869 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001870
you.chen35020192022-05-06 11:30:57 +08001871 CHECK_IDX(idx, CTRL_STA);
1872
1873 CHECK_WPA_CTRL(CTRL_STA);
1874
1875 DO_REQUEST(lynq_scan_result_cmd);
1876
1877 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1878 *len = count - 1;
1879 *list = malloc(sizeof (scan_info_s) * *len);
1880
1881 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1882 for (index=0; index <count_words; index++) {
1883 printf("----header: %s\n", split_words[index]);
1884 }
1885
1886 for(index = 1;index < count; index++) {
1887 printf("---- %s\n",split_lines[index]);
1888 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1889 if (count_words < 4)
1890 continue;
1891 printf("count: %d, %s\n", count_words, split_words[0]);
1892 //bssid / frequency / signal level / flags / ssid
1893 p = (*list) + index - 1;
1894 strcpy(p->mac, split_words[0]);
1895 p->band = convert_band_from_freq(atoi(split_words[1]));
1896 p->rssi = -1 * atoi( split_words[2]);
1897 p->auth = convert_max_auth_from_flag(split_words[3]);
1898 strcpy(p->ssid, split_words[4]);
1899 }
1900
1901 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001902}
qs.xiong97fa59b2022-04-07 05:41:29 -04001903
you.chen35020192022-05-06 11:30:57 +08001904int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1905{
1906 int count, net_no, index;
1907 int net_no_list[128];
1908 lynq_wifi_auth_s net_auth;
1909 char lynq_remove_cmd[MAX_CMD];
1910
1911 if (ssid == NULL || *ssid == '\0') {
1912 printf("bad ssid\n");
1913 return -1;
1914 }
1915
1916 CHECK_IDX(idx, CTRL_STA);
1917
1918 CHECK_WPA_CTRL(CTRL_STA);
1919
1920 net_no = -1;
1921 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1922
1923 for (index=0; index < count; index++) {
1924 net_auth = -1;
1925 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1926 net_no = net_no_list[index];
1927 break;
1928 }
1929 }
1930
1931 if (net_no < 0) {
1932 return 0;
1933 }
1934
1935 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1936
1937 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1938 DO_OK_FAIL_REQUEST(cmd_save_config);
1939
1940 return 0;
1941}
1942
1943int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1944{
1945 int count, index;
1946 int net_no_list[128];
1947 char freq[16];
1948
1949 if (list == NULL || len == NULL) {
1950 printf("bad param\n");
1951 return -1;
1952 }
1953
1954 CHECK_IDX(idx, CTRL_STA);
1955
1956// CHECK_WPA_CTRL(CTRL_STA);
1957
1958 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1959 printf("count is %d\n", count);
1960
1961 *list = malloc(sizeof (saved_ap_info_s) * count);
1962 *len = count;
1963
1964 for (index=0; index < count; index++) {
1965 printf("to get ssid %d\n", index);
1966 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1967 printf("to get bssid\n");
1968 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1969 printf("to get inner_get_network_auth\n");
1970 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1971 printf("to get frequency\n");
1972 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1973 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1974 }
1975 else {
1976 (*list)[index].base_info.band = -1;
1977 }
1978
1979 }
1980
1981 return 0;
1982}
1983
1984int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1985{
1986 const char *lynq_scan_cmd = "SCAN";
1987
1988 CHECK_IDX(idx, CTRL_STA);
1989
1990 CHECK_WPA_CTRL(CTRL_STA);
1991
1992 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1993
1994 return 0;
1995}
1996
1997int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1998 if (cb == NULL) {
1999 return -1;
2000 }
2001
2002 g_ap_callback_priv = priv;
2003 g_ap_callback_func = cb;
2004
2005 return 0;
2006}
2007
2008int lynq_unreg_ap_event_callback(void * priv) {
2009 if (g_ap_callback_priv == priv) {
2010 g_ap_callback_func = NULL;
2011 g_ap_callback_priv = NULL;
2012 return 0;
2013 }
2014 return -1;
2015}
2016
2017int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2018 if (cb == NULL) {
2019 return -1;
2020 }
2021
2022 g_sta_callback_priv = priv;
2023 g_sta_callback_func = cb;
2024
2025 return 0;
2026}
2027
2028int lynq_unreg_sta_event_callback(void * priv) {
2029 if (g_sta_callback_priv == priv) {
2030 g_sta_callback_func = NULL;
2031 g_sta_callback_priv = NULL;
2032 return 0;
2033 }
2034 return -1;
2035}
2036
2037
2038static int inner_get_status_info_state (int interface, char *state) {
2039 curr_status_info curr_state;
2040 curr_state.ap = NULL;
2041 curr_state.state = state;
2042 return inner_get_status_info(interface, &curr_state);
2043}
2044
2045int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2046{
2047 char state[MAX_CMD];
2048 const char * STATE_COMPLETED = "COMPLETED";
2049 CHECK_IDX(idx, CTRL_AP);
2050
2051 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2052 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2053 return 0;
2054 }
2055
2056 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2057 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2058 }
2059 else {
2060 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2061 }
2062
2063 return 0;
2064}
2065
2066int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2067 char state[MAX_CMD];
2068 const char * STATE_COMPLETED = "COMPLETED";
2069 CHECK_IDX(idx, CTRL_STA);
2070
2071 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2072 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2073 return 0;
2074 }
2075
2076 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2077 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2078 }
2079 else {
2080 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2081 }
2082
2083 return 0;
2084}
2085
2086int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2087// CHECK_IDX(idx, CTRL_AP);
2088// int ret = 0;
2089// size_t reply_len = MAX_RET;
2090// char cmd_reply[MAX_RET]={0};
2091// const char * cmd_str = "GET country";
2092// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2093// do{
2094// if (NULL == s_lynq_wpa_ctrl) {
2095// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2096// if (NULL == s_lynq_wpa_ctrl ) {
2097// printf("wpa_ctrl_open fail\n");
2098// return -1;
2099// }
2100// }
2101// }while(0);
2102
2103// do {
2104// reply_len = MAX_RET;
2105// cmd_reply[0] = '\0';
2106// printf("to call [%s]\n", cmd_str);
2107// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
2108// if (ret != 0) {
2109// printf("call ##cmd_str fail %d\n", ret);
2110// return ret;
2111// }
2112// cmd_reply[reply_len+1] = '\0';
2113// printf("cmd replay [ %s ]\n", cmd_reply);
2114// }while(0);
2115
2116 FILE *fp;
2117 size_t i = 0;
2118 char lynq_cmd_ret[MAX_RET]={0};
2119
2120// CHECK_IDX(idx, CTRL_AP);
2121
2122 if((fp=popen("wl country","r"))==NULL)
2123 {
2124 perror("popen error!");
2125 return -1;
2126 }
2127 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2128 {
2129 perror("fread fail!");
2130 return -1;
2131 }
2132
2133 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2134 if (lynq_cmd_ret[i] == ' ') {
2135 lynq_cmd_ret[i] = '\0';
2136 break;
2137 }
2138 }
2139
2140 strcpy(country_code,lynq_cmd_ret);
2141 printf("---country code %s\n", country_code);
2142
2143 int ret=pclose(fp);
2144 if(ret==-1)
2145 {
2146 perror("close file faild");
2147 }
2148
2149 return 0;
2150}
2151
2152int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2153// const char * cmd_str = "GET country";
2154// CHECK_IDX(idx, CTRL_AP);
2155// CHECK_WPA_CTRL(CTRL_STA);
2156
2157// DO_REQUEST(cmd_str);
2158// printf("result %s\n", cmd_reply);
2159
2160 if (country_code == NULL || *country_code == '\0') {
2161 printf("bad country code\n");
2162 return -1;
2163 }
2164
2165 char lynq_country_cmd[MAX_CMD];
2166 sprintf(lynq_country_cmd, "wl country %s", country_code);
2167 if (system(lynq_country_cmd) == 0) {
2168 return 0;
2169 }
2170
2171 return -1;
2172}
2173
2174int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2175{
2176 if (mac == NULL) {
2177 return -1;
2178 }
2179
2180 CHECK_IDX(idx, CTRL_STA);
2181 ap_info_s ap;
2182 ap.ap_mac[0] = '\0';
2183
2184 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2185 return -1;
2186 }
2187 strcpy(mac, ap.ap_mac);
2188
2189 return 0;
2190}
2191
2192int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2193{
2194 struct ifaddrs *ifaddr, *ifa;
2195 int family, s;
2196 char host[NI_MAXHOST];
2197 const char * ifaName = "wlan0";
2198 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002199 ifaName = "tether";
you.chen35020192022-05-06 11:30:57 +08002200 }
2201 else if (idx != 0) {
2202 return -1;
2203 }
2204
2205 if (getifaddrs(&ifaddr) == -1)
2206 {
2207 perror("getifaddrs");
2208 return -1;
2209 //exit(EXIT_FAILURE);
2210 }
2211
2212
2213 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2214 {
2215 if (ifa->ifa_addr == NULL)
2216 continue;
2217 host[0] = '\0';
2218 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2219
2220 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2221 if((strcmp(ifa->ifa_name,ifaName)==0))
2222 {
2223 if (s != 0)
2224 {
2225 // printf("getnameinfo() failed: %s", gai_strerror(s));
2226 //exit(EXIT_FAILURE);
2227 }
2228 freeifaddrs(ifaddr);
2229 strcpy(ip, host);
2230 printf("ip %s\n", ip);
2231 return 0;
2232 //printf(" Interface : <%s>",ifa->ifa_name );
2233 //printf(" Address : <%s>", host);
2234 }
2235 }
2236 return -1;
2237}
2238
2239int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2240{
2241 int count;
2242 size_t i;
2243 char *split_words[128] = {0};
2244 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2245
2246 CHECK_WPA_CTRL(idx);
2247
2248 DO_REQUEST(lynq_get_mac_cmd);
2249
2250 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2251 return -1;
2252 }
2253
2254 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2255
2256 if (count < 2) {
2257 return -1;
2258 }
2259
2260 for (i=0; i < strlen(split_words[1]); i++ ) {
2261 if (split_words[1][i] != ' ') {
2262 break;
2263 }
2264 }
2265
2266 strcpy(mac, split_words[1] + i);
2267
2268 return 0;
2269}
2270
2271int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2272{
2273// int count;
2274// char *split_words[128] = {0};
2275// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2276
2277// if (rssi == NULL) {
2278// return -1;
2279// }
2280
2281// CHECK_IDX(idx, CTRL_STA);
2282
2283// CHECK_WPA_CTRL(CTRL_STA);
2284
2285// DO_REQUEST(lynq_get_rssi_cmd);
2286
2287// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2288// return -1;
2289// }
2290
2291// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2292
2293// if (count < 2) {
2294// return -1;
2295// }
2296
2297// *rssi = atoi(split_words[1]) * -1;
2298
2299 FILE *fp;
2300 size_t i = 0;
2301 char lynq_cmd_ret[MAX_RET]={0};
2302
2303// CHECK_IDX(idx, CTRL_AP);
2304
you.chen9f17e4d2022-06-06 17:18:18 +08002305 if((fp=popen("wl rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002306 {
2307 perror("popen error!");
2308 return -1;
2309 }
2310 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2311 {
2312 perror("fread fail!");
2313 return -1;
2314 }
you.chen9f17e4d2022-06-06 17:18:18 +08002315 *rssi = atoi(lynq_cmd_ret) * -1;
you.chen35020192022-05-06 11:30:57 +08002316
2317 return 0;
2318}
2319
2320int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2321{
2322 if (band == NULL) {
2323 return -1;
2324 }
2325
2326 CHECK_IDX(idx, CTRL_STA);
2327 ap_info_s ap;
2328 ap.band = -1;
2329
2330 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2331 return -1;
2332 }
2333 *band = ap.band;
2334
2335 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002336}