blob: 11155d79293ae2f0813a7cc7d84766835dd7ffd9 [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
987 system("connmanctl tether wifi off");
988
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;
1494 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1495 return 0;
1496 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001497
you.chen35020192022-05-06 11:30:57 +08001498 if (strcmp(ap.ap_ssid, ssid) != 0) {
1499 return 0;
1500 }
1501
1502 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001503}
qs.xiong97fa59b2022-04-07 05:41:29 -04001504
you.chen35020192022-05-06 11:30:57 +08001505int lynq_wifi_sta_start(lynq_wifi_index_e idx) {
qs.xiong7a105ce2022-03-02 09:43:11 -05001506
you.chen35020192022-05-06 11:30:57 +08001507 CHECK_IDX(idx, CTRL_STA);
1508 if (system("ifconfig | grep wlan0") != 0) {
1509 return -1;
1510 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001511
you.chen35020192022-05-06 11:30:57 +08001512 return system("connmanctl enable wifi");
qs.xiong7a105ce2022-03-02 09:43:11 -05001513}
1514
you.chen35020192022-05-06 11:30:57 +08001515int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001516{
you.chen35020192022-05-06 11:30:57 +08001517 CHECK_IDX(idx, CTRL_STA);
1518
1519 return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001520}
qs.xiong7a105ce2022-03-02 09:43:11 -05001521
you.chen35020192022-05-06 11:30:57 +08001522//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1523// int i, count;
1524// char *p;
1525// const char * FLAG_SSID = "ssid=";
1526// const char * FLAG_SBSID = "bssid=";
1527// const char * FLAG_KEY_MGMT = "key_mgmt=";
1528// const char * FLAG_FREQ = "freq=";
1529// char lynq_sta_cmd[MAX_CMD];
1530// char *split_lines[128] = {0};
1531
1532// CHECK_WPA_CTRL(CTRL_AP);
1533
1534// sprintf(lynq_sta_cmd, "STA %s", bssid);
1535
1536// DO_REQUEST(lynq_sta_cmd);
1537
1538// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1539
1540// for(i=0; i < count; i++) {
1541// p = strstr(split_lines[i], FLAG_SSID);
1542// if (p != NULL) {
1543// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1544// continue;
1545// }
1546// }
1547
1548// lynq_get_interface_ip(idx, ap->ap_ip);
1549// lynq_ap_password_set(idx, ap->psw);
1550
1551// return 0;
1552//}
1553
1554static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1555 curr_status_info curr_state;
1556 curr_state.ap = ap;
1557 curr_state.state = NULL;
1558 return inner_get_status_info(interface, &curr_state);
1559}
1560
1561int 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 -04001562{
you.chen35020192022-05-06 11:30:57 +08001563 int ip_count, index, i, line_count;
1564 const char *lynq_first_sta_cmd = "STA-FIRST";
1565 char lynq_next_sta_cmd[MAX_CMD];
1566 char *bssid[1024] = {0};
1567 char *mac_list[128] = {0};
1568 char *ip_list[128] = {0};
1569 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001570
you.chen35020192022-05-06 11:30:57 +08001571 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001572
you.chen35020192022-05-06 11:30:57 +08001573 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001574
you.chen35020192022-05-06 11:30:57 +08001575// ap_info_s * tmp_ap;
1576// device_info_s * tmp_list;
1577 if (ap == NULL || list == NULL || len == NULL) {
1578 printf("bad input param");
1579 return -1;
1580 }
1581
1582// ap = &tmp_ap;
1583// list = &tmp_list;
1584 *ap = malloc(sizeof (ap_info_s));
1585
1586 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1587 return -1;
1588 }
1589
1590 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1591 lynq_ap_password_get(idx, (*ap)->psw);
1592
1593 ip_count = get_ip_mac_list(mac_list, ip_list);
1594 printf("get count %d\n", ip_count);
1595
1596 DO_REQUEST(lynq_first_sta_cmd);
1597
1598 index = 0;
1599 while (reply_len > 0) {
1600 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1601 break;
1602 }
1603 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1604 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1605 strcpy(bssid[index], split_lines[0]);
1606 index++;
1607 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1608 reply_len = MAX_RET;
1609 cmd_reply[0] = '\0';
1610 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
1611 if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
1612 break;
1613 }
1614 }
1615
1616 *len = index;
1617
1618 *list = malloc(sizeof(device_info_s) * (*len));
1619 for (index=0; index < *len; index++) {
1620 strcpy((*list)[index].sta_mac, bssid[index]);
1621 for(i=0;i < ip_count; i++ ) {
1622 if (strcmp(bssid[index], mac_list[i]) == 0) {
1623 strcpy((*list)[index].sta_ip, ip_list[i]);
1624 break;
1625 }
1626 }
1627 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1628 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1629 free(bssid[index]);
1630 }
1631
1632 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001633}
1634
you.chen35020192022-05-06 11:30:57 +08001635int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001636{
you.chen35020192022-05-06 11:30:57 +08001637 int i, count, index, count_words;
1638 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
1639 char *split_lines[128] = {0};
1640 char *split_words[128] = {0};
1641 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001642
you.chen35020192022-05-06 11:30:57 +08001643 if (list == NULL || len == NULL) {
1644 return -1;
1645 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001646
you.chen35020192022-05-06 11:30:57 +08001647 CHECK_IDX(idx, CTRL_STA);
1648
1649 CHECK_WPA_CTRL(CTRL_STA);
1650
1651 DO_REQUEST(lynq_scan_result_cmd);
1652
1653 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1654 *len = count - 1;
1655 *list = malloc(sizeof (scan_info_s) * *len);
1656
1657 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
1658 for (index=0; index <count_words; index++) {
1659 printf("----header: %s\n", split_words[index]);
1660 }
1661
1662 for(index = 1;index < count; index++) {
1663 printf("---- %s\n",split_lines[index]);
1664 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
1665 if (count_words < 4)
1666 continue;
1667 printf("count: %d, %s\n", count_words, split_words[0]);
1668 //bssid / frequency / signal level / flags / ssid
1669 p = (*list) + index - 1;
1670 strcpy(p->mac, split_words[0]);
1671 p->band = convert_band_from_freq(atoi(split_words[1]));
1672 p->rssi = -1 * atoi( split_words[2]);
1673 p->auth = convert_max_auth_from_flag(split_words[3]);
1674 strcpy(p->ssid, split_words[4]);
1675 }
1676
1677 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04001678}
qs.xiong97fa59b2022-04-07 05:41:29 -04001679
you.chen35020192022-05-06 11:30:57 +08001680int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
1681{
1682 int count, net_no, index;
1683 int net_no_list[128];
1684 lynq_wifi_auth_s net_auth;
1685 char lynq_remove_cmd[MAX_CMD];
1686
1687 if (ssid == NULL || *ssid == '\0') {
1688 printf("bad ssid\n");
1689 return -1;
1690 }
1691
1692 CHECK_IDX(idx, CTRL_STA);
1693
1694 CHECK_WPA_CTRL(CTRL_STA);
1695
1696 net_no = -1;
1697 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1698
1699 for (index=0; index < count; index++) {
1700 net_auth = -1;
1701 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1702 net_no = net_no_list[index];
1703 break;
1704 }
1705 }
1706
1707 if (net_no < 0) {
1708 return 0;
1709 }
1710
1711 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
1712
1713 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
1714 DO_OK_FAIL_REQUEST(cmd_save_config);
1715
1716 return 0;
1717}
1718
1719int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
1720{
1721 int count, index;
1722 int net_no_list[128];
1723 char freq[16];
1724
1725 if (list == NULL || len == NULL) {
1726 printf("bad param\n");
1727 return -1;
1728 }
1729
1730 CHECK_IDX(idx, CTRL_STA);
1731
1732// CHECK_WPA_CTRL(CTRL_STA);
1733
1734 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
1735 printf("count is %d\n", count);
1736
1737 *list = malloc(sizeof (saved_ap_info_s) * count);
1738 *len = count;
1739
1740 for (index=0; index < count; index++) {
1741 printf("to get ssid %d\n", index);
1742 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
1743 printf("to get bssid\n");
1744 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
1745 printf("to get inner_get_network_auth\n");
1746 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
1747 printf("to get frequency\n");
1748 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
1749 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
1750 }
1751 else {
1752 (*list)[index].base_info.band = -1;
1753 }
1754
1755 }
1756
1757 return 0;
1758}
1759
1760int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
1761{
1762 const char *lynq_scan_cmd = "SCAN";
1763
1764 CHECK_IDX(idx, CTRL_STA);
1765
1766 CHECK_WPA_CTRL(CTRL_STA);
1767
1768 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
1769
1770 return 0;
1771}
1772
1773int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
1774 if (cb == NULL) {
1775 return -1;
1776 }
1777
1778 g_ap_callback_priv = priv;
1779 g_ap_callback_func = cb;
1780
1781 return 0;
1782}
1783
1784int lynq_unreg_ap_event_callback(void * priv) {
1785 if (g_ap_callback_priv == priv) {
1786 g_ap_callback_func = NULL;
1787 g_ap_callback_priv = NULL;
1788 return 0;
1789 }
1790 return -1;
1791}
1792
1793int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
1794 if (cb == NULL) {
1795 return -1;
1796 }
1797
1798 g_sta_callback_priv = priv;
1799 g_sta_callback_func = cb;
1800
1801 return 0;
1802}
1803
1804int lynq_unreg_sta_event_callback(void * priv) {
1805 if (g_sta_callback_priv == priv) {
1806 g_sta_callback_func = NULL;
1807 g_sta_callback_priv = NULL;
1808 return 0;
1809 }
1810 return -1;
1811}
1812
1813
1814static int inner_get_status_info_state (int interface, char *state) {
1815 curr_status_info curr_state;
1816 curr_state.ap = NULL;
1817 curr_state.state = state;
1818 return inner_get_status_info(interface, &curr_state);
1819}
1820
1821int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
1822{
1823 char state[MAX_CMD];
1824 const char * STATE_COMPLETED = "COMPLETED";
1825 CHECK_IDX(idx, CTRL_AP);
1826
1827 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
1828 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1829 return 0;
1830 }
1831
1832 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1833 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
1834 }
1835 else {
1836 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
1837 }
1838
1839 return 0;
1840}
1841
1842int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
1843 char state[MAX_CMD];
1844 const char * STATE_COMPLETED = "COMPLETED";
1845 CHECK_IDX(idx, CTRL_STA);
1846
1847 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
1848 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1849 return 0;
1850 }
1851
1852 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
1853 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
1854 }
1855 else {
1856 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
1857 }
1858
1859 return 0;
1860}
1861
1862int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
1863// CHECK_IDX(idx, CTRL_AP);
1864// int ret = 0;
1865// size_t reply_len = MAX_RET;
1866// char cmd_reply[MAX_RET]={0};
1867// const char * cmd_str = "GET country";
1868// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
1869// do{
1870// if (NULL == s_lynq_wpa_ctrl) {
1871// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
1872// if (NULL == s_lynq_wpa_ctrl ) {
1873// printf("wpa_ctrl_open fail\n");
1874// return -1;
1875// }
1876// }
1877// }while(0);
1878
1879// do {
1880// reply_len = MAX_RET;
1881// cmd_reply[0] = '\0';
1882// printf("to call [%s]\n", cmd_str);
1883// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
1884// if (ret != 0) {
1885// printf("call ##cmd_str fail %d\n", ret);
1886// return ret;
1887// }
1888// cmd_reply[reply_len+1] = '\0';
1889// printf("cmd replay [ %s ]\n", cmd_reply);
1890// }while(0);
1891
1892 FILE *fp;
1893 size_t i = 0;
1894 char lynq_cmd_ret[MAX_RET]={0};
1895
1896// CHECK_IDX(idx, CTRL_AP);
1897
1898 if((fp=popen("wl country","r"))==NULL)
1899 {
1900 perror("popen error!");
1901 return -1;
1902 }
1903 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
1904 {
1905 perror("fread fail!");
1906 return -1;
1907 }
1908
1909 for(i=0; i < strlen(lynq_cmd_ret); i++) {
1910 if (lynq_cmd_ret[i] == ' ') {
1911 lynq_cmd_ret[i] = '\0';
1912 break;
1913 }
1914 }
1915
1916 strcpy(country_code,lynq_cmd_ret);
1917 printf("---country code %s\n", country_code);
1918
1919 int ret=pclose(fp);
1920 if(ret==-1)
1921 {
1922 perror("close file faild");
1923 }
1924
1925 return 0;
1926}
1927
1928int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
1929// const char * cmd_str = "GET country";
1930// CHECK_IDX(idx, CTRL_AP);
1931// CHECK_WPA_CTRL(CTRL_STA);
1932
1933// DO_REQUEST(cmd_str);
1934// printf("result %s\n", cmd_reply);
1935
1936 if (country_code == NULL || *country_code == '\0') {
1937 printf("bad country code\n");
1938 return -1;
1939 }
1940
1941 char lynq_country_cmd[MAX_CMD];
1942 sprintf(lynq_country_cmd, "wl country %s", country_code);
1943 if (system(lynq_country_cmd) == 0) {
1944 return 0;
1945 }
1946
1947 return -1;
1948}
1949
1950int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
1951{
1952 if (mac == NULL) {
1953 return -1;
1954 }
1955
1956 CHECK_IDX(idx, CTRL_STA);
1957 ap_info_s ap;
1958 ap.ap_mac[0] = '\0';
1959
1960 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
1961 return -1;
1962 }
1963 strcpy(mac, ap.ap_mac);
1964
1965 return 0;
1966}
1967
1968int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
1969{
1970 struct ifaddrs *ifaddr, *ifa;
1971 int family, s;
1972 char host[NI_MAXHOST];
1973 const char * ifaName = "wlan0";
1974 if (idx == 1) {
1975 ifaName = "ap0";
1976 }
1977 else if (idx != 0) {
1978 return -1;
1979 }
1980
1981 if (getifaddrs(&ifaddr) == -1)
1982 {
1983 perror("getifaddrs");
1984 return -1;
1985 //exit(EXIT_FAILURE);
1986 }
1987
1988
1989 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
1990 {
1991 if (ifa->ifa_addr == NULL)
1992 continue;
1993 host[0] = '\0';
1994 s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
1995
1996 printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
1997 if((strcmp(ifa->ifa_name,ifaName)==0))
1998 {
1999 if (s != 0)
2000 {
2001 // printf("getnameinfo() failed: %s", gai_strerror(s));
2002 //exit(EXIT_FAILURE);
2003 }
2004 freeifaddrs(ifaddr);
2005 strcpy(ip, host);
2006 printf("ip %s\n", ip);
2007 return 0;
2008 //printf(" Interface : <%s>",ifa->ifa_name );
2009 //printf(" Address : <%s>", host);
2010 }
2011 }
2012 return -1;
2013}
2014
2015int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2016{
2017 int count;
2018 size_t i;
2019 char *split_words[128] = {0};
2020 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2021
2022 CHECK_WPA_CTRL(idx);
2023
2024 DO_REQUEST(lynq_get_mac_cmd);
2025
2026 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2027 return -1;
2028 }
2029
2030 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2031
2032 if (count < 2) {
2033 return -1;
2034 }
2035
2036 for (i=0; i < strlen(split_words[1]); i++ ) {
2037 if (split_words[1][i] != ' ') {
2038 break;
2039 }
2040 }
2041
2042 strcpy(mac, split_words[1] + i);
2043
2044 return 0;
2045}
2046
2047int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2048{
2049// int count;
2050// char *split_words[128] = {0};
2051// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2052
2053// if (rssi == NULL) {
2054// return -1;
2055// }
2056
2057// CHECK_IDX(idx, CTRL_STA);
2058
2059// CHECK_WPA_CTRL(CTRL_STA);
2060
2061// DO_REQUEST(lynq_get_rssi_cmd);
2062
2063// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2064// return -1;
2065// }
2066
2067// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2068
2069// if (count < 2) {
2070// return -1;
2071// }
2072
2073// *rssi = atoi(split_words[1]) * -1;
2074
2075 FILE *fp;
2076 size_t i = 0;
2077 char lynq_cmd_ret[MAX_RET]={0};
2078
2079// CHECK_IDX(idx, CTRL_AP);
2080
2081 if((fp=popen("wl country","r"))==NULL)
2082 {
2083 perror("popen error!");
2084 return -1;
2085 }
2086 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2087 {
2088 perror("fread fail!");
2089 return -1;
2090 }
2091 *rssi = atoi(lynq_cmd_ret);
2092
2093 return 0;
2094}
2095
2096int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2097{
2098 if (band == NULL) {
2099 return -1;
2100 }
2101
2102 CHECK_IDX(idx, CTRL_STA);
2103 ap_info_s ap;
2104 ap.band = -1;
2105
2106 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2107 return -1;
2108 }
2109 *band = ap.band;
2110
2111 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002112}