blob: d0e87509018d5affe2d0cd1d2695a83a758bd50e [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;
41
42pthread_t g_sta_watcher_pid = 0;
43volatile int g_sta_watcher_stop_flag = 0;
44volatile int g_sta_scan_finish_flag = 0;
45
46void * g_ap_callback_priv = NULL;
47AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
48void * g_sta_callback_priv = NULL;
49STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
50
51//const char * CTRL_PATH="/var/run/wpa_supplicant";
52const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
53//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
54const char * cmd_list_networks = "LIST_NETWORKS";
55const char * cmd_save_config = "SAVE_CONFIG";
56const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
57
58static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
59
60
61typedef struct __curr_status_info {
62 ap_info_s *ap;
63 char * state;
64 int net_no;
65}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040066
67#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040068{\
you.chen35020192022-05-06 11:30:57 +080069 perror((str));\
70 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040071}
72
you.chen35020192022-05-06 11:30:57 +080073#define CHECK_IDX(idx, type) do { \
74 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
75 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
76 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
77 return -1; \
78 } \
79 }while (0)
80
81#define CHECK_WPA_CTRL(index) int ret = 0;\
82 size_t reply_len = MAX_RET; \
83 char cmd_reply[MAX_RET]={0}; \
84 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
85 do{ \
86 if (NULL == g_lynq_wpa_ctrl[index]) { \
87 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
88 if (NULL == g_lynq_wpa_ctrl[index]) { \
89 printf("wpa_ctrl_open fail\n"); \
90 return -1; \
91 } \
92 } \
93 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
94 }while(0)
95
96#define DO_REQUEST(cmd_str) do { \
97 reply_len = MAX_RET;\
98 cmd_reply[0] = '\0'; \
99 printf("to call [%s]\n", cmd_str); \
100 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
101 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800102 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800103 return ret; \
104 } \
105 cmd_reply[reply_len+1] = '\0'; \
106 printf("cmd replay [ %s ]\n", cmd_reply); \
107 }while(0)
108
109#define DO_OK_FAIL_REQUEST(cmd_str) do { \
110 DO_REQUEST(cmd_str); \
111 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800112 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800113 return -1; \
114 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800115 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800116 return -1; \
117 } \
118 }while (0)
119
120
121
122static void APWatcherThreadProc() {
123 size_t len = MAX_RET;
124 char msg_notify[MAX_RET];
125
126 struct wpa_ctrl *lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
127 if (lynq_wpa_ctrl == NULL)
128 return;
129
130 wpa_ctrl_attach(lynq_wpa_ctrl);
131
132 while (g_ap_watcher_stop_flag == 0) {
133 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
134 usleep(100*1000);
135 continue;
136 }
137 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
138 msg_notify[len+1] = '\0';
139 printf("ap------> %s\n", msg_notify);
140 if (g_ap_callback_func == NULL) {
141 continue;
142 }
143 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
144 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
145 }
146 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
147 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
148 }
149 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
150 } // end while (g_ap_watcher_stop_flag == 0)
151 wpa_ctrl_detach(lynq_wpa_ctrl);
152 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiong97fa59b2022-04-07 05:41:29 -0400153}
154
you.chen35020192022-05-06 11:30:57 +0800155static void STAWatcherThreadProc() {
156 size_t len = MAX_RET;
157 char msg_notify[MAX_RET];
158 char *pReason;
159 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400160
you.chen35020192022-05-06 11:30:57 +0800161 struct wpa_ctrl *lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
162 if (lynq_wpa_ctrl == NULL)
163 return;
qs.xiongf1b525b2022-03-31 00:58:23 -0400164
you.chen35020192022-05-06 11:30:57 +0800165 wpa_ctrl_attach(lynq_wpa_ctrl);
166
167 while (g_sta_watcher_stop_flag == 0) {
168 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
169 usleep(100*1000);
170 continue;
171 }
172 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
173 msg_notify[len+1] = '\0';
174 printf("sta ------> %s\n", msg_notify);
175 if (strstr(msg_notify, state_scan_result) != NULL) {
176 g_sta_scan_finish_flag = 1;
177 }
178
179 if (g_sta_callback_func == NULL) {
180 continue;
181 }
182 error = -1;
183 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
184 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
185 }
186 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
187 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
188 }
189 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
190 pReason = strstr(msg_notify, "reason=");
191 if (pReason != NULL) {
192 pReason += strlen("reason=");
193 if (memcpy(pReason, "CONN_FAILED", 11) == 0) {
194 error = LYNQ_TIME_OUT;
195 }
196 else if (memcpy(pReason, "WRONG_KEY", 9) == 0) {
197 error = LYNQ_PSW_ERROR;
198 }
199 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
200 }
201 }
202 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
203 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
204 }
205 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
206 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
207 }
208 }
209 }
210 wpa_ctrl_detach(lynq_wpa_ctrl);
211 wpa_ctrl_close(lynq_wpa_ctrl);
qs.xiongf1b525b2022-03-31 00:58:23 -0400212}
213
qs.xiong1af5daf2022-03-14 09:12:12 -0400214int lynq_wifi_enable(void)
215{
you.chen35020192022-05-06 11:30:57 +0800216 int ret = 0;
217 const char * cmd_check_service =
218 "state=`systemctl is-active wg870_drv_insmod.service`\n"
219 "[ \"\"$state == \"active\" ] && exit 0\n"
220 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
221// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
222// return 0;
223// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400224
you.chen35020192022-05-06 11:30:57 +0800225 ret = system(cmd_check_service);
226 if (ret != 0) {
227 return -1;
228 }
229
230 if (g_ap_watcher_pid == 0 ) {
231 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
232 if(ret<0){
233 return -1;
234 }
235 }
236
237 if (g_sta_watcher_pid == 0 ) {
238 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
239 if(ret<0){
240 return -1;
241 }
242 }
243
244 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500245}
246
qs.xiong1af5daf2022-03-14 09:12:12 -0400247int lynq_wifi_disable(void)
248{
you.chen35020192022-05-06 11:30:57 +0800249 g_ap_watcher_stop_flag = 1;
250 g_sta_watcher_stop_flag = 1;
251 if (g_ap_watcher_pid != 0)
252 pthread_join(g_ap_watcher_pid, NULL);
253 if (g_sta_watcher_pid != 0)
254 pthread_join(g_sta_watcher_pid, NULL);
255 if (g_lynq_wpa_ctrl[0] != NULL)
256 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
257 if (g_lynq_wpa_ctrl[1] != NULL)
258 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
259 g_ap_watcher_pid = 0;
260 g_sta_watcher_pid = 0;
261 g_lynq_wpa_ctrl[0] = NULL;
262 g_lynq_wpa_ctrl[1] = NULL;
263 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500264 return 0;
265}
qs.xiong1af5daf2022-03-14 09:12:12 -0400266
you.chen35020192022-05-06 11:30:57 +0800267static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
268
269 char lynq_cmd_get[128]={0};
270
271 if (out_put == NULL) {
272 printf("output ptr is null\n");
273 return -1;
274 }
275 if (param_name == NULL) {
276 printf("param ptr is null");
277 return -1;
278 }
279 if (param_name[0] == '\0') {
280 printf("param is empty");
281 return -1;
282 }
283
284 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
285
286 CHECK_WPA_CTRL(interface);
287
288 DO_REQUEST(lynq_cmd_get);
289
290 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
291 return -1;
292 }
293
294 printf("reply len %d, %08x\n", reply_len, (int)out_put);
295 memcpy(out_put, cmd_reply, reply_len + 1);
296 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500297}
qs.xiong1af5daf2022-03-14 09:12:12 -0400298
you.chen35020192022-05-06 11:30:57 +0800299static int lynq_split(char * str, int len, char delimiter, char * results[]) {
300 int ret = 0;
301 char * end = str + len - 1;
302 results[ret++] = str;
303 while(str < end) {
304 if (*str == delimiter) {
305 *str++ = '\0';
306 results[ret++] = str;
307 continue;
308 }
309 str++;
310 }
311 if (*str == delimiter) {
312 *str = '\0';
313 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400314
you.chen35020192022-05-06 11:30:57 +0800315 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500316}
317
you.chen35020192022-05-06 11:30:57 +0800318static void trim_space(char * p, int count) {
319 char * begin = p;
320 p += count;
321 printf("%C-%C||\n", *begin, *p);
322 while (p >= begin ) {
323 if (*p == ' ') {
324 *p-- = '\0';
325 }
326 else {
327 break;
328 }
329 }
330}
331
332static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
333 FILE * fp;
334 int len, ret;
335 int count, count_words, index;
336 int mac_start, mac_end;
337 int ip_start, ip_end;
338 char *split_lines[128] = {0};
339 char *buff;
340 const char * ip_header = "IP address";
341 const char * mac_header = "HW address";
342 const char * zero_mac = "00:00:00:00:00:00";
343
344 fp = fopen("/proc/net/arp", "rb");
345 if (NULL == fp) {
346 printf("open file fail\n");
347 return -1;
348 }
349
350 buff = alloca(MAX_RET);
351 fseek(fp, 0, SEEK_SET);
352 len = fread(buff, 1, MAX_RET, fp);
353 fclose(fp);
354 if (len <= 0) {
355 printf("read file fail\n");
356 return -1;
357 }
358 printf("file : %s\n", buff);
359
360 count = lynq_split(buff, len, '\n', split_lines);
361 printf("----- %s\n", split_lines[0]);
362
363 mac_end = 0;
364 count_words = strlen(split_lines[0]);
365 if (strstr(split_lines[0], mac_header) != NULL) {
366 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
367 mac_end = mac_start + strlen(mac_header) + 1;
368 while (mac_end < count_words) {
369 if (split_lines[0][mac_end] != ' ') {
370 break;
371 }
372 mac_end++;
373 }
374 }
375
376 ip_end = 0;
377 if (strstr(split_lines[0], ip_header) != NULL) {
378 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
379 ip_end = ip_start + strlen(ip_header) + 1;
380 while (ip_end < count_words) {
381 if (split_lines[0][ip_end] != ' ') {
382 break;
383 }
384 ip_end++;
385 }
386 }
387
388 if (mac_end == 0 || ip_end == 0) {
389 return 0;
390 }
391
392 ret = 0;
393 for(index = 1;index < count; index++) {
394 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
395 continue;
396 }
397 mac_list[ret] = malloc(mac_end - mac_start + 1);
398 ip_list[ret] = malloc(ip_end - ip_start + 1);
399 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
400 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
401 trim_space(mac_list[ret], mac_end - mac_start - 1);
402 trim_space(ip_list[ret], ip_end - ip_start - 1);
403 ret++;
404 }
405
406 return ret;
407}
408
409static int get_hostname_by_ip(char *ip, char *hostname) {
410 struct in_addr addr ={0};
411 struct hostent *ht;
412
413 if (ip == NULL || *ip == '\0' || hostname == NULL) {
414 return -1;
415 }
416
417 if (inet_aton(ip, &addr) == 0) {
418 printf("---inet_aton fail\n");
419 return -1;
420 }
421
422 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
423
424 if (ht == NULL) {
425 printf("---gethostbyaddr fail\n");
426 herror(NULL);
427 return -1;
428 }
429
430 strcpy(hostname, ht->h_name);
431
432 return 0;
433}
434
435static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
436{
437 int count, index, words_count;
438 char * split_lines[128]= {0};
439 char * split_words[128] = {0};
440 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
441
442 CHECK_WPA_CTRL(ap_sta);
443
444 DO_REQUEST(lynq_wifi_list_networks);
445
446 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
447
448 //@todo check ssid field to compatible
449
450 ret = 0;
451 for(index=1; index < count; index++) {
452 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
453 if (words_count > 2) {
454 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
455 net_no_list[ret++] = atoi(split_words[0]);
456 }
457 }
458 }
459
460 return ret;
461}
462
463static int lynq_add_network(int ap_sta) {
464 int i=0;
465 CHECK_WPA_CTRL(ap_sta);
466 const char *lynq_wifi_add_network = "ADD_NETWORK";
467
468 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800469 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800470 return -1;
471 }
472
473 for(int i=0;i<reply_len;i++) {
474 if(cmd_reply[i] == '\n') {
475 cmd_reply[i] = '\0';
476 break;
477 }
478 }
479 return atoi(cmd_reply);
480}
you.chena6cd55a2022-05-08 12:20:18 +0800481
you.chen35020192022-05-06 11:30:57 +0800482static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
483{
484 int count, index;
485 int net_no_list[128];
486
487
488 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
489 for (index=0; index < count; index++) {
490 if (net_no_list[index] == net_no) {
491 return 0;
492 }
493 }
494
495 if (count >= 1)
496 index = net_no_list[count - 1];
497 else
498 index = -1;
499
500 while (index < net_no ) {
501 index = lynq_add_network(ap_sta);
502 if (index >= net_no) { // required network no created
503 return 0;
504 }
you.chena6cd55a2022-05-08 12:20:18 +0800505 else if( index < 0) {
506 printf("add network fail\n");
507 return -1;
508 }
you.chen35020192022-05-06 11:30:57 +0800509 }
510
511 if (index < 0)
512 return -1;
513
514 return 0;
515}
516
517static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
518 if (freq > 5000 && freq < 6000) {
519 return LYNQ_WIFI_5G_band;
520 }
521 else if (freq > 2000 && freq < 3000) {
522 return LYNQ_WIFI_2G_band;
523 }
524 return LYNQ_WIFI_2_and_5G_band;
525}
526
527static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
528 if (key_mgmt != NULL) {
529 if (memcmp( key_mgmt, "NONE", 4) == 0) {
530 return LYNQ_WIFI_AUTH_OPEN;
531 }
532 else if (memcmp( key_mgmt, "WEP", 3) == 0){
533 return LYNQ_WIFI_AUTH_WEP;
534 }
535 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
536 return LYNQ_WIFI_AUTH_WPA_PSK;
537 }
538 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
539 return LYNQ_WIFI_AUTH_WPA2_PSK;
540 }
541 }
542
543 return -1;
544}
545
546static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
547 if (flag != NULL) {
548 if (strstr( flag, "WPA2-PSK") != NULL){
549 return LYNQ_WIFI_AUTH_WPA2_PSK;
550 }
551 else if (strstr( flag, "WPA-PSK") != NULL){
552 return LYNQ_WIFI_AUTH_WPA_PSK;
553 }
554 else if (strstr( flag, "WEP") != NULL){
555 return LYNQ_WIFI_AUTH_WEP;
556 }
557 else if (strstr( flag, "NONE") != NULL) {
558 return LYNQ_WIFI_AUTH_OPEN;
559 }
560 }
561
562 return -1;
563}
564
565static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
566 switch (bw) {
567 case 10:
568 return LYNQ_WIFI_BANDWIDTH_HT10;
569 break;
570 case 20:
571 return LYNQ_WIFI_BANDWIDTH_HT20;
572 break;
573 case 40:
574 return LYNQ_WIFI_BANDWIDTH_HT40;
575 break;
576 case 80:
577 return LYNQ_WIFI_BANDWIDTH_HT80;
578 break;
579 default:
580 break;
581 }
582
583 return -1;
584}
585
586static int inner_get_status_info(int interface, curr_status_info *curr_state) {
587 int i, count;
588 char *p;
589 const char *lynq_status_cmd = "STATUS";
590 const char * FLAG_SSID = "ssid=";
591 const char * FLAG_SBSID = "bssid=";
592 const char * FLAG_KEY_MGMT = "key_mgmt=";
593 const char * FLAG_FREQ = "freq=";
594 const char * FLAG_STATE = "wpa_state=";
595 const char * FLAG_ID = "id=";
596 char *split_lines[128] = {0};
597
598 CHECK_WPA_CTRL(interface);
599
600 if (curr_state == NULL) {
601 return -1;
602 }
603
604 DO_REQUEST(lynq_status_cmd);
605
606 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
607
608 curr_state->net_no = -1;
609 ret = -1;
610 for(i=0; i < count; i++) {
611 if (curr_state->ap != NULL) {
612 p = strstr(split_lines[i], FLAG_SSID);
613 if (p != NULL) {
614 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
615 ret = 0;
616 continue;
617 }
618 p = strstr(split_lines[i], FLAG_SBSID);
619 if (p != NULL) {
620 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
621 ret = 0;
622 continue;
623 }
624 p = strstr(split_lines[i], FLAG_KEY_MGMT);
625 if (p != NULL) {
626 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
627 ret = 0;
628 continue;
629 }
630 p = strstr(split_lines[i], FLAG_FREQ);
631 if (p != NULL) {
632 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
633 ret = 0;
634 continue;
635 }
636 } // end if (ap != NULL)
637 if (curr_state->state != NULL) {
638 p = strstr(split_lines[i], FLAG_STATE);
639 if (p != NULL) {
640 strcpy(curr_state->state, p + strlen(FLAG_STATE));
641 ret = 0;
642 continue;
643 }
644
645 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800646 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800647 ret = 0;
648 curr_state->net_no = atoi(p);
649 printf("net_no %d, -- %s\n", curr_state->net_no, p);
650 }
651 }
652
653 return ret;
654}
655
656
qs.xiongf1b525b2022-03-31 00:58:23 -0400657int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500658{
you.chen35020192022-05-06 11:30:57 +0800659 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500660
you.chen35020192022-05-06 11:30:57 +0800661 if (ap_ssid == NULL) {
662 printf("ap_ssid is null\n");
663 return -1;
664 }
665 else {
666 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
667 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400668
you.chen35020192022-05-06 11:30:57 +0800669 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
670 return -1;
671 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400672
you.chen35020192022-05-06 11:30:57 +0800673 CHECK_IDX(idx, CTRL_AP);
674
675 CHECK_WPA_CTRL(CTRL_AP);
676
677 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
678
679 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
680 DO_OK_FAIL_REQUEST(cmd_save_config);
681
qs.xiong7a105ce2022-03-02 09:43:11 -0500682 return 0;
you.chen35020192022-05-06 11:30:57 +0800683
qs.xiong7a105ce2022-03-02 09:43:11 -0500684}
685
you.chen35020192022-05-06 11:30:57 +0800686int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500687{
you.chen35020192022-05-06 11:30:57 +0800688 CHECK_IDX(idx, CTRL_AP);
689 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500690}
691
qs.xiongf1b525b2022-03-31 00:58:23 -0400692int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500693{
you.chen35020192022-05-06 11:30:57 +0800694 char lynq_wifi_frequency_cmd[128]={0};
695 char lynq_cmd_mode[128]={0};
696 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500697
you.chen35020192022-05-06 11:30:57 +0800698 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
699 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
700 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400701
you.chen35020192022-05-06 11:30:57 +0800702 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
703 return -1;
704 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400705
you.chen35020192022-05-06 11:30:57 +0800706 CHECK_IDX(idx, CTRL_AP);
707
708 CHECK_WPA_CTRL(CTRL_AP);
709
710 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
711 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
712 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
713
714 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
715 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
716 DO_OK_FAIL_REQUEST(cmd_save_config);
717 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
qs.xiong7a105ce2022-03-02 09:43:11 -0500718
qs.xiong1af5daf2022-03-14 09:12:12 -0400719 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500720}
721
qs.xiongf1b525b2022-03-31 00:58:23 -0400722int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500723{
you.chen35020192022-05-06 11:30:57 +0800724 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400725
you.chen35020192022-05-06 11:30:57 +0800726 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400727
you.chen35020192022-05-06 11:30:57 +0800728 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
729 return -1;
730 }
731 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400732
733 return 0;
734}
735
qs.xiongf1b525b2022-03-31 00:58:23 -0400736int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
737{
you.chen35020192022-05-06 11:30:57 +0800738 CHECK_IDX(idx, CTRL_AP);
739 switch(bandwidth){
740 case LYNQ_WIFI_BANDWIDTH_HT10:
741 {
742 printf("bandwith [%d] not support now\n", bandwidth);
743 return -1;
744 }
745 case LYNQ_WIFI_BANDWIDTH_HT20:
746 {
747 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
748 system("wl down");
749 if (system(lynq_cmd_bandwith) != 0 ){
750 return -1;
751 }
752 system("wl up");
753 break;
754 }
755 case LYNQ_WIFI_BANDWIDTH_HT40:
756 {
757 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
758 sprintf(lynq_cmd_bandwith, "wl chanspec ");
759 system("wl down");
760 if (system(lynq_cmd_bandwith) != 0 ){
761 return -1;
762 }
763 system("wl up");
764 break;
765 }
766 case LYNQ_WIFI_BANDWIDTH_HT80:
767 {
768 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
769 system("wl down");
770 if (system(lynq_cmd_bandwith) != 0 ){
771 return -1;
772 }
773 system("wl up");
774 break;
775 }
776 default:
777 {
778 printf("auth type [%d] not support now\n", bandwidth);
779 return -1;
780 }
781 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400782
783
you.chen35020192022-05-06 11:30:57 +0800784 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400785}
you.chen35020192022-05-06 11:30:57 +0800786
qs.xiongf1b525b2022-03-31 00:58:23 -0400787int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
788{
you.chen35020192022-05-06 11:30:57 +0800789 int count = 0;
790 int index = 0;
791 char *split_words[128] = {0};
792 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400793
you.chen35020192022-05-06 11:30:57 +0800794 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500795
you.chen35020192022-05-06 11:30:57 +0800796 CHECK_WPA_CTRL(CTRL_AP);
797
798 DO_REQUEST(lynq_chanspec_cmd);
799
800 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
801 for(;index < count; index++) {
802 if (strncmp(split_words[index], "bw", 2) != 0) {
803 continue;
804 }
805
806 index++;
807 if (index >= count) {
808 return -1;
809 }
810
811 printf("bw %s\n", split_words[index]);
812 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
813 return 0;
814 }
815
816 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500817}
qs.xiong0fb469a2022-04-14 03:50:45 -0400818
qs.xiongf1b525b2022-03-31 00:58:23 -0400819int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500820{
you.chen35020192022-05-06 11:30:57 +0800821 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400822
you.chen35020192022-05-06 11:30:57 +0800823 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400824
you.chen35020192022-05-06 11:30:57 +0800825 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400826
you.chen35020192022-05-06 11:30:57 +0800827 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
828 return -1;
829 }
830
831 system("wl down");
832 if (system(lynq_cmd_channel) != 0 ){
833 return -1;
834 }
835 system("wl up");
836 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500837}
qs.xiong0fb469a2022-04-14 03:50:45 -0400838
qs.xiongf1b525b2022-03-31 00:58:23 -0400839int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500840{
you.chen35020192022-05-06 11:30:57 +0800841 int count = 0;
842 int index = 0;
843 char *split_words[128] = {0};
844 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400845
you.chen35020192022-05-06 11:30:57 +0800846 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400847
you.chen35020192022-05-06 11:30:57 +0800848 CHECK_WPA_CTRL(CTRL_AP);
849
850 DO_REQUEST(lynq_chanspec_cmd);
851
852 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
853 for(;index < count; index++) {
854 printf("---- %s\n",split_words[index]);
855 if (strncmp(split_words[index], "channel", 2) != 0) {
856 continue;
857 }
858
859 index++;
860 if (index >= count) {
861 return -1;
862 }
863
864 *channel = atoi(split_words[index]);
865 return 0;
866 }
867
868 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500869}
870
871
you.chen35020192022-05-06 11:30:57 +0800872int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500873{
you.chen35020192022-05-06 11:30:57 +0800874 CHECK_IDX(idx, CTRL_AP);
875
876 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
877 return -1;
878 }
879
880 CHECK_WPA_CTRL(CTRL_AP);
881
qs.xiong7a105ce2022-03-02 09:43:11 -0500882 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400883 case LYNQ_WIFI_AUTH_OPEN:
qs.xiong7a105ce2022-03-02 09:43:11 -0500884 {
you.chen35020192022-05-06 11:30:57 +0800885 char lynq_auth_cmd[64]={0};
886 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500887
you.chen35020192022-05-06 11:30:57 +0800888 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
889 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500890 break;
891 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400892 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +0800893 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -0500894 {
qs.xiongf1b525b2022-03-31 00:58:23 -0400895
you.chen35020192022-05-06 11:30:57 +0800896 char lynq_auth_cmd[64]={0};
897 char lynq_psk_cmd[64]={0};
898 char lynq_pairwise_cmd[64]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -0400899
you.chen35020192022-05-06 11:30:57 +0800900 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
901 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
902 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
903 }
904 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
905 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +0800906 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +0800907 }
908// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
909// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
910 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500911
you.chen35020192022-05-06 11:30:57 +0800912 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
913 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
914 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
915 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500916 break;
you.chen35020192022-05-06 11:30:57 +0800917 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500918 default:
you.chen35020192022-05-06 11:30:57 +0800919 {
920 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -0500921 return -1;
you.chen35020192022-05-06 11:30:57 +0800922 }
923 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500924
qs.xiong7a105ce2022-03-02 09:43:11 -0500925 return 0;
926}
927
you.chen35020192022-05-06 11:30:57 +0800928int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500929{
you.chen35020192022-05-06 11:30:57 +0800930 char lynq_auth_str[MAX_RET] = {0};
931
932 CHECK_IDX(idx, CTRL_AP);
933
934 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
935 return -1;
936 }
937
938 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
939 *auth = LYNQ_WIFI_AUTH_OPEN;
940 }
941 else {
942 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
943 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500944
qs.xiong1af5daf2022-03-14 09:12:12 -0400945 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500946}
qs.xiong1af5daf2022-03-14 09:12:12 -0400947
qs.xiong1af5daf2022-03-14 09:12:12 -0400948
qs.xiong7a105ce2022-03-02 09:43:11 -0500949
qs.xiongf1b525b2022-03-31 00:58:23 -0400950int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500951{
you.chen35020192022-05-06 11:30:57 +0800952 char LYNQ_WIFI_CMD[128]={0};
953 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
954 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -0400955
you.chen35020192022-05-06 11:30:57 +0800956 CHECK_IDX(idx, CTRL_AP);
957
958 CHECK_WPA_CTRL(CTRL_AP);
959
960// system("connmanctl enable wifi");
961// system("connmanctl tether wifi on cy-test 12345678");
962// system("ifconfig wlan0 down");
963// system("ifconfig wlan0 up");
964// system("ifconfig wlan0 up");
965
966 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
967 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
968
969 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
970 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
971
qs.xiong7a105ce2022-03-02 09:43:11 -0500972 return 0;
973}
974
qs.xiongf1b525b2022-03-31 00:58:23 -0400975int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500976{
you.chen35020192022-05-06 11:30:57 +0800977 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500978}
979
qs.xiongf1b525b2022-03-31 00:58:23 -0400980int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500981{
you.chen35020192022-05-06 11:30:57 +0800982 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -0400983
you.chen35020192022-05-06 11:30:57 +0800984 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -0400985
you.chen35020192022-05-06 11:30:57 +0800986 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -0400987
you.chen35020192022-05-06 11:30:57 +0800988 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
989
990 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
991
you.chenb4b121c2022-05-06 17:50:16 +0800992// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +0800993
qs.xiong7a105ce2022-03-02 09:43:11 -0500994 return 0;
995}
qs.xiong1af5daf2022-03-14 09:12:12 -0400996
qs.xiongf1b525b2022-03-31 00:58:23 -0400997int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500998{
you.chen35020192022-05-06 11:30:57 +0800999 char lynq_disable_cmd[128] = {0};
1000 char lynq_select_cmd[128] = {0};
1001 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001002
you.chen35020192022-05-06 11:30:57 +08001003 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001004
you.chen35020192022-05-06 11:30:57 +08001005 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001006
you.chen35020192022-05-06 11:30:57 +08001007 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1008 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1009
1010 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1011 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1012 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001013
1014 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001015}
1016
qs.xiongf1b525b2022-03-31 00:58:23 -04001017int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001018{
you.chen35020192022-05-06 11:30:57 +08001019 char lynq_disable_cmd[128] = {0};
1020 char lynq_select_cmd[128] = {0};
1021 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001022
you.chen35020192022-05-06 11:30:57 +08001023 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001024
you.chen35020192022-05-06 11:30:57 +08001025 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001026
you.chen35020192022-05-06 11:30:57 +08001027 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1028 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1029
1030 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1031 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1032 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001033
1034 return 0;
1035}
qs.xiongf1b525b2022-03-31 00:58:23 -04001036
you.chen35020192022-05-06 11:30:57 +08001037int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001038{
qs.xiongf1b525b2022-03-31 00:58:23 -04001039 int pass_len;
you.chen35020192022-05-06 11:30:57 +08001040 char lynq_tmp_cmd[300]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001041 pass_len=strlen(password);
you.chen35020192022-05-06 11:30:57 +08001042 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001043 return -1;
you.chen35020192022-05-06 11:30:57 +08001044 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001045
you.chen35020192022-05-06 11:30:57 +08001046 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001047
you.chen35020192022-05-06 11:30:57 +08001048 CHECK_WPA_CTRL(CTRL_AP);
1049
1050 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1051
1052 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1053 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001054
qs.xiongf1b525b2022-03-31 00:58:23 -04001055 return 0;
1056}
1057
you.chen35020192022-05-06 11:30:57 +08001058int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001059{
you.chen35020192022-05-06 11:30:57 +08001060 FILE * fp;
1061 int len, ret;
1062 int count, index;
1063 char *split_lines[128] = {0};
1064 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001065
you.chen35020192022-05-06 11:30:57 +08001066 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001067
you.chen35020192022-05-06 11:30:57 +08001068 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1069// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1070 if (NULL == fp) {
1071 printf("open file fail\n");
1072 return -1;
1073 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001074
you.chen35020192022-05-06 11:30:57 +08001075 buff = alloca(MAX_RET);
1076 fseek(fp, 0, SEEK_SET);
1077 len = fread(buff, 1, MAX_RET, fp);
1078 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001079
you.chen35020192022-05-06 11:30:57 +08001080 for(index=0; index < len; index ++) {
1081 if (memcmp(buff + index, "network={", 9) != 0) {
1082 continue;
1083 }
1084 p = buff + index + 9;
1085 for (; index < len; index ++ ) {
1086 if (buff[index] != '}') {
1087 continue;
1088 }
1089 buff[index] = '\0';
1090 break;
1091 }
1092 len = buff + index - p;
1093 }
1094
1095 count = lynq_split(p, len, '\n', split_lines);
1096
1097 ret = -1;
1098 for(index=0; index < count; index++) {
1099 p = strstr(split_lines[index], "psk=");
1100 if (p == NULL) {
1101 continue;
1102 }
1103 p += 4;
1104 if (*p == '\"') {
1105 p++;
1106 }
1107
1108 strcpy(password, p);
1109
1110 while(*password != '\0') {
1111 if (*password == '\"') {
1112 *password = '\0';
1113 break;
1114 }
1115 password++;
1116 }
1117 ret = 0;
1118 break;
1119 } //end for(index=0; index < count; index++)
1120
1121 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001122}
1123
you.chen35020192022-05-06 11:30:57 +08001124static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1125 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001126 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001127
you.chen35020192022-05-06 11:30:57 +08001128 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1129 return -1;
1130 }
1131
1132 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001133
1134 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1135 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1136 if (strcmp(lynq_proto_str, "RSN") == 0) {
1137 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1138 }
1139 }
1140 }
you.chen35020192022-05-06 11:30:57 +08001141 return 0;
1142}
1143
1144int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001145{
you.chen35020192022-05-06 11:30:57 +08001146 int pass_len, net_no, count, index;
1147 char lynq_tmp_cmd[300]={0};
1148 int net_no_list[128];
1149 lynq_wifi_auth_s net_auth;
1150 pass_len=strlen(password);
1151 if(pass_len < 8 || pass_len >= 64){
1152 return -1;
1153 }
1154
1155 CHECK_IDX(idx, CTRL_STA);
1156
1157 net_no = -1;
1158 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1159
1160 for (index=0; index < count; index++) {
1161 net_auth = -1;
1162 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1163 net_no = net_no_list[index];
1164 break;
1165 }
1166 }
1167
1168 if (net_no < 0) {
1169 return -1;
1170 }
1171
1172 CHECK_WPA_CTRL(CTRL_STA);
1173
1174 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1175
1176 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1177 DO_OK_FAIL_REQUEST(cmd_save_config);
1178
1179 return 0;
1180}
1181
1182int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1183
1184 FILE * fp;
1185 int len, ret;
1186 int count, index;
1187 char *split_lines[128] = {0};
1188 char *buff, *p;
1189
1190 CHECK_IDX(idx, CTRL_STA);
1191
1192 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1193 if (NULL == fp) {
1194 printf("open file fail\n");
1195 return -1;
1196 }
1197
1198 buff = alloca(MAX_RET);
1199 fseek(fp, 0, SEEK_SET);
1200 len = fread(buff, 1, MAX_RET, fp);
1201 fclose(fp);
1202
1203 for(index=0; index < len; index ++) {
1204 for(; index < len; index ++) {
1205 if (memcmp(buff + index, "network={", 9) != 0) {
1206 continue;
1207 }
1208 p = buff + index + 9;
1209 for (; index < len; index ++ ) {
1210 if (buff[index] != '}') {
1211 continue;
1212 }
1213 buff[index] = '\0';
1214 break;
1215 }
1216 len = buff + index - p;
1217 }
1218
1219 if (strstr(p, ap->ap_ssid) != NULL) {
1220 break;
1221 }
1222 }
1223
1224 if (index >= len) {
1225 return -1;
1226 }
1227
1228 count = lynq_split(p, len, '\n', split_lines);
1229
1230 ret = -1;
1231 for(index=0; index < count; index++) {
1232 p = strstr(split_lines[index], "psk=");
1233 if (p == NULL) {
1234 continue;
1235 }
1236 p += 4;
1237 if (*p == '\"') {
1238 p++;
1239 }
1240
1241 strcpy(password, p);
1242
1243 while(*password != '\0') {
1244 if (*password == '\"') {
1245 *password = '\0';
1246 break;
1247 }
1248 password++;
1249 }
1250 ret = 0;
1251 break;
1252 } //end for(index=0; index < count; index++)
1253
1254 return ret;
1255}
1256
1257static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1258{
qs.xiong97fa59b2022-04-07 05:41:29 -04001259 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001260
you.chen35020192022-05-06 11:30:57 +08001261 if (sta_ssid == NULL) {
1262 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001263 return -1;
you.chen35020192022-05-06 11:30:57 +08001264 }
1265
1266 CHECK_WPA_CTRL(CTRL_STA);
1267
1268 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1269
1270 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1271// DO_OK_FAIL_REQUEST(cmd_save_config);
1272
qs.xiong7a105ce2022-03-02 09:43:11 -05001273 return 0;
1274
1275}
1276
you.chen35020192022-05-06 11:30:57 +08001277static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001278{
you.chen35020192022-05-06 11:30:57 +08001279 char lynq_disable_cmd[128]={0};
1280 char lynq_select_cmd[128]={0};
1281
1282 CHECK_WPA_CTRL(CTRL_STA);
1283
1284 if (save != 0) {
1285 DO_OK_FAIL_REQUEST(cmd_save_config);
1286 }
1287
1288 if (start_flag == 0) {
1289 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1290 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1291 }
1292 else {
1293 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1294 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1295 }
1296
1297 return 0;
1298}
1299
1300int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1301{
1302 CHECK_IDX(idx, CTRL_STA);
1303
1304 return inner_get_param(CTRL_STA, idx, "ssid", sta_ssid);
1305}
1306
1307int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1308{
1309 scan_info_s *scan_list;
1310 saved_ap_info_s *save_list;
1311 int scan_len=0;
1312 int save_len=0;
1313 int best_index = -1;
1314 int best_scan_index = -1;
1315 int best_rssi = 0;
1316 int i, j;
1317
1318 CHECK_IDX(idx, CTRL_STA);
1319 if (info == NULL) {
1320 return -1;
1321 }
1322
1323 curr_status_info curr_state;
1324 ap_info_s ap_info;
1325 curr_state.ap = &ap_info;
1326 curr_state.state = NULL;
1327
1328 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1329 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1330 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1331 lynq_get_connect_ap_rssi(idx, &info->rssi);
1332 return 0;
1333 }
1334
1335 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1336 return -1;
1337 }
1338
1339 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1340 return -1;
1341 }
1342
1343 for (i=0; i < save_len; i++) {
1344 for (j=0; j < scan_len; j++) {
1345 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1346 && save_list[i].base_info.auth == scan_list[j].auth) {
1347 if (best_rssi == 0) {
1348 best_rssi = scan_list[j].rssi;
1349 }
1350 else if (best_rssi > scan_list[j].rssi) {
1351 best_index = i;
1352 best_scan_index = j;
1353 best_rssi = scan_list[j].rssi;
1354 }
1355 break;
1356 }
1357 }
1358 }
1359
1360 if (best_index >= 0) {
1361 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1362 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1363 info->rssi = best_rssi;
1364 return 0;
1365 }
1366
1367 return -1;
1368}
1369
1370static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1371{
1372 char lynq_auth_cmd[64]={0};
1373 char lynq_ket_mgmt_cmd[64]={0};
1374 char lynq_pairwise_cmd[64]={0};
1375 char lynq_psk_cmd[64]={0};
1376
1377 CHECK_WPA_CTRL(CTRL_STA);
1378
qs.xiong1af5daf2022-03-14 09:12:12 -04001379 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001380 case LYNQ_WIFI_AUTH_OPEN:
1381 {
1382 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001383
you.chen35020192022-05-06 11:30:57 +08001384 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1385// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001386 break;
1387 }
you.chen35020192022-05-06 11:30:57 +08001388 case LYNQ_WIFI_AUTH_WPA_PSK:
1389 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001390 {
you.chen35020192022-05-06 11:30:57 +08001391 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1392 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1393 }
1394 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001395 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001396 }
1397 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1398 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001399
you.chen35020192022-05-06 11:30:57 +08001400 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1401 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1402 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001403
you.chen35020192022-05-06 11:30:57 +08001404 if (password != NULL) {
1405 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1406 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1407 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001408
you.chen35020192022-05-06 11:30:57 +08001409// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001410 break;
you.chen35020192022-05-06 11:30:57 +08001411 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001412 default:
1413 return -1;
you.chen35020192022-05-06 11:30:57 +08001414 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001415
qs.xiongf1b525b2022-03-31 00:58:23 -04001416 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001417}
qs.xiong7a105ce2022-03-02 09:43:11 -05001418
you.chen35020192022-05-06 11:30:57 +08001419static int inner_get_curr_net_no(int interface) {
1420 curr_status_info curr_state;
1421 curr_state.ap = NULL;
1422 curr_state.state = NULL;
1423
1424 if (0 != inner_get_status_info(interface, &curr_state)) {
1425 return -1;
1426 }
1427
1428 return curr_state.net_no;
1429}
1430
1431int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001432{
you.chen35020192022-05-06 11:30:57 +08001433 int net_no;
1434 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001435
you.chen35020192022-05-06 11:30:57 +08001436 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001437
you.chen35020192022-05-06 11:30:57 +08001438 if (net_no < 0) {
1439 return -1;
1440 }
1441
1442 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001443}
1444
you.chen35020192022-05-06 11:30:57 +08001445int 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 -05001446{
you.chen35020192022-05-06 11:30:57 +08001447 int count, net_no, index;
1448 int net_no_list[128];
1449 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001450
you.chen35020192022-05-06 11:30:57 +08001451 if (ssid == NULL || *ssid == '\0') {
1452 printf("bad ssid\n");
1453 return -1;
1454 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001455
you.chen35020192022-05-06 11:30:57 +08001456 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1457 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1458 printf("bad password\n");
1459 return -1;
1460 }
1461 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001462
you.chen35020192022-05-06 11:30:57 +08001463 CHECK_IDX(idx, CTRL_STA);
1464
1465 net_no = -1;
1466 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1467
1468 for (index=0; index < count; index++) {
1469 net_auth = -1;
1470 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1471 net_no = net_no_list[index];
1472 break;
1473 }
1474 }
1475
1476 if (net_no < 0) {
1477 net_no = lynq_add_network(CTRL_STA);
1478 if (net_no == -1) {
1479 return -1;
1480 }
1481
1482 printf("net no is %d\n", net_no);
1483 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1484 return -1;
1485 }
1486 }
1487
1488 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1489 return -1;
1490 }
1491
1492 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001493}
1494
you.chen35020192022-05-06 11:30:57 +08001495int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001496{
you.chen35020192022-05-06 11:30:57 +08001497 ap_info_s ap;
1498 curr_status_info curr_state;
1499 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001500
you.chen35020192022-05-06 11:30:57 +08001501 if (ssid == NULL || *ssid == '\0') {
1502 return -1;
1503 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001504
you.chen35020192022-05-06 11:30:57 +08001505 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001506
you.chen35020192022-05-06 11:30:57 +08001507 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001508 curr_state.state = NULL;
1509
you.chen35020192022-05-06 11:30:57 +08001510 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1511 return 0;
1512 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001513
you.chen35020192022-05-06 11:30:57 +08001514 if (strcmp(ap.ap_ssid, ssid) != 0) {
1515 return 0;
1516 }
1517
1518 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001519}
qs.xiong97fa59b2022-04-07 05:41:29 -04001520
you.chena6cd55a2022-05-08 12:20:18 +08001521int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1522{
1523// const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong7a105ce2022-03-02 09:43:11 -05001524
you.chen35020192022-05-06 11:30:57 +08001525 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001526 CHECK_WPA_CTRL(CTRL_STA);
1527
1528 system("connmanctl enable wifi");
1529
you.chen35020192022-05-06 11:30:57 +08001530 if (system("ifconfig | grep wlan0") != 0) {
1531 return -1;
1532 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001533
you.chena6cd55a2022-05-08 12:20:18 +08001534// DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
1535
1536 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001537}
1538
you.chen35020192022-05-06 11:30:57 +08001539int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001540{
you.chena6cd55a2022-05-08 12:20:18 +08001541 char lynq_disable_network_cmd[MAX_CMD];
1542 curr_status_info curr_state;
1543 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001544
you.chena6cd55a2022-05-08 12:20:18 +08001545 CHECK_IDX(idx, CTRL_STA);
1546 CHECK_WPA_CTRL(CTRL_STA);
1547
1548 curr_state.ap = &ap_info;
1549 curr_state.state = NULL;
1550
1551 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1552 return 0;
1553 }
1554
1555 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1556 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1557
1558 DO_OK_FAIL_REQUEST(cmd_save_config);
1559
1560 return 0;
1561// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001562}
qs.xiong7a105ce2022-03-02 09:43:11 -05001563
you.chen35020192022-05-06 11:30:57 +08001564//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1565// int i, count;
1566// char *p;
1567// const char * FLAG_SSID = "ssid=";
1568// const char * FLAG_SBSID = "bssid=";
1569// const char * FLAG_KEY_MGMT = "key_mgmt=";
1570// const char * FLAG_FREQ = "freq=";
1571// char lynq_sta_cmd[MAX_CMD];
1572// char *split_lines[128] = {0};
1573
1574// CHECK_WPA_CTRL(CTRL_AP);
1575
1576// sprintf(lynq_sta_cmd, "STA %s", bssid);
1577
1578// DO_REQUEST(lynq_sta_cmd);
1579
1580// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1581
1582// for(i=0; i < count; i++) {
1583// p = strstr(split_lines[i], FLAG_SSID);
1584// if (p != NULL) {
1585// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1586// continue;
1587// }
1588// }
1589
1590// lynq_get_interface_ip(idx, ap->ap_ip);
1591// lynq_ap_password_set(idx, ap->psw);
1592
1593// return 0;
1594//}
1595
1596static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1597 curr_status_info curr_state;
1598 curr_state.ap = ap;
1599 curr_state.state = NULL;
1600 return inner_get_status_info(interface, &curr_state);
1601}
1602
1603int 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 -04001604{
you.chen35020192022-05-06 11:30:57 +08001605 int ip_count, index, i, line_count;
1606 const char *lynq_first_sta_cmd = "STA-FIRST";
1607 char lynq_next_sta_cmd[MAX_CMD];
1608 char *bssid[1024] = {0};
1609 char *mac_list[128] = {0};
1610 char *ip_list[128] = {0};
1611 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001612
you.chen35020192022-05-06 11:30:57 +08001613 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001614
you.chen35020192022-05-06 11:30:57 +08001615 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001616
you.chen35020192022-05-06 11:30:57 +08001617// ap_info_s * tmp_ap;
1618// device_info_s * tmp_list;
1619 if (ap == NULL || list == NULL || len == NULL) {
1620 printf("bad input param");
1621 return -1;
1622 }
1623
1624// ap = &tmp_ap;
1625// list = &tmp_list;
1626 *ap = malloc(sizeof (ap_info_s));
1627
1628 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1629 return -1;
1630 }
1631
1632 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1633 lynq_ap_password_get(idx, (*ap)->psw);
1634
1635 ip_count = get_ip_mac_list(mac_list, ip_list);
1636 printf("get count %d\n", ip_count);
1637
1638 DO_REQUEST(lynq_first_sta_cmd);
1639
1640 index = 0;
1641 while (reply_len > 0) {
1642 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1643 break;
1644 }
1645 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1646 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1647 strcpy(bssid[index], split_lines[0]);
1648 index++;
1649 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1650 reply_len = MAX_RET;
1651 cmd_reply[0] = '\0';
1652 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1653 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1654 break;
1655 }
1656 }
1657
1658 *len = index;
1659
1660 *list = malloc(sizeof(device_info_s) * (*len));
1661 for (index=0; index < *len; index++) {
1662 strcpy((*list)[index].sta_mac, bssid[index]);
1663 for(i=0;i < ip_count; i++ ) {
1664 if (strcmp(bssid[index], mac_list[i]) == 0) {
1665 strcpy((*list)[index].sta_ip, ip_list[i]);
1666 break;
1667 }
1668 }
1669 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1670 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1671 free(bssid[index]);
1672 }
1673
1674 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001675}
1676
you.chen35020192022-05-06 11:30:57 +08001677int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001678{
you.chen35020192022-05-06 11:30:57 +08001679 int i, count, index, count_words;
1680 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1681 char *split_lines[128] = {0};
1682 char *split_words[128] = {0};
1683 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001684
you.chen35020192022-05-06 11:30:57 +08001685 if (list == NULL || len == NULL) {
1686 return -1;
1687 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001688
you.chen35020192022-05-06 11:30:57 +08001689 CHECK_IDX(idx, CTRL_STA);
1690
1691 CHECK_WPA_CTRL(CTRL_STA);
1692
1693 DO_REQUEST(lynq_scan_result_cmd);
1694
1695 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1696 *len = count - 1;
1697 *list = malloc(sizeof (scan_info_s) * *len);
1698
1699 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1700 for (index=0; index <count_words; index++) {
1701 printf("----header: %s\n", split_words[index]);
1702 }
1703
1704 for(index = 1;index < count; index++) {
1705 printf("---- %s\n",split_lines[index]);
1706 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1707 if (count_words < 4)
1708 continue;
1709 printf("count: %d, %s\n", count_words, split_words[0]);
1710 //bssid / frequency / signal level / flags / ssid
1711 p = (*list) + index - 1;
1712 strcpy(p->mac, split_words[0]);
1713 p->band = convert_band_from_freq(atoi(split_words[1]));
1714 p->rssi = -1 * atoi( split_words[2]);
1715 p->auth = convert_max_auth_from_flag(split_words[3]);
1716 strcpy(p->ssid, split_words[4]);
1717 }
1718
1719 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001720}
qs.xiong97fa59b2022-04-07 05:41:29 -04001721
you.chen35020192022-05-06 11:30:57 +08001722int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1723{
1724 int count, net_no, index;
1725 int net_no_list[128];
1726 lynq_wifi_auth_s net_auth;
1727 char lynq_remove_cmd[MAX_CMD];
1728
1729 if (ssid == NULL || *ssid == '\0') {
1730 printf("bad ssid\n");
1731 return -1;
1732 }
1733
1734 CHECK_IDX(idx, CTRL_STA);
1735
1736 CHECK_WPA_CTRL(CTRL_STA);
1737
1738 net_no = -1;
1739 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1740
1741 for (index=0; index < count; index++) {
1742 net_auth = -1;
1743 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1744 net_no = net_no_list[index];
1745 break;
1746 }
1747 }
1748
1749 if (net_no < 0) {
1750 return 0;
1751 }
1752
1753 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1754
1755 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1756 DO_OK_FAIL_REQUEST(cmd_save_config);
1757
1758 return 0;
1759}
1760
1761int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1762{
1763 int count, index;
1764 int net_no_list[128];
1765 char freq[16];
1766
1767 if (list == NULL || len == NULL) {
1768 printf("bad param\n");
1769 return -1;
1770 }
1771
1772 CHECK_IDX(idx, CTRL_STA);
1773
1774// CHECK_WPA_CTRL(CTRL_STA);
1775
1776 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1777 printf("count is %d\n", count);
1778
1779 *list = malloc(sizeof (saved_ap_info_s) * count);
1780 *len = count;
1781
1782 for (index=0; index < count; index++) {
1783 printf("to get ssid %d\n", index);
1784 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1785 printf("to get bssid\n");
1786 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1787 printf("to get inner_get_network_auth\n");
1788 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1789 printf("to get frequency\n");
1790 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1791 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1792 }
1793 else {
1794 (*list)[index].base_info.band = -1;
1795 }
1796
1797 }
1798
1799 return 0;
1800}
1801
1802int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1803{
1804 const char *lynq_scan_cmd = "SCAN";
1805
1806 CHECK_IDX(idx, CTRL_STA);
1807
1808 CHECK_WPA_CTRL(CTRL_STA);
1809
1810 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1811
1812 return 0;
1813}
1814
1815int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1816 if (cb == NULL) {
1817 return -1;
1818 }
1819
1820 g_ap_callback_priv = priv;
1821 g_ap_callback_func = cb;
1822
1823 return 0;
1824}
1825
1826int lynq_unreg_ap_event_callback(void * priv) {
1827 if (g_ap_callback_priv == priv) {
1828 g_ap_callback_func = NULL;
1829 g_ap_callback_priv = NULL;
1830 return 0;
1831 }
1832 return -1;
1833}
1834
1835int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
1836 if (cb == NULL) {
1837 return -1;
1838 }
1839
1840 g_sta_callback_priv = priv;
1841 g_sta_callback_func = cb;
1842
1843 return 0;
1844}
1845
1846int lynq_unreg_sta_event_callback(void * priv) {
1847 if (g_sta_callback_priv == priv) {
1848 g_sta_callback_func = NULL;
1849 g_sta_callback_priv = NULL;
1850 return 0;
1851 }
1852 return -1;
1853}
1854
1855
1856static int inner_get_status_info_state (int interface, char *state) {
1857 curr_status_info curr_state;
1858 curr_state.ap = NULL;
1859 curr_state.state = state;
1860 return inner_get_status_info(interface, &curr_state);
1861}
1862
1863int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
1864{
1865 char state[MAX_CMD];
1866 const char * STATE_COMPLETED = "COMPLETED";
1867 CHECK_IDX(idx, CTRL_AP);
1868
1869 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
1870 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1871 return 0;
1872 }
1873
1874 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1875 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
1876 }
1877 else {
1878 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1879 }
1880
1881 return 0;
1882}
1883
1884int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
1885 char state[MAX_CMD];
1886 const char * STATE_COMPLETED = "COMPLETED";
1887 CHECK_IDX(idx, CTRL_STA);
1888
1889 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
1890 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1891 return 0;
1892 }
1893
1894 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1895 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
1896 }
1897 else {
1898 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1899 }
1900
1901 return 0;
1902}
1903
1904int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
1905// CHECK_IDX(idx, CTRL_AP);
1906// int ret = 0;
1907// size_t reply_len = MAX_RET;
1908// char cmd_reply[MAX_RET]={0};
1909// const char * cmd_str = "GET country";
1910// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
1911// do{
1912// if (NULL == s_lynq_wpa_ctrl) {
1913// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
1914// if (NULL == s_lynq_wpa_ctrl ) {
1915// printf("wpa_ctrl_open fail\n");
1916// return -1;
1917// }
1918// }
1919// }while(0);
1920
1921// do {
1922// reply_len = MAX_RET;
1923// cmd_reply[0] = '\0';
1924// printf("to call [%s]\n", cmd_str);
1925// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
1926// if (ret != 0) {
1927// printf("call ##cmd_str fail %d\n", ret);
1928// return ret;
1929// }
1930// cmd_reply[reply_len+1] = '\0';
1931// printf("cmd replay [ %s ]\n", cmd_reply);
1932// }while(0);
1933
1934 FILE *fp;
1935 size_t i = 0;
1936 char lynq_cmd_ret[MAX_RET]={0};
1937
1938// CHECK_IDX(idx, CTRL_AP);
1939
1940 if((fp=popen("wl country","r"))==NULL)
1941 {
1942 perror("popen error!");
1943 return -1;
1944 }
1945 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
1946 {
1947 perror("fread fail!");
1948 return -1;
1949 }
1950
1951 for(i=0; i < strlen(lynq_cmd_ret); i++) {
1952 if (lynq_cmd_ret[i] == ' ') {
1953 lynq_cmd_ret[i] = '\0';
1954 break;
1955 }
1956 }
1957
1958 strcpy(country_code,lynq_cmd_ret);
1959 printf("---country code %s\n", country_code);
1960
1961 int ret=pclose(fp);
1962 if(ret==-1)
1963 {
1964 perror("close file faild");
1965 }
1966
1967 return 0;
1968}
1969
1970int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
1971// const char * cmd_str = "GET country";
1972// CHECK_IDX(idx, CTRL_AP);
1973// CHECK_WPA_CTRL(CTRL_STA);
1974
1975// DO_REQUEST(cmd_str);
1976// printf("result %s\n", cmd_reply);
1977
1978 if (country_code == NULL || *country_code == '\0') {
1979 printf("bad country code\n");
1980 return -1;
1981 }
1982
1983 char lynq_country_cmd[MAX_CMD];
1984 sprintf(lynq_country_cmd, "wl country %s", country_code);
1985 if (system(lynq_country_cmd) == 0) {
1986 return 0;
1987 }
1988
1989 return -1;
1990}
1991
1992int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
1993{
1994 if (mac == NULL) {
1995 return -1;
1996 }
1997
1998 CHECK_IDX(idx, CTRL_STA);
1999 ap_info_s ap;
2000 ap.ap_mac[0] = '\0';
2001
2002 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2003 return -1;
2004 }
2005 strcpy(mac, ap.ap_mac);
2006
2007 return 0;
2008}
2009
2010int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2011{
2012 struct ifaddrs *ifaddr, *ifa;
2013 int family, s;
2014 char host[NI_MAXHOST];
2015 const char * ifaName = "wlan0";
2016 if (idx == 1) {
2017 ifaName = "ap0";
2018 }
2019 else if (idx != 0) {
2020 return -1;
2021 }
2022
2023 if (getifaddrs(&ifaddr) == -1)
2024 {
2025 perror("getifaddrs");
2026 return -1;
2027 //exit(EXIT_FAILURE);
2028 }
2029
2030
2031 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
2032 {
2033 if (ifa->ifa_addr == NULL)
2034 continue;
2035 host[0] = '\0';
2036 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
2037
2038 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
2039 if((strcmp(ifa->ifa_name,ifaName)==0))
2040 {
2041 if (s != 0)
2042 {
2043 // printf("getnameinfo() failed: %s", gai_strerror(s));
2044 //exit(EXIT_FAILURE);
2045 }
2046 freeifaddrs(ifaddr);
2047 strcpy(ip, host);
2048 printf("ip %s\n", ip);
2049 return 0;
2050 //printf(" Interface : <%s>",ifa->ifa_name );
2051 //printf(" Address : <%s>", host);
2052 }
2053 }
2054 return -1;
2055}
2056
2057int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2058{
2059 int count;
2060 size_t i;
2061 char *split_words[128] = {0};
2062 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2063
2064 CHECK_WPA_CTRL(idx);
2065
2066 DO_REQUEST(lynq_get_mac_cmd);
2067
2068 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2069 return -1;
2070 }
2071
2072 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2073
2074 if (count < 2) {
2075 return -1;
2076 }
2077
2078 for (i=0; i < strlen(split_words[1]); i++ ) {
2079 if (split_words[1][i] != ' ') {
2080 break;
2081 }
2082 }
2083
2084 strcpy(mac, split_words[1] + i);
2085
2086 return 0;
2087}
2088
2089int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2090{
2091// int count;
2092// char *split_words[128] = {0};
2093// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2094
2095// if (rssi == NULL) {
2096// return -1;
2097// }
2098
2099// CHECK_IDX(idx, CTRL_STA);
2100
2101// CHECK_WPA_CTRL(CTRL_STA);
2102
2103// DO_REQUEST(lynq_get_rssi_cmd);
2104
2105// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2106// return -1;
2107// }
2108
2109// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2110
2111// if (count < 2) {
2112// return -1;
2113// }
2114
2115// *rssi = atoi(split_words[1]) * -1;
2116
2117 FILE *fp;
2118 size_t i = 0;
2119 char lynq_cmd_ret[MAX_RET]={0};
2120
2121// CHECK_IDX(idx, CTRL_AP);
2122
2123 if((fp=popen("wl country","r"))==NULL)
2124 {
2125 perror("popen error!");
2126 return -1;
2127 }
2128 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2129 {
2130 perror("fread fail!");
2131 return -1;
2132 }
2133 *rssi = atoi(lynq_cmd_ret);
2134
2135 return 0;
2136}
2137
2138int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2139{
2140 if (band == NULL) {
2141 return -1;
2142 }
2143
2144 CHECK_IDX(idx, CTRL_STA);
2145 ap_info_s ap;
2146 ap.band = -1;
2147
2148 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2149 return -1;
2150 }
2151 *band = ap.band;
2152
2153 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002154}