blob: 682fa6d0fa0fef0b74c3e74b812fc55aa4472826 [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) { \
102 printf("call ##cmd_str fail %d\n", ret); \
103 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 ) {\
112 printf("cmd ##cmd_str return FAIL\n"); \
113 return -1; \
114 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
115 printf("cmd ##cmd_str return not OK|FAIL\n"); \
116 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);
469 if (memcpy(cmd_reply, "FAIL", 4) == 0) {
470 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}
481static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
482{
483 int count, index;
484 int net_no_list[128];
485
486
487 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
488 for (index=0; index < count; index++) {
489 if (net_no_list[index] == net_no) {
490 return 0;
491 }
492 }
493
494 if (count >= 1)
495 index = net_no_list[count - 1];
496 else
497 index = -1;
498
499 while (index < net_no ) {
500 index = lynq_add_network(ap_sta);
501 if (index >= net_no) { // required network no created
502 return 0;
503 }
504 }
505
506 if (index < 0)
507 return -1;
508
509 return 0;
510}
511
512static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
513 if (freq > 5000 && freq < 6000) {
514 return LYNQ_WIFI_5G_band;
515 }
516 else if (freq > 2000 && freq < 3000) {
517 return LYNQ_WIFI_2G_band;
518 }
519 return LYNQ_WIFI_2_and_5G_band;
520}
521
522static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
523 if (key_mgmt != NULL) {
524 if (memcmp( key_mgmt, "NONE", 4) == 0) {
525 return LYNQ_WIFI_AUTH_OPEN;
526 }
527 else if (memcmp( key_mgmt, "WEP", 3) == 0){
528 return LYNQ_WIFI_AUTH_WEP;
529 }
530 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
531 return LYNQ_WIFI_AUTH_WPA_PSK;
532 }
533 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
534 return LYNQ_WIFI_AUTH_WPA2_PSK;
535 }
536 }
537
538 return -1;
539}
540
541static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
542 if (flag != NULL) {
543 if (strstr( flag, "WPA2-PSK") != NULL){
544 return LYNQ_WIFI_AUTH_WPA2_PSK;
545 }
546 else if (strstr( flag, "WPA-PSK") != NULL){
547 return LYNQ_WIFI_AUTH_WPA_PSK;
548 }
549 else if (strstr( flag, "WEP") != NULL){
550 return LYNQ_WIFI_AUTH_WEP;
551 }
552 else if (strstr( flag, "NONE") != NULL) {
553 return LYNQ_WIFI_AUTH_OPEN;
554 }
555 }
556
557 return -1;
558}
559
560static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
561 switch (bw) {
562 case 10:
563 return LYNQ_WIFI_BANDWIDTH_HT10;
564 break;
565 case 20:
566 return LYNQ_WIFI_BANDWIDTH_HT20;
567 break;
568 case 40:
569 return LYNQ_WIFI_BANDWIDTH_HT40;
570 break;
571 case 80:
572 return LYNQ_WIFI_BANDWIDTH_HT80;
573 break;
574 default:
575 break;
576 }
577
578 return -1;
579}
580
581static int inner_get_status_info(int interface, curr_status_info *curr_state) {
582 int i, count;
583 char *p;
584 const char *lynq_status_cmd = "STATUS";
585 const char * FLAG_SSID = "ssid=";
586 const char * FLAG_SBSID = "bssid=";
587 const char * FLAG_KEY_MGMT = "key_mgmt=";
588 const char * FLAG_FREQ = "freq=";
589 const char * FLAG_STATE = "wpa_state=";
590 const char * FLAG_ID = "id=";
591 char *split_lines[128] = {0};
592
593 CHECK_WPA_CTRL(interface);
594
595 if (curr_state == NULL) {
596 return -1;
597 }
598
599 DO_REQUEST(lynq_status_cmd);
600
601 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
602
603 curr_state->net_no = -1;
604 ret = -1;
605 for(i=0; i < count; i++) {
606 if (curr_state->ap != NULL) {
607 p = strstr(split_lines[i], FLAG_SSID);
608 if (p != NULL) {
609 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
610 ret = 0;
611 continue;
612 }
613 p = strstr(split_lines[i], FLAG_SBSID);
614 if (p != NULL) {
615 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
616 ret = 0;
617 continue;
618 }
619 p = strstr(split_lines[i], FLAG_KEY_MGMT);
620 if (p != NULL) {
621 curr_state->ap->auth = convert_auth_from_key_mgmt(p);
622 ret = 0;
623 continue;
624 }
625 p = strstr(split_lines[i], FLAG_FREQ);
626 if (p != NULL) {
627 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
628 ret = 0;
629 continue;
630 }
631 } // end if (ap != NULL)
632 if (curr_state->state != NULL) {
633 p = strstr(split_lines[i], FLAG_STATE);
634 if (p != NULL) {
635 strcpy(curr_state->state, p + strlen(FLAG_STATE));
636 ret = 0;
637 continue;
638 }
639
640 } //end else if (state != NULL)
641 if ((p = strstr(split_lines[i], FLAG_ID)) != NULL) {
642 ret = 0;
643 curr_state->net_no = atoi(p);
644 printf("net_no %d, -- %s\n", curr_state->net_no, p);
645 }
646 }
647
648 return ret;
649}
650
651
qs.xiongf1b525b2022-03-31 00:58:23 -0400652int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500653{
you.chen35020192022-05-06 11:30:57 +0800654 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500655
you.chen35020192022-05-06 11:30:57 +0800656 if (ap_ssid == NULL) {
657 printf("ap_ssid is null\n");
658 return -1;
659 }
660 else {
661 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
662 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400663
you.chen35020192022-05-06 11:30:57 +0800664 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
665 return -1;
666 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400667
you.chen35020192022-05-06 11:30:57 +0800668 CHECK_IDX(idx, CTRL_AP);
669
670 CHECK_WPA_CTRL(CTRL_AP);
671
672 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
673
674 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
675 DO_OK_FAIL_REQUEST(cmd_save_config);
676
qs.xiong7a105ce2022-03-02 09:43:11 -0500677 return 0;
you.chen35020192022-05-06 11:30:57 +0800678
qs.xiong7a105ce2022-03-02 09:43:11 -0500679}
680
you.chen35020192022-05-06 11:30:57 +0800681int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500682{
you.chen35020192022-05-06 11:30:57 +0800683 CHECK_IDX(idx, CTRL_AP);
684 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500685}
686
qs.xiongf1b525b2022-03-31 00:58:23 -0400687int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500688{
you.chen35020192022-05-06 11:30:57 +0800689 char lynq_wifi_frequency_cmd[128]={0};
690 char lynq_cmd_mode[128]={0};
691 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500692
you.chen35020192022-05-06 11:30:57 +0800693 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
694 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
695 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400696
you.chen35020192022-05-06 11:30:57 +0800697 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
698 return -1;
699 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400700
you.chen35020192022-05-06 11:30:57 +0800701 CHECK_IDX(idx, CTRL_AP);
702
703 CHECK_WPA_CTRL(CTRL_AP);
704
705 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
706 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
707 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
708
709 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
710 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
711 DO_OK_FAIL_REQUEST(cmd_save_config);
712 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
qs.xiong7a105ce2022-03-02 09:43:11 -0500713
qs.xiong1af5daf2022-03-14 09:12:12 -0400714 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500715}
716
qs.xiongf1b525b2022-03-31 00:58:23 -0400717int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500718{
you.chen35020192022-05-06 11:30:57 +0800719 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400720
you.chen35020192022-05-06 11:30:57 +0800721 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400722
you.chen35020192022-05-06 11:30:57 +0800723 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
724 return -1;
725 }
726 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400727
728 return 0;
729}
730
qs.xiongf1b525b2022-03-31 00:58:23 -0400731int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
732{
you.chen35020192022-05-06 11:30:57 +0800733 CHECK_IDX(idx, CTRL_AP);
734 switch(bandwidth){
735 case LYNQ_WIFI_BANDWIDTH_HT10:
736 {
737 printf("bandwith [%d] not support now\n", bandwidth);
738 return -1;
739 }
740 case LYNQ_WIFI_BANDWIDTH_HT20:
741 {
742 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
743 system("wl down");
744 if (system(lynq_cmd_bandwith) != 0 ){
745 return -1;
746 }
747 system("wl up");
748 break;
749 }
750 case LYNQ_WIFI_BANDWIDTH_HT40:
751 {
752 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
753 sprintf(lynq_cmd_bandwith, "wl chanspec ");
754 system("wl down");
755 if (system(lynq_cmd_bandwith) != 0 ){
756 return -1;
757 }
758 system("wl up");
759 break;
760 }
761 case LYNQ_WIFI_BANDWIDTH_HT80:
762 {
763 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
764 system("wl down");
765 if (system(lynq_cmd_bandwith) != 0 ){
766 return -1;
767 }
768 system("wl up");
769 break;
770 }
771 default:
772 {
773 printf("auth type [%d] not support now\n", bandwidth);
774 return -1;
775 }
776 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400777
778
you.chen35020192022-05-06 11:30:57 +0800779 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400780}
you.chen35020192022-05-06 11:30:57 +0800781
qs.xiongf1b525b2022-03-31 00:58:23 -0400782int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
783{
you.chen35020192022-05-06 11:30:57 +0800784 int count = 0;
785 int index = 0;
786 char *split_words[128] = {0};
787 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400788
you.chen35020192022-05-06 11:30:57 +0800789 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500790
you.chen35020192022-05-06 11:30:57 +0800791 CHECK_WPA_CTRL(CTRL_AP);
792
793 DO_REQUEST(lynq_chanspec_cmd);
794
795 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
796 for(;index < count; index++) {
797 if (strncmp(split_words[index], "bw", 2) != 0) {
798 continue;
799 }
800
801 index++;
802 if (index >= count) {
803 return -1;
804 }
805
806 printf("bw %s\n", split_words[index]);
807 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
808 return 0;
809 }
810
811 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500812}
qs.xiong0fb469a2022-04-14 03:50:45 -0400813
qs.xiongf1b525b2022-03-31 00:58:23 -0400814int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500815{
you.chen35020192022-05-06 11:30:57 +0800816 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400817
you.chen35020192022-05-06 11:30:57 +0800818 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400819
you.chen35020192022-05-06 11:30:57 +0800820 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400821
you.chen35020192022-05-06 11:30:57 +0800822 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
823 return -1;
824 }
825
826 system("wl down");
827 if (system(lynq_cmd_channel) != 0 ){
828 return -1;
829 }
830 system("wl up");
831 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500832}
qs.xiong0fb469a2022-04-14 03:50:45 -0400833
qs.xiongf1b525b2022-03-31 00:58:23 -0400834int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500835{
you.chen35020192022-05-06 11:30:57 +0800836 int count = 0;
837 int index = 0;
838 char *split_words[128] = {0};
839 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400840
you.chen35020192022-05-06 11:30:57 +0800841 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -0400842
you.chen35020192022-05-06 11:30:57 +0800843 CHECK_WPA_CTRL(CTRL_AP);
844
845 DO_REQUEST(lynq_chanspec_cmd);
846
847 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
848 for(;index < count; index++) {
849 printf("---- %s\n",split_words[index]);
850 if (strncmp(split_words[index], "channel", 2) != 0) {
851 continue;
852 }
853
854 index++;
855 if (index >= count) {
856 return -1;
857 }
858
859 *channel = atoi(split_words[index]);
860 return 0;
861 }
862
863 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500864}
865
866
you.chen35020192022-05-06 11:30:57 +0800867int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500868{
you.chen35020192022-05-06 11:30:57 +0800869 CHECK_IDX(idx, CTRL_AP);
870
871 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
872 return -1;
873 }
874
875 CHECK_WPA_CTRL(CTRL_AP);
876
qs.xiong7a105ce2022-03-02 09:43:11 -0500877 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -0400878 case LYNQ_WIFI_AUTH_OPEN:
qs.xiong7a105ce2022-03-02 09:43:11 -0500879 {
you.chen35020192022-05-06 11:30:57 +0800880 char lynq_auth_cmd[64]={0};
881 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500882
you.chen35020192022-05-06 11:30:57 +0800883 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
884 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500885 break;
886 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400887 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +0800888 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -0500889 {
qs.xiongf1b525b2022-03-31 00:58:23 -0400890
you.chen35020192022-05-06 11:30:57 +0800891 char lynq_auth_cmd[64]={0};
892 char lynq_psk_cmd[64]={0};
893 char lynq_pairwise_cmd[64]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -0400894
you.chen35020192022-05-06 11:30:57 +0800895 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
896 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
897 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
898 }
899 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
900 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
901 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA2-PSK", AP_NETWORK_0);
902 }
903// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
904// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
905 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -0500906
you.chen35020192022-05-06 11:30:57 +0800907 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
908 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
909 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
910 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500911 break;
you.chen35020192022-05-06 11:30:57 +0800912 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500913 default:
you.chen35020192022-05-06 11:30:57 +0800914 {
915 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -0500916 return -1;
you.chen35020192022-05-06 11:30:57 +0800917 }
918 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500919
qs.xiong7a105ce2022-03-02 09:43:11 -0500920 return 0;
921}
922
you.chen35020192022-05-06 11:30:57 +0800923int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -0500924{
you.chen35020192022-05-06 11:30:57 +0800925 char lynq_auth_str[MAX_RET] = {0};
926
927 CHECK_IDX(idx, CTRL_AP);
928
929 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
930 return -1;
931 }
932
933 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
934 *auth = LYNQ_WIFI_AUTH_OPEN;
935 }
936 else {
937 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
938 }
qs.xiong7a105ce2022-03-02 09:43:11 -0500939
qs.xiong1af5daf2022-03-14 09:12:12 -0400940 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500941}
qs.xiong1af5daf2022-03-14 09:12:12 -0400942
qs.xiong1af5daf2022-03-14 09:12:12 -0400943
qs.xiong7a105ce2022-03-02 09:43:11 -0500944
qs.xiongf1b525b2022-03-31 00:58:23 -0400945int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500946{
you.chen35020192022-05-06 11:30:57 +0800947 char LYNQ_WIFI_CMD[128]={0};
948 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
949 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -0400950
you.chen35020192022-05-06 11:30:57 +0800951 CHECK_IDX(idx, CTRL_AP);
952
953 CHECK_WPA_CTRL(CTRL_AP);
954
955// system("connmanctl enable wifi");
956// system("connmanctl tether wifi on cy-test 12345678");
957// system("ifconfig wlan0 down");
958// system("ifconfig wlan0 up");
959// system("ifconfig wlan0 up");
960
961 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
962 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
963
964 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
965 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
966
qs.xiong7a105ce2022-03-02 09:43:11 -0500967 return 0;
968}
969
qs.xiongf1b525b2022-03-31 00:58:23 -0400970int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500971{
you.chen35020192022-05-06 11:30:57 +0800972 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500973}
974
qs.xiongf1b525b2022-03-31 00:58:23 -0400975int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500976{
you.chen35020192022-05-06 11:30:57 +0800977 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -0400978
you.chen35020192022-05-06 11:30:57 +0800979 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -0400980
you.chen35020192022-05-06 11:30:57 +0800981 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -0400982
you.chen35020192022-05-06 11:30:57 +0800983 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
984
985 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
986
you.chenb4b121c2022-05-06 17:50:16 +0800987// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +0800988
qs.xiong7a105ce2022-03-02 09:43:11 -0500989 return 0;
990}
qs.xiong1af5daf2022-03-14 09:12:12 -0400991
qs.xiongf1b525b2022-03-31 00:58:23 -0400992int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -0500993{
you.chen35020192022-05-06 11:30:57 +0800994 char lynq_disable_cmd[128] = {0};
995 char lynq_select_cmd[128] = {0};
996 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -0400997
you.chen35020192022-05-06 11:30:57 +0800998 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500999
you.chen35020192022-05-06 11:30:57 +08001000 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001001
you.chen35020192022-05-06 11:30:57 +08001002 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1003 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1004
1005 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1006 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1007 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001008
1009 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001010}
1011
qs.xiongf1b525b2022-03-31 00:58:23 -04001012int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001013{
you.chen35020192022-05-06 11:30:57 +08001014 char lynq_disable_cmd[128] = {0};
1015 char lynq_select_cmd[128] = {0};
1016 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001017
you.chen35020192022-05-06 11:30:57 +08001018 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001019
you.chen35020192022-05-06 11:30:57 +08001020 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001021
you.chen35020192022-05-06 11:30:57 +08001022 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1023 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1024
1025 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1026 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1027 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001028
1029 return 0;
1030}
qs.xiongf1b525b2022-03-31 00:58:23 -04001031
you.chen35020192022-05-06 11:30:57 +08001032int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001033{
qs.xiongf1b525b2022-03-31 00:58:23 -04001034 int pass_len;
you.chen35020192022-05-06 11:30:57 +08001035 char lynq_tmp_cmd[300]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001036 pass_len=strlen(password);
you.chen35020192022-05-06 11:30:57 +08001037 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001038 return -1;
you.chen35020192022-05-06 11:30:57 +08001039 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001040
you.chen35020192022-05-06 11:30:57 +08001041 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001042
you.chen35020192022-05-06 11:30:57 +08001043 CHECK_WPA_CTRL(CTRL_AP);
1044
1045 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1046
1047 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1048 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001049
qs.xiongf1b525b2022-03-31 00:58:23 -04001050 return 0;
1051}
1052
you.chen35020192022-05-06 11:30:57 +08001053int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001054{
you.chen35020192022-05-06 11:30:57 +08001055 FILE * fp;
1056 int len, ret;
1057 int count, index;
1058 char *split_lines[128] = {0};
1059 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001060
you.chen35020192022-05-06 11:30:57 +08001061 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001062
you.chen35020192022-05-06 11:30:57 +08001063 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1064// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1065 if (NULL == fp) {
1066 printf("open file fail\n");
1067 return -1;
1068 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001069
you.chen35020192022-05-06 11:30:57 +08001070 buff = alloca(MAX_RET);
1071 fseek(fp, 0, SEEK_SET);
1072 len = fread(buff, 1, MAX_RET, fp);
1073 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001074
you.chen35020192022-05-06 11:30:57 +08001075 for(index=0; index < len; index ++) {
1076 if (memcmp(buff + index, "network={", 9) != 0) {
1077 continue;
1078 }
1079 p = buff + index + 9;
1080 for (; index < len; index ++ ) {
1081 if (buff[index] != '}') {
1082 continue;
1083 }
1084 buff[index] = '\0';
1085 break;
1086 }
1087 len = buff + index - p;
1088 }
1089
1090 count = lynq_split(p, len, '\n', split_lines);
1091
1092 ret = -1;
1093 for(index=0; index < count; index++) {
1094 p = strstr(split_lines[index], "psk=");
1095 if (p == NULL) {
1096 continue;
1097 }
1098 p += 4;
1099 if (*p == '\"') {
1100 p++;
1101 }
1102
1103 strcpy(password, p);
1104
1105 while(*password != '\0') {
1106 if (*password == '\"') {
1107 *password = '\0';
1108 break;
1109 }
1110 password++;
1111 }
1112 ret = 0;
1113 break;
1114 } //end for(index=0; index < count; index++)
1115
1116 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001117}
1118
you.chen35020192022-05-06 11:30:57 +08001119static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1120 char lynq_auth_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001121
you.chen35020192022-05-06 11:30:57 +08001122 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1123 return -1;
1124 }
1125
1126 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
1127 return 0;
1128}
1129
1130int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001131{
you.chen35020192022-05-06 11:30:57 +08001132 int pass_len, net_no, count, index;
1133 char lynq_tmp_cmd[300]={0};
1134 int net_no_list[128];
1135 lynq_wifi_auth_s net_auth;
1136 pass_len=strlen(password);
1137 if(pass_len < 8 || pass_len >= 64){
1138 return -1;
1139 }
1140
1141 CHECK_IDX(idx, CTRL_STA);
1142
1143 net_no = -1;
1144 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1145
1146 for (index=0; index < count; index++) {
1147 net_auth = -1;
1148 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1149 net_no = net_no_list[index];
1150 break;
1151 }
1152 }
1153
1154 if (net_no < 0) {
1155 return -1;
1156 }
1157
1158 CHECK_WPA_CTRL(CTRL_STA);
1159
1160 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1161
1162 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1163 DO_OK_FAIL_REQUEST(cmd_save_config);
1164
1165 return 0;
1166}
1167
1168int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1169
1170 FILE * fp;
1171 int len, ret;
1172 int count, index;
1173 char *split_lines[128] = {0};
1174 char *buff, *p;
1175
1176 CHECK_IDX(idx, CTRL_STA);
1177
1178 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1179 if (NULL == fp) {
1180 printf("open file fail\n");
1181 return -1;
1182 }
1183
1184 buff = alloca(MAX_RET);
1185 fseek(fp, 0, SEEK_SET);
1186 len = fread(buff, 1, MAX_RET, fp);
1187 fclose(fp);
1188
1189 for(index=0; index < len; index ++) {
1190 for(; index < len; index ++) {
1191 if (memcmp(buff + index, "network={", 9) != 0) {
1192 continue;
1193 }
1194 p = buff + index + 9;
1195 for (; index < len; index ++ ) {
1196 if (buff[index] != '}') {
1197 continue;
1198 }
1199 buff[index] = '\0';
1200 break;
1201 }
1202 len = buff + index - p;
1203 }
1204
1205 if (strstr(p, ap->ap_ssid) != NULL) {
1206 break;
1207 }
1208 }
1209
1210 if (index >= len) {
1211 return -1;
1212 }
1213
1214 count = lynq_split(p, len, '\n', split_lines);
1215
1216 ret = -1;
1217 for(index=0; index < count; index++) {
1218 p = strstr(split_lines[index], "psk=");
1219 if (p == NULL) {
1220 continue;
1221 }
1222 p += 4;
1223 if (*p == '\"') {
1224 p++;
1225 }
1226
1227 strcpy(password, p);
1228
1229 while(*password != '\0') {
1230 if (*password == '\"') {
1231 *password = '\0';
1232 break;
1233 }
1234 password++;
1235 }
1236 ret = 0;
1237 break;
1238 } //end for(index=0; index < count; index++)
1239
1240 return ret;
1241}
1242
1243static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1244{
qs.xiong97fa59b2022-04-07 05:41:29 -04001245 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001246
you.chen35020192022-05-06 11:30:57 +08001247 if (sta_ssid == NULL) {
1248 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001249 return -1;
you.chen35020192022-05-06 11:30:57 +08001250 }
1251
1252 CHECK_WPA_CTRL(CTRL_STA);
1253
1254 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1255
1256 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1257// DO_OK_FAIL_REQUEST(cmd_save_config);
1258
qs.xiong7a105ce2022-03-02 09:43:11 -05001259 return 0;
1260
1261}
1262
you.chen35020192022-05-06 11:30:57 +08001263static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001264{
you.chen35020192022-05-06 11:30:57 +08001265 char lynq_disable_cmd[128]={0};
1266 char lynq_select_cmd[128]={0};
1267
1268 CHECK_WPA_CTRL(CTRL_STA);
1269
1270 if (save != 0) {
1271 DO_OK_FAIL_REQUEST(cmd_save_config);
1272 }
1273
1274 if (start_flag == 0) {
1275 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
1276 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1277 }
1278 else {
1279 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1280 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1281 }
1282
1283 return 0;
1284}
1285
1286int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1287{
1288 CHECK_IDX(idx, CTRL_STA);
1289
1290 return inner_get_param(CTRL_STA, idx, "ssid", sta_ssid);
1291}
1292
1293int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1294{
1295 scan_info_s *scan_list;
1296 saved_ap_info_s *save_list;
1297 int scan_len=0;
1298 int save_len=0;
1299 int best_index = -1;
1300 int best_scan_index = -1;
1301 int best_rssi = 0;
1302 int i, j;
1303
1304 CHECK_IDX(idx, CTRL_STA);
1305 if (info == NULL) {
1306 return -1;
1307 }
1308
1309 curr_status_info curr_state;
1310 ap_info_s ap_info;
1311 curr_state.ap = &ap_info;
1312 curr_state.state = NULL;
1313
1314 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1315 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
1316 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1317 lynq_get_connect_ap_rssi(idx, &info->rssi);
1318 return 0;
1319 }
1320
1321 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
1322 return -1;
1323 }
1324
1325 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
1326 return -1;
1327 }
1328
1329 for (i=0; i < save_len; i++) {
1330 for (j=0; j < scan_len; j++) {
1331 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1332 && save_list[i].base_info.auth == scan_list[j].auth) {
1333 if (best_rssi == 0) {
1334 best_rssi = scan_list[j].rssi;
1335 }
1336 else if (best_rssi > scan_list[j].rssi) {
1337 best_index = i;
1338 best_scan_index = j;
1339 best_rssi = scan_list[j].rssi;
1340 }
1341 break;
1342 }
1343 }
1344 }
1345
1346 if (best_index >= 0) {
1347 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1348 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1349 info->rssi = best_rssi;
1350 return 0;
1351 }
1352
1353 return -1;
1354}
1355
1356static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1357{
1358 char lynq_auth_cmd[64]={0};
1359 char lynq_ket_mgmt_cmd[64]={0};
1360 char lynq_pairwise_cmd[64]={0};
1361 char lynq_psk_cmd[64]={0};
1362
1363 CHECK_WPA_CTRL(CTRL_STA);
1364
qs.xiong1af5daf2022-03-14 09:12:12 -04001365 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001366 case LYNQ_WIFI_AUTH_OPEN:
1367 {
1368 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001369
you.chen35020192022-05-06 11:30:57 +08001370 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1371// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001372 break;
1373 }
you.chen35020192022-05-06 11:30:57 +08001374 case LYNQ_WIFI_AUTH_WPA_PSK:
1375 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001376 {
you.chen35020192022-05-06 11:30:57 +08001377 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1378 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1379 }
1380 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1381 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", net_no);
1382 }
1383 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1384 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001385
you.chen35020192022-05-06 11:30:57 +08001386 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1387 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1388 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001389
you.chen35020192022-05-06 11:30:57 +08001390 if (password != NULL) {
1391 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1392 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1393 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001394
you.chen35020192022-05-06 11:30:57 +08001395// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001396 break;
you.chen35020192022-05-06 11:30:57 +08001397 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001398 default:
1399 return -1;
you.chen35020192022-05-06 11:30:57 +08001400 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001401
qs.xiongf1b525b2022-03-31 00:58:23 -04001402 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001403}
qs.xiong7a105ce2022-03-02 09:43:11 -05001404
you.chen35020192022-05-06 11:30:57 +08001405static int inner_get_curr_net_no(int interface) {
1406 curr_status_info curr_state;
1407 curr_state.ap = NULL;
1408 curr_state.state = NULL;
1409
1410 if (0 != inner_get_status_info(interface, &curr_state)) {
1411 return -1;
1412 }
1413
1414 return curr_state.net_no;
1415}
1416
1417int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001418{
you.chen35020192022-05-06 11:30:57 +08001419 int net_no;
1420 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001421
you.chen35020192022-05-06 11:30:57 +08001422 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001423
you.chen35020192022-05-06 11:30:57 +08001424 if (net_no < 0) {
1425 return -1;
1426 }
1427
1428 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001429}
1430
you.chen35020192022-05-06 11:30:57 +08001431int 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 -05001432{
you.chen35020192022-05-06 11:30:57 +08001433 int count, net_no, index;
1434 int net_no_list[128];
1435 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001436
you.chen35020192022-05-06 11:30:57 +08001437 if (ssid == NULL || *ssid == '\0') {
1438 printf("bad ssid\n");
1439 return -1;
1440 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001441
you.chen35020192022-05-06 11:30:57 +08001442 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1443 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1444 printf("bad password\n");
1445 return -1;
1446 }
1447 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001448
you.chen35020192022-05-06 11:30:57 +08001449 CHECK_IDX(idx, CTRL_STA);
1450
1451 net_no = -1;
1452 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1453
1454 for (index=0; index < count; index++) {
1455 net_auth = -1;
1456 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1457 net_no = net_no_list[index];
1458 break;
1459 }
1460 }
1461
1462 if (net_no < 0) {
1463 net_no = lynq_add_network(CTRL_STA);
1464 if (net_no == -1) {
1465 return -1;
1466 }
1467
1468 printf("net no is %d\n", net_no);
1469 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1470 return -1;
1471 }
1472 }
1473
1474 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1475 return -1;
1476 }
1477
1478 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001479}
1480
you.chen35020192022-05-06 11:30:57 +08001481int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001482{
you.chen35020192022-05-06 11:30:57 +08001483 ap_info_s ap;
1484 curr_status_info curr_state;
1485 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001486
you.chen35020192022-05-06 11:30:57 +08001487 if (ssid == NULL || *ssid == '\0') {
1488 return -1;
1489 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001490
you.chen35020192022-05-06 11:30:57 +08001491 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001492
you.chen35020192022-05-06 11:30:57 +08001493 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001494 curr_state.state = NULL;
1495
you.chen35020192022-05-06 11:30:57 +08001496 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1497 return 0;
1498 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001499
you.chen35020192022-05-06 11:30:57 +08001500 if (strcmp(ap.ap_ssid, ssid) != 0) {
1501 return 0;
1502 }
1503
1504 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001505}
qs.xiong97fa59b2022-04-07 05:41:29 -04001506
you.chen35020192022-05-06 11:30:57 +08001507int lynq_wifi_sta_start(lynq_wifi_index_e idx) {
qs.xiong7a105ce2022-03-02 09:43:11 -05001508
you.chen35020192022-05-06 11:30:57 +08001509 CHECK_IDX(idx, CTRL_STA);
1510 if (system("ifconfig | grep wlan0") != 0) {
1511 return -1;
1512 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001513
you.chen35020192022-05-06 11:30:57 +08001514 return system("connmanctl enable wifi");
qs.xiong7a105ce2022-03-02 09:43:11 -05001515}
1516
you.chen35020192022-05-06 11:30:57 +08001517int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001518{
you.chen35020192022-05-06 11:30:57 +08001519 CHECK_IDX(idx, CTRL_STA);
1520
1521 return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001522}
qs.xiong7a105ce2022-03-02 09:43:11 -05001523
you.chen35020192022-05-06 11:30:57 +08001524//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1525// int i, count;
1526// char *p;
1527// const char * FLAG_SSID = "ssid=";
1528// const char * FLAG_SBSID = "bssid=";
1529// const char * FLAG_KEY_MGMT = "key_mgmt=";
1530// const char * FLAG_FREQ = "freq=";
1531// char lynq_sta_cmd[MAX_CMD];
1532// char *split_lines[128] = {0};
1533
1534// CHECK_WPA_CTRL(CTRL_AP);
1535
1536// sprintf(lynq_sta_cmd, "STA %s", bssid);
1537
1538// DO_REQUEST(lynq_sta_cmd);
1539
1540// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1541
1542// for(i=0; i < count; i++) {
1543// p = strstr(split_lines[i], FLAG_SSID);
1544// if (p != NULL) {
1545// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1546// continue;
1547// }
1548// }
1549
1550// lynq_get_interface_ip(idx, ap->ap_ip);
1551// lynq_ap_password_set(idx, ap->psw);
1552
1553// return 0;
1554//}
1555
1556static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1557 curr_status_info curr_state;
1558 curr_state.ap = ap;
1559 curr_state.state = NULL;
1560 return inner_get_status_info(interface, &curr_state);
1561}
1562
1563int 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 -04001564{
you.chen35020192022-05-06 11:30:57 +08001565 int ip_count, index, i, line_count;
1566 const char *lynq_first_sta_cmd = "STA-FIRST";
1567 char lynq_next_sta_cmd[MAX_CMD];
1568 char *bssid[1024] = {0};
1569 char *mac_list[128] = {0};
1570 char *ip_list[128] = {0};
1571 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001572
you.chen35020192022-05-06 11:30:57 +08001573 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001574
you.chen35020192022-05-06 11:30:57 +08001575 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001576
you.chen35020192022-05-06 11:30:57 +08001577// ap_info_s * tmp_ap;
1578// device_info_s * tmp_list;
1579 if (ap == NULL || list == NULL || len == NULL) {
1580 printf("bad input param");
1581 return -1;
1582 }
1583
1584// ap = &tmp_ap;
1585// list = &tmp_list;
1586 *ap = malloc(sizeof (ap_info_s));
1587
1588 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1589 return -1;
1590 }
1591
1592 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1593 lynq_ap_password_get(idx, (*ap)->psw);
1594
1595 ip_count = get_ip_mac_list(mac_list, ip_list);
1596 printf("get count %d\n", ip_count);
1597
1598 DO_REQUEST(lynq_first_sta_cmd);
1599
1600 index = 0;
1601 while (reply_len > 0) {
1602 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1603 break;
1604 }
1605 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1606 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1607 strcpy(bssid[index], split_lines[0]);
1608 index++;
1609 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1610 reply_len = MAX_RET;
1611 cmd_reply[0] = '\0';
1612 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1613 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1614 break;
1615 }
1616 }
1617
1618 *len = index;
1619
1620 *list = malloc(sizeof(device_info_s) * (*len));
1621 for (index=0; index < *len; index++) {
1622 strcpy((*list)[index].sta_mac, bssid[index]);
1623 for(i=0;i < ip_count; i++ ) {
1624 if (strcmp(bssid[index], mac_list[i]) == 0) {
1625 strcpy((*list)[index].sta_ip, ip_list[i]);
1626 break;
1627 }
1628 }
1629 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1630 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1631 free(bssid[index]);
1632 }
1633
1634 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001635}
1636
you.chen35020192022-05-06 11:30:57 +08001637int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001638{
you.chen35020192022-05-06 11:30:57 +08001639 int i, count, index, count_words;
1640 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1641 char *split_lines[128] = {0};
1642 char *split_words[128] = {0};
1643 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001644
you.chen35020192022-05-06 11:30:57 +08001645 if (list == NULL || len == NULL) {
1646 return -1;
1647 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001648
you.chen35020192022-05-06 11:30:57 +08001649 CHECK_IDX(idx, CTRL_STA);
1650
1651 CHECK_WPA_CTRL(CTRL_STA);
1652
1653 DO_REQUEST(lynq_scan_result_cmd);
1654
1655 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1656 *len = count - 1;
1657 *list = malloc(sizeof (scan_info_s) * *len);
1658
1659 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1660 for (index=0; index <count_words; index++) {
1661 printf("----header: %s\n", split_words[index]);
1662 }
1663
1664 for(index = 1;index < count; index++) {
1665 printf("---- %s\n",split_lines[index]);
1666 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1667 if (count_words < 4)
1668 continue;
1669 printf("count: %d, %s\n", count_words, split_words[0]);
1670 //bssid / frequency / signal level / flags / ssid
1671 p = (*list) + index - 1;
1672 strcpy(p->mac, split_words[0]);
1673 p->band = convert_band_from_freq(atoi(split_words[1]));
1674 p->rssi = -1 * atoi( split_words[2]);
1675 p->auth = convert_max_auth_from_flag(split_words[3]);
1676 strcpy(p->ssid, split_words[4]);
1677 }
1678
1679 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001680}
qs.xiong97fa59b2022-04-07 05:41:29 -04001681
you.chen35020192022-05-06 11:30:57 +08001682int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1683{
1684 int count, net_no, index;
1685 int net_no_list[128];
1686 lynq_wifi_auth_s net_auth;
1687 char lynq_remove_cmd[MAX_CMD];
1688
1689 if (ssid == NULL || *ssid == '\0') {
1690 printf("bad ssid\n");
1691 return -1;
1692 }
1693
1694 CHECK_IDX(idx, CTRL_STA);
1695
1696 CHECK_WPA_CTRL(CTRL_STA);
1697
1698 net_no = -1;
1699 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1700
1701 for (index=0; index < count; index++) {
1702 net_auth = -1;
1703 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1704 net_no = net_no_list[index];
1705 break;
1706 }
1707 }
1708
1709 if (net_no < 0) {
1710 return 0;
1711 }
1712
1713 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1714
1715 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1716 DO_OK_FAIL_REQUEST(cmd_save_config);
1717
1718 return 0;
1719}
1720
1721int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1722{
1723 int count, index;
1724 int net_no_list[128];
1725 char freq[16];
1726
1727 if (list == NULL || len == NULL) {
1728 printf("bad param\n");
1729 return -1;
1730 }
1731
1732 CHECK_IDX(idx, CTRL_STA);
1733
1734// CHECK_WPA_CTRL(CTRL_STA);
1735
1736 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1737 printf("count is %d\n", count);
1738
1739 *list = malloc(sizeof (saved_ap_info_s) * count);
1740 *len = count;
1741
1742 for (index=0; index < count; index++) {
1743 printf("to get ssid %d\n", index);
1744 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1745 printf("to get bssid\n");
1746 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1747 printf("to get inner_get_network_auth\n");
1748 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1749 printf("to get frequency\n");
1750 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1751 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1752 }
1753 else {
1754 (*list)[index].base_info.band = -1;
1755 }
1756
1757 }
1758
1759 return 0;
1760}
1761
1762int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1763{
1764 const char *lynq_scan_cmd = "SCAN";
1765
1766 CHECK_IDX(idx, CTRL_STA);
1767
1768 CHECK_WPA_CTRL(CTRL_STA);
1769
1770 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1771
1772 return 0;
1773}
1774
1775int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1776 if (cb == NULL) {
1777 return -1;
1778 }
1779
1780 g_ap_callback_priv = priv;
1781 g_ap_callback_func = cb;
1782
1783 return 0;
1784}
1785
1786int lynq_unreg_ap_event_callback(void * priv) {
1787 if (g_ap_callback_priv == priv) {
1788 g_ap_callback_func = NULL;
1789 g_ap_callback_priv = NULL;
1790 return 0;
1791 }
1792 return -1;
1793}
1794
1795int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
1796 if (cb == NULL) {
1797 return -1;
1798 }
1799
1800 g_sta_callback_priv = priv;
1801 g_sta_callback_func = cb;
1802
1803 return 0;
1804}
1805
1806int lynq_unreg_sta_event_callback(void * priv) {
1807 if (g_sta_callback_priv == priv) {
1808 g_sta_callback_func = NULL;
1809 g_sta_callback_priv = NULL;
1810 return 0;
1811 }
1812 return -1;
1813}
1814
1815
1816static int inner_get_status_info_state (int interface, char *state) {
1817 curr_status_info curr_state;
1818 curr_state.ap = NULL;
1819 curr_state.state = state;
1820 return inner_get_status_info(interface, &curr_state);
1821}
1822
1823int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
1824{
1825 char state[MAX_CMD];
1826 const char * STATE_COMPLETED = "COMPLETED";
1827 CHECK_IDX(idx, CTRL_AP);
1828
1829 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
1830 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1831 return 0;
1832 }
1833
1834 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1835 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
1836 }
1837 else {
1838 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1839 }
1840
1841 return 0;
1842}
1843
1844int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
1845 char state[MAX_CMD];
1846 const char * STATE_COMPLETED = "COMPLETED";
1847 CHECK_IDX(idx, CTRL_STA);
1848
1849 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
1850 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1851 return 0;
1852 }
1853
1854 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1855 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
1856 }
1857 else {
1858 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1859 }
1860
1861 return 0;
1862}
1863
1864int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
1865// CHECK_IDX(idx, CTRL_AP);
1866// int ret = 0;
1867// size_t reply_len = MAX_RET;
1868// char cmd_reply[MAX_RET]={0};
1869// const char * cmd_str = "GET country";
1870// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
1871// do{
1872// if (NULL == s_lynq_wpa_ctrl) {
1873// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
1874// if (NULL == s_lynq_wpa_ctrl ) {
1875// printf("wpa_ctrl_open fail\n");
1876// return -1;
1877// }
1878// }
1879// }while(0);
1880
1881// do {
1882// reply_len = MAX_RET;
1883// cmd_reply[0] = '\0';
1884// printf("to call [%s]\n", cmd_str);
1885// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
1886// if (ret != 0) {
1887// printf("call ##cmd_str fail %d\n", ret);
1888// return ret;
1889// }
1890// cmd_reply[reply_len+1] = '\0';
1891// printf("cmd replay [ %s ]\n", cmd_reply);
1892// }while(0);
1893
1894 FILE *fp;
1895 size_t i = 0;
1896 char lynq_cmd_ret[MAX_RET]={0};
1897
1898// CHECK_IDX(idx, CTRL_AP);
1899
1900 if((fp=popen("wl country","r"))==NULL)
1901 {
1902 perror("popen error!");
1903 return -1;
1904 }
1905 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
1906 {
1907 perror("fread fail!");
1908 return -1;
1909 }
1910
1911 for(i=0; i < strlen(lynq_cmd_ret); i++) {
1912 if (lynq_cmd_ret[i] == ' ') {
1913 lynq_cmd_ret[i] = '\0';
1914 break;
1915 }
1916 }
1917
1918 strcpy(country_code,lynq_cmd_ret);
1919 printf("---country code %s\n", country_code);
1920
1921 int ret=pclose(fp);
1922 if(ret==-1)
1923 {
1924 perror("close file faild");
1925 }
1926
1927 return 0;
1928}
1929
1930int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
1931// const char * cmd_str = "GET country";
1932// CHECK_IDX(idx, CTRL_AP);
1933// CHECK_WPA_CTRL(CTRL_STA);
1934
1935// DO_REQUEST(cmd_str);
1936// printf("result %s\n", cmd_reply);
1937
1938 if (country_code == NULL || *country_code == '\0') {
1939 printf("bad country code\n");
1940 return -1;
1941 }
1942
1943 char lynq_country_cmd[MAX_CMD];
1944 sprintf(lynq_country_cmd, "wl country %s", country_code);
1945 if (system(lynq_country_cmd) == 0) {
1946 return 0;
1947 }
1948
1949 return -1;
1950}
1951
1952int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
1953{
1954 if (mac == NULL) {
1955 return -1;
1956 }
1957
1958 CHECK_IDX(idx, CTRL_STA);
1959 ap_info_s ap;
1960 ap.ap_mac[0] = '\0';
1961
1962 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
1963 return -1;
1964 }
1965 strcpy(mac, ap.ap_mac);
1966
1967 return 0;
1968}
1969
1970int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
1971{
1972 struct ifaddrs *ifaddr, *ifa;
1973 int family, s;
1974 char host[NI_MAXHOST];
1975 const char * ifaName = "wlan0";
1976 if (idx == 1) {
1977 ifaName = "ap0";
1978 }
1979 else if (idx != 0) {
1980 return -1;
1981 }
1982
1983 if (getifaddrs(&ifaddr) == -1)
1984 {
1985 perror("getifaddrs");
1986 return -1;
1987 //exit(EXIT_FAILURE);
1988 }
1989
1990
1991 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
1992 {
1993 if (ifa->ifa_addr == NULL)
1994 continue;
1995 host[0] = '\0';
1996 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1997
1998 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
1999 if((strcmp(ifa->ifa_name,ifaName)==0))
2000 {
2001 if (s != 0)
2002 {
2003 // printf("getnameinfo() failed: %s", gai_strerror(s));
2004 //exit(EXIT_FAILURE);
2005 }
2006 freeifaddrs(ifaddr);
2007 strcpy(ip, host);
2008 printf("ip %s\n", ip);
2009 return 0;
2010 //printf(" Interface : <%s>",ifa->ifa_name );
2011 //printf(" Address : <%s>", host);
2012 }
2013 }
2014 return -1;
2015}
2016
2017int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2018{
2019 int count;
2020 size_t i;
2021 char *split_words[128] = {0};
2022 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2023
2024 CHECK_WPA_CTRL(idx);
2025
2026 DO_REQUEST(lynq_get_mac_cmd);
2027
2028 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2029 return -1;
2030 }
2031
2032 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2033
2034 if (count < 2) {
2035 return -1;
2036 }
2037
2038 for (i=0; i < strlen(split_words[1]); i++ ) {
2039 if (split_words[1][i] != ' ') {
2040 break;
2041 }
2042 }
2043
2044 strcpy(mac, split_words[1] + i);
2045
2046 return 0;
2047}
2048
2049int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2050{
2051// int count;
2052// char *split_words[128] = {0};
2053// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2054
2055// if (rssi == NULL) {
2056// return -1;
2057// }
2058
2059// CHECK_IDX(idx, CTRL_STA);
2060
2061// CHECK_WPA_CTRL(CTRL_STA);
2062
2063// DO_REQUEST(lynq_get_rssi_cmd);
2064
2065// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2066// return -1;
2067// }
2068
2069// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2070
2071// if (count < 2) {
2072// return -1;
2073// }
2074
2075// *rssi = atoi(split_words[1]) * -1;
2076
2077 FILE *fp;
2078 size_t i = 0;
2079 char lynq_cmd_ret[MAX_RET]={0};
2080
2081// CHECK_IDX(idx, CTRL_AP);
2082
2083 if((fp=popen("wl country","r"))==NULL)
2084 {
2085 perror("popen error!");
2086 return -1;
2087 }
2088 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2089 {
2090 perror("fread fail!");
2091 return -1;
2092 }
2093 *rssi = atoi(lynq_cmd_ret);
2094
2095 return 0;
2096}
2097
2098int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2099{
2100 if (band == NULL) {
2101 return -1;
2102 }
2103
2104 CHECK_IDX(idx, CTRL_STA);
2105 ap_info_s ap;
2106 ap.band = -1;
2107
2108 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2109 return -1;
2110 }
2111 *band = ap.band;
2112
2113 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002114}