blob: c60a6d4f30bd3ca09f073461b311f508322fc368 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen35020192022-05-06 11:30:57 +080062
you.chenb25bdf62022-09-02 17:44:14 +080063struct local_wpa_ctrl{
64 struct wpa_ctrl *ctrl;
65 pthread_mutex_t mutex;
66};
67
68static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
69
70static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +080071
72
73typedef struct __curr_status_info {
74 ap_info_s *ap;
75 char * state;
76 int net_no;
77}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040078
you.chenb25bdf62022-09-02 17:44:14 +080079static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
80 char *reply, size_t *reply_len,
81 void (*msg_cb)(char *msg, size_t len))
82{
83 int ret;
84 if (ctrl->ctrl == NULL) {
85 printf("local_wpa_ctrl_request ctrl is null\n");
86 return -1;
87 }
88 pthread_mutex_lock(&ctrl->mutex);
89 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
90 pthread_mutex_unlock(&ctrl->mutex);
91 return ret;
92}
93
94static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
95 int repeat_cnt;
96 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
97 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
98 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
99 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
100// printf("wait enable finish\n");
101 usleep(500 * 1000);
102 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
103 }
104 if (NULL == g_lynq_wpa_ctrl[index]) {
105 goto out_addr;
106 }
107 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
108 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
109 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
110 printf("wpa_ctrl_open fail\n");
111 goto out_addr;
112 }
113 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
114 }
115 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
116out_addr:
117 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
118 return lynq_wpa_ctrl;
119}
120
qs.xiong97fa59b2022-04-07 05:41:29 -0400121#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400122{\
you.chen35020192022-05-06 11:30:57 +0800123 perror((str));\
124 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400125}
126
you.chen35020192022-05-06 11:30:57 +0800127#define CHECK_IDX(idx, type) do { \
128 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
129 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
130 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
131 return -1; \
132 } \
133 }while (0)
134
135#define CHECK_WPA_CTRL(index) int ret = 0;\
136 size_t reply_len = MAX_RET; \
137 char cmd_reply[MAX_RET]={0}; \
you.chenb25bdf62022-09-02 17:44:14 +0800138 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800139 do{ \
you.chenb25bdf62022-09-02 17:44:14 +0800140 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
141 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800142 }while(0)
143
144#define DO_REQUEST(cmd_str) do { \
145 reply_len = MAX_RET;\
146 cmd_reply[0] = '\0'; \
you.chenb25bdf62022-09-02 17:44:14 +0800147 printf("to call 3 [%s]\n", cmd_str); \
148 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800149 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800150 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800151 return ret; \
152 } \
153 cmd_reply[reply_len+1] = '\0'; \
154 printf("cmd replay [ %s ]\n", cmd_reply); \
155 }while(0)
156
157#define DO_OK_FAIL_REQUEST(cmd_str) do { \
158 DO_REQUEST(cmd_str); \
159 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800160 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800161 return -1; \
162 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800163 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800164 return -1; \
165 } \
166 }while (0)
167
168
169
170static void APWatcherThreadProc() {
171 size_t len = MAX_RET;
172 char msg_notify[MAX_RET];
173
you.chen6c2dd9c2022-05-16 17:55:28 +0800174 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800175 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800176
177 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800178 if (lynq_wpa_ctrl == NULL) {
179 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
180 if (lynq_wpa_ctrl == NULL) {
181 usleep(100*1000);
182 continue;
183 }
184
185 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800186 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800187 }
188
you.chen35020192022-05-06 11:30:57 +0800189 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
190 usleep(100*1000);
191 continue;
192 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800193 memset(msg_notify, 0, MAX_RET);
194 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800195 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
196 msg_notify[len+1] = '\0';
197 printf("ap------> %s\n", msg_notify);
198 if (g_ap_callback_func == NULL) {
199 continue;
200 }
201 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
202 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
203 }
204 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
205 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
206 }
207 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
208 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800209 if (lynq_wpa_ctrl != NULL) {
210 wpa_ctrl_detach(lynq_wpa_ctrl);
211 wpa_ctrl_close(lynq_wpa_ctrl);
212 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400213}
214
you.chen35020192022-05-06 11:30:57 +0800215static void STAWatcherThreadProc() {
216 size_t len = MAX_RET;
217 char msg_notify[MAX_RET];
218 char *pReason;
219 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400220
you.chen6c2dd9c2022-05-16 17:55:28 +0800221 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800222 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800223
224 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800225 if (lynq_wpa_ctrl == NULL) {
226 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
227 if (lynq_wpa_ctrl == NULL) {
228 usleep(100*1000);
229 continue;
230 }
231
232 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800233 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800234 }
235
you.chen35020192022-05-06 11:30:57 +0800236 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
237 usleep(100*1000);
238 continue;
239 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800240 memset(msg_notify, 0, MAX_RET);
241 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800242 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
243 msg_notify[len+1] = '\0';
244 printf("sta ------> %s\n", msg_notify);
245 if (strstr(msg_notify, state_scan_result) != NULL) {
246 g_sta_scan_finish_flag = 1;
247 }
248
249 if (g_sta_callback_func == NULL) {
250 continue;
251 }
252 error = -1;
253 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
254 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
255 }
256 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
257 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
258 }
259 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
260 pReason = strstr(msg_notify, "reason=");
261 if (pReason != NULL) {
262 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800263 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800264 error = LYNQ_TIME_OUT;
265 }
you.chen92fd5d32022-05-25 10:09:47 +0800266 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800267 error = LYNQ_PSW_ERROR;
268 }
269 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
270 }
271 }
272 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
273 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
274 }
275 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
276 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
277 }
278 }
279 }
you.chen92fd5d32022-05-25 10:09:47 +0800280 if (lynq_wpa_ctrl != NULL) {
281 wpa_ctrl_detach(lynq_wpa_ctrl);
282 wpa_ctrl_close(lynq_wpa_ctrl);
283 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400284}
285
qs.xiong1af5daf2022-03-14 09:12:12 -0400286int lynq_wifi_enable(void)
287{
you.chen35020192022-05-06 11:30:57 +0800288 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800289 int i;
you.chenb25bdf62022-09-02 17:44:14 +0800290
291 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
292
293 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
294 goto out_enable;
295 }
296
you.chen35020192022-05-06 11:30:57 +0800297 const char * cmd_check_service =
298 "state=`systemctl is-active wg870_drv_insmod.service`\n"
299 "[ \"\"$state == \"active\" ] && exit 0\n"
300 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
301// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
302// return 0;
303// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400304
you.chen35020192022-05-06 11:30:57 +0800305 ret = system(cmd_check_service);
306 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800307 printf("service state %d\n", ret);
you.chenb25bdf62022-09-02 17:44:14 +0800308 ret = -1;
309 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800310 }
311
you.chen6c2dd9c2022-05-16 17:55:28 +0800312 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800313 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800314 break;
315 }
316 usleep(300*1000);
317 }
318
319 if (i >= 10) {
you.chenb25bdf62022-09-02 17:44:14 +0800320 ret = -1;
321 goto out_enable;
you.chen6c2dd9c2022-05-16 17:55:28 +0800322 }
323
you.chen9f17e4d2022-06-06 17:18:18 +0800324 //@todo delete add temp check for socket avilable start (20220606)
325 for (i=0; i<60; i++)
326 {
327 if (system("netstat -an | grep -q DGRAM") == 0) {
328 break;
329 }
330 sleep(1);
331 }
332
333 if (i >= 60)
334 {
you.chenb25bdf62022-09-02 17:44:14 +0800335 ret = -1;
336 goto out_enable;
you.chen9f17e4d2022-06-06 17:18:18 +0800337 }
338 //@todo delete add temp check for socket avilable end (20220606)
339
you.chena6fa5b22022-05-18 10:28:19 +0800340 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800341 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800342 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800343 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800344 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800345 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800346 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800347 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800348 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800349 }
350
you.chen35020192022-05-06 11:30:57 +0800351 if (g_ap_watcher_pid == 0 ) {
352 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
353 if(ret<0){
you.chenb25bdf62022-09-02 17:44:14 +0800354 ret = -1;
355 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800356 }
357 }
358
359 if (g_sta_watcher_pid == 0 ) {
360 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
361 if(ret<0){
you.chenb25bdf62022-09-02 17:44:14 +0800362 ret = -1;
363 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800364 }
365 }
366
you.chena6fa5b22022-05-18 10:28:19 +0800367 for (i=0; i<10; i++) {
368 usleep(300*1000);
369 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
370 break;
371 }
372 }
373
you.chenb25bdf62022-09-02 17:44:14 +0800374 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
375 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
376 out_enable:
377 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800378 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500379}
380
qs.xiong1af5daf2022-03-14 09:12:12 -0400381int lynq_wifi_disable(void)
382{
you.chenb25bdf62022-09-02 17:44:14 +0800383 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800384 g_ap_watcher_stop_flag = 1;
385 g_sta_watcher_stop_flag = 1;
386 if (g_ap_watcher_pid != 0)
387 pthread_join(g_ap_watcher_pid, NULL);
388 if (g_sta_watcher_pid != 0)
389 pthread_join(g_sta_watcher_pid, NULL);
390 if (g_lynq_wpa_ctrl[0] != NULL)
391 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
392 if (g_lynq_wpa_ctrl[1] != NULL)
393 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
394 g_ap_watcher_pid = 0;
395 g_sta_watcher_pid = 0;
396 g_lynq_wpa_ctrl[0] = NULL;
397 g_lynq_wpa_ctrl[1] = NULL;
398 system("systemctl stop wg870_drv_insmod.service");
you.chenb25bdf62022-09-02 17:44:14 +0800399 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong7a105ce2022-03-02 09:43:11 -0500400 return 0;
401}
qs.xiong1af5daf2022-03-14 09:12:12 -0400402
you.chen35020192022-05-06 11:30:57 +0800403static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
404
405 char lynq_cmd_get[128]={0};
406
407 if (out_put == NULL) {
408 printf("output ptr is null\n");
409 return -1;
410 }
411 if (param_name == NULL) {
412 printf("param ptr is null");
413 return -1;
414 }
415 if (param_name[0] == '\0') {
416 printf("param is empty");
417 return -1;
418 }
419
420 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
421
422 CHECK_WPA_CTRL(interface);
423
424 DO_REQUEST(lynq_cmd_get);
425
426 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
427 return -1;
428 }
429
you.chena6fa5b22022-05-18 10:28:19 +0800430// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800431 memcpy(out_put, cmd_reply, reply_len + 1);
432 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500433}
qs.xiong1af5daf2022-03-14 09:12:12 -0400434
you.chen35020192022-05-06 11:30:57 +0800435static int lynq_split(char * str, int len, char delimiter, char * results[]) {
436 int ret = 0;
437 char * end = str + len - 1;
438 results[ret++] = str;
439 while(str < end) {
440 if (*str == delimiter) {
441 *str++ = '\0';
442 results[ret++] = str;
443 continue;
444 }
445 str++;
446 }
447 if (*str == delimiter) {
448 *str = '\0';
449 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400450
you.chen35020192022-05-06 11:30:57 +0800451 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500452}
453
you.chen35020192022-05-06 11:30:57 +0800454static void trim_space(char * p, int count) {
455 char * begin = p;
456 p += count;
457 printf("%C-%C||\n", *begin, *p);
458 while (p >= begin ) {
459 if (*p == ' ') {
460 *p-- = '\0';
461 }
462 else {
463 break;
464 }
465 }
466}
467
468static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
469 FILE * fp;
470 int len, ret;
471 int count, count_words, index;
472 int mac_start, mac_end;
473 int ip_start, ip_end;
474 char *split_lines[128] = {0};
475 char *buff;
476 const char * ip_header = "IP address";
477 const char * mac_header = "HW address";
478 const char * zero_mac = "00:00:00:00:00:00";
479
480 fp = fopen("/proc/net/arp", "rb");
481 if (NULL == fp) {
482 printf("open file fail\n");
483 return -1;
484 }
485
486 buff = alloca(MAX_RET);
487 fseek(fp, 0, SEEK_SET);
488 len = fread(buff, 1, MAX_RET, fp);
489 fclose(fp);
490 if (len <= 0) {
491 printf("read file fail\n");
492 return -1;
493 }
494 printf("file : %s\n", buff);
495
496 count = lynq_split(buff, len, '\n', split_lines);
497 printf("----- %s\n", split_lines[0]);
498
499 mac_end = 0;
500 count_words = strlen(split_lines[0]);
501 if (strstr(split_lines[0], mac_header) != NULL) {
502 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
503 mac_end = mac_start + strlen(mac_header) + 1;
504 while (mac_end < count_words) {
505 if (split_lines[0][mac_end] != ' ') {
506 break;
507 }
508 mac_end++;
509 }
510 }
511
512 ip_end = 0;
513 if (strstr(split_lines[0], ip_header) != NULL) {
514 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
515 ip_end = ip_start + strlen(ip_header) + 1;
516 while (ip_end < count_words) {
517 if (split_lines[0][ip_end] != ' ') {
518 break;
519 }
520 ip_end++;
521 }
522 }
523
524 if (mac_end == 0 || ip_end == 0) {
525 return 0;
526 }
527
528 ret = 0;
529 for(index = 1;index < count; index++) {
530 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
531 continue;
532 }
533 mac_list[ret] = malloc(mac_end - mac_start + 1);
534 ip_list[ret] = malloc(ip_end - ip_start + 1);
535 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
536 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
537 trim_space(mac_list[ret], mac_end - mac_start - 1);
538 trim_space(ip_list[ret], ip_end - ip_start - 1);
539 ret++;
540 }
541
542 return ret;
543}
544
you.chen9ac66392022-08-06 17:01:16 +0800545static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
546{
547 int i;
548 if (mac_list != NULL && mac_cnt > 0) {
549 for(i = 0; i< mac_cnt; i++)
550 {
551 if (NULL != mac_list[i])
552 {
553 free(mac_list[i]);
554 mac_list[i] = NULL;
555 }
556 }
557 }
558 if (ip_list != NULL && ip_cnt > 0) {
559 for(i = 0; i< mac_cnt; i++)
560 {
561 if (NULL != ip_list[i])
562 {
563 free(ip_list[i]);
564 ip_list[i] = NULL;
565 }
566 }
567 }
568}
569
you.chen35020192022-05-06 11:30:57 +0800570static int get_hostname_by_ip(char *ip, char *hostname) {
571 struct in_addr addr ={0};
572 struct hostent *ht;
573
574 if (ip == NULL || *ip == '\0' || hostname == NULL) {
575 return -1;
576 }
577
578 if (inet_aton(ip, &addr) == 0) {
579 printf("---inet_aton fail\n");
580 return -1;
581 }
582
583 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
584
585 if (ht == NULL) {
586 printf("---gethostbyaddr fail\n");
587 herror(NULL);
588 return -1;
589 }
590
591 strcpy(hostname, ht->h_name);
592
593 return 0;
594}
595
596static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
597{
598 int count, index, words_count;
599 char * split_lines[128]= {0};
600 char * split_words[128] = {0};
601 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
602
603 CHECK_WPA_CTRL(ap_sta);
604
605 DO_REQUEST(lynq_wifi_list_networks);
606
607 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
608
609 //@todo check ssid field to compatible
610
611 ret = 0;
612 for(index=1; index < count; index++) {
613 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
614 if (words_count > 2) {
615 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
616 net_no_list[ret++] = atoi(split_words[0]);
617 }
618 }
619 }
620
621 return ret;
622}
623
624static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800625 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800626 CHECK_WPA_CTRL(ap_sta);
627 const char *lynq_wifi_add_network = "ADD_NETWORK";
628
629 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800630 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800631 return -1;
632 }
633
you.chen6c2dd9c2022-05-16 17:55:28 +0800634 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800635 if(cmd_reply[i] == '\n') {
636 cmd_reply[i] = '\0';
637 break;
638 }
639 }
640 return atoi(cmd_reply);
641}
you.chena6cd55a2022-05-08 12:20:18 +0800642
you.chen35020192022-05-06 11:30:57 +0800643static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
644{
645 int count, index;
646 int net_no_list[128];
647
648
649 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
650 for (index=0; index < count; index++) {
651 if (net_no_list[index] == net_no) {
652 return 0;
653 }
654 }
655
656 if (count >= 1)
657 index = net_no_list[count - 1];
658 else
659 index = -1;
660
661 while (index < net_no ) {
662 index = lynq_add_network(ap_sta);
663 if (index >= net_no) { // required network no created
664 return 0;
665 }
you.chena6cd55a2022-05-08 12:20:18 +0800666 else if( index < 0) {
667 printf("add network fail\n");
668 return -1;
669 }
you.chen35020192022-05-06 11:30:57 +0800670 }
671
672 if (index < 0)
673 return -1;
674
675 return 0;
676}
677
678static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
679 if (freq > 5000 && freq < 6000) {
680 return LYNQ_WIFI_5G_band;
681 }
682 else if (freq > 2000 && freq < 3000) {
683 return LYNQ_WIFI_2G_band;
684 }
685 return LYNQ_WIFI_2_and_5G_band;
686}
687
688static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
689 if (key_mgmt != NULL) {
690 if (memcmp( key_mgmt, "NONE", 4) == 0) {
691 return LYNQ_WIFI_AUTH_OPEN;
692 }
693 else if (memcmp( key_mgmt, "WEP", 3) == 0){
694 return LYNQ_WIFI_AUTH_WEP;
695 }
696 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
697 return LYNQ_WIFI_AUTH_WPA_PSK;
698 }
699 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
700 return LYNQ_WIFI_AUTH_WPA2_PSK;
701 }
702 }
703
704 return -1;
705}
706
707static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
708 if (flag != NULL) {
709 if (strstr( flag, "WPA2-PSK") != NULL){
710 return LYNQ_WIFI_AUTH_WPA2_PSK;
711 }
712 else if (strstr( flag, "WPA-PSK") != NULL){
713 return LYNQ_WIFI_AUTH_WPA_PSK;
714 }
715 else if (strstr( flag, "WEP") != NULL){
716 return LYNQ_WIFI_AUTH_WEP;
717 }
718 else if (strstr( flag, "NONE") != NULL) {
719 return LYNQ_WIFI_AUTH_OPEN;
720 }
721 }
722
723 return -1;
724}
725
726static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
727 switch (bw) {
728 case 10:
729 return LYNQ_WIFI_BANDWIDTH_HT10;
730 break;
731 case 20:
732 return LYNQ_WIFI_BANDWIDTH_HT20;
733 break;
734 case 40:
735 return LYNQ_WIFI_BANDWIDTH_HT40;
736 break;
737 case 80:
738 return LYNQ_WIFI_BANDWIDTH_HT80;
739 break;
740 default:
741 break;
742 }
743
744 return -1;
745}
746
747static int inner_get_status_info(int interface, curr_status_info *curr_state) {
748 int i, count;
749 char *p;
750 const char *lynq_status_cmd = "STATUS";
751 const char * FLAG_SSID = "ssid=";
752 const char * FLAG_SBSID = "bssid=";
753 const char * FLAG_KEY_MGMT = "key_mgmt=";
754 const char * FLAG_FREQ = "freq=";
755 const char * FLAG_STATE = "wpa_state=";
756 const char * FLAG_ID = "id=";
757 char *split_lines[128] = {0};
758
759 CHECK_WPA_CTRL(interface);
760
761 if (curr_state == NULL) {
762 return -1;
763 }
764
765 DO_REQUEST(lynq_status_cmd);
766
767 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
768
769 curr_state->net_no = -1;
770 ret = -1;
771 for(i=0; i < count; i++) {
772 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800773 p = strstr(split_lines[i], FLAG_SBSID);
774 if (p != NULL) {
775 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
776 ret = 0;
777 continue;
778 }
you.chenf58b3c92022-06-21 16:53:48 +0800779 p = strstr(split_lines[i], FLAG_SSID);
780 if (p != NULL) {
781 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
782 ret = 0;
783 continue;
784 }
you.chen35020192022-05-06 11:30:57 +0800785 p = strstr(split_lines[i], FLAG_KEY_MGMT);
786 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800787 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
788 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800789 ret = 0;
790 continue;
791 }
792 p = strstr(split_lines[i], FLAG_FREQ);
793 if (p != NULL) {
794 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
795 ret = 0;
796 continue;
797 }
798 } // end if (ap != NULL)
799 if (curr_state->state != NULL) {
800 p = strstr(split_lines[i], FLAG_STATE);
801 if (p != NULL) {
802 strcpy(curr_state->state, p + strlen(FLAG_STATE));
803 ret = 0;
804 continue;
805 }
806
807 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800808 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800809 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800810 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800811 printf("net_no %d, -- %s\n", curr_state->net_no, p);
812 }
813 }
814
815 return ret;
816}
817
818
qs.xiongf1b525b2022-03-31 00:58:23 -0400819int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500820{
you.chen35020192022-05-06 11:30:57 +0800821 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500822
you.chen35020192022-05-06 11:30:57 +0800823 if (ap_ssid == NULL) {
824 printf("ap_ssid is null\n");
825 return -1;
826 }
827 else {
828 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
829 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400830
you.chen35020192022-05-06 11:30:57 +0800831 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
832 return -1;
833 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400834
you.chen35020192022-05-06 11:30:57 +0800835 CHECK_IDX(idx, CTRL_AP);
836
837 CHECK_WPA_CTRL(CTRL_AP);
838
839 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
840
841 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
842 DO_OK_FAIL_REQUEST(cmd_save_config);
843
qs.xiong7a105ce2022-03-02 09:43:11 -0500844 return 0;
you.chen35020192022-05-06 11:30:57 +0800845
qs.xiong7a105ce2022-03-02 09:43:11 -0500846}
847
you.chen35020192022-05-06 11:30:57 +0800848int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500849{
you.chen6c2dd9c2022-05-16 17:55:28 +0800850 int len;
you.chen35020192022-05-06 11:30:57 +0800851 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800852 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
853 return -1;
854 len = strlen(ap_ssid);
855 if (ap_ssid[0] == '\"') {
856 memmove(ap_ssid, ap_ssid + 1, len - 1);
857 len -= 1;
858 }
859 if (len > 0 && ap_ssid[len-1] == '\"') {
860 ap_ssid[len-1] = '\0';
861 }
862 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500863}
864
qs.xiongf1b525b2022-03-31 00:58:23 -0400865int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500866{
you.chen35020192022-05-06 11:30:57 +0800867 char lynq_wifi_frequency_cmd[128]={0};
868 char lynq_cmd_mode[128]={0};
869 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500870
you.chen35020192022-05-06 11:30:57 +0800871 if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
872 PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
873 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400874
you.chen35020192022-05-06 11:30:57 +0800875 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
876 return -1;
877 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400878
you.chen35020192022-05-06 11:30:57 +0800879 CHECK_IDX(idx, CTRL_AP);
880
881 CHECK_WPA_CTRL(CTRL_AP);
882
883 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
884 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
885 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
886
you.chen6c2dd9c2022-05-16 17:55:28 +0800887 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800888 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
889 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
890 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500891
qs.xiong1af5daf2022-03-14 09:12:12 -0400892 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500893}
894
qs.xiongf1b525b2022-03-31 00:58:23 -0400895int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500896{
you.chen35020192022-05-06 11:30:57 +0800897 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400898
you.chen35020192022-05-06 11:30:57 +0800899 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400900
you.chen35020192022-05-06 11:30:57 +0800901 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
902 return -1;
903 }
904 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400905
906 return 0;
907}
908
qs.xiongf1b525b2022-03-31 00:58:23 -0400909int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
910{
you.chen35020192022-05-06 11:30:57 +0800911 CHECK_IDX(idx, CTRL_AP);
912 switch(bandwidth){
913 case LYNQ_WIFI_BANDWIDTH_HT10:
914 {
915 printf("bandwith [%d] not support now\n", bandwidth);
916 return -1;
917 }
918 case LYNQ_WIFI_BANDWIDTH_HT20:
919 {
920 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
921 system("wl down");
922 if (system(lynq_cmd_bandwith) != 0 ){
923 return -1;
924 }
925 system("wl up");
926 break;
927 }
928 case LYNQ_WIFI_BANDWIDTH_HT40:
929 {
930 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
931 sprintf(lynq_cmd_bandwith, "wl chanspec ");
932 system("wl down");
933 if (system(lynq_cmd_bandwith) != 0 ){
934 return -1;
935 }
936 system("wl up");
937 break;
938 }
939 case LYNQ_WIFI_BANDWIDTH_HT80:
940 {
941 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
942 system("wl down");
943 if (system(lynq_cmd_bandwith) != 0 ){
944 return -1;
945 }
946 system("wl up");
947 break;
948 }
949 default:
950 {
951 printf("auth type [%d] not support now\n", bandwidth);
952 return -1;
953 }
954 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400955
956
you.chen35020192022-05-06 11:30:57 +0800957 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400958}
you.chen35020192022-05-06 11:30:57 +0800959
qs.xiongf1b525b2022-03-31 00:58:23 -0400960int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
961{
you.chen35020192022-05-06 11:30:57 +0800962 int count = 0;
963 int index = 0;
964 char *split_words[128] = {0};
965 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400966
you.chen35020192022-05-06 11:30:57 +0800967 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500968
you.chen35020192022-05-06 11:30:57 +0800969 CHECK_WPA_CTRL(CTRL_AP);
970
971 DO_REQUEST(lynq_chanspec_cmd);
972
973 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
974 for(;index < count; index++) {
975 if (strncmp(split_words[index], "bw", 2) != 0) {
976 continue;
977 }
978
979 index++;
980 if (index >= count) {
981 return -1;
982 }
983
984 printf("bw %s\n", split_words[index]);
985 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
986 return 0;
987 }
988
989 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500990}
qs.xiong0fb469a2022-04-14 03:50:45 -0400991
qs.xiongf1b525b2022-03-31 00:58:23 -0400992int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500993{
you.chen35020192022-05-06 11:30:57 +0800994 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400995
you.chen35020192022-05-06 11:30:57 +0800996 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400997
you.chen35020192022-05-06 11:30:57 +0800998 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400999
you.chen35020192022-05-06 11:30:57 +08001000 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
1001 return -1;
1002 }
1003
1004 system("wl down");
1005 if (system(lynq_cmd_channel) != 0 ){
1006 return -1;
1007 }
1008 system("wl up");
1009 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001010}
qs.xiong0fb469a2022-04-14 03:50:45 -04001011
qs.xiongf1b525b2022-03-31 00:58:23 -04001012int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001013{
you.chen35020192022-05-06 11:30:57 +08001014 int count = 0;
1015 int index = 0;
1016 char *split_words[128] = {0};
1017 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001018
you.chen35020192022-05-06 11:30:57 +08001019 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001020
you.chen35020192022-05-06 11:30:57 +08001021 CHECK_WPA_CTRL(CTRL_AP);
1022
1023 DO_REQUEST(lynq_chanspec_cmd);
1024
1025 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1026 for(;index < count; index++) {
1027 printf("---- %s\n",split_words[index]);
1028 if (strncmp(split_words[index], "channel", 2) != 0) {
1029 continue;
1030 }
1031
1032 index++;
1033 if (index >= count) {
1034 return -1;
1035 }
1036
1037 *channel = atoi(split_words[index]);
1038 return 0;
1039 }
1040
1041 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001042}
1043
1044
you.chen35020192022-05-06 11:30:57 +08001045int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001046{
you.chen6c2dd9c2022-05-16 17:55:28 +08001047 char ssid[MAX_CMD] = {0};
1048 int freq = 0;
1049 char lynq_auth_cmd[64]={0};
1050 char lynq_auth_alg_cmd[64]={0};
1051 char lynq_psk_cmd[64]={0};
1052 char lynq_pairwise_cmd[64]={0};
1053 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001054 CHECK_IDX(idx, CTRL_AP);
1055
you.chen6c2dd9c2022-05-16 17:55:28 +08001056 CHECK_WPA_CTRL(CTRL_AP);
1057
you.chen35020192022-05-06 11:30:57 +08001058 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1059 return -1;
1060 }
1061
you.chen92fd5d32022-05-25 10:09:47 +08001062 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001063 if (org_auth == auth) {
1064 return 0;
1065 }
1066 else {
1067 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1068 ssid[0] = '\0';
1069 }
1070 lynq_wifi_ap_frequency_get(idx, &freq);
1071
1072 DO_OK_FAIL_REQUEST(cmd_disconnect);
1073 DO_OK_FAIL_REQUEST(cmd_remove_all);
1074 if (ssid[0] != '\0') {
1075 lynq_wifi_ap_ssid_set(idx, ssid);
1076 }
1077 if (freq != 0) {
1078 lynq_wifi_ap_frequency_set(idx, freq);
1079 }
1080 }
1081 }
you.chen35020192022-05-06 11:30:57 +08001082
qs.xiong7a105ce2022-03-02 09:43:11 -05001083 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001084 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001085 {
you.chen35020192022-05-06 11:30:57 +08001086 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001087 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001088
you.chen35020192022-05-06 11:30:57 +08001089 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001090 break;
1091 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001092 case LYNQ_WIFI_AUTH_WEP:
1093 {
1094 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001095 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001096 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1097
1098 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1099 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1100 break;
1101 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001102 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001103 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001104 {
you.chen35020192022-05-06 11:30:57 +08001105 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1106 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1107 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1108 }
1109 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001110 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001111 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001112 }
1113// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1114// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1115 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001116
you.chen35020192022-05-06 11:30:57 +08001117 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1118 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1119 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001120 break;
you.chen35020192022-05-06 11:30:57 +08001121 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001122 default:
you.chen35020192022-05-06 11:30:57 +08001123 {
1124 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001125 return -1;
you.chen35020192022-05-06 11:30:57 +08001126 }
1127 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001128 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001129
qs.xiong7a105ce2022-03-02 09:43:11 -05001130 return 0;
1131}
1132
you.chen35020192022-05-06 11:30:57 +08001133int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001134{
you.chen35020192022-05-06 11:30:57 +08001135 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001136 char lynq_auth_alg_str[MAX_RET] = {0};
1137 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001138
1139 CHECK_IDX(idx, CTRL_AP);
1140
1141 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1142 return -1;
1143 }
1144
1145 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001146 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1147 *auth = LYNQ_WIFI_AUTH_OPEN;
1148 }
1149 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1150 *auth = LYNQ_WIFI_AUTH_WEP;
1151 }
1152 else {
1153 *auth = LYNQ_WIFI_AUTH_OPEN;
1154 }
you.chen35020192022-05-06 11:30:57 +08001155 }
you.chen92fd5d32022-05-25 10:09:47 +08001156 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001157 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001158 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001159 }
1160 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1161 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1162 }
1163 else {
1164 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1165 }
you.chen35020192022-05-06 11:30:57 +08001166 }
you.chen92fd5d32022-05-25 10:09:47 +08001167 else {
1168 *auth = -1;
1169 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001170
you.chen6c2dd9c2022-05-16 17:55:28 +08001171 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001172}
qs.xiong1af5daf2022-03-14 09:12:12 -04001173
qs.xiong1af5daf2022-03-14 09:12:12 -04001174
qs.xiongf1b525b2022-03-31 00:58:23 -04001175int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001176{
you.chen35020192022-05-06 11:30:57 +08001177 char LYNQ_WIFI_CMD[128]={0};
1178 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1179 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001180
you.chen35020192022-05-06 11:30:57 +08001181 CHECK_IDX(idx, CTRL_AP);
1182
1183 CHECK_WPA_CTRL(CTRL_AP);
1184
1185// system("connmanctl enable wifi");
1186// system("connmanctl tether wifi on cy-test 12345678");
1187// system("ifconfig wlan0 down");
1188// system("ifconfig wlan0 up");
1189// system("ifconfig wlan0 up");
1190
1191 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1192 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1193
1194 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1195 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1196
qs.xiong7a105ce2022-03-02 09:43:11 -05001197 return 0;
1198}
1199
qs.xiongf1b525b2022-03-31 00:58:23 -04001200int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001201{
you.chen35020192022-05-06 11:30:57 +08001202 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001203}
1204
qs.xiongf1b525b2022-03-31 00:58:23 -04001205int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001206{
you.chen35020192022-05-06 11:30:57 +08001207 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001208
you.chen35020192022-05-06 11:30:57 +08001209 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001210
you.chen35020192022-05-06 11:30:57 +08001211 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001212
you.chen35020192022-05-06 11:30:57 +08001213 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1214
1215 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1216
you.chenb4b121c2022-05-06 17:50:16 +08001217// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001218
qs.xiong7a105ce2022-03-02 09:43:11 -05001219 return 0;
1220}
qs.xiong1af5daf2022-03-14 09:12:12 -04001221
qs.xiongf1b525b2022-03-31 00:58:23 -04001222int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001223{
you.chen35020192022-05-06 11:30:57 +08001224 char lynq_disable_cmd[128] = {0};
1225 char lynq_select_cmd[128] = {0};
1226 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001227
you.chen35020192022-05-06 11:30:57 +08001228 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001229
you.chen35020192022-05-06 11:30:57 +08001230 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001231
you.chen35020192022-05-06 11:30:57 +08001232 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1233 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1234
1235 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1236 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1237 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001238
1239 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001240}
1241
qs.xiongf1b525b2022-03-31 00:58:23 -04001242int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001243{
you.chen35020192022-05-06 11:30:57 +08001244 char lynq_disable_cmd[128] = {0};
1245 char lynq_select_cmd[128] = {0};
1246 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001247
you.chen35020192022-05-06 11:30:57 +08001248 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001249
you.chen35020192022-05-06 11:30:57 +08001250 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001251
you.chen35020192022-05-06 11:30:57 +08001252 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1253 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1254
1255 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1256 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1257 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001258
1259 return 0;
1260}
qs.xiongf1b525b2022-03-31 00:58:23 -04001261
you.chen35020192022-05-06 11:30:57 +08001262int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001263{
qs.xiongf1b525b2022-03-31 00:58:23 -04001264 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001265 char lynq_tmp_cmd[MAX_CMD] = {0};
1266 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001267 if( password == NULL ){
1268 return -1;
1269 }
1270 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001271 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001272 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001273 return -1;
you.chen35020192022-05-06 11:30:57 +08001274 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001275
you.chen35020192022-05-06 11:30:57 +08001276 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001277
you.chen6c2dd9c2022-05-16 17:55:28 +08001278 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1279 return -1;
1280 }
1281 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1282 return -1;
1283 }
1284
you.chen35020192022-05-06 11:30:57 +08001285 CHECK_WPA_CTRL(CTRL_AP);
1286
you.chen6c2dd9c2022-05-16 17:55:28 +08001287 if (auth == LYNQ_WIFI_AUTH_WEP) {
1288 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1289 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1290 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1291 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1292 }
1293 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1294 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1295 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1296 }
1297 else {
1298 return -1;
1299 }
you.chen35020192022-05-06 11:30:57 +08001300
you.chen35020192022-05-06 11:30:57 +08001301 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001302
qs.xiongf1b525b2022-03-31 00:58:23 -04001303 return 0;
1304}
1305
you.chen35020192022-05-06 11:30:57 +08001306int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001307{
you.chen35020192022-05-06 11:30:57 +08001308 FILE * fp;
1309 int len, ret;
1310 int count, index;
1311 char *split_lines[128] = {0};
1312 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001313
you.chen35020192022-05-06 11:30:57 +08001314 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001315
you.chen35020192022-05-06 11:30:57 +08001316 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1317// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1318 if (NULL == fp) {
1319 printf("open file fail\n");
1320 return -1;
1321 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001322
you.chen35020192022-05-06 11:30:57 +08001323 buff = alloca(MAX_RET);
1324 fseek(fp, 0, SEEK_SET);
1325 len = fread(buff, 1, MAX_RET, fp);
1326 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001327
you.chen35020192022-05-06 11:30:57 +08001328 for(index=0; index < len; index ++) {
1329 if (memcmp(buff + index, "network={", 9) != 0) {
1330 continue;
1331 }
1332 p = buff + index + 9;
1333 for (; index < len; index ++ ) {
1334 if (buff[index] != '}') {
1335 continue;
1336 }
1337 buff[index] = '\0';
1338 break;
1339 }
1340 len = buff + index - p;
1341 }
1342
1343 count = lynq_split(p, len, '\n', split_lines);
1344
1345 ret = -1;
1346 for(index=0; index < count; index++) {
1347 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001348 if (p != NULL) {
1349 p += 4;
1350 if (*p == '\"') {
1351 p++;
1352 }
you.chen35020192022-05-06 11:30:57 +08001353 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001354 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1355 p += 9;
1356 if (*p == '\"') {
1357 p++;
1358 }
1359 }
1360 else {
1361 continue;
you.chen35020192022-05-06 11:30:57 +08001362 }
1363
1364 strcpy(password, p);
1365
1366 while(*password != '\0') {
1367 if (*password == '\"') {
1368 *password = '\0';
1369 break;
1370 }
1371 password++;
1372 }
1373 ret = 0;
1374 break;
1375 } //end for(index=0; index < count; index++)
1376
1377 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001378}
1379
you.chen35020192022-05-06 11:30:57 +08001380static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1381 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001382 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001383
you.chen35020192022-05-06 11:30:57 +08001384 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1385 return -1;
1386 }
1387
1388 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001389
1390 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1391 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1392 if (strcmp(lynq_proto_str, "RSN") == 0) {
1393 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1394 }
1395 }
1396 }
you.chen35020192022-05-06 11:30:57 +08001397 return 0;
1398}
1399
1400int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001401{
you.chen35020192022-05-06 11:30:57 +08001402 int pass_len, net_no, count, index;
1403 char lynq_tmp_cmd[300]={0};
1404 int net_no_list[128];
1405 lynq_wifi_auth_s net_auth;
1406 pass_len=strlen(password);
1407 if(pass_len < 8 || pass_len >= 64){
1408 return -1;
1409 }
1410
1411 CHECK_IDX(idx, CTRL_STA);
1412
1413 net_no = -1;
1414 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1415
1416 for (index=0; index < count; index++) {
1417 net_auth = -1;
1418 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1419 net_no = net_no_list[index];
1420 break;
1421 }
1422 }
1423
1424 if (net_no < 0) {
1425 return -1;
1426 }
1427
1428 CHECK_WPA_CTRL(CTRL_STA);
1429
1430 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1431
1432 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1433 DO_OK_FAIL_REQUEST(cmd_save_config);
1434
1435 return 0;
1436}
1437
1438int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1439
1440 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001441 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001442 int count, index;
1443 char *split_lines[128] = {0};
1444 char *buff, *p;
1445
you.chen755332b2022-08-06 16:59:10 +08001446 network_len = 0;
1447 p = NULL;
1448
you.chen35020192022-05-06 11:30:57 +08001449 CHECK_IDX(idx, CTRL_STA);
1450
you.chen755332b2022-08-06 16:59:10 +08001451 if (NULL == password) {
1452 printf("bad param\n");
1453 return -1;
1454 }
1455
you.chen35020192022-05-06 11:30:57 +08001456 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1457 if (NULL == fp) {
1458 printf("open file fail\n");
1459 return -1;
1460 }
1461
1462 buff = alloca(MAX_RET);
1463 fseek(fp, 0, SEEK_SET);
1464 len = fread(buff, 1, MAX_RET, fp);
1465 fclose(fp);
1466
1467 for(index=0; index < len; index ++) {
1468 for(; index < len; index ++) {
1469 if (memcmp(buff + index, "network={", 9) != 0) {
1470 continue;
1471 }
1472 p = buff + index + 9;
1473 for (; index < len; index ++ ) {
1474 if (buff[index] != '}') {
1475 continue;
1476 }
1477 buff[index] = '\0';
1478 break;
1479 }
you.chen755332b2022-08-06 16:59:10 +08001480 network_len = buff + index - p;
1481 break;
you.chen35020192022-05-06 11:30:57 +08001482 }
1483
1484 if (strstr(p, ap->ap_ssid) != NULL) {
1485 break;
1486 }
1487 }
1488
you.chen755332b2022-08-06 16:59:10 +08001489 if (index >= len || NULL == p || network_len <= 0) {
you.chenb25bdf62022-09-02 17:44:14 +08001490 printf("index:%d-%d, %p, network_len:%d\n", index, len, p, network_len);
you.chen35020192022-05-06 11:30:57 +08001491 return -1;
1492 }
1493
you.chen755332b2022-08-06 16:59:10 +08001494 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001495
1496 ret = -1;
1497 for(index=0; index < count; index++) {
1498 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001499 if (p != NULL) {
1500 p += 4;
1501 if (*p == '\"') {
1502 p++;
1503 }
you.chen35020192022-05-06 11:30:57 +08001504 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001505 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1506 p += 9;
1507 if (*p == '\"') {
1508 p++;
1509 }
1510 }
1511 else {
1512 continue;
you.chen35020192022-05-06 11:30:57 +08001513 }
1514
1515 strcpy(password, p);
1516
1517 while(*password != '\0') {
1518 if (*password == '\"') {
1519 *password = '\0';
1520 break;
1521 }
1522 password++;
1523 }
1524 ret = 0;
1525 break;
1526 } //end for(index=0; index < count; index++)
1527
1528 return ret;
1529}
1530
1531static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1532{
qs.xiong97fa59b2022-04-07 05:41:29 -04001533 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001534
you.chen35020192022-05-06 11:30:57 +08001535 if (sta_ssid == NULL) {
1536 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001537 return -1;
you.chen35020192022-05-06 11:30:57 +08001538 }
1539
1540 CHECK_WPA_CTRL(CTRL_STA);
1541
1542 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1543
1544 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1545// DO_OK_FAIL_REQUEST(cmd_save_config);
1546
qs.xiong7a105ce2022-03-02 09:43:11 -05001547 return 0;
1548
1549}
1550
you.chen35020192022-05-06 11:30:57 +08001551static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001552{
you.chen35020192022-05-06 11:30:57 +08001553 char lynq_disable_cmd[128]={0};
1554 char lynq_select_cmd[128]={0};
1555
1556 CHECK_WPA_CTRL(CTRL_STA);
1557
1558 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001559 if (start_flag != 0)
1560 {
1561 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1562 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1563 }
1564 else
1565 {
1566 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1567 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1568 }
you.chen35020192022-05-06 11:30:57 +08001569 DO_OK_FAIL_REQUEST(cmd_save_config);
1570 }
1571
1572 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001573 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001574 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1575 }
1576 else {
1577 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1578 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1579 }
1580
1581 return 0;
1582}
1583
1584int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1585{
1586 CHECK_IDX(idx, CTRL_STA);
1587
you.chen6c2dd9c2022-05-16 17:55:28 +08001588 curr_status_info curr_state;
1589 ap_info_s ap_info;
1590 curr_state.ap = &ap_info;
1591 curr_state.state = NULL;
1592
1593 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1594 strcpy(sta_ssid, ap_info.ap_ssid);
1595 return 0;
1596 }
1597
1598 return -1;
you.chen35020192022-05-06 11:30:57 +08001599}
1600
1601int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1602{
you.chen9ac66392022-08-06 17:01:16 +08001603 scan_info_s *scan_list = NULL;
1604 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001605 int scan_len=0;
1606 int save_len=0;
1607 int best_index = -1;
1608 int best_scan_index = -1;
1609 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001610 int i, j, ret;
1611
1612 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001613
1614 CHECK_IDX(idx, CTRL_STA);
1615 if (info == NULL) {
1616 return -1;
1617 }
1618
1619 curr_status_info curr_state;
1620 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001621 char status[64];
you.chen35020192022-05-06 11:30:57 +08001622
you.chen9ac66392022-08-06 17:01:16 +08001623 memset(&ap_info, 0, sizeof (ap_info));
1624 memset(status, 0, sizeof (status));
1625
1626 curr_state.ap = &ap_info;
1627 curr_state.state = status;
1628
1629 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001630 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001631 if (strcmp(status, STATE_COMPLETED) == 0)
1632 {
1633 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1634 }
1635 else
1636 {
1637 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1638 }
you.chen35020192022-05-06 11:30:57 +08001639 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001640 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001641 return 0;
1642 }
1643
you.chen9ac66392022-08-06 17:01:16 +08001644 lynq_wifi_sta_start_scan(idx);
1645
you.chen35020192022-05-06 11:30:57 +08001646 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001647 if (NULL != scan_list)
1648 {
1649 free(scan_list);
1650 }
you.chen35020192022-05-06 11:30:57 +08001651 return -1;
1652 }
1653
1654 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001655 if (NULL != scan_list)
1656 {
1657 free(scan_list);
1658 }
1659 if (NULL != save_list)
1660 {
1661 free(save_list);
1662 }
you.chen35020192022-05-06 11:30:57 +08001663 return -1;
1664 }
1665
1666 for (i=0; i < save_len; i++) {
1667 for (j=0; j < scan_len; j++) {
1668 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1669 && save_list[i].base_info.auth == scan_list[j].auth) {
1670 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001671 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001672 best_rssi = scan_list[j].rssi;
1673 }
1674 else if (best_rssi > scan_list[j].rssi) {
1675 best_index = i;
1676 best_scan_index = j;
1677 best_rssi = scan_list[j].rssi;
1678 }
1679 break;
1680 }
1681 }
1682 }
1683
1684 if (best_index >= 0) {
1685 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1686 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1687 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001688 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001689 }
1690
you.chen9ac66392022-08-06 17:01:16 +08001691 if (NULL != scan_list)
1692 {
1693 free(scan_list);
1694 }
1695 if (NULL != save_list)
1696 {
1697 free(save_list);
1698 }
1699
1700 return ret;
you.chen35020192022-05-06 11:30:57 +08001701}
1702
1703static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1704{
1705 char lynq_auth_cmd[64]={0};
1706 char lynq_ket_mgmt_cmd[64]={0};
1707 char lynq_pairwise_cmd[64]={0};
1708 char lynq_psk_cmd[64]={0};
1709
1710 CHECK_WPA_CTRL(CTRL_STA);
1711
qs.xiong1af5daf2022-03-14 09:12:12 -04001712 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001713 case LYNQ_WIFI_AUTH_OPEN:
1714 {
1715 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001716
you.chen35020192022-05-06 11:30:57 +08001717 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1718// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001719 break;
1720 }
you.chen35020192022-05-06 11:30:57 +08001721 case LYNQ_WIFI_AUTH_WPA_PSK:
1722 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001723 {
you.chen35020192022-05-06 11:30:57 +08001724 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1725 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1726 }
1727 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001728 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001729 }
1730 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1731 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001732
you.chen35020192022-05-06 11:30:57 +08001733 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1734 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1735 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001736
you.chen35020192022-05-06 11:30:57 +08001737 if (password != NULL) {
1738 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1739 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1740 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001741
you.chen35020192022-05-06 11:30:57 +08001742// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001743 break;
you.chen35020192022-05-06 11:30:57 +08001744 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001745 default:
1746 return -1;
you.chen35020192022-05-06 11:30:57 +08001747 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001748
qs.xiongf1b525b2022-03-31 00:58:23 -04001749 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001750}
qs.xiong7a105ce2022-03-02 09:43:11 -05001751
you.chen35020192022-05-06 11:30:57 +08001752static int inner_get_curr_net_no(int interface) {
1753 curr_status_info curr_state;
1754 curr_state.ap = NULL;
1755 curr_state.state = NULL;
1756
1757 if (0 != inner_get_status_info(interface, &curr_state)) {
1758 return -1;
1759 }
1760
1761 return curr_state.net_no;
1762}
1763
1764int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001765{
you.chen35020192022-05-06 11:30:57 +08001766 int net_no;
1767 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001768
you.chen35020192022-05-06 11:30:57 +08001769 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001770
you.chen35020192022-05-06 11:30:57 +08001771 if (net_no < 0) {
1772 return -1;
1773 }
1774
1775 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001776}
1777
you.chen35020192022-05-06 11:30:57 +08001778int 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 -05001779{
you.chen35020192022-05-06 11:30:57 +08001780 int count, net_no, index;
1781 int net_no_list[128];
1782 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001783
you.chen35020192022-05-06 11:30:57 +08001784 if (ssid == NULL || *ssid == '\0') {
1785 printf("bad ssid\n");
1786 return -1;
1787 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001788
you.chen35020192022-05-06 11:30:57 +08001789 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1790 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1791 printf("bad password\n");
1792 return -1;
1793 }
1794 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001795
you.chen35020192022-05-06 11:30:57 +08001796 CHECK_IDX(idx, CTRL_STA);
1797
1798 net_no = -1;
1799 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1800
1801 for (index=0; index < count; index++) {
1802 net_auth = -1;
1803 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1804 net_no = net_no_list[index];
1805 break;
1806 }
1807 }
1808
1809 if (net_no < 0) {
1810 net_no = lynq_add_network(CTRL_STA);
1811 if (net_no == -1) {
1812 return -1;
1813 }
1814
1815 printf("net no is %d\n", net_no);
1816 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1817 return -1;
1818 }
1819 }
1820
1821 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1822 return -1;
1823 }
1824
1825 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001826}
1827
you.chen35020192022-05-06 11:30:57 +08001828int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001829{
you.chen35020192022-05-06 11:30:57 +08001830 ap_info_s ap;
1831 curr_status_info curr_state;
1832 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001833
you.chen35020192022-05-06 11:30:57 +08001834 if (ssid == NULL || *ssid == '\0') {
1835 return -1;
1836 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001837
you.chen35020192022-05-06 11:30:57 +08001838 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001839
you.chen35020192022-05-06 11:30:57 +08001840 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001841 curr_state.state = NULL;
1842
you.chen35020192022-05-06 11:30:57 +08001843 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1844 return 0;
1845 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001846
you.chen35020192022-05-06 11:30:57 +08001847 if (strcmp(ap.ap_ssid, ssid) != 0) {
1848 return 0;
1849 }
1850
1851 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001852}
qs.xiong97fa59b2022-04-07 05:41:29 -04001853
you.chena6cd55a2022-05-08 12:20:18 +08001854int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1855{
you.chenc29444e2022-06-07 18:01:16 +08001856 const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
you.chen01bfac32022-06-07 10:36:00 +08001857 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001858
you.chen35020192022-05-06 11:30:57 +08001859 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001860 CHECK_WPA_CTRL(CTRL_STA);
1861
1862 system("connmanctl enable wifi");
1863
you.chena6fa5b22022-05-18 10:28:19 +08001864 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001865 return -1;
1866 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001867
you.chen01bfac32022-06-07 10:36:00 +08001868 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001869 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001870 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001871
1872 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001873}
1874
you.chen35020192022-05-06 11:30:57 +08001875int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001876{
you.chena6cd55a2022-05-08 12:20:18 +08001877 char lynq_disable_network_cmd[MAX_CMD];
1878 curr_status_info curr_state;
1879 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001880
you.chena6cd55a2022-05-08 12:20:18 +08001881 CHECK_IDX(idx, CTRL_STA);
1882 CHECK_WPA_CTRL(CTRL_STA);
1883
1884 curr_state.ap = &ap_info;
1885 curr_state.state = NULL;
1886
1887 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1888 return 0;
1889 }
1890
1891 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1892 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1893
1894 DO_OK_FAIL_REQUEST(cmd_save_config);
1895
1896 return 0;
1897// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001898}
qs.xiong7a105ce2022-03-02 09:43:11 -05001899
you.chen35020192022-05-06 11:30:57 +08001900//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1901// int i, count;
1902// char *p;
1903// const char * FLAG_SSID = "ssid=";
1904// const char * FLAG_SBSID = "bssid=";
1905// const char * FLAG_KEY_MGMT = "key_mgmt=";
1906// const char * FLAG_FREQ = "freq=";
1907// char lynq_sta_cmd[MAX_CMD];
1908// char *split_lines[128] = {0};
1909
1910// CHECK_WPA_CTRL(CTRL_AP);
1911
1912// sprintf(lynq_sta_cmd, "STA %s", bssid);
1913
1914// DO_REQUEST(lynq_sta_cmd);
1915
1916// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1917
1918// for(i=0; i < count; i++) {
1919// p = strstr(split_lines[i], FLAG_SSID);
1920// if (p != NULL) {
1921// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1922// continue;
1923// }
1924// }
1925
1926// lynq_get_interface_ip(idx, ap->ap_ip);
1927// lynq_ap_password_set(idx, ap->psw);
1928
1929// return 0;
1930//}
1931
1932static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1933 curr_status_info curr_state;
1934 curr_state.ap = ap;
1935 curr_state.state = NULL;
1936 return inner_get_status_info(interface, &curr_state);
1937}
1938
1939int 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 -04001940{
you.chen35020192022-05-06 11:30:57 +08001941 int ip_count, index, i, line_count;
1942 const char *lynq_first_sta_cmd = "STA-FIRST";
1943 char lynq_next_sta_cmd[MAX_CMD];
1944 char *bssid[1024] = {0};
1945 char *mac_list[128] = {0};
1946 char *ip_list[128] = {0};
1947 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001948
you.chen35020192022-05-06 11:30:57 +08001949 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001950
you.chen35020192022-05-06 11:30:57 +08001951 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001952
you.chen35020192022-05-06 11:30:57 +08001953// ap_info_s * tmp_ap;
1954// device_info_s * tmp_list;
1955 if (ap == NULL || list == NULL || len == NULL) {
1956 printf("bad input param");
1957 return -1;
1958 }
1959
1960// ap = &tmp_ap;
1961// list = &tmp_list;
1962 *ap = malloc(sizeof (ap_info_s));
1963
1964 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1965 return -1;
1966 }
1967
1968 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1969 lynq_ap_password_get(idx, (*ap)->psw);
1970
1971 ip_count = get_ip_mac_list(mac_list, ip_list);
1972 printf("get count %d\n", ip_count);
1973
1974 DO_REQUEST(lynq_first_sta_cmd);
1975
1976 index = 0;
1977 while (reply_len > 0) {
1978 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1979 break;
1980 }
1981 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1982 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1983 strcpy(bssid[index], split_lines[0]);
1984 index++;
1985 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1986 reply_len = MAX_RET;
1987 cmd_reply[0] = '\0';
you.chenb25bdf62022-09-02 17:44:14 +08001988 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen9ac66392022-08-06 17:01:16 +08001989 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1990 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001991 break;
1992 }
1993 }
1994
1995 *len = index;
1996
1997 *list = malloc(sizeof(device_info_s) * (*len));
1998 for (index=0; index < *len; index++) {
1999 strcpy((*list)[index].sta_mac, bssid[index]);
2000 for(i=0;i < ip_count; i++ ) {
2001 if (strcmp(bssid[index], mac_list[i]) == 0) {
2002 strcpy((*list)[index].sta_ip, ip_list[i]);
2003 break;
2004 }
2005 }
2006 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
2007 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
2008 free(bssid[index]);
2009 }
2010
you.chen9ac66392022-08-06 17:01:16 +08002011 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2012
you.chen35020192022-05-06 11:30:57 +08002013 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002014}
2015
you.chen35020192022-05-06 11:30:57 +08002016int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002017{
you.chen35020192022-05-06 11:30:57 +08002018 int i, count, index, count_words;
2019 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2020 char *split_lines[128] = {0};
2021 char *split_words[128] = {0};
2022 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002023
you.chen35020192022-05-06 11:30:57 +08002024 if (list == NULL || len == NULL) {
2025 return -1;
2026 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002027
you.chen9ac66392022-08-06 17:01:16 +08002028 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2029 {
2030 usleep(100 * 1000);
2031 }
2032
you.chen35020192022-05-06 11:30:57 +08002033 CHECK_IDX(idx, CTRL_STA);
2034
2035 CHECK_WPA_CTRL(CTRL_STA);
2036
2037 DO_REQUEST(lynq_scan_result_cmd);
2038
2039 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2040 *len = count - 1;
2041 *list = malloc(sizeof (scan_info_s) * *len);
2042
2043 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2044 for (index=0; index <count_words; index++) {
2045 printf("----header: %s\n", split_words[index]);
2046 }
2047
2048 for(index = 1;index < count; index++) {
2049 printf("---- %s\n",split_lines[index]);
2050 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2051 if (count_words < 4)
2052 continue;
2053 printf("count: %d, %s\n", count_words, split_words[0]);
2054 //bssid / frequency / signal level / flags / ssid
2055 p = (*list) + index - 1;
2056 strcpy(p->mac, split_words[0]);
2057 p->band = convert_band_from_freq(atoi(split_words[1]));
2058 p->rssi = -1 * atoi( split_words[2]);
2059 p->auth = convert_max_auth_from_flag(split_words[3]);
2060 strcpy(p->ssid, split_words[4]);
2061 }
2062
2063 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002064}
qs.xiong97fa59b2022-04-07 05:41:29 -04002065
you.chen35020192022-05-06 11:30:57 +08002066int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2067{
2068 int count, net_no, index;
2069 int net_no_list[128];
2070 lynq_wifi_auth_s net_auth;
2071 char lynq_remove_cmd[MAX_CMD];
2072
2073 if (ssid == NULL || *ssid == '\0') {
2074 printf("bad ssid\n");
2075 return -1;
2076 }
2077
2078 CHECK_IDX(idx, CTRL_STA);
2079
2080 CHECK_WPA_CTRL(CTRL_STA);
2081
2082 net_no = -1;
2083 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2084
2085 for (index=0; index < count; index++) {
2086 net_auth = -1;
2087 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2088 net_no = net_no_list[index];
2089 break;
2090 }
2091 }
2092
2093 if (net_no < 0) {
2094 return 0;
2095 }
2096
2097 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2098
2099 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2100 DO_OK_FAIL_REQUEST(cmd_save_config);
2101
2102 return 0;
2103}
2104
2105int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2106{
you.chen755332b2022-08-06 16:59:10 +08002107 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002108 int net_no_list[128];
2109 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002110 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002111
2112 if (list == NULL || len == NULL) {
2113 printf("bad param\n");
2114 return -1;
2115 }
2116
2117 CHECK_IDX(idx, CTRL_STA);
2118
2119// CHECK_WPA_CTRL(CTRL_STA);
2120
2121 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2122 printf("count is %d\n", count);
2123
2124 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002125 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002126 *len = count;
2127
2128 for (index=0; index < count; index++) {
2129 printf("to get ssid %d\n", index);
2130 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002131
2132 ssid_ptr = (*list)[index].base_info.ap_ssid;
2133 ssid_len = strlen(ssid_ptr);
2134 if (ssid_ptr[0] == '\"') {
2135 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2136 ssid_len -= 1;
2137 }
2138 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2139 ssid_ptr[ssid_len - 1] = '\0';
2140 }
2141
you.chen35020192022-05-06 11:30:57 +08002142 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002143 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002144 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2145 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2146 }
2147 else {
2148 (*list)[index].base_info.band = -1;
2149 }
2150
you.chen755332b2022-08-06 16:59:10 +08002151 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002152 }
2153
2154 return 0;
2155}
2156
2157int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2158{
2159 const char *lynq_scan_cmd = "SCAN";
2160
2161 CHECK_IDX(idx, CTRL_STA);
2162
2163 CHECK_WPA_CTRL(CTRL_STA);
2164
2165 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002166 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002167
2168 return 0;
2169}
2170
2171int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2172 if (cb == NULL) {
2173 return -1;
2174 }
2175
2176 g_ap_callback_priv = priv;
2177 g_ap_callback_func = cb;
2178
2179 return 0;
2180}
2181
2182int lynq_unreg_ap_event_callback(void * priv) {
2183 if (g_ap_callback_priv == priv) {
2184 g_ap_callback_func = NULL;
2185 g_ap_callback_priv = NULL;
2186 return 0;
2187 }
2188 return -1;
2189}
2190
2191int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2192 if (cb == NULL) {
2193 return -1;
2194 }
2195
2196 g_sta_callback_priv = priv;
2197 g_sta_callback_func = cb;
2198
2199 return 0;
2200}
2201
2202int lynq_unreg_sta_event_callback(void * priv) {
2203 if (g_sta_callback_priv == priv) {
2204 g_sta_callback_func = NULL;
2205 g_sta_callback_priv = NULL;
2206 return 0;
2207 }
2208 return -1;
2209}
2210
2211
2212static int inner_get_status_info_state (int interface, char *state) {
2213 curr_status_info curr_state;
2214 curr_state.ap = NULL;
2215 curr_state.state = state;
2216 return inner_get_status_info(interface, &curr_state);
2217}
2218
2219int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2220{
2221 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002222 CHECK_IDX(idx, CTRL_AP);
2223
2224 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2225 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2226 return 0;
2227 }
2228
2229 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2230 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2231 }
2232 else {
2233 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2234 }
2235
2236 return 0;
2237}
2238
2239int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2240 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002241 CHECK_IDX(idx, CTRL_STA);
2242
2243 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2244 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2245 return 0;
2246 }
2247
2248 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2249 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2250 }
2251 else {
2252 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2253 }
2254
2255 return 0;
2256}
2257
2258int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2259// CHECK_IDX(idx, CTRL_AP);
2260// int ret = 0;
2261// size_t reply_len = MAX_RET;
2262// char cmd_reply[MAX_RET]={0};
2263// const char * cmd_str = "GET country";
2264// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2265// do{
2266// if (NULL == s_lynq_wpa_ctrl) {
2267// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2268// if (NULL == s_lynq_wpa_ctrl ) {
2269// printf("wpa_ctrl_open fail\n");
2270// return -1;
2271// }
2272// }
2273// }while(0);
2274
2275// do {
2276// reply_len = MAX_RET;
2277// cmd_reply[0] = '\0';
2278// printf("to call [%s]\n", cmd_str);
you.chenb25bdf62022-09-02 17:44:14 +08002279// ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen35020192022-05-06 11:30:57 +08002280// if (ret != 0) {
2281// printf("call ##cmd_str fail %d\n", ret);
2282// return ret;
2283// }
2284// cmd_reply[reply_len+1] = '\0';
2285// printf("cmd replay [ %s ]\n", cmd_reply);
2286// }while(0);
2287
2288 FILE *fp;
2289 size_t i = 0;
2290 char lynq_cmd_ret[MAX_RET]={0};
2291
2292// CHECK_IDX(idx, CTRL_AP);
2293
2294 if((fp=popen("wl country","r"))==NULL)
2295 {
2296 perror("popen error!");
2297 return -1;
2298 }
2299 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2300 {
2301 perror("fread fail!");
2302 return -1;
2303 }
2304
2305 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2306 if (lynq_cmd_ret[i] == ' ') {
2307 lynq_cmd_ret[i] = '\0';
2308 break;
2309 }
2310 }
2311
2312 strcpy(country_code,lynq_cmd_ret);
2313 printf("---country code %s\n", country_code);
2314
2315 int ret=pclose(fp);
2316 if(ret==-1)
2317 {
2318 perror("close file faild");
2319 }
2320
2321 return 0;
2322}
2323
2324int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2325// const char * cmd_str = "GET country";
2326// CHECK_IDX(idx, CTRL_AP);
2327// CHECK_WPA_CTRL(CTRL_STA);
2328
2329// DO_REQUEST(cmd_str);
2330// printf("result %s\n", cmd_reply);
2331
2332 if (country_code == NULL || *country_code == '\0') {
2333 printf("bad country code\n");
2334 return -1;
2335 }
2336
2337 char lynq_country_cmd[MAX_CMD];
2338 sprintf(lynq_country_cmd, "wl country %s", country_code);
2339 if (system(lynq_country_cmd) == 0) {
2340 return 0;
2341 }
2342
2343 return -1;
2344}
2345
2346int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2347{
2348 if (mac == NULL) {
2349 return -1;
2350 }
2351
2352 CHECK_IDX(idx, CTRL_STA);
2353 ap_info_s ap;
2354 ap.ap_mac[0] = '\0';
2355
2356 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2357 return -1;
2358 }
2359 strcpy(mac, ap.ap_mac);
2360
2361 return 0;
2362}
2363
2364int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2365{
you.chen9ac66392022-08-06 17:01:16 +08002366 struct ifaddrs *ifaddr_header, *ifaddr;
2367 struct in_addr * ifa;
2368 const char * ifaName = "wlan0";
2369 if (ip == NULL)
2370 {
you.chenf58b3c92022-06-21 16:53:48 +08002371 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002372 }
you.chenf58b3c92022-06-21 16:53:48 +08002373
you.chen9ac66392022-08-06 17:01:16 +08002374 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002375 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002376 }
2377 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002378 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002379 }
you.chen35020192022-05-06 11:30:57 +08002380
you.chen9ac66392022-08-06 17:01:16 +08002381 if (getifaddrs(&ifaddr_header) == -1)
2382 {
you.chen35020192022-05-06 11:30:57 +08002383 perror("getifaddrs");
2384 return -1;
2385 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002386 }
you.chen35020192022-05-06 11:30:57 +08002387
2388
you.chen9ac66392022-08-06 17:01:16 +08002389 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2390 {
2391 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002392 continue;
you.chen9ac66392022-08-06 17:01:16 +08002393 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2394 {
2395 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2396 {
2397 // is a valid IP4 Address
2398 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2399 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2400 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2401 freeifaddrs(ifaddr_header);
2402 printf("ip %s\n", ip);
2403 return 0;
2404 }
2405 }
2406 }
2407 return -1;
you.chen35020192022-05-06 11:30:57 +08002408}
2409
2410int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2411{
2412 int count;
2413 size_t i;
2414 char *split_words[128] = {0};
2415 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2416
2417 CHECK_WPA_CTRL(idx);
2418
2419 DO_REQUEST(lynq_get_mac_cmd);
2420
2421 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2422 return -1;
2423 }
2424
2425 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2426
2427 if (count < 2) {
2428 return -1;
2429 }
2430
2431 for (i=0; i < strlen(split_words[1]); i++ ) {
2432 if (split_words[1][i] != ' ') {
2433 break;
2434 }
2435 }
2436
2437 strcpy(mac, split_words[1] + i);
2438
2439 return 0;
2440}
2441
2442int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2443{
2444// int count;
2445// char *split_words[128] = {0};
2446// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2447
2448// if (rssi == NULL) {
2449// return -1;
2450// }
2451
2452// CHECK_IDX(idx, CTRL_STA);
2453
2454// CHECK_WPA_CTRL(CTRL_STA);
2455
2456// DO_REQUEST(lynq_get_rssi_cmd);
2457
2458// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2459// return -1;
2460// }
2461
2462// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2463
2464// if (count < 2) {
2465// return -1;
2466// }
2467
2468// *rssi = atoi(split_words[1]) * -1;
2469
2470 FILE *fp;
2471 size_t i = 0;
2472 char lynq_cmd_ret[MAX_RET]={0};
2473
2474// CHECK_IDX(idx, CTRL_AP);
2475
you.chen9f17e4d2022-06-06 17:18:18 +08002476 if((fp=popen("wl rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002477 {
2478 perror("popen error!");
2479 return -1;
2480 }
2481 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2482 {
2483 perror("fread fail!");
2484 return -1;
2485 }
you.chen9f17e4d2022-06-06 17:18:18 +08002486 *rssi = atoi(lynq_cmd_ret) * -1;
you.chen35020192022-05-06 11:30:57 +08002487
2488 return 0;
2489}
2490
2491int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2492{
2493 if (band == NULL) {
2494 return -1;
2495 }
2496
2497 CHECK_IDX(idx, CTRL_STA);
2498 ap_info_s ap;
2499 ap.band = -1;
2500
2501 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2502 return -1;
2503 }
2504 *band = ap.band;
2505
2506 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002507}
you.chenf58b3c92022-06-21 16:53:48 +08002508
2509int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2510{
2511 int index;
2512 int ip_count = 0;
2513 char bssid[1024] = {0};
2514 char *mac_list[128] = {0};
2515 char *ip_list[128] = {0};
2516
2517 if (ip == NULL)
2518 {
2519 printf("invalid param");
2520 return -1;
2521 }
2522
2523 CHECK_IDX(idx, CTRL_STA);
2524
2525 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2526 {
2527 return -1;
2528 }
2529
2530 ip_count = get_ip_mac_list(mac_list, ip_list);
2531
2532 for (index=0; index < ip_count; index++)
2533 {
2534 if (strcmp(bssid, mac_list[index]) == 0)
2535 {
2536 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002537
2538 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2539
you.chenf58b3c92022-06-21 16:53:48 +08002540 return 0;
2541 }
2542 }
2543
2544 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002545 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2546
you.chenf58b3c92022-06-21 16:53:48 +08002547 return -1;
2548}
2549
2550