blob: 25e429b3307ce09a8088da7732fca391d432fccb [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.chend2fef3f2023-02-13 10:50:35 +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
you.chen0f5c6432022-11-07 18:31:14 +080072//you.chen add for tv-box start
73volatile int g_gbw_enabled = 0;
74char * g_gbw_mac = NULL;
75pthread_t g_gbw_watcher_pid = 0;
76static int startGBW();
77static int stopGBW();
78//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +080079
80typedef struct __curr_status_info {
81 ap_info_s *ap;
82 char * state;
83 int net_no;
84}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040085
you.chend2fef3f2023-02-13 10:50:35 +080086static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
87 char *reply, size_t *reply_len,
88 void (*msg_cb)(char *msg, size_t len))
89{
90 int ret;
91 if (ctrl->ctrl == NULL) {
92 printf("local_wpa_ctrl_request ctrl is null\n");
93 return -1;
94 }
95 pthread_mutex_lock(&ctrl->mutex);
96 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
97 pthread_mutex_unlock(&ctrl->mutex);
98 return ret;
99}
100
101static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
102 int repeat_cnt;
103 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
104 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
105 printf("inner_get_wpa_ctrl\n");
106 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
107 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
108// printf("wait enable finish\n");
109 usleep(500 * 1000);
110 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
111 }
112 if (NULL == g_lynq_wpa_ctrl[index]) {
113 printf("NULL == g_lynq_wpa_ctrl[index]\n");
114 goto out_addr;
115 }
116 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
117 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
118 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
119 printf("wpa_ctrl_open fail\n");
120 goto out_addr;
121 }
122 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
123 }
124 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
125out_addr:
126 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
127 return lynq_wpa_ctrl;
128}
129
qs.xiong97fa59b2022-04-07 05:41:29 -0400130#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400131{\
you.chen35020192022-05-06 11:30:57 +0800132 perror((str));\
133 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400134}
135
you.chen35020192022-05-06 11:30:57 +0800136#define CHECK_IDX(idx, type) do { \
137 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
138 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
139 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
140 return -1; \
141 } \
142 }while (0)
143
144#define CHECK_WPA_CTRL(index) int ret = 0;\
145 size_t reply_len = MAX_RET; \
146 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800147 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800148 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800149 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
150 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800151 }while(0)
152
153#define DO_REQUEST(cmd_str) do { \
154 reply_len = MAX_RET;\
155 cmd_reply[0] = '\0'; \
qs.xiongfdbc58e2022-09-29 11:37:32 +0800156 printf("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800157 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 +0800158 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800159 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800160 return ret; \
161 } \
162 cmd_reply[reply_len+1] = '\0'; \
163 printf("cmd replay [ %s ]\n", cmd_reply); \
164 }while(0)
165
166#define DO_OK_FAIL_REQUEST(cmd_str) do { \
167 DO_REQUEST(cmd_str); \
168 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800169 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800170 return -1; \
171 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800172 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800173 return -1; \
174 } \
175 }while (0)
176
177
you.chend2fef3f2023-02-13 10:50:35 +0800178static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
you.chen35020192022-05-06 11:30:57 +0800179
180static void APWatcherThreadProc() {
181 size_t len = MAX_RET;
182 char msg_notify[MAX_RET];
183
you.chen6c2dd9c2022-05-16 17:55:28 +0800184 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800185 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800186
187 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800188 if (lynq_wpa_ctrl == NULL) {
189 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
190 if (lynq_wpa_ctrl == NULL) {
191 usleep(100*1000);
192 continue;
193 }
194
195 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800196 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800197 }
198
you.chen35020192022-05-06 11:30:57 +0800199 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
200 usleep(100*1000);
201 continue;
202 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800203 memset(msg_notify, 0, MAX_RET);
204 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800205 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
206 msg_notify[len+1] = '\0';
207 printf("ap------> %s\n", msg_notify);
you.chen0f5c6432022-11-07 18:31:14 +0800208//you.chen change for tv-box start
you.chen35020192022-05-06 11:30:57 +0800209 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800210 if (g_ap_callback_func != NULL)
211 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
212 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
213 printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
214 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
215 stopGBW();
216 }
217 }
you.chen35020192022-05-06 11:30:57 +0800218 }
219 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800220 if (g_ap_callback_func != NULL)
221 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
222 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
223 printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
224 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
225 startGBW();
226 }
227 }
you.chen35020192022-05-06 11:30:57 +0800228 }
you.chen0f5c6432022-11-07 18:31:14 +0800229//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800230 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
231 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800232 if (lynq_wpa_ctrl != NULL) {
233 wpa_ctrl_detach(lynq_wpa_ctrl);
234 wpa_ctrl_close(lynq_wpa_ctrl);
235 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400236}
237
you.chen35020192022-05-06 11:30:57 +0800238static void STAWatcherThreadProc() {
239 size_t len = MAX_RET;
240 char msg_notify[MAX_RET];
241 char *pReason;
242 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400243
you.chen6c2dd9c2022-05-16 17:55:28 +0800244 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800245 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800246
247 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800248 if (lynq_wpa_ctrl == NULL) {
249 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
250 if (lynq_wpa_ctrl == NULL) {
251 usleep(100*1000);
252 continue;
253 }
254
255 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800256 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800257 }
258
you.chen35020192022-05-06 11:30:57 +0800259 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
260 usleep(100*1000);
261 continue;
262 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800263 memset(msg_notify, 0, MAX_RET);
264 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800265 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
266 msg_notify[len+1] = '\0';
267 printf("sta ------> %s\n", msg_notify);
268 if (strstr(msg_notify, state_scan_result) != NULL) {
269 g_sta_scan_finish_flag = 1;
270 }
271
272 if (g_sta_callback_func == NULL) {
273 continue;
274 }
275 error = -1;
276 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
277 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
278 }
279 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
280 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
281 }
282 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
283 pReason = strstr(msg_notify, "reason=");
284 if (pReason != NULL) {
285 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800286 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800287 error = LYNQ_TIME_OUT;
288 }
you.chen92fd5d32022-05-25 10:09:47 +0800289 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800290 error = LYNQ_PSW_ERROR;
291 }
292 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
293 }
294 }
295 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
296 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
297 }
298 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
299 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
300 }
301 }
302 }
you.chen92fd5d32022-05-25 10:09:47 +0800303 if (lynq_wpa_ctrl != NULL) {
304 wpa_ctrl_detach(lynq_wpa_ctrl);
305 wpa_ctrl_close(lynq_wpa_ctrl);
306 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400307}
308
qs.xiong1af5daf2022-03-14 09:12:12 -0400309int lynq_wifi_enable(void)
310{
you.chen35020192022-05-06 11:30:57 +0800311 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800312 int i;
you.chend2fef3f2023-02-13 10:50:35 +0800313 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
314
315 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
316 goto out_enable;
317 }
318
319 printf("lynq_wifi_enable1\n");
you.chen35020192022-05-06 11:30:57 +0800320 const char * cmd_check_service =
321 "state=`systemctl is-active wg870_drv_insmod.service`\n"
322 "[ \"\"$state == \"active\" ] && exit 0\n"
323 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
324// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
325// return 0;
326// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400327
you.chen35020192022-05-06 11:30:57 +0800328 ret = system(cmd_check_service);
329 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800330 printf("service state %d\n", ret);
you.chend2fef3f2023-02-13 10:50:35 +0800331 ret = -1;
332 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800333 }
lhfe8da902022-10-11 18:55:36 +0800334
you.chen6c2dd9c2022-05-16 17:55:28 +0800335 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800336 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800337 break;
338 }
339 usleep(300*1000);
340 }
341
342 if (i >= 10) {
you.chend2fef3f2023-02-13 10:50:35 +0800343 ret = -1;
344 goto out_enable;
you.chen6c2dd9c2022-05-16 17:55:28 +0800345 }
346
you.chen9f17e4d2022-06-06 17:18:18 +0800347 //@todo delete add temp check for socket avilable start (20220606)
348 for (i=0; i<60; i++)
349 {
350 if (system("netstat -an | grep -q DGRAM") == 0) {
351 break;
352 }
353 sleep(1);
354 }
355
356 if (i >= 60)
357 {
you.chend2fef3f2023-02-13 10:50:35 +0800358 ret = -1;
359 goto out_enable;
you.chen9f17e4d2022-06-06 17:18:18 +0800360 }
361 //@todo delete add temp check for socket avilable end (20220606)
362
you.chena6fa5b22022-05-18 10:28:19 +0800363 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800364 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800365 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800366 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800367 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800368 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800369 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800370 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800371 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800372 }
qs.xiong55f32762023-02-16 15:59:45 +0800373 ret = system("wl -i wlan0 PM 0");
374 if( ret != 0)
375 {
376 printf("Set sta PM work mode to 0 fail,ret is%d",ret);
377 }
you.chen35020192022-05-06 11:30:57 +0800378 if (g_ap_watcher_pid == 0 ) {
379 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
380 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800381 ret = -1;
382 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800383 }
384 }
385
386 if (g_sta_watcher_pid == 0 ) {
387 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
388 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800389 ret = -1;
390 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800391 }
392 }
393
you.chena6fa5b22022-05-18 10:28:19 +0800394 for (i=0; i<10; i++) {
395 usleep(300*1000);
396 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
397 break;
398 }
399 }
400
you.chend2fef3f2023-02-13 10:50:35 +0800401 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
402 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
403 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
404 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
405out_enable:
406 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800407 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500408}
409
qs.xiong1af5daf2022-03-14 09:12:12 -0400410int lynq_wifi_disable(void)
411{
you.chend2fef3f2023-02-13 10:50:35 +0800412 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800413 g_ap_watcher_stop_flag = 1;
414 g_sta_watcher_stop_flag = 1;
415 if (g_ap_watcher_pid != 0)
416 pthread_join(g_ap_watcher_pid, NULL);
417 if (g_sta_watcher_pid != 0)
418 pthread_join(g_sta_watcher_pid, NULL);
419 if (g_lynq_wpa_ctrl[0] != NULL)
420 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
421 if (g_lynq_wpa_ctrl[1] != NULL)
422 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
423 g_ap_watcher_pid = 0;
424 g_sta_watcher_pid = 0;
425 g_lynq_wpa_ctrl[0] = NULL;
426 g_lynq_wpa_ctrl[1] = NULL;
427 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +0800428 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
429 return 0;
430}
431
432static inline char inner_convert_char(char in)
433{
434 if (in >= '0' && in <= '9')
435 {
436 return in - '0';
437 }
438 else if (in >= 'a' && in <= 'f')
439 {
440 return in - 'a' + 10;
441 }
442 else if (in >= 'A' && in <= 'F')
443 {
444 return in - 'A' + 10;
445 }
446 else
447 {
448 return '\xff';
449 }
450}
451
452static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
453{
454 char *p;
455 size_t pos = 0;
456 if (NULL == out_ssid)
457 return;
458 //printf("input ssid=[%s]\n", ssid);
459 memset(out_ssid, 0, out_ssid_len);
460 if (NULL == ssid)
461 return;
462 p = strchr(ssid, '\\');
463 if (NULL == p)
464 {
465 strncpy(out_ssid, ssid, out_ssid_len);
466 //printf(" first %s\n", out_ssid);
467 }
468 else
469 {
470 pos = p - ssid;
471 memcpy(out_ssid, ssid, pos);
472 //printf("pos %lu -- %s\n", pos, out_ssid);
473 for(; pos < out_ssid_len; pos ++)
474 {
475 if (p[0] == '\0')
476 {
477 //printf(" out %s\n", out_ssid);
478 return;
479 }
480 else if (p[0] != '\\')
481 {
482 out_ssid[pos] = p[0];
483 p += 1;
484 }
485 else if (p[1] == 'x' || p[1] == 'X')
486 {
487 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
488 p += 4;
489 }
490 else if (p[1] == '\\')
491 {
492 out_ssid[pos] = '\\';
493 p += 2;
494 }
495 else if (p[1] == 't')
496 {
497 out_ssid[pos] = '\t';
498 p += 2;
499 }
500 else if (p[1] == 'r')
501 {
502 out_ssid[pos] = '\r';
503 p += 2;
504 }
505 else if (p[1] == 'n')
506 {
507 out_ssid[pos] = '\n';
508 p += 2;
509 }//todo find a better way to convert?
510 }
511 }
512 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500513}
qs.xiong1af5daf2022-03-14 09:12:12 -0400514
you.chen35020192022-05-06 11:30:57 +0800515static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +0800516 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +0800517 char lynq_cmd_get[128]={0};
518
519 if (out_put == NULL) {
520 printf("output ptr is null\n");
521 return -1;
522 }
523 if (param_name == NULL) {
524 printf("param ptr is null");
525 return -1;
526 }
527 if (param_name[0] == '\0') {
528 printf("param is empty");
529 return -1;
530 }
531
532 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
533
534 CHECK_WPA_CTRL(interface);
535
536 DO_REQUEST(lynq_cmd_get);
537
538 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
539 return -1;
540 }
541
you.chena6fa5b22022-05-18 10:28:19 +0800542// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +0800543 if (strcmp(param_name, "ssid") == 0)
544 {
545 if (cmd_reply[0] == '\"') {
546 ssid_len = reply_len - 1;
547 memcpy(out_put, cmd_reply + 1, ssid_len);
548 if (out_put[ssid_len-1] == '\"')
549 {
550 out_put[ssid_len-1] = '\0';
551 }
552 else
553 {
554 out_put[ssid_len] = '\0';
555 }
556 }
557 else{
558 ssid_len = reply_len / 2;
559 for(i=0; i<ssid_len; i++)
560 {
561 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
562 }
563 out_put[ssid_len] = '\0';
564 }
565 }
566 else
567 {
568 memcpy(out_put, cmd_reply, reply_len + 1);
569 }
you.chen35020192022-05-06 11:30:57 +0800570 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500571}
qs.xiong1af5daf2022-03-14 09:12:12 -0400572
you.chen35020192022-05-06 11:30:57 +0800573static int lynq_split(char * str, int len, char delimiter, char * results[]) {
574 int ret = 0;
575 char * end = str + len - 1;
576 results[ret++] = str;
577 while(str < end) {
578 if (*str == delimiter) {
579 *str++ = '\0';
580 results[ret++] = str;
581 continue;
582 }
583 str++;
584 }
585 if (*str == delimiter) {
586 *str = '\0';
587 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400588
you.chen35020192022-05-06 11:30:57 +0800589 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500590}
591
you.chend2fef3f2023-02-13 10:50:35 +0800592static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
593{
594 char * p;
595 int ret = 0;
596 char cmd[256]={0};
597 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +0800598 return -1;
you.chend2fef3f2023-02-13 10:50:35 +0800599 memset(ip, 0, ip_len);
qs.xiong13673462023-02-21 19:12:54 +0800600 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | head -1 | awk '{print $1}'", mac);
you.chend2fef3f2023-02-13 10:50:35 +0800601 ret = exec_cmd(cmd, ip, ip_len);
602 p = strchr(ip, '\n');
603 if (NULL != p)
604 {
605 *p = '\0';
you.chen35020192022-05-06 11:30:57 +0800606 }
you.chend2fef3f2023-02-13 10:50:35 +0800607 printf("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +0800608 return ret;
609}
610
you.chend2fef3f2023-02-13 10:50:35 +0800611static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +0800612 struct in_addr addr ={0};
613 struct hostent *ht;
614
615 if (ip == NULL || *ip == '\0' || hostname == NULL) {
616 return -1;
617 }
618
you.chend2fef3f2023-02-13 10:50:35 +0800619 *hostname = '\0';
you.chen35020192022-05-06 11:30:57 +0800620 if (inet_aton(ip, &addr) == 0) {
621 printf("---inet_aton fail\n");
622 return -1;
623 }
624
625 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
626
627 if (ht == NULL) {
628 printf("---gethostbyaddr fail\n");
629 herror(NULL);
630 return -1;
631 }
632
633 strcpy(hostname, ht->h_name);
634
635 return 0;
636}
637
638static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
639{
640 int count, index, words_count;
641 char * split_lines[128]= {0};
642 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +0800643 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +0800644 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
645
646 CHECK_WPA_CTRL(ap_sta);
647
648 DO_REQUEST(lynq_wifi_list_networks);
649
650 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
651
652 //@todo check ssid field to compatible
653
654 ret = 0;
655 for(index=1; index < count; index++) {
656 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
657 if (words_count > 2) {
you.chend2fef3f2023-02-13 10:50:35 +0800658 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
659 if (ssid == NULL || strcmp(local_ssid, ssid) == 0) {
you.chen35020192022-05-06 11:30:57 +0800660 net_no_list[ret++] = atoi(split_words[0]);
661 }
662 }
663 }
664
665 return ret;
666}
667
668static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800669 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800670 CHECK_WPA_CTRL(ap_sta);
671 const char *lynq_wifi_add_network = "ADD_NETWORK";
672
673 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800674 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800675 return -1;
676 }
677
you.chen6c2dd9c2022-05-16 17:55:28 +0800678 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800679 if(cmd_reply[i] == '\n') {
680 cmd_reply[i] = '\0';
681 break;
682 }
683 }
684 return atoi(cmd_reply);
685}
you.chena6cd55a2022-05-08 12:20:18 +0800686
you.chen35020192022-05-06 11:30:57 +0800687static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
688{
689 int count, index;
690 int net_no_list[128];
691
692
693 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
694 for (index=0; index < count; index++) {
695 if (net_no_list[index] == net_no) {
696 return 0;
697 }
698 }
699
700 if (count >= 1)
701 index = net_no_list[count - 1];
702 else
703 index = -1;
704
705 while (index < net_no ) {
706 index = lynq_add_network(ap_sta);
707 if (index >= net_no) { // required network no created
708 return 0;
709 }
you.chena6cd55a2022-05-08 12:20:18 +0800710 else if( index < 0) {
711 printf("add network fail\n");
712 return -1;
713 }
you.chen35020192022-05-06 11:30:57 +0800714 }
715
716 if (index < 0)
717 return -1;
718
719 return 0;
720}
721
722static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
723 if (freq > 5000 && freq < 6000) {
724 return LYNQ_WIFI_5G_band;
725 }
726 else if (freq > 2000 && freq < 3000) {
727 return LYNQ_WIFI_2G_band;
728 }
729 return LYNQ_WIFI_2_and_5G_band;
730}
731
732static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
733 if (key_mgmt != NULL) {
734 if (memcmp( key_mgmt, "NONE", 4) == 0) {
735 return LYNQ_WIFI_AUTH_OPEN;
736 }
737 else if (memcmp( key_mgmt, "WEP", 3) == 0){
738 return LYNQ_WIFI_AUTH_WEP;
739 }
740 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
741 return LYNQ_WIFI_AUTH_WPA_PSK;
742 }
743 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
744 return LYNQ_WIFI_AUTH_WPA2_PSK;
745 }
746 }
747
748 return -1;
749}
750
751static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
752 if (flag != NULL) {
753 if (strstr( flag, "WPA2-PSK") != NULL){
754 return LYNQ_WIFI_AUTH_WPA2_PSK;
755 }
756 else if (strstr( flag, "WPA-PSK") != NULL){
757 return LYNQ_WIFI_AUTH_WPA_PSK;
758 }
759 else if (strstr( flag, "WEP") != NULL){
760 return LYNQ_WIFI_AUTH_WEP;
761 }
762 else if (strstr( flag, "NONE") != NULL) {
763 return LYNQ_WIFI_AUTH_OPEN;
764 }
qs.xiongd215fa32023-02-18 16:16:26 +0800765 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0) {
you.chend2fef3f2023-02-13 10:50:35 +0800766 return LYNQ_WIFI_AUTH_OPEN;
767 }
you.chen35020192022-05-06 11:30:57 +0800768 }
769
770 return -1;
771}
772
773static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
774 switch (bw) {
775 case 10:
776 return LYNQ_WIFI_BANDWIDTH_HT10;
777 break;
778 case 20:
779 return LYNQ_WIFI_BANDWIDTH_HT20;
780 break;
781 case 40:
782 return LYNQ_WIFI_BANDWIDTH_HT40;
783 break;
784 case 80:
785 return LYNQ_WIFI_BANDWIDTH_HT80;
786 break;
787 default:
788 break;
789 }
790
791 return -1;
792}
793
794static int inner_get_status_info(int interface, curr_status_info *curr_state) {
795 int i, count;
796 char *p;
797 const char *lynq_status_cmd = "STATUS";
798 const char * FLAG_SSID = "ssid=";
799 const char * FLAG_SBSID = "bssid=";
800 const char * FLAG_KEY_MGMT = "key_mgmt=";
801 const char * FLAG_FREQ = "freq=";
802 const char * FLAG_STATE = "wpa_state=";
803 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +0800804 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +0800805 char *split_lines[128] = {0};
806
807 CHECK_WPA_CTRL(interface);
808
809 if (curr_state == NULL) {
810 return -1;
811 }
812
813 DO_REQUEST(lynq_status_cmd);
814
815 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
816
817 curr_state->net_no = -1;
818 ret = -1;
819 for(i=0; i < count; i++) {
820 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800821 p = strstr(split_lines[i], FLAG_SBSID);
822 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800823 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +0800824 ret = 0;
825 continue;
826 }
you.chenf58b3c92022-06-21 16:53:48 +0800827 p = strstr(split_lines[i], FLAG_SSID);
828 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800829 inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid));
you.chenf58b3c92022-06-21 16:53:48 +0800830 ret = 0;
831 continue;
832 }
you.chen35020192022-05-06 11:30:57 +0800833 p = strstr(split_lines[i], FLAG_KEY_MGMT);
834 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800835 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
836 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800837 ret = 0;
838 continue;
839 }
840 p = strstr(split_lines[i], FLAG_FREQ);
841 if (p != NULL) {
842 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
843 ret = 0;
844 continue;
845 }
you.chend2fef3f2023-02-13 10:50:35 +0800846 p = strstr(split_lines[i], FLAG_IPADDR);
847 if (p != NULL) {
848 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
849 ret = 0;
850 continue;
851 }
you.chen35020192022-05-06 11:30:57 +0800852 } // end if (ap != NULL)
853 if (curr_state->state != NULL) {
854 p = strstr(split_lines[i], FLAG_STATE);
855 if (p != NULL) {
856 strcpy(curr_state->state, p + strlen(FLAG_STATE));
857 ret = 0;
858 continue;
859 }
860
861 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800862 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800863 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800864 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800865 printf("net_no %d, -- %s\n", curr_state->net_no, p);
866 }
867 }
868
869 return ret;
870}
871
872
qs.xiongf1b525b2022-03-31 00:58:23 -0400873int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500874{
you.chen35020192022-05-06 11:30:57 +0800875 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500876
you.chen35020192022-05-06 11:30:57 +0800877 if (ap_ssid == NULL) {
878 printf("ap_ssid is null\n");
879 return -1;
880 }
881 else {
882 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
883 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400884
you.chen35020192022-05-06 11:30:57 +0800885 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
886 return -1;
887 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400888
you.chen35020192022-05-06 11:30:57 +0800889 CHECK_IDX(idx, CTRL_AP);
890
891 CHECK_WPA_CTRL(CTRL_AP);
892
893 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
894
895 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
896 DO_OK_FAIL_REQUEST(cmd_save_config);
897
qs.xiong7a105ce2022-03-02 09:43:11 -0500898 return 0;
you.chen35020192022-05-06 11:30:57 +0800899
qs.xiong7a105ce2022-03-02 09:43:11 -0500900}
901
you.chen35020192022-05-06 11:30:57 +0800902int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500903{
you.chen35020192022-05-06 11:30:57 +0800904 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +0800905 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500906}
907
qs.xiongc9c79f72022-10-17 15:27:18 +0800908/*****
909 *frequency <------>channel
910 *
911 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
912 *
913 *
914 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
915 *
916 *
917 * */
918static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +0800919 int legitimate_frequency[]={2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825};
920 int i;
921 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
922
qs.xiong69a332b2022-12-02 09:58:57 +0800923 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +0800924 {
925 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +0800926 break;
qs.xiongc9c79f72022-10-17 15:27:18 +0800927 }
qs.xiongc00b6032022-11-29 16:28:03 +0800928
929 if(i == arr_len)
930 {
931 printf("input frequency is eero--->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +0800932 return -1;
933 }
qs.xiongc00b6032022-11-29 16:28:03 +0800934
qs.xiongc9c79f72022-10-17 15:27:18 +0800935 return 0;
936}
qs.xiong13673462023-02-21 19:12:54 +0800937
938static int lynq_check_frequencyby_country_code(int input_frequency)
939{
940 char str_cnc[]="CN";
941 char str_dest[20]="";
942
943 if( lynq_get_country_code(1,str_dest) != 0 )
944 {
945 printf("get country_code error\n");
946 return -1;
947 }
948 if( strncmp(str_dest,str_cnc,2) != 0 )
949 {
950 return 0;
951 }else if( 2473 < input_frequency && input_frequency < 5744)
952 {
953 printf("input frequency is bad\n");
954 return -1;
955 }
956 return 0;
957}
qs.xiongf1b525b2022-03-31 00:58:23 -0400958int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500959{
qs.xiongc00b6032022-11-29 16:28:03 +0800960 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +0800961 char lynq_wifi_frequency_cmd[128]={0};
962 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800963 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500964
qs.xiongc9c79f72022-10-17 15:27:18 +0800965 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +0800966 check = lynq_check_set_frequency(lynq_wifi_frequency);
967 if(check != 0)
968 {
969 printf("do check frequency error\n");
qs.xiongc9c79f72022-10-17 15:27:18 +0800970 return -1;
you.chen35020192022-05-06 11:30:57 +0800971 }
qs.xiong13673462023-02-21 19:12:54 +0800972 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
973 if(check != 0)
974 {
975 printf("do check frequency error\n");
976 return -1;
977 }
978
qs.xiongc00b6032022-11-29 16:28:03 +0800979 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
980 {
you.chen35020192022-05-06 11:30:57 +0800981 return -1;
982 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400983
you.chen35020192022-05-06 11:30:57 +0800984 CHECK_IDX(idx, CTRL_AP);
985
986 CHECK_WPA_CTRL(CTRL_AP);
987
988 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
989 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
990 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
991
you.chen6c2dd9c2022-05-16 17:55:28 +0800992 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800993 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
994 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
995 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500996
qs.xiong1af5daf2022-03-14 09:12:12 -0400997 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500998}
999
qs.xiongf1b525b2022-03-31 00:58:23 -04001000int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001001{
you.chen35020192022-05-06 11:30:57 +08001002 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001003
you.chen35020192022-05-06 11:30:57 +08001004 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001005
you.chen35020192022-05-06 11:30:57 +08001006 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
1007 return -1;
1008 }
1009 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001010
1011 return 0;
1012}
1013
qs.xiongf1b525b2022-03-31 00:58:23 -04001014int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1015{
you.chen35020192022-05-06 11:30:57 +08001016 CHECK_IDX(idx, CTRL_AP);
1017 switch(bandwidth){
1018 case LYNQ_WIFI_BANDWIDTH_HT10:
1019 {
1020 printf("bandwith [%d] not support now\n", bandwidth);
1021 return -1;
1022 }
1023 case LYNQ_WIFI_BANDWIDTH_HT20:
1024 {
1025 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1026 system("wl down");
1027 if (system(lynq_cmd_bandwith) != 0 ){
1028 return -1;
1029 }
1030 system("wl up");
1031 break;
1032 }
1033 case LYNQ_WIFI_BANDWIDTH_HT40:
1034 {
qs.xiong10379192023-02-21 13:19:42 +08001035 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001036 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1037 system("wl down");
1038 if (system(lynq_cmd_bandwith) != 0 ){
1039 return -1;
1040 }
1041 system("wl up");
1042 break;
1043 }
1044 case LYNQ_WIFI_BANDWIDTH_HT80:
1045 {
qs.xiong10379192023-02-21 13:19:42 +08001046 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001047 system("wl down");
1048 if (system(lynq_cmd_bandwith) != 0 ){
1049 return -1;
1050 }
1051 system("wl up");
1052 break;
1053 }
1054 default:
1055 {
1056 printf("auth type [%d] not support now\n", bandwidth);
1057 return -1;
1058 }
1059 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001060
1061
you.chen35020192022-05-06 11:30:57 +08001062 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001063}
you.chen35020192022-05-06 11:30:57 +08001064
qs.xiongf1b525b2022-03-31 00:58:23 -04001065int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1066{
you.chen35020192022-05-06 11:30:57 +08001067 int count = 0;
1068 int index = 0;
1069 char *split_words[128] = {0};
1070 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001071
you.chen35020192022-05-06 11:30:57 +08001072 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001073
you.chen35020192022-05-06 11:30:57 +08001074 CHECK_WPA_CTRL(CTRL_AP);
1075
1076 DO_REQUEST(lynq_chanspec_cmd);
1077
1078 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1079 for(;index < count; index++) {
1080 if (strncmp(split_words[index], "bw", 2) != 0) {
1081 continue;
1082 }
1083
1084 index++;
1085 if (index >= count) {
1086 return -1;
1087 }
1088
1089 printf("bw %s\n", split_words[index]);
1090 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1091 return 0;
1092 }
1093
1094 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001095}
qs.xiong0fb469a2022-04-14 03:50:45 -04001096
qs.xiongf1b525b2022-03-31 00:58:23 -04001097int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001098{
you.chen35020192022-05-06 11:30:57 +08001099 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -04001100
you.chen35020192022-05-06 11:30:57 +08001101 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001102
you.chen35020192022-05-06 11:30:57 +08001103 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001104
you.chen35020192022-05-06 11:30:57 +08001105 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
1106 return -1;
1107 }
1108
1109 system("wl down");
1110 if (system(lynq_cmd_channel) != 0 ){
1111 return -1;
1112 }
1113 system("wl up");
1114 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001115}
qs.xiong0fb469a2022-04-14 03:50:45 -04001116
qs.xiongf1b525b2022-03-31 00:58:23 -04001117int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001118{
you.chen35020192022-05-06 11:30:57 +08001119 int count = 0;
1120 int index = 0;
1121 char *split_words[128] = {0};
1122 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001123
you.chen35020192022-05-06 11:30:57 +08001124 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001125
you.chen35020192022-05-06 11:30:57 +08001126 CHECK_WPA_CTRL(CTRL_AP);
1127
1128 DO_REQUEST(lynq_chanspec_cmd);
1129
1130 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1131 for(;index < count; index++) {
1132 printf("---- %s\n",split_words[index]);
1133 if (strncmp(split_words[index], "channel", 2) != 0) {
1134 continue;
1135 }
1136
1137 index++;
1138 if (index >= count) {
1139 return -1;
1140 }
1141
1142 *channel = atoi(split_words[index]);
1143 return 0;
1144 }
1145
1146 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001147}
1148
1149
you.chen35020192022-05-06 11:30:57 +08001150int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001151{
you.chen6c2dd9c2022-05-16 17:55:28 +08001152 char ssid[MAX_CMD] = {0};
1153 int freq = 0;
1154 char lynq_auth_cmd[64]={0};
1155 char lynq_auth_alg_cmd[64]={0};
1156 char lynq_psk_cmd[64]={0};
1157 char lynq_pairwise_cmd[64]={0};
1158 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001159 CHECK_IDX(idx, CTRL_AP);
1160
you.chen6c2dd9c2022-05-16 17:55:28 +08001161 CHECK_WPA_CTRL(CTRL_AP);
1162
you.chen35020192022-05-06 11:30:57 +08001163 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1164 return -1;
1165 }
1166
you.chen92fd5d32022-05-25 10:09:47 +08001167 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001168 if (org_auth == auth) {
1169 return 0;
1170 }
1171 else {
1172 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1173 ssid[0] = '\0';
1174 }
1175 lynq_wifi_ap_frequency_get(idx, &freq);
1176
1177 DO_OK_FAIL_REQUEST(cmd_disconnect);
1178 DO_OK_FAIL_REQUEST(cmd_remove_all);
1179 if (ssid[0] != '\0') {
1180 lynq_wifi_ap_ssid_set(idx, ssid);
1181 }
1182 if (freq != 0) {
1183 lynq_wifi_ap_frequency_set(idx, freq);
1184 }
1185 }
1186 }
you.chen35020192022-05-06 11:30:57 +08001187
qs.xiong7a105ce2022-03-02 09:43:11 -05001188 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001189 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001190 {
you.chen35020192022-05-06 11:30:57 +08001191 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001192 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001193
you.chen35020192022-05-06 11:30:57 +08001194 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001195 break;
1196 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001197 case LYNQ_WIFI_AUTH_WEP:
1198 {
1199 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001200 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001201 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1202
1203 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1204 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1205 break;
1206 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001207 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001208 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001209 {
you.chen35020192022-05-06 11:30:57 +08001210 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1211 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1212 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1213 }
1214 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001215 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001216 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001217 }
1218// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1219// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1220 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001221
you.chen35020192022-05-06 11:30:57 +08001222 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1223 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1224 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001225 break;
you.chen35020192022-05-06 11:30:57 +08001226 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001227 default:
you.chen35020192022-05-06 11:30:57 +08001228 {
1229 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001230 return -1;
you.chen35020192022-05-06 11:30:57 +08001231 }
1232 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001233 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001234
qs.xiong7a105ce2022-03-02 09:43:11 -05001235 return 0;
1236}
1237
you.chen35020192022-05-06 11:30:57 +08001238int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001239{
you.chen35020192022-05-06 11:30:57 +08001240 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001241 char lynq_auth_alg_str[MAX_RET] = {0};
1242 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001243
1244 CHECK_IDX(idx, CTRL_AP);
1245
1246 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1247 return -1;
1248 }
1249
1250 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001251 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1252 *auth = LYNQ_WIFI_AUTH_OPEN;
1253 }
1254 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1255 *auth = LYNQ_WIFI_AUTH_WEP;
1256 }
1257 else {
1258 *auth = LYNQ_WIFI_AUTH_OPEN;
1259 }
you.chen35020192022-05-06 11:30:57 +08001260 }
you.chen92fd5d32022-05-25 10:09:47 +08001261 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001262 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001263 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001264 }
1265 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1266 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1267 }
1268 else {
1269 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1270 }
you.chen35020192022-05-06 11:30:57 +08001271 }
you.chen92fd5d32022-05-25 10:09:47 +08001272 else {
1273 *auth = -1;
1274 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001275
you.chen6c2dd9c2022-05-16 17:55:28 +08001276 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001277}
qs.xiong1af5daf2022-03-14 09:12:12 -04001278
qs.xiong1af5daf2022-03-14 09:12:12 -04001279
qs.xiongf1b525b2022-03-31 00:58:23 -04001280int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001281{
you.chen35020192022-05-06 11:30:57 +08001282 char LYNQ_WIFI_CMD[128]={0};
1283 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1284 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001285
you.chen35020192022-05-06 11:30:57 +08001286 CHECK_IDX(idx, CTRL_AP);
1287
1288 CHECK_WPA_CTRL(CTRL_AP);
1289
1290// system("connmanctl enable wifi");
1291// system("connmanctl tether wifi on cy-test 12345678");
1292// system("ifconfig wlan0 down");
1293// system("ifconfig wlan0 up");
1294// system("ifconfig wlan0 up");
1295
1296 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1297 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1298
1299 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1300 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1301
qs.xiong7a105ce2022-03-02 09:43:11 -05001302 return 0;
1303}
1304
qs.xiongf1b525b2022-03-31 00:58:23 -04001305int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001306{
you.chen35020192022-05-06 11:30:57 +08001307 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001308}
1309
qs.xiongf1b525b2022-03-31 00:58:23 -04001310int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001311{
you.chen35020192022-05-06 11:30:57 +08001312 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -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 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001317
you.chen35020192022-05-06 11:30:57 +08001318 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1319
1320 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1321
you.chenb4b121c2022-05-06 17:50:16 +08001322// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001323
qs.xiong7a105ce2022-03-02 09:43:11 -05001324 return 0;
1325}
qs.xiong1af5daf2022-03-14 09:12:12 -04001326
qs.xiongf1b525b2022-03-31 00:58:23 -04001327int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001328{
you.chen35020192022-05-06 11:30:57 +08001329 char lynq_disable_cmd[128] = {0};
1330 char lynq_select_cmd[128] = {0};
1331 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001332
you.chen35020192022-05-06 11:30:57 +08001333 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001334
you.chen35020192022-05-06 11:30:57 +08001335 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001336
you.chen35020192022-05-06 11:30:57 +08001337 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1338 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1339
1340 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1341 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1342 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001343
1344 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001345}
1346
qs.xiongf1b525b2022-03-31 00:58:23 -04001347int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001348{
you.chen35020192022-05-06 11:30:57 +08001349 char lynq_disable_cmd[128] = {0};
1350 char lynq_select_cmd[128] = {0};
1351 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001352
you.chen35020192022-05-06 11:30:57 +08001353 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001354
you.chen35020192022-05-06 11:30:57 +08001355 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001356
you.chen35020192022-05-06 11:30:57 +08001357 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1358 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1359
1360 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1361 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1362 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001363
1364 return 0;
1365}
qs.xiongf1b525b2022-03-31 00:58:23 -04001366
you.chen35020192022-05-06 11:30:57 +08001367int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001368{
qs.xiongf1b525b2022-03-31 00:58:23 -04001369 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001370 char lynq_tmp_cmd[MAX_CMD] = {0};
1371 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001372 if( password == NULL ){
1373 return -1;
1374 }
1375 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001376 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001377 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001378 return -1;
you.chen35020192022-05-06 11:30:57 +08001379 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001380
you.chen35020192022-05-06 11:30:57 +08001381 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001382
you.chen6c2dd9c2022-05-16 17:55:28 +08001383 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1384 return -1;
1385 }
1386 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1387 return -1;
1388 }
1389
you.chen35020192022-05-06 11:30:57 +08001390 CHECK_WPA_CTRL(CTRL_AP);
1391
you.chen6c2dd9c2022-05-16 17:55:28 +08001392 if (auth == LYNQ_WIFI_AUTH_WEP) {
1393 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1394 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1395 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1396 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1397 }
1398 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1399 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1400 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1401 }
1402 else {
1403 return -1;
1404 }
you.chen35020192022-05-06 11:30:57 +08001405
you.chen35020192022-05-06 11:30:57 +08001406 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001407
qs.xiongf1b525b2022-03-31 00:58:23 -04001408 return 0;
1409}
1410
you.chen35020192022-05-06 11:30:57 +08001411int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001412{
you.chen35020192022-05-06 11:30:57 +08001413 FILE * fp;
1414 int len, ret;
1415 int count, index;
1416 char *split_lines[128] = {0};
1417 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001418
you.chen35020192022-05-06 11:30:57 +08001419 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001420
you.chen35020192022-05-06 11:30:57 +08001421 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1422// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1423 if (NULL == fp) {
1424 printf("open file fail\n");
1425 return -1;
1426 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001427
you.chen35020192022-05-06 11:30:57 +08001428 buff = alloca(MAX_RET);
1429 fseek(fp, 0, SEEK_SET);
1430 len = fread(buff, 1, MAX_RET, fp);
1431 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001432
you.chen35020192022-05-06 11:30:57 +08001433 for(index=0; index < len; index ++) {
1434 if (memcmp(buff + index, "network={", 9) != 0) {
1435 continue;
1436 }
1437 p = buff + index + 9;
1438 for (; index < len; index ++ ) {
1439 if (buff[index] != '}') {
1440 continue;
1441 }
1442 buff[index] = '\0';
1443 break;
1444 }
1445 len = buff + index - p;
1446 }
1447
1448 count = lynq_split(p, len, '\n', split_lines);
1449
1450 ret = -1;
1451 for(index=0; index < count; index++) {
1452 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001453 if (p != NULL) {
1454 p += 4;
1455 if (*p == '\"') {
1456 p++;
1457 }
you.chen35020192022-05-06 11:30:57 +08001458 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001459 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1460 p += 9;
1461 if (*p == '\"') {
1462 p++;
1463 }
1464 }
1465 else {
1466 continue;
you.chen35020192022-05-06 11:30:57 +08001467 }
1468
1469 strcpy(password, p);
1470
1471 while(*password != '\0') {
1472 if (*password == '\"') {
1473 *password = '\0';
1474 break;
1475 }
1476 password++;
1477 }
1478 ret = 0;
1479 break;
1480 } //end for(index=0; index < count; index++)
1481
1482 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001483}
1484
you.chen35020192022-05-06 11:30:57 +08001485static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1486 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001487 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001488
you.chen35020192022-05-06 11:30:57 +08001489 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1490 return -1;
1491 }
1492
1493 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001494
1495 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1496 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1497 if (strcmp(lynq_proto_str, "RSN") == 0) {
1498 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1499 }
1500 }
1501 }
you.chen35020192022-05-06 11:30:57 +08001502 return 0;
1503}
1504
1505int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001506{
you.chen35020192022-05-06 11:30:57 +08001507 int pass_len, net_no, count, index;
1508 char lynq_tmp_cmd[300]={0};
1509 int net_no_list[128];
1510 lynq_wifi_auth_s net_auth;
1511 pass_len=strlen(password);
1512 if(pass_len < 8 || pass_len >= 64){
1513 return -1;
1514 }
1515
1516 CHECK_IDX(idx, CTRL_STA);
1517
1518 net_no = -1;
1519 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1520
1521 for (index=0; index < count; index++) {
1522 net_auth = -1;
1523 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1524 net_no = net_no_list[index];
1525 break;
1526 }
1527 }
1528
1529 if (net_no < 0) {
1530 return -1;
1531 }
1532
1533 CHECK_WPA_CTRL(CTRL_STA);
1534
1535 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1536
1537 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1538 DO_OK_FAIL_REQUEST(cmd_save_config);
1539
1540 return 0;
1541}
1542
1543int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1544
1545 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08001546 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001547 int count, index;
1548 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001549 char *buff, *p, *ssid, *ssid_end_flag;
1550 char tmp_ssid[128]={0};
you.chen35020192022-05-06 11:30:57 +08001551
you.chen755332b2022-08-06 16:59:10 +08001552 network_len = 0;
1553 p = NULL;
1554
you.chen35020192022-05-06 11:30:57 +08001555 CHECK_IDX(idx, CTRL_STA);
1556
you.chen755332b2022-08-06 16:59:10 +08001557 if (NULL == password) {
1558 printf("bad param\n");
1559 return -1;
1560 }
1561
you.chen35020192022-05-06 11:30:57 +08001562 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1563 if (NULL == fp) {
1564 printf("open file fail\n");
1565 return -1;
1566 }
1567
1568 buff = alloca(MAX_RET);
1569 fseek(fp, 0, SEEK_SET);
1570 len = fread(buff, 1, MAX_RET, fp);
1571 fclose(fp);
1572
1573 for(index=0; index < len; index ++) {
1574 for(; index < len; index ++) {
1575 if (memcmp(buff + index, "network={", 9) != 0) {
1576 continue;
1577 }
1578 p = buff + index + 9;
1579 for (; index < len; index ++ ) {
1580 if (buff[index] != '}') {
1581 continue;
1582 }
1583 buff[index] = '\0';
1584 break;
1585 }
you.chen755332b2022-08-06 16:59:10 +08001586 network_len = buff + index - p;
1587 break;
you.chen35020192022-05-06 11:30:57 +08001588 }
1589
qs.xiongb3f26af2023-02-17 18:41:07 +08001590 if (p == NULL)
1591 return -1;
1592
you.chend2fef3f2023-02-13 10:50:35 +08001593 ssid = strstr(p, "ssid=");
1594 if (ssid != NULL) {
1595 ssid += strlen("ssid=");
1596 if (ssid[0] == '\"') {
1597 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
1598 break;
1599 }
1600 else{
1601 ssid_end_flag = strstr(ssid, "\n");
1602 if (ssid_end_flag != NULL)
1603 {
1604 ssid_len = (ssid_end_flag - ssid) / 2;
1605 for(i=0; i<ssid_len; i++)
1606 {
1607 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
1608 }
1609 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
1610 break;
1611 }
1612 }
you.chen35020192022-05-06 11:30:57 +08001613 }
you.chend2fef3f2023-02-13 10:50:35 +08001614
you.chen35020192022-05-06 11:30:57 +08001615 }
1616
you.chen755332b2022-08-06 16:59:10 +08001617 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001618 return -1;
1619 }
1620
you.chen755332b2022-08-06 16:59:10 +08001621 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001622
1623 ret = -1;
1624 for(index=0; index < count; index++) {
1625 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001626 if (p != NULL) {
1627 p += 4;
1628 if (*p == '\"') {
1629 p++;
1630 }
you.chen35020192022-05-06 11:30:57 +08001631 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001632 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1633 p += 9;
1634 if (*p == '\"') {
1635 p++;
1636 }
1637 }
1638 else {
1639 continue;
you.chen35020192022-05-06 11:30:57 +08001640 }
1641
qs.xiong13673462023-02-21 19:12:54 +08001642 if (*p == '\"')
1643 p++;
1644 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08001645
qs.xiong13673462023-02-21 19:12:54 +08001646 p = password;
1647 while(password - p < 64 && *password != '\0') {
you.chen35020192022-05-06 11:30:57 +08001648 if (*password == '\"') {
1649 *password = '\0';
1650 break;
1651 }
1652 password++;
1653 }
1654 ret = 0;
1655 break;
1656 } //end for(index=0; index < count; index++)
1657
1658 return ret;
1659}
1660
1661static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1662{
qs.xiong97fa59b2022-04-07 05:41:29 -04001663 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001664
you.chen35020192022-05-06 11:30:57 +08001665 if (sta_ssid == NULL) {
1666 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001667 return -1;
you.chen35020192022-05-06 11:30:57 +08001668 }
1669
1670 CHECK_WPA_CTRL(CTRL_STA);
1671
1672 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1673
1674 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1675// DO_OK_FAIL_REQUEST(cmd_save_config);
1676
qs.xiong7a105ce2022-03-02 09:43:11 -05001677 return 0;
1678
1679}
1680
you.chen35020192022-05-06 11:30:57 +08001681static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001682{
you.chen35020192022-05-06 11:30:57 +08001683 char lynq_disable_cmd[128]={0};
1684 char lynq_select_cmd[128]={0};
1685
1686 CHECK_WPA_CTRL(CTRL_STA);
1687
1688 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001689 if (start_flag != 0)
1690 {
1691 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1692 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1693 }
1694 else
1695 {
1696 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1697 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1698 }
you.chen35020192022-05-06 11:30:57 +08001699 DO_OK_FAIL_REQUEST(cmd_save_config);
1700 }
1701
1702 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001703 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001704 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1705 }
1706 else {
1707 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1708 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1709 }
1710
1711 return 0;
1712}
1713
1714int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1715{
1716 CHECK_IDX(idx, CTRL_STA);
1717
you.chen6c2dd9c2022-05-16 17:55:28 +08001718 curr_status_info curr_state;
1719 ap_info_s ap_info;
1720 curr_state.ap = &ap_info;
1721 curr_state.state = NULL;
1722
1723 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
you.chend2fef3f2023-02-13 10:50:35 +08001724 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08001725 return 0;
1726 }
1727
1728 return -1;
you.chen35020192022-05-06 11:30:57 +08001729}
1730
1731int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1732{
you.chen9ac66392022-08-06 17:01:16 +08001733 scan_info_s *scan_list = NULL;
1734 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001735 int scan_len=0;
1736 int save_len=0;
1737 int best_index = -1;
1738 int best_scan_index = -1;
1739 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001740 int i, j, ret;
1741
1742 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001743
1744 CHECK_IDX(idx, CTRL_STA);
1745 if (info == NULL) {
1746 return -1;
1747 }
1748
1749 curr_status_info curr_state;
1750 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001751 char status[64];
you.chen35020192022-05-06 11:30:57 +08001752
you.chen9ac66392022-08-06 17:01:16 +08001753 memset(&ap_info, 0, sizeof (ap_info));
1754 memset(status, 0, sizeof (status));
1755
1756 curr_state.ap = &ap_info;
1757 curr_state.state = status;
1758
1759 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001760 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001761 if (strcmp(status, STATE_COMPLETED) == 0)
1762 {
1763 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1764 }
1765 else
1766 {
1767 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1768 }
you.chen35020192022-05-06 11:30:57 +08001769 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001770 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001771 return 0;
1772 }
1773
you.chen9ac66392022-08-06 17:01:16 +08001774 lynq_wifi_sta_start_scan(idx);
1775
you.chen35020192022-05-06 11:30:57 +08001776 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001777 if (NULL != scan_list)
1778 {
1779 free(scan_list);
1780 }
you.chen35020192022-05-06 11:30:57 +08001781 return -1;
1782 }
1783
1784 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001785 if (NULL != scan_list)
1786 {
1787 free(scan_list);
1788 }
1789 if (NULL != save_list)
1790 {
1791 free(save_list);
1792 }
you.chen35020192022-05-06 11:30:57 +08001793 return -1;
1794 }
1795
1796 for (i=0; i < save_len; i++) {
1797 for (j=0; j < scan_len; j++) {
1798 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1799 && save_list[i].base_info.auth == scan_list[j].auth) {
1800 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001801 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001802 best_rssi = scan_list[j].rssi;
1803 }
1804 else if (best_rssi > scan_list[j].rssi) {
1805 best_index = i;
1806 best_scan_index = j;
1807 best_rssi = scan_list[j].rssi;
1808 }
you.chend2fef3f2023-02-13 10:50:35 +08001809 strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac));
you.chen35020192022-05-06 11:30:57 +08001810 break;
1811 }
1812 }
1813 }
1814
1815 if (best_index >= 0) {
1816 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08001817 inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip));
you.chen35020192022-05-06 11:30:57 +08001818 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1819 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001820 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001821 }
1822
you.chen9ac66392022-08-06 17:01:16 +08001823 if (NULL != scan_list)
1824 {
1825 free(scan_list);
1826 }
1827 if (NULL != save_list)
1828 {
1829 free(save_list);
1830 }
1831
1832 return ret;
you.chen35020192022-05-06 11:30:57 +08001833}
1834
1835static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1836{
1837 char lynq_auth_cmd[64]={0};
1838 char lynq_ket_mgmt_cmd[64]={0};
1839 char lynq_pairwise_cmd[64]={0};
1840 char lynq_psk_cmd[64]={0};
1841
1842 CHECK_WPA_CTRL(CTRL_STA);
1843
qs.xiong1af5daf2022-03-14 09:12:12 -04001844 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001845 case LYNQ_WIFI_AUTH_OPEN:
1846 {
1847 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001848
you.chen35020192022-05-06 11:30:57 +08001849 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1850// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001851 break;
1852 }
you.chen35020192022-05-06 11:30:57 +08001853 case LYNQ_WIFI_AUTH_WPA_PSK:
1854 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001855 {
you.chen35020192022-05-06 11:30:57 +08001856 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1857 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1858 }
1859 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001860 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001861 }
1862 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1863 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001864
you.chen35020192022-05-06 11:30:57 +08001865 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1866 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1867 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001868
you.chen35020192022-05-06 11:30:57 +08001869 if (password != NULL) {
1870 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1871 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1872 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001873
you.chen35020192022-05-06 11:30:57 +08001874// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001875 break;
you.chen35020192022-05-06 11:30:57 +08001876 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001877 default:
1878 return -1;
you.chen35020192022-05-06 11:30:57 +08001879 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001880
qs.xiongf1b525b2022-03-31 00:58:23 -04001881 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001882}
qs.xiong7a105ce2022-03-02 09:43:11 -05001883
you.chen35020192022-05-06 11:30:57 +08001884static int inner_get_curr_net_no(int interface) {
1885 curr_status_info curr_state;
1886 curr_state.ap = NULL;
1887 curr_state.state = NULL;
1888
1889 if (0 != inner_get_status_info(interface, &curr_state)) {
1890 return -1;
1891 }
1892
1893 return curr_state.net_no;
1894}
1895
1896int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001897{
you.chen35020192022-05-06 11:30:57 +08001898 int net_no;
1899 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001900
you.chen35020192022-05-06 11:30:57 +08001901 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001902
you.chen35020192022-05-06 11:30:57 +08001903 if (net_no < 0) {
1904 return -1;
1905 }
1906
1907 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001908}
1909
you.chen35020192022-05-06 11:30:57 +08001910int 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 -05001911{
you.chen35020192022-05-06 11:30:57 +08001912 int count, net_no, index;
1913 int net_no_list[128];
1914 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001915
you.chen35020192022-05-06 11:30:57 +08001916 if (ssid == NULL || *ssid == '\0') {
1917 printf("bad ssid\n");
1918 return -1;
1919 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001920
you.chen35020192022-05-06 11:30:57 +08001921 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1922 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1923 printf("bad password\n");
1924 return -1;
1925 }
1926 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001927
you.chen35020192022-05-06 11:30:57 +08001928 CHECK_IDX(idx, CTRL_STA);
1929
1930 net_no = -1;
1931 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1932
1933 for (index=0; index < count; index++) {
1934 net_auth = -1;
1935 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1936 net_no = net_no_list[index];
1937 break;
1938 }
1939 }
1940
1941 if (net_no < 0) {
1942 net_no = lynq_add_network(CTRL_STA);
1943 if (net_no == -1) {
1944 return -1;
1945 }
1946
1947 printf("net no is %d\n", net_no);
1948 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1949 return -1;
1950 }
1951 }
1952
1953 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1954 return -1;
1955 }
1956
1957 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001958}
1959
you.chen35020192022-05-06 11:30:57 +08001960int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001961{
you.chen35020192022-05-06 11:30:57 +08001962 ap_info_s ap;
1963 curr_status_info curr_state;
1964 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001965
you.chen35020192022-05-06 11:30:57 +08001966 if (ssid == NULL || *ssid == '\0') {
1967 return -1;
1968 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001969
you.chen35020192022-05-06 11:30:57 +08001970 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001971
you.chen35020192022-05-06 11:30:57 +08001972 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001973 curr_state.state = NULL;
1974
you.chen35020192022-05-06 11:30:57 +08001975 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1976 return 0;
1977 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001978
you.chen35020192022-05-06 11:30:57 +08001979 if (strcmp(ap.ap_ssid, ssid) != 0) {
1980 return 0;
1981 }
1982
1983 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001984}
qs.xiong97fa59b2022-04-07 05:41:29 -04001985
you.chena6cd55a2022-05-08 12:20:18 +08001986int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1987{
qs.xiongad2f89d2023-01-18 13:17:41 +08001988// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
1989// const char *lynq_reconnect_cmd = "RECONNECT";
1990 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
1991 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
1992// const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all";
qs.xiong7a105ce2022-03-02 09:43:11 -05001993
you.chen35020192022-05-06 11:30:57 +08001994 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001995 CHECK_WPA_CTRL(CTRL_STA);
1996
1997 system("connmanctl enable wifi");
1998
qs.xiongad2f89d2023-01-18 13:17:41 +08001999 if (system("ifconfig | grep -q wlan0") != 0)
2000 {
you.chen35020192022-05-06 11:30:57 +08002001 return -1;
2002 }
qs.xiong9c99fa92022-03-15 08:03:26 -04002003
qs.xiongad2f89d2023-01-18 13:17:41 +08002004// DO_OK_FAIL_REQUEST(cmd_remove_all);
2005// system(lynq_first_sta_cmd);
2006// system(lynq_reconfigure_cmd);
2007// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
2008 system(lynq_enable_sta_cmd);
2009 system(lynq_reconnect_cmd);
2010// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08002011 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002012}
2013
you.chen35020192022-05-06 11:30:57 +08002014int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04002015{
qs.xiongad2f89d2023-01-18 13:17:41 +08002016// char lynq_disable_network_cmd[MAX_CMD];
2017// curr_status_info curr_state;
2018// ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08002019
qs.xiongad2f89d2023-01-18 13:17:41 +08002020 const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08002021 CHECK_IDX(idx, CTRL_STA);
2022 CHECK_WPA_CTRL(CTRL_STA);
2023
qs.xiongad2f89d2023-01-18 13:17:41 +08002024// curr_state.ap = &ap_info;
2025// curr_state.state = NULL;
you.chena6cd55a2022-05-08 12:20:18 +08002026
qs.xiongad2f89d2023-01-18 13:17:41 +08002027// if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
2028// return 0;
2029// }
you.chena6cd55a2022-05-08 12:20:18 +08002030
qs.xiongad2f89d2023-01-18 13:17:41 +08002031// sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
2032// DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
2033 system(lynq_disable_sta_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08002034 DO_OK_FAIL_REQUEST(cmd_save_config);
2035
2036 return 0;
2037// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04002038}
qs.xiong7a105ce2022-03-02 09:43:11 -05002039
you.chen35020192022-05-06 11:30:57 +08002040//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
2041// int i, count;
2042// char *p;
2043// const char * FLAG_SSID = "ssid=";
2044// const char * FLAG_SBSID = "bssid=";
2045// const char * FLAG_KEY_MGMT = "key_mgmt=";
2046// const char * FLAG_FREQ = "freq=";
2047// char lynq_sta_cmd[MAX_CMD];
2048// char *split_lines[128] = {0};
2049
2050// CHECK_WPA_CTRL(CTRL_AP);
2051
2052// sprintf(lynq_sta_cmd, "STA %s", bssid);
2053
2054// DO_REQUEST(lynq_sta_cmd);
2055
2056// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2057
2058// for(i=0; i < count; i++) {
2059// p = strstr(split_lines[i], FLAG_SSID);
2060// if (p != NULL) {
2061// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
2062// continue;
2063// }
2064// }
2065
2066// lynq_get_interface_ip(idx, ap->ap_ip);
2067// lynq_ap_password_set(idx, ap->psw);
2068
2069// return 0;
2070//}
2071
2072static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
2073 curr_status_info curr_state;
2074 curr_state.ap = ap;
2075 curr_state.state = NULL;
2076 return inner_get_status_info(interface, &curr_state);
2077}
2078
2079int 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 -04002080{
you.chend2fef3f2023-02-13 10:50:35 +08002081 int index, line_count;
2082 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08002083 const char *lynq_first_sta_cmd = "STA-FIRST";
2084 char lynq_next_sta_cmd[MAX_CMD];
2085 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08002086 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002087
you.chen35020192022-05-06 11:30:57 +08002088 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002089
you.chen35020192022-05-06 11:30:57 +08002090 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002091
you.chen35020192022-05-06 11:30:57 +08002092// ap_info_s * tmp_ap;
2093// device_info_s * tmp_list;
2094 if (ap == NULL || list == NULL || len == NULL) {
2095 printf("bad input param");
2096 return -1;
2097 }
2098
2099// ap = &tmp_ap;
2100// list = &tmp_list;
2101 *ap = malloc(sizeof (ap_info_s));
2102
2103 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
2104 return -1;
2105 }
2106
2107 lynq_get_interface_ip(idx, (*ap)->ap_ip);
2108 lynq_ap_password_get(idx, (*ap)->psw);
2109
you.chen35020192022-05-06 11:30:57 +08002110 DO_REQUEST(lynq_first_sta_cmd);
2111
2112 index = 0;
2113 while (reply_len > 0) {
2114 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2115 break;
2116 }
2117 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2118 bssid[index] = malloc(strlen(split_lines[0]) + 1);
2119 strcpy(bssid[index], split_lines[0]);
2120 index++;
2121 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
2122 reply_len = MAX_RET;
2123 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08002124 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 +08002125 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
2126 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08002127 break;
2128 }
2129 }
2130
2131 *len = index;
2132
2133 *list = malloc(sizeof(device_info_s) * (*len));
2134 for (index=0; index < *len; index++) {
you.chend2fef3f2023-02-13 10:50:35 +08002135 dev_info = &(*list)[index];
2136 memset(dev_info, 0, sizeof(device_info_s));
2137 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
2138 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
2139 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
2140 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08002141 free(bssid[index]);
2142 }
2143
2144 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002145}
2146
you.chen35020192022-05-06 11:30:57 +08002147int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002148{
you.chen35020192022-05-06 11:30:57 +08002149 int i, count, index, count_words;
2150 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2151 char *split_lines[128] = {0};
2152 char *split_words[128] = {0};
2153 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002154
you.chen35020192022-05-06 11:30:57 +08002155 if (list == NULL || len == NULL) {
2156 return -1;
2157 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002158
you.chen9ac66392022-08-06 17:01:16 +08002159 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2160 {
2161 usleep(100 * 1000);
2162 }
2163
you.chen35020192022-05-06 11:30:57 +08002164 CHECK_IDX(idx, CTRL_STA);
2165
2166 CHECK_WPA_CTRL(CTRL_STA);
2167
2168 DO_REQUEST(lynq_scan_result_cmd);
2169
2170 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2171 *len = count - 1;
2172 *list = malloc(sizeof (scan_info_s) * *len);
2173
2174 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2175 for (index=0; index <count_words; index++) {
2176 printf("----header: %s\n", split_words[index]);
2177 }
2178
2179 for(index = 1;index < count; index++) {
2180 printf("---- %s\n",split_lines[index]);
2181 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2182 if (count_words < 4)
2183 continue;
2184 printf("count: %d, %s\n", count_words, split_words[0]);
2185 //bssid / frequency / signal level / flags / ssid
2186 p = (*list) + index - 1;
2187 strcpy(p->mac, split_words[0]);
2188 p->band = convert_band_from_freq(atoi(split_words[1]));
2189 p->rssi = -1 * atoi( split_words[2]);
2190 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chend2fef3f2023-02-13 10:50:35 +08002191 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
you.chen35020192022-05-06 11:30:57 +08002192 }
2193
2194 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002195}
qs.xiong97fa59b2022-04-07 05:41:29 -04002196
you.chen35020192022-05-06 11:30:57 +08002197int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2198{
2199 int count, net_no, index;
2200 int net_no_list[128];
2201 lynq_wifi_auth_s net_auth;
2202 char lynq_remove_cmd[MAX_CMD];
2203
2204 if (ssid == NULL || *ssid == '\0') {
2205 printf("bad ssid\n");
2206 return -1;
2207 }
2208
2209 CHECK_IDX(idx, CTRL_STA);
2210
2211 CHECK_WPA_CTRL(CTRL_STA);
2212
2213 net_no = -1;
2214 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2215
2216 for (index=0; index < count; index++) {
2217 net_auth = -1;
2218 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2219 net_no = net_no_list[index];
2220 break;
2221 }
2222 }
2223
2224 if (net_no < 0) {
2225 return 0;
2226 }
2227
2228 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2229
2230 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2231 DO_OK_FAIL_REQUEST(cmd_save_config);
2232
2233 return 0;
2234}
2235
2236int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2237{
you.chend2fef3f2023-02-13 10:50:35 +08002238 int count, index;
you.chen35020192022-05-06 11:30:57 +08002239 int net_no_list[128];
2240 char freq[16];
2241
2242 if (list == NULL || len == NULL) {
2243 printf("bad param\n");
2244 return -1;
2245 }
2246
2247 CHECK_IDX(idx, CTRL_STA);
2248
2249// CHECK_WPA_CTRL(CTRL_STA);
2250
2251 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2252 printf("count is %d\n", count);
2253
2254 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002255 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002256 *len = count;
2257
2258 for (index=0; index < count; index++) {
2259 printf("to get ssid %d\n", index);
2260 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002261 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002262 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002263 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2264 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2265 }
2266 else {
2267 (*list)[index].base_info.band = -1;
2268 }
2269
you.chen755332b2022-08-06 16:59:10 +08002270 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002271 }
2272
2273 return 0;
2274}
2275
2276int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2277{
2278 const char *lynq_scan_cmd = "SCAN";
2279
2280 CHECK_IDX(idx, CTRL_STA);
2281
2282 CHECK_WPA_CTRL(CTRL_STA);
2283
you.chen9ac66392022-08-06 17:01:16 +08002284 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08002285 DO_REQUEST(lynq_scan_cmd);
2286 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 ) {
2287 return 0;
2288 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) {
2289 g_sta_scan_finish_flag = 1;
2290 return -1;
2291 }
you.chen35020192022-05-06 11:30:57 +08002292
2293 return 0;
2294}
2295
2296int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2297 if (cb == NULL) {
2298 return -1;
2299 }
2300
2301 g_ap_callback_priv = priv;
2302 g_ap_callback_func = cb;
2303
2304 return 0;
2305}
2306
2307int lynq_unreg_ap_event_callback(void * priv) {
2308 if (g_ap_callback_priv == priv) {
2309 g_ap_callback_func = NULL;
2310 g_ap_callback_priv = NULL;
2311 return 0;
2312 }
2313 return -1;
2314}
2315
2316int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2317 if (cb == NULL) {
2318 return -1;
2319 }
2320
2321 g_sta_callback_priv = priv;
2322 g_sta_callback_func = cb;
2323
2324 return 0;
2325}
2326
2327int lynq_unreg_sta_event_callback(void * priv) {
2328 if (g_sta_callback_priv == priv) {
2329 g_sta_callback_func = NULL;
2330 g_sta_callback_priv = NULL;
2331 return 0;
2332 }
2333 return -1;
2334}
2335
2336
2337static int inner_get_status_info_state (int interface, char *state) {
2338 curr_status_info curr_state;
2339 curr_state.ap = NULL;
2340 curr_state.state = state;
2341 return inner_get_status_info(interface, &curr_state);
2342}
2343
2344int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2345{
2346 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002347 CHECK_IDX(idx, CTRL_AP);
2348
2349 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2350 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2351 return 0;
2352 }
2353
2354 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2355 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2356 }
2357 else {
2358 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2359 }
2360
2361 return 0;
2362}
2363
2364int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2365 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002366 CHECK_IDX(idx, CTRL_STA);
2367
2368 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2369 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2370 return 0;
2371 }
2372
2373 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2374 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2375 }
2376 else {
2377 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2378 }
2379
2380 return 0;
2381}
2382
2383int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2384// CHECK_IDX(idx, CTRL_AP);
2385// int ret = 0;
2386// size_t reply_len = MAX_RET;
2387// char cmd_reply[MAX_RET]={0};
2388// const char * cmd_str = "GET country";
2389// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2390// do{
2391// if (NULL == s_lynq_wpa_ctrl) {
2392// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2393// if (NULL == s_lynq_wpa_ctrl ) {
2394// printf("wpa_ctrl_open fail\n");
2395// return -1;
2396// }
2397// }
2398// }while(0);
2399
2400// do {
2401// reply_len = MAX_RET;
2402// cmd_reply[0] = '\0';
2403// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08002404// 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 +08002405// if (ret != 0) {
2406// printf("call ##cmd_str fail %d\n", ret);
2407// return ret;
2408// }
2409// cmd_reply[reply_len+1] = '\0';
2410// printf("cmd replay [ %s ]\n", cmd_reply);
2411// }while(0);
2412
2413 FILE *fp;
2414 size_t i = 0;
2415 char lynq_cmd_ret[MAX_RET]={0};
2416
2417// CHECK_IDX(idx, CTRL_AP);
2418
2419 if((fp=popen("wl country","r"))==NULL)
2420 {
2421 perror("popen error!");
2422 return -1;
2423 }
2424 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2425 {
2426 perror("fread fail!");
2427 return -1;
2428 }
2429
2430 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2431 if (lynq_cmd_ret[i] == ' ') {
2432 lynq_cmd_ret[i] = '\0';
2433 break;
2434 }
2435 }
2436
2437 strcpy(country_code,lynq_cmd_ret);
2438 printf("---country code %s\n", country_code);
2439
2440 int ret=pclose(fp);
2441 if(ret==-1)
2442 {
2443 perror("close file faild");
2444 }
2445
2446 return 0;
2447}
2448
2449int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2450// const char * cmd_str = "GET country";
2451// CHECK_IDX(idx, CTRL_AP);
2452// CHECK_WPA_CTRL(CTRL_STA);
2453
2454// DO_REQUEST(cmd_str);
2455// printf("result %s\n", cmd_reply);
2456
2457 if (country_code == NULL || *country_code == '\0') {
2458 printf("bad country code\n");
2459 return -1;
2460 }
2461
2462 char lynq_country_cmd[MAX_CMD];
2463 sprintf(lynq_country_cmd, "wl country %s", country_code);
2464 if (system(lynq_country_cmd) == 0) {
2465 return 0;
2466 }
2467
2468 return -1;
2469}
2470
2471int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2472{
2473 if (mac == NULL) {
2474 return -1;
2475 }
2476
2477 CHECK_IDX(idx, CTRL_STA);
2478 ap_info_s ap;
2479 ap.ap_mac[0] = '\0';
2480
2481 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2482 return -1;
2483 }
2484 strcpy(mac, ap.ap_mac);
2485
2486 return 0;
2487}
2488
2489int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2490{
you.chen9ac66392022-08-06 17:01:16 +08002491 struct ifaddrs *ifaddr_header, *ifaddr;
2492 struct in_addr * ifa;
2493 const char * ifaName = "wlan0";
2494 if (ip == NULL)
2495 {
you.chenf58b3c92022-06-21 16:53:48 +08002496 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002497 }
you.chenf58b3c92022-06-21 16:53:48 +08002498
you.chen9ac66392022-08-06 17:01:16 +08002499 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002500 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002501 }
2502 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002503 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002504 }
you.chen35020192022-05-06 11:30:57 +08002505
you.chen9ac66392022-08-06 17:01:16 +08002506 if (getifaddrs(&ifaddr_header) == -1)
2507 {
you.chen35020192022-05-06 11:30:57 +08002508 perror("getifaddrs");
2509 return -1;
2510 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002511 }
you.chen35020192022-05-06 11:30:57 +08002512
2513
you.chen9ac66392022-08-06 17:01:16 +08002514 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2515 {
2516 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002517 continue;
you.chen9ac66392022-08-06 17:01:16 +08002518 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2519 {
2520 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2521 {
2522 // is a valid IP4 Address
2523 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2524 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2525 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2526 freeifaddrs(ifaddr_header);
2527 printf("ip %s\n", ip);
2528 return 0;
2529 }
2530 }
2531 }
qs.xiongc4f007c2023-02-08 18:16:58 +08002532 freeifaddrs(ifaddr_header);
you.chen9ac66392022-08-06 17:01:16 +08002533 return -1;
you.chen35020192022-05-06 11:30:57 +08002534}
2535
2536int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2537{
2538 int count;
2539 size_t i;
2540 char *split_words[128] = {0};
2541 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2542
2543 CHECK_WPA_CTRL(idx);
2544
2545 DO_REQUEST(lynq_get_mac_cmd);
2546
2547 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2548 return -1;
2549 }
2550
2551 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2552
2553 if (count < 2) {
2554 return -1;
2555 }
2556
2557 for (i=0; i < strlen(split_words[1]); i++ ) {
2558 if (split_words[1][i] != ' ') {
2559 break;
2560 }
2561 }
2562
2563 strcpy(mac, split_words[1] + i);
2564
2565 return 0;
2566}
2567
2568int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2569{
2570// int count;
2571// char *split_words[128] = {0};
2572// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2573
2574// if (rssi == NULL) {
2575// return -1;
2576// }
2577
2578// CHECK_IDX(idx, CTRL_STA);
2579
2580// CHECK_WPA_CTRL(CTRL_STA);
2581
2582// DO_REQUEST(lynq_get_rssi_cmd);
2583
2584// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2585// return -1;
2586// }
2587
2588// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2589
2590// if (count < 2) {
2591// return -1;
2592// }
2593
2594// *rssi = atoi(split_words[1]) * -1;
2595
2596 FILE *fp;
2597 size_t i = 0;
2598 char lynq_cmd_ret[MAX_RET]={0};
2599
2600// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002601/*******change other cmd to get rssi*******
2602 *
2603 *wl rssi ---> wl -i wlan0 rssi
2604 *
2605 ***** change by qs.xiong 20221011*******/
2606 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002607 {
2608 perror("popen error!");
2609 return -1;
2610 }
2611 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2612 {
2613 perror("fread fail!");
2614 return -1;
2615 }
you.chen9f17e4d2022-06-06 17:18:18 +08002616 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002617/****** if got rssi is 0,means sta didn't connected any device****/
2618 if(*rssi == 0)
2619 {
2620 printf("sta didn't connected any ap device,please check connection\n");
2621 }
you.chen35020192022-05-06 11:30:57 +08002622
2623 return 0;
2624}
2625
2626int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2627{
2628 if (band == NULL) {
2629 return -1;
2630 }
2631
2632 CHECK_IDX(idx, CTRL_STA);
2633 ap_info_s ap;
2634 ap.band = -1;
2635
2636 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2637 return -1;
2638 }
2639 *band = ap.band;
2640
2641 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002642}
you.chenf58b3c92022-06-21 16:53:48 +08002643
2644int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2645{
qs.xionge4cbf1c2023-02-28 18:22:49 +08002646 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08002647
2648 if (ip == NULL)
2649 {
2650 printf("invalid param");
2651 return -1;
2652 }
2653
2654 CHECK_IDX(idx, CTRL_STA);
2655
qs.xionge4cbf1c2023-02-28 18:22:49 +08002656 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08002657 {
2658 return -1;
2659 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08002660
2661 return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
you.chenf58b3c92022-06-21 16:53:48 +08002662}
2663
qs.xiong026c5c72022-10-17 11:15:45 +08002664int lynq_ap_connect_num(int sta_number)
2665{
2666 char lynq_limit_cmd[32]={0};
2667 int ret;
2668 if((sta_number < 1 ) && (sta_number > 15)){
2669 printf("sta_number: not in range\n",sta_number);
2670 return -1;
2671 }
2672 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2673 ret = system(lynq_limit_cmd);
2674 if(ret != 0){
2675 printf("cmd of limit ap devices number error\n");
2676 }
2677 return 0;
2678}
you.chenf58b3c92022-06-21 16:53:48 +08002679
qs.xiong77905552022-10-17 11:19:57 +08002680int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2681{
2682
2683 char lynq_wifi_acs_cmd[128]={0};
2684 char lynq_cmd_mode[128]={0};
2685 char lynq_cmd_slect[128]={0};
2686
2687 if((acs_mode != 2) && (acs_mode != 5)){
2688 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2689 }
2690
2691 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2692 return -1;
2693 }
2694
2695 CHECK_IDX(idx, CTRL_AP);
2696
2697 CHECK_WPA_CTRL(CTRL_AP);
2698
2699 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2700 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2701 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2702
2703 DO_OK_FAIL_REQUEST(cmd_disconnect);
2704 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2705 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2706 DO_OK_FAIL_REQUEST(cmd_save_config);
2707 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2708
2709 return 0;
2710}
you.chen0f5c6432022-11-07 18:31:14 +08002711//you.chen add for tv-box start
2712static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2713 FILE *fp;
2714 //printf("to exec cmd:%s\n", str_cmd);
2715 if((fp=popen(str_cmd,"r"))==NULL)
2716 {
2717 perror("popen error!");
2718 return -1;
2719 }
2720 if((fread(str_cmd_ret,max_len,1,fp))<0)
2721 {
2722 perror("fread fail!");
2723 fclose(fp);
2724 return -1;
2725 }
2726 fclose(fp);
2727 return 0;
2728}
2729
2730static int get_netmask_length(const char* mask)
2731{
2732 int masklen=0, i=0;
2733 int netmask=0;
2734
2735 if(mask == NULL)
2736 {
2737 return 0;
2738 }
2739
2740 struct in_addr ip_addr;
2741 if( inet_aton(mask, &ip_addr) )
2742 {
2743 netmask = ntohl(ip_addr.s_addr);
2744 }else{
2745 netmask = 0;
2746 return 0;
2747 }
2748
2749 while(0 == (netmask & 0x01) && i<32)
2750 {
2751 i++;
2752 netmask = netmask>>1;
2753 }
2754 masklen = 32-i;
2755 return masklen;
2756}
2757
2758static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2759 int mask_len;
2760 char *p;
2761 char tmp[64] = {0};
2762 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2763 return -1;
2764 p = strstr(str_cmd_ret, "Mask:");
2765 if (p == NULL)
2766 return -1;
2767 mask_len = get_netmask_length(p + 5);
2768 if (mask_len == 0)
2769 return -1;
2770 p = strstr(str_cmd_ret, "inet addr:");
2771 if (p == NULL)
2772 return -1;
2773 strcpy(tmp, p + 10);
2774 p = strstr(tmp, " ");
2775 if (p != NULL)
2776 *p = '\0';
2777 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2778 return 0;
2779}
2780
2781static void GBWWatchThreadProc() {
2782 int i,n, nloop, nmax, ncheckcount, nidlecount;
2783 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2784 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2785 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2786 char *results[16] = {0};
2787 char str_cmd[256] = {0};
2788 char str_cmd_ret[128] = {0};
2789 char dest_ip[32] = {0};
2790 lastAP1Bytes = lastAP2Bytes = 0;
2791 lastAP1Drop = lastAP2Drop = 0;
2792 lastAP1Speed = lastAP2Speed = 0;
2793 setAP1Speed = 50;
2794 setAP2Speed = 80;
2795 nloop = 0;
2796 nmax = 6;
2797 ncheckcount = nidlecount = 0;
2798
2799 printf("------gbw thread run\n");
2800 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2801 while (dest_ip[0] == '\0') {
2802 sleep(1);
2803 str_cmd_ret[0] = '\0';
2804 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2805 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2806 if (str_cmd_ret[n] == '\n'){
2807 str_cmd_ret[n] = '\0';
2808 break;
2809 }
2810 }
2811 if (str_cmd_ret[0] != '\0')
2812 {
2813 strcpy(dest_ip, str_cmd_ret);
2814 }
2815 }
2816
2817 system("tc qdisc del dev tether root > /dev/null 2>&1");
2818 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2819 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2820 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2821 {
2822 printf("not get tether info\n");
2823 return;
2824 }
2825 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret);
2826 system(str_cmd);
2827 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2828 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2829 //printf("----cmd:%s\n", str_cmd);
2830 system(str_cmd);
2831
2832 while (1) {
2833 sleep(1);
2834 memset(str_cmd, 0, sizeof(str_cmd));
2835 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2836 continue;
2837 //printf("ap1 --- %s\n", str_cmd);
2838 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2839 if (n > 9) {
2840 if (strcmp(results[1], "Sent") == 0) {
2841 currAP1Bytes = atoll(results[2]);
2842 }
2843 if (strcmp(results[6], "(dropped") == 0) {
2844 currAP1Drop = atoi(results[7]);
2845 }
2846 }
2847
2848 memset(str_cmd, 0, sizeof(str_cmd));
2849 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2850 continue;
2851 //printf("ap2 --- %s\n", str_cmd);
2852 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2853 if (n > 9) {
2854 if (strcmp(results[1], "Sent") == 0) {
2855 currAP2Bytes = atoll(results[2]);
2856 }
2857 if (strcmp(results[6], "(dropped") == 0) {
2858 currAP2Drop = atoi(results[7]);
2859 }
2860 }
2861
2862 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2863 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2864 lastAP1Bytes = currAP1Bytes;
2865 lastAP2Bytes = currAP2Bytes;
2866 continue;
2867 }
2868
2869 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2870 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2871 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2872 lastAP1Speed = currAP1Speed;
2873 lastAP2Speed = currAP2Speed;
2874 lastAP1Bytes = currAP1Bytes;
2875 lastAP2Bytes = currAP2Bytes;
2876
2877 currSetAP1Speed = setAP1Speed;
2878 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2879 ncheckcount++;
2880 if (ncheckcount > 3) {
2881 ncheckcount = 0;
2882 currSetAP1Speed = 5;
2883 }
2884 }
2885 else {
2886 ncheckcount = 0;
2887 if (currAP1Speed < 5)
2888 nidlecount++;
2889 else
2890 nidlecount = 0;
2891
2892 }
2893
2894 if (nidlecount > 60 ){
2895 currSetAP1Speed = 50;
2896 }
2897
2898 if (currSetAP1Speed != setAP1Speed) {
2899 setAP1Speed = currSetAP1Speed;
2900 sprintf(str_cmd, "tc class replace dev tether parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4));
2901 //printf("------***change speed: %s\n", str_cmd);
2902 system(str_cmd);
2903 }
2904 }
2905}
2906
2907int enableGBW(const char* mac) {
2908 int i,len;
2909 char get_ipaddr_cmd[128]={0};
2910 ap_info_s *ap;
2911 device_info_s * list;
2912
2913 if (mac == NULL || g_gbw_enabled == 1)
2914 return -1;
2915 len = strlen(mac);
2916 g_gbw_mac = malloc(len + 1);
2917 for(i=0;i<len;i++) {
2918 if (mac[i] >= 'A' && mac[i] <= 'Z')
2919 {
2920 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2921 }
2922 else
2923 g_gbw_mac[i] = mac[i];
2924 }
2925 g_gbw_mac[i] = '\0';
2926 g_gbw_enabled = 1;
2927
2928 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2929 if (system(get_ipaddr_cmd) == 0) {
2930 //startGBW();
2931 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2932 for (i=0;i<len;i++) {
2933 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2934 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2935 startGBW();
2936 }
2937 free(ap);
2938 free(list);
2939 }
2940 }
2941 return 0;
2942}
2943
2944int disableGBW() {
2945 stopGBW();
2946 free(g_gbw_mac);
2947 g_gbw_mac = NULL;
2948 g_gbw_enabled = 1;
2949 return 0;
2950}
2951
2952static int startGBW() {
2953 if (g_gbw_watcher_pid != 0) {
2954 stopGBW();
2955 }
2956 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2957}
2958
2959static int stopGBW() {
2960 void* retval;
2961 pthread_cancel(g_gbw_watcher_pid);
2962 pthread_join(g_gbw_watcher_pid, &retval);
2963 g_gbw_watcher_pid = 0;
2964 system("tc qdisc del dev tether root");
2965}
2966//you.chen add for tv-box end