blob: 3d05de0649eaa3694c125d46ad8bb758ce1506f0 [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.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen0c9bee22023-10-25 13:03:14 +080057volatile int g_sta_fake_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
qs.xiong6ad0e822023-11-24 17:53:30 +080070
you.chen35020192022-05-06 11:30:57 +080071//const char * CTRL_PATH="/var/run/wpa_supplicant";
72const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
73//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
74const char * cmd_list_networks = "LIST_NETWORKS";
75const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080076const char * cmd_disconnect = "DISCONNECT";
77const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080078const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080079const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080080const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080081const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080082
you.chenf711c8a2023-04-13 13:49:45 +080083const char * cmd_ping = "PING";
84const char * rsp_pong = "PONG";
85const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
86const int MAX_IDLE_COUNT = 600; // 60s
87
you.chenc9928582023-04-24 15:39:37 +080088const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
89const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
90const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
91const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
92const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
93
94static char s_ap_iterface_name[64] = {0};
95
you.chend2fef3f2023-02-13 10:50:35 +080096struct local_wpa_ctrl{
97 struct wpa_ctrl *ctrl;
98 pthread_mutex_t mutex;
99};
100
qs.xiong09560402023-10-27 21:58:55 +0800101volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800102int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800103
you.chend2fef3f2023-02-13 10:50:35 +0800104static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800105static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
106static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiong8362e222024-03-22 11:20:56 +0800107static pthread_mutex_t s_run_cmd_func_mutex = PTHREAD_MUTEX_INITIALIZER;
108
qs.xiongfcc914b2023-07-06 21:16:20 +0800109// add for auto connect
110static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800111
112static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800113
you.chen0f5c6432022-11-07 18:31:14 +0800114//you.chen add for tv-box start
115volatile int g_gbw_enabled = 0;
116char * g_gbw_mac = NULL;
117pthread_t g_gbw_watcher_pid = 0;
118static int startGBW();
119static int stopGBW();
120//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800121
122typedef struct __curr_status_info {
123 ap_info_s *ap;
124 char * state;
125 int net_no;
126}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400127
you.chen70f377f2023-04-14 18:17:09 +0800128typedef enum {
129 INNER_STA_STATUS_INIT = 0,
130 INNER_STA_STATUS_CONNECTING,
131 INNER_STA_STATUS_ASSOCIATING,
132 INNER_STA_STATUS_ASSOCIATED,
133 INNER_STA_STATUS_CONNECTED,
134 INNER_STA_STATUS_DISCONNECTING,
135 INNER_STA_STATUS_DISCONNECTED,
136 INNER_STA_STATUS_CANCEL,
137}inner_sta_status_s;
138
139static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
140static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800141volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800142static error_number_s s_sta_error_number = -1;
143static char s_sta_current_connecting_ssid[64] = {0};
144static struct timespec s_sta_connect_timeout;
145const int MAX_CONNNECT_TIME = 15; // second
146pthread_t g_global_watcher_pid = 0;
147static int s_service_invoke_timeout_cnt=0;
148const int FAKE_MAX_INT_VALUE = 99999;
149
qs.xiong09560402023-10-27 21:58:55 +0800150static void print_disconnect_list()
151{
152 int i;
153 for( i = 0; i < g_history_disconnect_valid_num; i++ )
154 {
155 RLOGD(" list of g_history_disconnect_valid_num is %d histroy_list[%d]:%d --------- %d",g_history_disconnect_valid_num,i,g_history_disconnect_net[i],__LINE__);
156 }
157
158 return;
159}
160
161// idex ----> history_disconnect_list[x] index
162static int removeElement(int idex)
163{
164 RLOGD("into removeElement");
165 if( index < 0 )
166 {
167 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
168 return -1;
169 }
170 RLOGD("%s line: %d g_history_disconnect_net[%d]: %d end g_history_disconnect_net[%d]:%d",__func__,__LINE__,idex,g_history_disconnect_net[idex],g_history_disconnect_valid_num-1, g_history_disconnect_net[g_history_disconnect_valid_num-1]);
171 g_history_disconnect_net[idex] = g_history_disconnect_net[g_history_disconnect_valid_num-1]; //g_history_disconnect_vaild_num -1 for get last index
172 g_history_disconnect_valid_num --;
173 RLOGD("end removeElement");
174 return 0;
175}
176static int check_history_disconenct_ap_list(int val)
177{
178 print_disconnect_list();
179 RLOGD("WIFI[check_history_disconenct_ap_list]into check_history_disconenct_ap_list && input val is %d g_history_disconnect_valid_num is %d line",val,g_history_disconnect_valid_num,__LINE__);
180 int i;
181 for( i = 0; i < g_history_disconnect_valid_num; i++)
182 {
183 if( val == g_history_disconnect_net[i] )
184 {
185 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
186 RLOGD("end check_history_disconenct_ap_list && return network index");
187 return i;
188 }
189 }
190 RLOGD("end check_history_disconenct_ap_list && return fail,didn't need remove networkid %d from list g_history_disconnect_valid_num is %d line %d",val,g_history_disconnect_valid_num,__LINE__);
191 return -1;
192}
193
194
195static void lynq_sta_removeElement(int net_no)
196{
197 int ret;
198
199 ret = check_history_disconenct_ap_list(net_no);
200 if( ret == -1 )
201 {
202 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
203 return;
204 }else
205 {
206 ret = removeElement(ret);
207 if( ret == 0 )
208 {
209 RLOGD("removeElement pass %s %d",__func__,__LINE__);
210 return;
211 }
212 }
213
214 return;
215}
216
you.chen70f377f2023-04-14 18:17:09 +0800217static void notify_service_invoke_fail(int error)
218{
qs.xiong17579bb2024-03-11 19:08:06 +0800219 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen70f377f2023-04-14 18:17:09 +0800220 pthread_mutex_lock(&s_global_check_mutex);
221 if (error == -2) //timeout
222 {
223 s_service_invoke_timeout_cnt++;
224 if (s_service_invoke_timeout_cnt > 10)
225 {
226 pthread_cond_signal(&s_global_check_cond);
227 }
228 }
229 else if (error == -1)
230 {
231 // check if can connect wpa service
232 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
233 if (lynq_wpa_ctrl == NULL)
234 {
235 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
236 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800237 }else
238 {
239 wpa_ctrl_close(lynq_wpa_ctrl);
240 }
you.chen70f377f2023-04-14 18:17:09 +0800241 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
242 if (lynq_wpa_ctrl == NULL)
243 {
244 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
245 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800246 }else
247 {
248 wpa_ctrl_close(lynq_wpa_ctrl);
you.chen70f377f2023-04-14 18:17:09 +0800249 }
you.chen70f377f2023-04-14 18:17:09 +0800250 }
251
252 pthread_mutex_unlock(&s_global_check_mutex);
253}
254
you.chenc9928582023-04-24 15:39:37 +0800255static int system_call_v(const char * fmt, ...)
256{
257 char str_cmd[256] = {0};
258 va_list args;
259 va_start(args, fmt);
260 vsprintf(str_cmd, fmt, args);
261 va_end(args);
262 printf("system call----------%s\n", str_cmd);
263 return system(str_cmd);
264}
265
you.chen0df3e7e2023-05-10 15:56:26 +0800266static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
267
268static const char * inner_get_ap_interface_name()
269{
270 char * p;
271 char cmd[128]={0};
272
273 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
274 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
275 {
276 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
277 return NULL;
278 }
279 p = strchr(s_ap_iterface_name, ' ');
280 if (NULL != p)
281 {
282 *p = '\0';
283 }
284 p = strchr(s_ap_iterface_name, '\n');
285 if (NULL != p)
286 {
287 *p = '\0';
288 }
289 if (s_ap_iterface_name[0] == '\0')
290 {
291 return NULL;
292 }
293
294 return s_ap_iterface_name;
295}
296
you.chen70f377f2023-04-14 18:17:09 +0800297static void check_tether_and_notify()
298{
299 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800300 if (inner_get_ap_interface_name() == NULL || 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chen70f377f2023-04-14 18:17:09 +0800301 {
302 return;
303 }
304 pthread_mutex_lock(&s_global_check_mutex);
305 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
306 pthread_cond_signal(&s_global_check_cond);
307 pthread_mutex_unlock(&s_global_check_mutex);
308}
309
you.chend2fef3f2023-02-13 10:50:35 +0800310static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
311 char *reply, size_t *reply_len,
312 void (*msg_cb)(char *msg, size_t len))
313{
314 int ret;
315 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800316 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800317 return -1;
318 }
319 pthread_mutex_lock(&ctrl->mutex);
320 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
321 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800322 if (ret != 0)
323 {
324 notify_service_invoke_fail(ret);
325 }
you.chend2fef3f2023-02-13 10:50:35 +0800326 return ret;
327}
328
329static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
330 int repeat_cnt;
331 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
332 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800333 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800334 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
335 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
336// printf("wait enable finish\n");
337 usleep(500 * 1000);
338 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
339 }
340 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800341 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800342 goto out_addr;
343 }
344 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
345 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
346 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800347 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800348 goto out_addr;
349 }
350 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
351 }
352 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
353out_addr:
354 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
355 return lynq_wpa_ctrl;
356}
357
qs.xiong97fa59b2022-04-07 05:41:29 -0400358#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400359{\
you.chen35020192022-05-06 11:30:57 +0800360 perror((str));\
361 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400362}
363
you.chen35020192022-05-06 11:30:57 +0800364#define CHECK_IDX(idx, type) do { \
365 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
366 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800367 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800368 return -1; \
369 } \
370 }while (0)
371
372#define CHECK_WPA_CTRL(index) int ret = 0;\
373 size_t reply_len = MAX_RET; \
374 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800375 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800376 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800377 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
378 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800379 }while(0)
380
381#define DO_REQUEST(cmd_str) do { \
382 reply_len = MAX_RET;\
383 cmd_reply[0] = '\0'; \
qs.xiongb04dc852024-03-27 21:55:32 +0800384 if( strstr(cmd_str,"psk") == NULL ) \
385 { \
386 RLOGD("to call [%s]\n", cmd_str); \
387 }else \
388 { \
389 RLOGD("to call SET_NETWORK psk ********\n"); \
390 } \
you.chend2fef3f2023-02-13 10:50:35 +0800391 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 +0800392 if (ret != 0) { \
qs.xiongb04dc852024-03-27 21:55:32 +0800393 if( strstr(cmd_str,"psk") == NULL ) \
394 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800395 RLOGE("call "#cmd_str" fail %d\n", ret); \
qs.xiongb04dc852024-03-27 21:55:32 +0800396 }else \
397 { \
398 RLOGE("call get or set psk fail %d\n",ret); \
399 } \
you.chen35020192022-05-06 11:30:57 +0800400 return ret; \
401 } \
402 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800403 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800404 }while(0)
405
406#define DO_OK_FAIL_REQUEST(cmd_str) do { \
qs.xiong8362e222024-03-22 11:20:56 +0800407 pthread_mutex_lock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800408 DO_REQUEST(cmd_str); \
409 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiongb04dc852024-03-27 21:55:32 +0800410 if( strstr(cmd_str,"psk") == NULL ) \
411 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800412 RLOGE("cmd "#cmd_str" return FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800413 }else \
414 { \
415 RLOGE("cmd SET_NETWORK psk ******** return FAIL\n"); \
416 } \
qs.xiong8362e222024-03-22 11:20:56 +0800417 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800418 return -1; \
419 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiongb04dc852024-03-27 21:55:32 +0800420 if ( strstr(cmd_str,"psk") == NULL ) \
421 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800422 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800423 }else \
424 { \
425 RLOGE("cmd get or set psk return not OK|FAIL\n"); \
426 } \
qs.xiong8362e222024-03-22 11:20:56 +0800427 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800428 return -1; \
429 } \
qs.xiong8362e222024-03-22 11:20:56 +0800430 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800431 }while (0)
432
433
you.chenf711c8a2023-04-13 13:49:45 +0800434static int check_connection(struct wpa_ctrl * wpa_ctrl)
435{
436 size_t reply_len = MAX_RET;
437 char cmd_reply[MAX_RET]={0};
438 int ret;
439
440 RLOGD("check_connection [%p]", wpa_ctrl);
441 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
442
443 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
444 {
445 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chen70f377f2023-04-14 18:17:09 +0800446 if (ret != 0)
447 {
448 notify_service_invoke_fail(ret);
449 }
you.chenf711c8a2023-04-13 13:49:45 +0800450 return -1;
451 }
452
453 return 0;
454}
455
456/**
457 * @brief check_pending_msg
458 * @param lynq_wpa_ctrl
459 * @return 1 has msg, 0 no msg, -1 error
460 */
461static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
462{
463 int ret;
464
465 if (*pp_lynq_wpa_ctrl == NULL) // need connect
466 {
467 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
468 if (*pp_lynq_wpa_ctrl == NULL)
469 {
470 usleep(SLEEP_TIME_ON_IDLE);
471 return -1;
472 }
473
474 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
475 if (ret == 0) // attach success
476 {
477 *started_flag = 1;
478 }
479 else
480 {
you.chen70f377f2023-04-14 18:17:09 +0800481 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800482 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
483 *pp_lynq_wpa_ctrl = NULL;
484 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800485 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800486 usleep(SLEEP_TIME_ON_IDLE);
487 return -1;
488 }
489 }
490
491 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
492 if ( ret == 0) // no pending messages
493 {
494 usleep(SLEEP_TIME_ON_IDLE);
495 *idle_count += 1;
496 if (*idle_count > MAX_IDLE_COUNT)
497 {
498 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
499 {
500 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
501 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
502 *pp_lynq_wpa_ctrl = NULL;
503 *idle_count = 0;
504 return -1;
505 }
506 *idle_count = 0;
507 }
508 return 0;
509 }
510 else if ( ret == -1) // on error
511 {
512 RLOGE("[wifi error]sta wpa_ctrl_pending");
513 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
514 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
515 *pp_lynq_wpa_ctrl = NULL;
516 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800517 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800518 return -1;
519 }
520
521 *idle_count = 0;
522 return 1;
523}
524
you.chen6d247052023-06-01 16:39:54 +0800525static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
526{
527 pthread_mutex_lock(&s_ap_callback_mutex);
528 if (g_ap_callback_func != NULL)
529 g_ap_callback_func(g_ap_callback_priv, status);
530 pthread_mutex_unlock(&s_ap_callback_mutex);
531}
532
you.chen35020192022-05-06 11:30:57 +0800533static void APWatcherThreadProc() {
534 size_t len = MAX_RET;
535 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800536 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800537 char *ptr = NULL;
538 char mac[32] = {0};
539 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800540
you.chen6c2dd9c2022-05-16 17:55:28 +0800541 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800542 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800543
qs.xiong9fbf74e2023-03-28 13:38:22 +0800544 while (g_ap_watcher_stop_flag == 0)
545 {
you.chenf711c8a2023-04-13 13:49:45 +0800546 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
547 {
you.chen70f377f2023-04-14 18:17:09 +0800548 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
549 {
550 check_tether_and_notify();
551 }
552
you.chen35020192022-05-06 11:30:57 +0800553 continue;
554 }
you.chenf711c8a2023-04-13 13:49:45 +0800555
you.chen6c2dd9c2022-05-16 17:55:28 +0800556 memset(msg_notify, 0, MAX_RET);
557 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800558 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800559 {
you.chen35020192022-05-06 11:30:57 +0800560 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800561 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800562 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800563 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800564 {
you.chen6d247052023-06-01 16:39:54 +0800565 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800566 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
567 if( ptr != NULL)
568 {
569 ptr += strlen("AP-STA-DISCONNECTED ");
570 memcpy(mac,ptr,17);
571 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
572 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
573 system(cmd);
574 }
575
qs.xiong9fbf74e2023-03-28 13:38:22 +0800576 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800577 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800578 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
579 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800580 {
you.chen0f5c6432022-11-07 18:31:14 +0800581 stopGBW();
582 }
583 }
you.chen35020192022-05-06 11:30:57 +0800584 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800585 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800586 {
you.chen6d247052023-06-01 16:39:54 +0800587 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800588 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800589 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800590 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
591 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800592 {
you.chen0f5c6432022-11-07 18:31:14 +0800593 startGBW();
594 }
595 }
you.chen35020192022-05-06 11:30:57 +0800596 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800597 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800598 {
599 RLOGD("APWatcherThreadProc ap------> service error");
600 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
601 }
602 else
603 {
604 RLOGD("APWatcherThreadProc ap------> going on check next msg");
605 }
you.chenf711c8a2023-04-13 13:49:45 +0800606 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800607 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
608 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800609 if (lynq_wpa_ctrl != NULL)
610 {
you.chen92fd5d32022-05-25 10:09:47 +0800611 wpa_ctrl_detach(lynq_wpa_ctrl);
612 wpa_ctrl_close(lynq_wpa_ctrl);
613 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400614}
615
you.chen70f377f2023-04-14 18:17:09 +0800616static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
617{
618 char * p;
619 const char * try_associat_flag = "Trying to associate";
620 const char * associated_flag = "Associated with ";
621
622 pthread_mutex_lock(&s_global_check_mutex);
623 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
624 {
625 pthread_mutex_unlock(&s_global_check_mutex);
626 return;
627 }
628
you.chen6e724162023-10-19 19:10:01 +0800629 // youchen@2023-10-17 add for "not notify connect fail directly" begin
630 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
631 {
632 s_sta_error_number = error_num;
633 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
634 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
635 pthread_cond_signal(&s_global_check_cond);
636 pthread_mutex_unlock(&s_global_check_mutex);
637 return;
638 }
639 // youchen@2023-10-17 add for "not notify connect fail directly" end
640
you.chen70f377f2023-04-14 18:17:09 +0800641 if (state == LYNQ_WIFI_STATUS_EGNORE)
642 {
643 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
644 {
645 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
646 }
647 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
648 {
649 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
650 }
651 }
652 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
653 {
654 s_sta_error_number = error_num;
655 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
656 {
657 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800658 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800659 pthread_cond_signal(&s_global_check_cond);
660 }
661 }
662 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
663 {
664 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800665 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800666 pthread_cond_signal(&s_global_check_cond);
667 }
668 pthread_mutex_unlock(&s_global_check_mutex);
669}
670
qs.xiongb37f8c42023-09-13 21:21:58 +0800671static int lynq_split(char * str, int len, char delimiter, char * results[]);
672static inline char inner_convert_char(char in);
673static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
674static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
675
676
677
qs.xiong44fac672023-08-29 16:15:55 +0800678/*
679just tmp add for fix sta connect ap fail check ap connect info
680return 0 --->Current no sta device connect this AP
681*/
682static int lynq_connected_ap_sta_status() {
683
684 FILE *fp;
685 size_t i = 0;
686 int ret;
687 char lynq_cmd_ret[MAX_RET]={0};
688
689 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
690 {
691 perror("popen error!");
692 return -1;
693 }
694 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
695 {
696 perror("fread fail!");
697 ret=pclose(fp);
698 if(ret == -1)
699 perror("close file faild");
700 return -1;
701 }
702 if( strlen(lynq_cmd_ret) < MAC_LEN)
703 {
704 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
705 ret=pclose(fp);
706 if(ret==-1)
707 {
708 perror("close file faild");
709 }
710 return 0;
711 }else{
712 ret=pclose(fp);
713 if(ret==-1)
714 {
715 perror("close file faild");
716 }
717 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
718 return 1;
719 }
720}
721
722/*
723 just tmp add for fix sta connect ap fail; check fw status
724 return 1 ----> fw status error; need wl down/up
725*/
726static int check_current_fw_status() {
727
728 FILE *fp;
729 FILE *fp1;
730 size_t i = 0;
731 int ret;
732 char lynq_cmd_ret_2g[MAX_RET]={0};
733 char lynq_cmd_ret_5g[MAX_RET]={0};
734
735 const char * fw_status = "0x0096"; //0x0096 is normal fw status
736
737 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
738 {
739 perror("popen error!");
740 return -1;
741 }
742 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
743 {
744 perror("fread fail!");
745 if(pclose(fp) == -1)
746 perror("close fp file faild");
747 return -1;
748 }
749
750 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
751 {
752 perror("popen error!");
753 if(pclose(fp) == -1)
754 perror("clsoe fp file faild");
755 return -1;
756 }
757 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
758 {
759 perror("fread fail!");
760 if(pclose(fp1) == -1)
761 perror("clsoe fp1 file faild");
762 if(pclose(fp) == -1)
763 perror("clsoe fp file faild");
764 return -1;
765 }
766
767 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
768 {
769 ret=pclose(fp);
770 if(ret==-1)
771 {
772 perror("close fp file faild");
773 }
774 ret=pclose(fp1);
775 if(ret==-1)
776 {
777 perror("close fp1 file faild");
778 }
779 return 0;
780 }else
781 {
782 ret=pclose(fp);
783 if(ret==-1)
784 {
785 perror("close file faild");
786 }
787 if(pclose(fp1) == -1)
788 {
789 perror("clsoe file fp1 faild");
790 }
791 RLOGD("current fw status --error--");
792 return 1;
793 }
794}
795
qs.xiong1d4263a2023-09-06 10:46:23 +0800796/*
797eg: wl counters info
798sh-3.2# wl counters
799counters_version 30
800datalen 1648
801Slice_index: 0
802reinitreason_counts: 0(0) 1(0) 2(3) 3(0) 4(0) 5(0) 6(0) 7(0) 8(0) 9(0) 10(0) 11(0) 12(0) 13(0) 14(2) 15(0) 16(0) 17(0) 18(0) 19(0) 20(0) 21(0) 22(0) 23(0) 24(0) 25(0) 26(0) 27(0) 28(0) 29(0) 30(0) 31(0) 32(0) 33(0) 34(0) 35(0) 36(0) 37(0) 38(0) 39(0) 40(0) 41(0) 42(0) 43(0) 44(0) 45(0) 46(0) 47(0) 48(0) 49(0) 50(0) 51(0) 52(0) 53(0) 54(0)
803reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
804
805check reinit status
806return 0 ===> fw did wl reinit cmd
807*/
808static int check_current_reinit_info()
809{
810 FILE *fp;
811 int ret;
812 char lynq_cmd_ret[MAX_RET]={0};
813 char * dest;
814 char destid[3]={0};
815 if((fp=popen("wl counters","r"))==NULL)
816 {
817 perror("popen error!");
818 return -1;
819 }
820 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
821 {
822 perror("fread fail!");
823 if(pclose(fp) == -1)
824 {
825 perror("close fp file faild");
826 }
827 return -1;
828 }
829 dest = strstr(lynq_cmd_ret,"reinit ");
830 if(dest != NULL)
831 {
832 dest +=strlen("reinit ");
833 RLOGD("current get dest str is %s",dest);
834 memcpy(destid,dest,2);
835 ret = atoi(destid);
836 RLOGD("get current wl counters cmd return counts is %d",ret);
837 if( ret != 0 )
838 {
839 RLOGD("current fw did run cmd wl reinit");
840 if( pclose(fp) == -1 )
841 {
842 perror("close fp file faild");
843 }
844 return 0;
845 }
846 }
847 if( pclose(fp) == -1 )
848 {
849 perror("close fp file faild");
850 }
851 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
852 return -1;
853}
854
qs.xiong44fac672023-08-29 16:15:55 +0800855static void APTmpWatcherThreadProc() {
856
857 int i = 0;
858 int delytime = 300;
859 g_ap_tmp_watcher_stop_flag = 0;
860
861 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
862 while(1)
863 {
864 sleep(1);
865 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800866 if ( (i % 30) == 0 )
867 {
868 if ( check_current_reinit_info() == 0 )
869 {
870 system("wl reset_cnts");
871 system("wl down");
872 system("wl up");
873 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
874 }
875 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800876 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800877 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800878 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800879 {
880 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
881 {
882 system("wl down");
883 system("wl up");
884 }
qs.xiong44fac672023-08-29 16:15:55 +0800885 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800886 }
887 if ( i == delytime )
888 {
qs.xiong44fac672023-08-29 16:15:55 +0800889 i = 0;
890 }
891 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
892 {
893 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
894 return;
895 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800896
qs.xiong44fac672023-08-29 16:15:55 +0800897 }
898
899}
900
qs.xiongf0128b12023-06-29 17:29:39 +0800901static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
902{
903 char LYNQ_DISABLE_CMD[128]={0};
904
905 CHECK_IDX(idx, CTRL_STA);
906 CHECK_WPA_CTRL(CTRL_STA);
907
908 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800909 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800910 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800911
qs.xiongf0128b12023-06-29 17:29:39 +0800912 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800913
qs.xiongf0128b12023-06-29 17:29:39 +0800914}
915
qs.xiongb37f8c42023-09-13 21:21:58 +0800916static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
917{
918 char LYNQ_ENABLE_CMD[128]={0};
919
920 CHECK_IDX(idx, CTRL_STA);
921 CHECK_WPA_CTRL(CTRL_STA);
922
923
924 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800925 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800926 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
927
928 return 0;
929
930}
931
932static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
933{
934
935 int index,networkid;
936
937 for ( index = 0; index < len ;index++)
938 {
939 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800940 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800941 {
942 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
943 }
944 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
945 }
946 return 0;
947
948}
949
950
951/*
952 dis_net_list user disconnect list
953*/
954static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
955{
qs.xiong09560402023-10-27 21:58:55 +0800956 RLOGD("enter %s %d",__func__,__LINE__);
957 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800958 int count,ncount,index;
959 int flag = 0;
960 int merge_index = 0;
961 int net_no_list[128];
962
qs.xiong09560402023-10-27 21:58:55 +0800963 for(ncount = 0;ncount < valid_num; ncount++ )
964 {
965 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
966 }
967
qs.xiongb37f8c42023-09-13 21:21:58 +0800968 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
969 for( count = 0; count < index; count++)
970 {
971 for(ncount = 0; ncount < valid_num; ncount++)
972 {
qs.xiong09560402023-10-27 21:58:55 +0800973 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800974 if(net_no_list[count] == dis_net_list[ncount])
975 {
qs.xiong09560402023-10-27 21:58:55 +0800976 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800977 flag = 1;
978 break;
979 }
980 }
981 if( flag != 1 )
982 {
983 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800984 RLOGD("out[%d]: %d net_no_list[%d]: %d %d",merge_index,out[merge_index],count,net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800985 merge_index ++;
986 }
987 flag = 0;
988 }
989 * outlen =merge_index;
990 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
991 return;
992}
qs.xiongf0128b12023-06-29 17:29:39 +0800993
qs.xiong455c30b2023-04-12 11:40:02 +0800994void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
995{
996 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800997 char *wpanetid;
998 char destid[3] = {0};
999 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +08001000 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1001 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1002 {
1003 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1004 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
1005 return;
1006 }
1007
qs.xiong20202422023-09-06 18:01:18 +08001008 if (strstr(modify, "Trying to associate") != NULL)
1009 {
1010 RLOGD("Current sta is Trying to associate");
1011 *state = LYNQ_WIFI_STATUS_EGNORE;
1012 g_sta_conncet_status_flag = 1;
1013 return;
1014 }
1015
qs.xiong455c30b2023-04-12 11:40:02 +08001016 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1017 {
1018 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +08001019 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001020 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001021 return;
1022 }
1023
qs.xiong1e81dfa2023-09-27 15:52:37 +08001024 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
1025 {
1026 *error = LYNQ_PSW_ERROR;
1027 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1028 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
1029 g_sta_conncet_status_flag = 0;
1030 return;
1031 }
qs.xiong455c30b2023-04-12 11:40:02 +08001032 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1033 {
qs.xiongf0128b12023-06-29 17:29:39 +08001034 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1035 wpanetid = strstr(modify,"id=");
1036 if ( wpanetid != NULL )
1037 {
1038 wpanetid +=strlen("id=");
1039 memcpy(destid,wpanetid,2);
1040 tmpdisid = atoi(destid);
1041
1042 }
qs.xiong455c30b2023-04-12 11:40:02 +08001043 pReason = strstr(modify, "reason=");
1044 if (pReason != NULL)
1045 {
1046 pReason += strlen("reason=");
1047 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1048 {
1049 *error = LYNQ_TIME_OUT;
1050 }
1051 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1052 {
1053 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001054 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001055 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001056 // you.chen@2023-10-17 only disable network during autoconnect
1057 if(tmpdisid != -1 && s_sta_status == INNER_STA_STATUS_INIT && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
qs.xiongf0128b12023-06-29 17:29:39 +08001058 {
1059 RLOGE("stop wlan0 network %d fail",tmpdisid);
1060 }
qs.xiong455c30b2023-04-12 11:40:02 +08001061 }
1062 else
1063 {
1064 *error = LYNQ_UNSPECIFIED_REASON;
1065 }
qs.xiong455c30b2023-04-12 11:40:02 +08001066 }
1067 else
1068 {
1069 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001070 }
qs.xiongf0128b12023-06-29 17:29:39 +08001071 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001072 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001073 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001074
1075 }
1076
1077 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1078 {
1079 *error = LYNQ_NOT_FIND_AP;
1080 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1081 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001082 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001083 return;
1084 }
1085
1086
1087 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1088 {
1089 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1090 pReason = strstr(modify, "status_code=");
1091 if (pReason != NULL)
1092 {
1093 pReason += strlen("status_code=");
1094 if (memcmp(pReason, "17", 2) == 0)
1095 {
1096 *error = LYNQ_AP_UNABLE_HANDLE;
1097 }
1098 else if (memcmp(pReason, "1",1) == 0)
1099 {
1100 *error = LYNQ_UNSPECIFIED_REASON;
1101 }
1102 else
1103 {
1104 *error = LYNQ_UNSPECIFIED_REASON;
1105 }
1106
1107 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1108 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001109 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001110 return;
1111 }
1112 else
1113 {
1114 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1115 *error = LYNQ_UNSPECIFIED_REASON;
1116 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1117 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1118 return;
1119 }
1120 }
1121
1122 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1123 {
1124 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1125 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1126 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1127 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1128 return;
1129 }
1130
1131 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1132 {
1133 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1134 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1135 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1136 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1137 return;
1138 }
1139
qs.xiongdf637b22023-10-26 19:30:07 +08001140 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1141 if (strstr(modify, "Association request to the driver failed") != NULL)
1142 {
1143 RLOGD("Association request to the driver failed --- recover");
1144 system("wl down");
1145 system("wl up");
1146 *error = LYNQ_UNSPECIFIED_REASON;
1147 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1148 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1149 return;
1150 }
1151 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1152
1153
you.chen32cb31e2023-04-13 14:05:45 +08001154 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001155 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001156 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001157 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1158 return;
1159
1160}
1161
qs.xiongfcc914b2023-07-06 21:16:20 +08001162void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1163{
1164 char *pReason;
1165 char *wpanetid;
1166 char destid[3];
1167 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1168 *networkid = -1;
1169 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1170 {
1171 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1172 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1173 return;
1174 }
1175 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1176 {
1177 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1178 wpanetid = strstr(modify,"id=");
1179 if ( wpanetid != NULL )
1180 {
1181 wpanetid +=strlen("id=");
1182 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1183 if (memcpy(destid,wpanetid,2) != NULL)
1184 {
1185 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1186 *networkid = atoi(destid);
1187 RLOGD("get networkid is %d\n",*networkid);
1188 }
1189 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1190 }
1191 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1192 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1193 return;
1194 }
1195 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1196 {
1197 wpanetid = strstr(modify,"id=");
1198 if ( wpanetid != NULL )
1199 {
1200 wpanetid +=strlen("id=");
1201 if (memcpy(destid,wpanetid,2) != NULL)
1202 {
1203 *networkid = atoi(destid);
1204 RLOGD("get networkid is %d\n",*networkid);
1205 }
1206 }
1207 pReason = strstr(modify, "reason=");
1208 if (pReason != NULL)
1209 {
1210 pReason += strlen("reason=");
1211 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1212 {
1213 *error = LYNQ_TIME_OUT;
1214 }
1215 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1216 {
1217 *error = LYNQ_PSW_ERROR;
1218 }
1219 else
1220 {
1221 *error = LYNQ_UNSPECIFIED_REASON;
1222 }
1223 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1224 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1225 return;
1226 }
1227 else
1228 {
1229 *error = LYNQ_UNSPECIFIED_REASON;
1230 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1231 return;
1232 }
1233 }
1234 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1235 {
1236 wpanetid = strstr(modify,"id=");
1237 if ( wpanetid != NULL )
1238 {
1239 wpanetid +=strlen("id=");
1240 if (memcpy(destid,wpanetid,2) != NULL)
1241 {
1242 *networkid = atoi(destid);
1243 RLOGD("get networkid is %d\n",*networkid);
1244 }
1245 }
1246 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1247 pReason = strstr(modify, "status_code=");
1248 if (pReason != NULL)
1249 {
1250 pReason += strlen("status_code=");
1251 if (memcmp(pReason, "17", 2) == 0)
1252 {
1253 *error = LYNQ_AP_UNABLE_HANDLE;
1254 }
1255 else if (memcmp(pReason, "1",1) == 0)
1256 {
1257 *error = LYNQ_UNSPECIFIED_REASON;
1258 }
1259 else
1260 {
1261 *error = LYNQ_UNSPECIFIED_REASON;
1262 }
1263 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1264 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1265 return;
1266 }
1267 else
1268 {
1269 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1270 *error = LYNQ_UNSPECIFIED_REASON;
1271 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001272 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001273 return;
1274 }
1275 }
1276 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1277 {
1278 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1279 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1280 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1281 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1282 return;
1283 }
1284 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1285 {
1286 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1287 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1288 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1289 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1290 return;
1291 }
1292 RLOGD("EVENT : %s\n", modify);
1293 *error = LYNQ_UNSPECIFIED_REASON;
1294 *state = LYNQ_WIFI_STATUS_EGNORE;
1295 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1296 return;
1297}
you.chen70f377f2023-04-14 18:17:09 +08001298static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1299{
you.chen6d247052023-06-01 16:39:54 +08001300 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001301 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1302 {
1303 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1304 g_sta_callback_func(g_sta_callback_priv, state, error);
1305 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1306 }
you.chen6d247052023-06-01 16:39:54 +08001307 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001308}
qs.xiongfcc914b2023-07-06 21:16:20 +08001309static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1310{
1311 pthread_mutex_lock(&s_sta_callback_mutex);
1312 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1313 {
1314 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1315 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1316 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1317 }
1318 pthread_mutex_unlock(&s_sta_callback_mutex);
1319}
you.chen70f377f2023-04-14 18:17:09 +08001320
you.chen35020192022-05-06 11:30:57 +08001321static void STAWatcherThreadProc() {
1322 size_t len = MAX_RET;
1323 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001324 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001325 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001326 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001327
you.chen6c2dd9c2022-05-16 17:55:28 +08001328 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001329 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001330
you.chen70f377f2023-04-14 18:17:09 +08001331 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001332 while (g_sta_watcher_stop_flag == 0)
1333 {
you.chenf711c8a2023-04-13 13:49:45 +08001334 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001335 {
you.chen35020192022-05-06 11:30:57 +08001336 continue;
1337 }
you.chenf711c8a2023-04-13 13:49:45 +08001338
you.chen6c2dd9c2022-05-16 17:55:28 +08001339 memset(msg_notify, 0, MAX_RET);
1340 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001341 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001342 {
you.chen35020192022-05-06 11:30:57 +08001343 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001344 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1345 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001346 {
you.chen35020192022-05-06 11:30:57 +08001347 g_sta_scan_finish_flag = 1;
1348 }
1349
qs.xiong9fbf74e2023-03-28 13:38:22 +08001350 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001351 {
you.chen35020192022-05-06 11:30:57 +08001352 continue;
1353 }
qs.xiong455c30b2023-04-12 11:40:02 +08001354 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001355 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1356 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1357 {
1358 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1359 }
you.chen0c9bee22023-10-25 13:03:14 +08001360 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1361 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1362 {
1363 RLOGD("donot report scan result during in call connect manual");
1364 }
you.chen6e724162023-10-19 19:10:01 +08001365 else
1366 {
1367 notify_connect_status(state, error);
1368 }
1369 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001370
1371 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001372 {
you.chen70f377f2023-04-14 18:17:09 +08001373 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001374 if (last_state != state)
1375 {
1376 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1377 {
1378 system_call_v("%s %s", sta_status_change_script, "connect");
1379 }
1380 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1381 {
1382 system_call_v("%s %s", sta_status_change_script, "disconnect");
1383 }
1384 }
qs.xiong09560402023-10-27 21:58:55 +08001385
you.chenc9928582023-04-24 15:39:37 +08001386 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001387 if (g_sta_fake_scan_finish_flag == 1)
1388 {
1389 g_sta_fake_scan_finish_flag = 0;
1390 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1391 }
you.chen32cb31e2023-04-13 14:05:45 +08001392 }
you.chen35020192022-05-06 11:30:57 +08001393 }
1394 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001395 if (lynq_wpa_ctrl != NULL)
1396 {
you.chen92fd5d32022-05-25 10:09:47 +08001397 wpa_ctrl_detach(lynq_wpa_ctrl);
1398 wpa_ctrl_close(lynq_wpa_ctrl);
1399 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001400}
qs.xiongfcc914b2023-07-06 21:16:20 +08001401static void STAAutoWatcherThreadProc() {
1402 size_t len = MAX_RET;
1403 char msg_notify[MAX_RET];
1404 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001405 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001406 int idle_count = 0;
1407 int networkid;
1408 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1409 g_sta_auto_watcher_stop_flag = 0;
1410 RLOGD("STAAutoWatcherThreadProc thread started ------");
1411 while (g_sta_auto_watcher_stop_flag == 0)
1412 {
1413 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1414 {
1415 continue;
1416 }
1417 memset(msg_notify, 0, MAX_RET);
1418 len = MAX_RET;
1419 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1420 {
1421 msg_notify[len+1] = '\0';
1422 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1423 if (strstr(msg_notify, state_scan_result) != NULL)
1424 {
1425 g_sta_auto_scan_finish_flag = 1;
1426 }
1427 if (g_sta_auto_callback_func == NULL)
1428 {
1429 continue;
1430 }
1431 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1432 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001433 }
1434 }
1435 if (lynq_wpa_ctrl != NULL)
1436 {
1437 wpa_ctrl_detach(lynq_wpa_ctrl);
1438 wpa_ctrl_close(lynq_wpa_ctrl);
1439 }
1440}
qs.xiongf1b525b2022-03-31 00:58:23 -04001441
you.chen70f377f2023-04-14 18:17:09 +08001442// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1443static void GlobalWatcherThreadProc()
1444{
1445 int ret, connect_timeout, service_abnormal;
1446 error_number_s error_num = -1;
1447 inner_sta_status_s sta_status;
1448 scan_info_s *scan_list = NULL;
1449 int i, scan_len=0;
1450 char connecting_ssid[64];
1451 struct timeval now;
1452
1453 RLOGD("GlobalWatcherThreadProc start to run");
1454
1455 while (1)
1456 {
1457 pthread_mutex_lock(&s_global_check_mutex);
1458 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1459 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1460 {
1461 pthread_mutex_unlock(&s_global_check_mutex);
1462 usleep(50*1000);
1463 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1464 continue;
1465 }
1466
1467 connect_timeout = 0;
1468 service_abnormal = 0;
1469 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1470 {
1471 while (1)
1472 {
1473 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1474 if (ret == ETIME)
1475 {
1476 connect_timeout = 1;
1477 }
1478 else if (ret != 0)
1479 {
1480 gettimeofday(&now,NULL);
1481 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1482 {
1483 usleep(SLEEP_TIME_ON_IDLE);
1484 continue;
1485 }
1486 connect_timeout = 1;
1487 }
1488 sta_status = s_sta_status;
1489 error_num = s_sta_error_number;
1490 s_sta_status = INNER_STA_STATUS_INIT;
1491 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1492 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1493 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1494 break;
1495 }
1496 }
1497 if (s_service_invoke_timeout_cnt > 10)
1498 {
1499 service_abnormal = 1;
1500 s_service_invoke_timeout_cnt = 0;
1501 }
1502 pthread_mutex_unlock(&s_global_check_mutex);
1503
1504 if (service_abnormal == 1)
1505 {
1506 sleep(1);
1507 RLOGE("wpa service is abnormal info app to exit");
1508 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001509
1510 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1511
you.chen70f377f2023-04-14 18:17:09 +08001512 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1513 }
1514
1515 if (sta_status == INNER_STA_STATUS_CANCEL)
1516 {
1517 continue;
1518 }
1519 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1520 {
1521 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1522 }
1523 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1524 {
1525 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1526 {
1527 for(i=0; i < scan_len;i++)
1528 {
1529 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1530 {
1531 error_num = LYNQ_AUTH_ERROR;
1532 break;
1533 }
1534 }
1535 free(scan_list);
1536 }
1537 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1538 }
1539 else if (connect_timeout == 0)
1540 {
1541 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1542 }
1543 else // wait timeout
1544 {
1545 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1546 {
1547 ; // wpa service abnormal
1548 }
1549 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1550 {
1551 RLOGD("GlobalWatcherThreadProc notify connected");
1552 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1553 }
1554 else
1555 {
1556 RLOGD("GlobalWatcherThreadProc notify timeout");
1557 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1558 }
1559 }
1560 } // while (1)
1561}
1562
qs.xiong1af5daf2022-03-14 09:12:12 -04001563int lynq_wifi_enable(void)
1564{
you.chen35020192022-05-06 11:30:57 +08001565 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001566 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001567 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001568 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1569
qs.xiong9fbf74e2023-03-28 13:38:22 +08001570 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1571 {
you.chend2fef3f2023-02-13 10:50:35 +08001572 goto out_enable;
1573 }
1574
you.chenc9928582023-04-24 15:39:37 +08001575 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001576 if (ret != 0)
1577 {
1578 //printf("service state %d\n", ret);
1579 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001580 ret = -1;
1581 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001582 }
lhfe8da902022-10-11 18:55:36 +08001583
you.chen70f377f2023-04-14 18:17:09 +08001584 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1585 {
1586 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1587 if(ret<0)
1588 {
1589 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1590 ret = -1;
1591 goto out_enable;
1592 }
1593 }
1594
you.chend2fef3f2023-02-13 10:50:35 +08001595 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1596 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1597 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1598 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1599out_enable:
1600 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001601 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001602}
1603
qs.xiong1af5daf2022-03-14 09:12:12 -04001604int lynq_wifi_disable(void)
1605{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001606 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001607 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001608 g_ap_watcher_stop_flag = 1;
1609 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001610 g_sta_auto_watcher_stop_flag = 1;
qs.xiong9d8f3102023-12-07 20:11:37 +08001611/* g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001612 if (g_ap_watcher_pid != 0)
1613 pthread_join(g_ap_watcher_pid, NULL);
qs.xiong9d8f3102023-12-07 20:11:37 +08001614*/
you.chen35020192022-05-06 11:30:57 +08001615 if (g_sta_watcher_pid != 0)
1616 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001617 if (g_sta_auto_watcher_pid != 0)
1618 pthread_join(g_sta_auto_watcher_pid, NULL);
qs.xiong17579bb2024-03-11 19:08:06 +08001619 if ( g_lynq_wpa_ctrl[0] != NULL)
1620 {
1621 if(g_lynq_wpa_ctrl[0]->ctrl != NULL)
1622 {
1623 wpa_ctrl_close(g_lynq_wpa_ctrl[0]->ctrl);
1624 }
1625 free(g_lynq_wpa_ctrl[0]);
1626 g_lynq_wpa_ctrl[0] = NULL;
1627 }
1628
1629 if ( g_lynq_wpa_ctrl[1] != NULL )
1630 {
1631 if( g_lynq_wpa_ctrl[1]->ctrl != NULL )
1632 {
1633 wpa_ctrl_close(g_lynq_wpa_ctrl[1]->ctrl);
1634 }
1635 free(g_lynq_wpa_ctrl[1]);
1636 g_lynq_wpa_ctrl[1] = NULL;
1637 }
qs.xiong44fac672023-08-29 16:15:55 +08001638 if (g_ap_tmp_watcher_pid != 0)
1639 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001640 g_ap_watcher_pid = 0;
1641 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001642 g_sta_auto_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08001643// g_ap_tmp_watcher_pid = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +08001644 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001645 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001646 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1647 return 0;
1648}
1649
1650static inline char inner_convert_char(char in)
1651{
1652 if (in >= '0' && in <= '9')
1653 {
1654 return in - '0';
1655 }
1656 else if (in >= 'a' && in <= 'f')
1657 {
1658 return in - 'a' + 10;
1659 }
1660 else if (in >= 'A' && in <= 'F')
1661 {
1662 return in - 'A' + 10;
1663 }
1664 else
1665 {
1666 return '\xff';
1667 }
1668}
1669
1670static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1671{
1672 char *p;
1673 size_t pos = 0;
1674 if (NULL == out_ssid)
1675 return;
1676 //printf("input ssid=[%s]\n", ssid);
1677 memset(out_ssid, 0, out_ssid_len);
1678 if (NULL == ssid)
1679 return;
1680 p = strchr(ssid, '\\');
1681 if (NULL == p)
1682 {
1683 strncpy(out_ssid, ssid, out_ssid_len);
1684 //printf(" first %s\n", out_ssid);
1685 }
1686 else
1687 {
1688 pos = p - ssid;
1689 memcpy(out_ssid, ssid, pos);
1690 //printf("pos %lu -- %s\n", pos, out_ssid);
1691 for(; pos < out_ssid_len; pos ++)
1692 {
1693 if (p[0] == '\0')
1694 {
1695 //printf(" out %s\n", out_ssid);
1696 return;
1697 }
1698 else if (p[0] != '\\')
1699 {
1700 out_ssid[pos] = p[0];
1701 p += 1;
1702 }
1703 else if (p[1] == 'x' || p[1] == 'X')
1704 {
1705 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1706 p += 4;
1707 }
1708 else if (p[1] == '\\')
1709 {
1710 out_ssid[pos] = '\\';
1711 p += 2;
1712 }
1713 else if (p[1] == 't')
1714 {
1715 out_ssid[pos] = '\t';
1716 p += 2;
1717 }
1718 else if (p[1] == 'r')
1719 {
1720 out_ssid[pos] = '\r';
1721 p += 2;
1722 }
1723 else if (p[1] == 'n')
1724 {
1725 out_ssid[pos] = '\n';
1726 p += 2;
you.chen34983432023-12-21 20:39:06 +08001727 }
1728 else
1729 {
1730 out_ssid[pos] = p[1];
1731 p += 2;
you.chend2fef3f2023-02-13 10:50:35 +08001732 }//todo find a better way to convert?
1733 }
1734 }
1735 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001736}
qs.xiong1af5daf2022-03-14 09:12:12 -04001737
you.chen35020192022-05-06 11:30:57 +08001738static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001739 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001740 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001741 RLOGD("enter inner_get_param");
1742 if (out_put == NULL)
1743 {
1744 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001745 return -1;
1746 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001747 if (param_name == NULL)
1748 {
1749 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001750 return -1;
1751 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001752 if (param_name[0] == '\0')
1753 {
1754 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001755 return -1;
1756 }
1757
1758 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1759
1760 CHECK_WPA_CTRL(interface);
1761
1762 DO_REQUEST(lynq_cmd_get);
1763
qs.xiong9fbf74e2023-03-28 13:38:22 +08001764 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1765 {
1766 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001767 return -1;
1768 }
1769
you.chena6fa5b22022-05-18 10:28:19 +08001770// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001771 if (strcmp(param_name, "ssid") == 0)
1772 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001773 if (cmd_reply[0] == '\"')
1774 {
you.chend2fef3f2023-02-13 10:50:35 +08001775 ssid_len = reply_len - 1;
1776 memcpy(out_put, cmd_reply + 1, ssid_len);
1777 if (out_put[ssid_len-1] == '\"')
1778 {
1779 out_put[ssid_len-1] = '\0';
1780 }
1781 else
1782 {
1783 out_put[ssid_len] = '\0';
1784 }
1785 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001786 else
1787 {
you.chend2fef3f2023-02-13 10:50:35 +08001788 ssid_len = reply_len / 2;
1789 for(i=0; i<ssid_len; i++)
1790 {
1791 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1792 }
1793 out_put[ssid_len] = '\0';
1794 }
1795 }
1796 else
1797 {
1798 memcpy(out_put, cmd_reply, reply_len + 1);
1799 }
you.chen35020192022-05-06 11:30:57 +08001800 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001801}
qs.xiong1af5daf2022-03-14 09:12:12 -04001802
you.chen35020192022-05-06 11:30:57 +08001803static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1804 int ret = 0;
1805 char * end = str + len - 1;
1806 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001807 while(str < end)
1808 {
1809 if (*str == delimiter)
1810 {
you.chen35020192022-05-06 11:30:57 +08001811 *str++ = '\0';
1812 results[ret++] = str;
1813 continue;
1814 }
1815 str++;
1816 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001817 if (*str == delimiter)
1818 {
you.chen35020192022-05-06 11:30:57 +08001819 *str = '\0';
1820 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001821
you.chen6ed36a62023-04-27 17:51:56 +08001822 results[ret] = NULL;
1823
you.chen35020192022-05-06 11:30:57 +08001824 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001825}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001826/*
1827 *add func to get conencted STA device ip from dnsmasq ap0.lease
1828 *return 0 means get ip success
1829 */
1830static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1831{
1832 char * p;
1833 int ret;
1834 char cmd[256]={0};
1835 if (NULL == mac || NULL == ip)
1836 return -1;
1837 memset(ip, 0, ip_len);
1838 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1839 ret = exec_cmd(cmd, ip, ip_len);
1840 if( ret == 0 )
1841 {
1842 p = strchr(ip, '\n');
1843 if (NULL != p)
1844 {
1845 *p = '\0';
1846 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1847 return ret;
1848 }else
1849 {
1850 ret = -1;
1851 }
1852 }
1853 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1854 return ret;
1855
1856}
qs.xiong7a105ce2022-03-02 09:43:11 -05001857
you.chend2fef3f2023-02-13 10:50:35 +08001858static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1859{
1860 char * p;
1861 int ret = 0;
1862 char cmd[256]={0};
1863 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001864 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001865 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001866 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001867 ret = exec_cmd(cmd, ip, ip_len);
1868 p = strchr(ip, '\n');
1869 if (NULL != p)
1870 {
1871 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001872 }else
1873 {
1874 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001875 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001876 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001877 return ret;
1878}
1879
you.chend2fef3f2023-02-13 10:50:35 +08001880static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001881 struct in_addr addr ={0};
1882 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001883 char cmd[64] = {0};
1884 char * p;
1885 int ret;
you.chen35020192022-05-06 11:30:57 +08001886
qs.xiong9fbf74e2023-03-28 13:38:22 +08001887 if (ip == NULL || *ip == '\0' || hostname == NULL)
1888 {
1889 RLOGE("ip == NULL or hostname == NULL");
1890 return -1;
you.chen35020192022-05-06 11:30:57 +08001891 }
1892
you.chend2fef3f2023-02-13 10:50:35 +08001893 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001894 if (inet_aton(ip, &addr) == 0)
1895 {
you.chen35020192022-05-06 11:30:57 +08001896 printf("---inet_aton fail\n");
1897 return -1;
1898 }
1899
1900 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1901
qs.xiong9fbf74e2023-03-28 13:38:22 +08001902 if (ht == NULL)
1903 {
you.chen186d3c32023-05-18 14:19:46 +08001904 hostname[0] = '\0';
1905 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1906 ret = exec_cmd(cmd, hostname, 32);
1907 if (ret == 0)
1908 {
1909 p = strchr(hostname, '\n');
1910 if (p != NULL)
1911 {
1912 *p = '\0';
1913 }
1914 return 0;
1915 }
1916 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001917 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001918 herror(NULL);
1919 return -1;
1920 }
1921
1922 strcpy(hostname, ht->h_name);
1923
1924 return 0;
1925}
1926
1927static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1928{
1929 int count, index, words_count;
1930 char * split_lines[128]= {0};
1931 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001932 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001933 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001935
1936 CHECK_WPA_CTRL(ap_sta);
1937
1938 DO_REQUEST(lynq_wifi_list_networks);
1939
1940 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1941
1942 //@todo check ssid field to compatible
1943
1944 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001945 for(index=1; index < count; index++)
1946 {
you.chen35020192022-05-06 11:30:57 +08001947 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001948 if (words_count > 2)
1949 {
you.chend2fef3f2023-02-13 10:50:35 +08001950 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001951 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1952 {
you.chen35020192022-05-06 11:30:57 +08001953 net_no_list[ret++] = atoi(split_words[0]);
1954 }
1955 }
1956 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001957 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001958 return ret;
1959}
1960
1961static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001962 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001963 CHECK_WPA_CTRL(ap_sta);
1964 const char *lynq_wifi_add_network = "ADD_NETWORK";
1965
qs.xiong9fbf74e2023-03-28 13:38:22 +08001966 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001967 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1969 {
1970 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001971 return -1;
1972 }
1973
qs.xiong9fbf74e2023-03-28 13:38:22 +08001974 for(i=0;i<reply_len;i++)
1975 {
1976 if(cmd_reply[i] == '\n')
1977 {
you.chen35020192022-05-06 11:30:57 +08001978 cmd_reply[i] = '\0';
1979 break;
1980 }
1981 }
1982 return atoi(cmd_reply);
1983}
you.chena6cd55a2022-05-08 12:20:18 +08001984
you.chen35020192022-05-06 11:30:57 +08001985static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1986{
1987 int count, index;
1988 int net_no_list[128];
1989
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001991 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001992 for (index=0; index < count; index++)
1993 {
1994 if (net_no_list[index] == net_no)
1995 {
you.chen35020192022-05-06 11:30:57 +08001996 return 0;
1997 }
1998 }
1999
2000 if (count >= 1)
2001 index = net_no_list[count - 1];
2002 else
2003 index = -1;
2004
qs.xiong9fbf74e2023-03-28 13:38:22 +08002005 while (index < net_no )
2006 {
you.chen35020192022-05-06 11:30:57 +08002007 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002008 if (index >= net_no)
2009 { // required network no created
2010 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08002011 return 0;
2012 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002013 else if( index < 0)
2014 {
2015 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08002016 return -1;
2017 }
you.chen35020192022-05-06 11:30:57 +08002018 }
2019
2020 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002021 {
2022 RLOGE("[lynq_check_network_number] network index < 0");
2023 return -1;
2024 }
2025 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08002026 return 0;
2027}
2028
2029static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08002030 if (freq > 5000 && freq < 6000)
2031 {
you.chen35020192022-05-06 11:30:57 +08002032 return LYNQ_WIFI_5G_band;
2033 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002034 else if (freq > 2000 && freq < 3000)
2035 {
you.chen35020192022-05-06 11:30:57 +08002036 return LYNQ_WIFI_2G_band;
2037 }
2038 return LYNQ_WIFI_2_and_5G_band;
2039}
2040
2041static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002042 if (key_mgmt != NULL)
2043 {
2044 if (memcmp( key_mgmt, "NONE", 4) == 0)
2045 {
you.chen35020192022-05-06 11:30:57 +08002046 return LYNQ_WIFI_AUTH_OPEN;
2047 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002048 else if (memcmp( key_mgmt, "WEP", 3) == 0)
2049 {
you.chen35020192022-05-06 11:30:57 +08002050 return LYNQ_WIFI_AUTH_WEP;
2051 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002052 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2053 {
you.chen35020192022-05-06 11:30:57 +08002054 return LYNQ_WIFI_AUTH_WPA_PSK;
2055 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002056 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2057 {
you.chen35020192022-05-06 11:30:57 +08002058 return LYNQ_WIFI_AUTH_WPA2_PSK;
2059 }
2060 }
2061
2062 return -1;
2063}
2064
2065static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002066 if (flag != NULL)
2067 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002068 if ( strstr(flag,"WPA2-PSK+SAE-CCMP") != NULL || strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || ( strstr(flag,"SAE-H2E") != NULL && strstr(flag,"WPS") == NULL ) )
qs.xiong3e506812023-04-06 11:08:48 +08002069 {
2070 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002071 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002072 {
2073 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2074 }else if (strstr( flag, "WPA2-PSK") != NULL)
2075 {
you.chen35020192022-05-06 11:30:57 +08002076 return LYNQ_WIFI_AUTH_WPA2_PSK;
2077 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002078 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002079 {
you.chen35020192022-05-06 11:30:57 +08002080 return LYNQ_WIFI_AUTH_WPA_PSK;
2081 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002082 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002083 {
you.chen35020192022-05-06 11:30:57 +08002084 return LYNQ_WIFI_AUTH_WEP;
2085 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002086 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002087 {
you.chen35020192022-05-06 11:30:57 +08002088 return LYNQ_WIFI_AUTH_OPEN;
2089 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002090 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002091 {
you.chend2fef3f2023-02-13 10:50:35 +08002092 return LYNQ_WIFI_AUTH_OPEN;
2093 }
qs.xiong46f41562023-07-11 21:06:47 +08002094 else
2095 {
2096 RLOGD("convert_max_auth_from_flag not-found auth mode");
2097 }
you.chen35020192022-05-06 11:30:57 +08002098 }
2099
2100 return -1;
2101}
2102
2103static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2104 switch (bw) {
2105 case 10:
2106 return LYNQ_WIFI_BANDWIDTH_HT10;
2107 break;
2108 case 20:
2109 return LYNQ_WIFI_BANDWIDTH_HT20;
2110 break;
2111 case 40:
2112 return LYNQ_WIFI_BANDWIDTH_HT40;
2113 break;
2114 case 80:
2115 return LYNQ_WIFI_BANDWIDTH_HT80;
2116 break;
2117 default:
2118 break;
2119 }
2120
2121 return -1;
2122}
2123
you.chen70f377f2023-04-14 18:17:09 +08002124static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002125static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2126 int i, count;
2127 char *p;
2128 const char *lynq_status_cmd = "STATUS";
2129 const char * FLAG_SSID = "ssid=";
2130 const char * FLAG_SBSID = "bssid=";
2131 const char * FLAG_KEY_MGMT = "key_mgmt=";
2132 const char * FLAG_FREQ = "freq=";
2133 const char * FLAG_STATE = "wpa_state=";
2134 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002135 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002136 char *split_lines[128] = {0};
2137
2138 CHECK_WPA_CTRL(interface);
2139
qs.xiong9fbf74e2023-03-28 13:38:22 +08002140 if (curr_state == NULL)
2141 {
2142 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002143 return -1;
2144 }
2145
2146 DO_REQUEST(lynq_status_cmd);
2147
2148 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2149
2150 curr_state->net_no = -1;
2151 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002152 for(i=0; i < count; i++)
2153 {
2154 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002155 {
you.chen35020192022-05-06 11:30:57 +08002156 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002158 {
you.chend2fef3f2023-02-13 10:50:35 +08002159 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002160 ret = 0;
2161 continue;
2162 }
you.chenf58b3c92022-06-21 16:53:48 +08002163 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002164 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002165 {
you.chend2fef3f2023-02-13 10:50:35 +08002166 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 +08002167 ret = 0;
2168 continue;
2169 }
you.chen35020192022-05-06 11:30:57 +08002170 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002171 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002172 {
you.chen450d0172022-07-15 17:56:48 +08002173 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002174 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002175 ret = 0;
2176 continue;
2177 }
2178 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002179 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002180 {
you.chen35020192022-05-06 11:30:57 +08002181 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2182 ret = 0;
2183 continue;
2184 }
you.chend2fef3f2023-02-13 10:50:35 +08002185 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002186 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002187 {
you.chend2fef3f2023-02-13 10:50:35 +08002188 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2189 ret = 0;
2190 continue;
2191 }
you.chen35020192022-05-06 11:30:57 +08002192 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002193 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002194 {
you.chen35020192022-05-06 11:30:57 +08002195 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002196 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002197 {
you.chen35020192022-05-06 11:30:57 +08002198 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2199 ret = 0;
2200 continue;
2201 }
2202
2203 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002204 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002205 {
you.chen35020192022-05-06 11:30:57 +08002206 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002207 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002208 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002209 }
2210 }
2211
you.chen70f377f2023-04-14 18:17:09 +08002212 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2213 {
2214 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2215 }
2216
you.chen35020192022-05-06 11:30:57 +08002217 return ret;
2218}
2219
qs.xiongf1b525b2022-03-31 00:58:23 -04002220int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002221{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002222 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002223 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002224
qs.xiong9fbf74e2023-03-28 13:38:22 +08002225 if (ap_ssid == NULL)
2226 {
2227 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002228 return -1;
2229 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002230 else
2231 {
2232 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002233 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002234
qs.xiong9fbf74e2023-03-28 13:38:22 +08002235 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2236 {
2237 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002238 return -1;
2239 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002240
you.chen35020192022-05-06 11:30:57 +08002241 CHECK_IDX(idx, CTRL_AP);
2242
2243 CHECK_WPA_CTRL(CTRL_AP);
2244
2245 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2246
2247 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2248 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002249
qs.xiong1b5e3472023-11-27 17:42:20 +08002250 RLOGD("[lynq_wifi_ap_ssid_set] set ssid succeed %d",__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002251 return 0;
you.chen35020192022-05-06 11:30:57 +08002252
qs.xiong7a105ce2022-03-02 09:43:11 -05002253}
2254
you.chen35020192022-05-06 11:30:57 +08002255int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002256{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002257 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002258 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002259 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002260}
2261
qs.xiongc9c79f72022-10-17 15:27:18 +08002262/*****
2263 *frequency <------>channel
2264 *
2265 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2266 *
2267 *
2268 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2269 *
2270 *
2271 * */
2272static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002273 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};
2274 int i;
2275 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2276
qs.xiong69a332b2022-12-02 09:58:57 +08002277 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002278 {
2279 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002280 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002281 }
qs.xiongc00b6032022-11-29 16:28:03 +08002282
2283 if(i == arr_len)
2284 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002286 return -1;
2287 }
qs.xiongc00b6032022-11-29 16:28:03 +08002288
qs.xiongc9c79f72022-10-17 15:27:18 +08002289 return 0;
2290}
qs.xiong13673462023-02-21 19:12:54 +08002291
2292static int lynq_check_frequencyby_country_code(int input_frequency)
2293{
2294 char str_cnc[]="CN";
2295 char str_dest[20]="";
2296
2297 if( lynq_get_country_code(1,str_dest) != 0 )
2298 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002299 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002300 return -1;
2301 }
2302 if( strncmp(str_dest,str_cnc,2) != 0 )
2303 {
2304 return 0;
2305 }else if( 2473 < input_frequency && input_frequency < 5744)
2306 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002307 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002308 return -1;
2309 }
2310 return 0;
2311}
qs.xiongf1b525b2022-03-31 00:58:23 -04002312int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002313{
qs.xiongc00b6032022-11-29 16:28:03 +08002314 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002315 char lynq_wifi_frequency_cmd[128]={0};
2316 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002317 char lynq_cmd_slect[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002318 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002319 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002320 check = lynq_check_set_frequency(lynq_wifi_frequency);
2321 if(check != 0)
2322 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002323 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002324 return -1;
you.chen35020192022-05-06 11:30:57 +08002325 }
qs.xiong13673462023-02-21 19:12:54 +08002326 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2327 if(check != 0)
2328 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002329 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002330 return -1;
2331 }
2332
qs.xiongc00b6032022-11-29 16:28:03 +08002333 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2334 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002336 return -1;
2337 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002338
you.chen35020192022-05-06 11:30:57 +08002339 CHECK_IDX(idx, CTRL_AP);
2340
2341 CHECK_WPA_CTRL(CTRL_AP);
2342
2343 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2344 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2345 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2346
you.chen6c2dd9c2022-05-16 17:55:28 +08002347 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002348 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2349 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2350 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002351
qs.xiong9fbf74e2023-03-28 13:38:22 +08002352 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002353}
2354
qs.xiongf1b525b2022-03-31 00:58:23 -04002355int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002356{
you.chen35020192022-05-06 11:30:57 +08002357 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002358 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002359 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002360
qs.xiong9fbf74e2023-03-28 13:38:22 +08002361 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2362 {
2363 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002364 return -1;
2365 }
2366 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002367
qs.xiong9fbf74e2023-03-28 13:38:22 +08002368 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002369}
2370
qs.xiongf1b525b2022-03-31 00:58:23 -04002371int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2372{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002373 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002374 CHECK_IDX(idx, CTRL_AP);
2375 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002376 case LYNQ_WIFI_BANDWIDTH_HT10:
2377 {
2378 RLOGE("bandwith [%d] not support now\n", bandwidth);
2379 return -1;
2380 }
2381 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002382 {
2383 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2384 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002385 if (system(lynq_cmd_bandwith) != 0 )
2386 {
2387 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002388 return -1;
2389 }
2390 system("wl up");
2391 break;
2392 }
2393 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002394 {
qs.xiong10379192023-02-21 13:19:42 +08002395 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002396 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2397 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002398 if (system(lynq_cmd_bandwith) != 0 )
2399 {
2400 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002401 return -1;
2402 }
2403 system("wl up");
2404 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405 }
you.chen35020192022-05-06 11:30:57 +08002406 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002407 {
qs.xiong10379192023-02-21 13:19:42 +08002408 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002409 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002410 if (system(lynq_cmd_bandwith) != 0 )
2411 {
2412 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002413 return -1;
2414 }
2415 system("wl up");
2416 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002417 }
2418 default:
you.chen35020192022-05-06 11:30:57 +08002419 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002420 RLOGE("auth type [%d] not support now\n", bandwidth);
2421 return -1;
you.chen35020192022-05-06 11:30:57 +08002422 }
2423 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002424
2425
you.chen35020192022-05-06 11:30:57 +08002426 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002427}
you.chen35020192022-05-06 11:30:57 +08002428
qs.xiongf1b525b2022-03-31 00:58:23 -04002429int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2430{
you.chen35020192022-05-06 11:30:57 +08002431 int count = 0;
2432 int index = 0;
2433 char *split_words[128] = {0};
2434 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002435 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002436 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002437
you.chen35020192022-05-06 11:30:57 +08002438 CHECK_WPA_CTRL(CTRL_AP);
2439
2440 DO_REQUEST(lynq_chanspec_cmd);
2441
2442 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2443 for(;index < count; index++) {
2444 if (strncmp(split_words[index], "bw", 2) != 0) {
2445 continue;
2446 }
2447
2448 index++;
2449 if (index >= count) {
2450 return -1;
2451 }
2452
qs.xiong9fbf74e2023-03-28 13:38:22 +08002453 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002454 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2455 return 0;
2456 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002457 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002458 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002459}
qs.xiong0fb469a2022-04-14 03:50:45 -04002460
qs.xiongf1b525b2022-03-31 00:58:23 -04002461int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002462{
you.chen35020192022-05-06 11:30:57 +08002463 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002464 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002465 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002466
you.chen35020192022-05-06 11:30:57 +08002467 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002468
qs.xiong9fbf74e2023-03-28 13:38:22 +08002469 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2470 {
you.chen35020192022-05-06 11:30:57 +08002471 return -1;
2472 }
2473
2474 system("wl down");
2475 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002476 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002477 return -1;
2478 }
2479 system("wl up");
2480 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002481}
qs.xiong0fb469a2022-04-14 03:50:45 -04002482
qs.xiongf1b525b2022-03-31 00:58:23 -04002483int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002484{
you.chen35020192022-05-06 11:30:57 +08002485 int count = 0;
2486 int index = 0;
2487 char *split_words[128] = {0};
2488 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002489 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002490 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002491
you.chen35020192022-05-06 11:30:57 +08002492 CHECK_WPA_CTRL(CTRL_AP);
2493
2494 DO_REQUEST(lynq_chanspec_cmd);
2495
2496 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002497 for(;index < count; index++)
2498 {
2499 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002500 if (strncmp(split_words[index], "channel", 2) != 0) {
2501 continue;
2502 }
2503
2504 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002505 if (index >= count)
2506 {
you.chen35020192022-05-06 11:30:57 +08002507 return -1;
2508 }
2509
2510 *channel = atoi(split_words[index]);
2511 return 0;
2512 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002513 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002514 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002515}
2516
2517
you.chen35020192022-05-06 11:30:57 +08002518int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002519{
you.chen6c2dd9c2022-05-16 17:55:28 +08002520 char ssid[MAX_CMD] = {0};
2521 int freq = 0;
2522 char lynq_auth_cmd[64]={0};
2523 char lynq_auth_alg_cmd[64]={0};
2524 char lynq_psk_cmd[64]={0};
2525 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002526 char lynq_ieee80211_cmd[64]={0};
2527 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002528 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002529 CHECK_IDX(idx, CTRL_AP);
2530
you.chen6c2dd9c2022-05-16 17:55:28 +08002531 CHECK_WPA_CTRL(CTRL_AP);
2532
qs.xiong9fbf74e2023-03-28 13:38:22 +08002533 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2534 {
2535 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002536 return -1;
2537 }
2538
you.chen92fd5d32022-05-25 10:09:47 +08002539 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002540 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002541 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002542 return 0;
2543 }
2544 else {
2545 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2546 ssid[0] = '\0';
2547 }
2548 lynq_wifi_ap_frequency_get(idx, &freq);
2549
2550 DO_OK_FAIL_REQUEST(cmd_disconnect);
2551 DO_OK_FAIL_REQUEST(cmd_remove_all);
2552 if (ssid[0] != '\0') {
2553 lynq_wifi_ap_ssid_set(idx, ssid);
2554 }
2555 if (freq != 0) {
2556 lynq_wifi_ap_frequency_set(idx, freq);
2557 }
2558 }
2559 }
you.chen35020192022-05-06 11:30:57 +08002560
qs.xiong9fbf74e2023-03-28 13:38:22 +08002561 switch(auth){
2562 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002563 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002564 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002565 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002566 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002567 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002568 break;
2569 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002570 case LYNQ_WIFI_AUTH_WEP:
2571 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002572 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002573 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002574 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002575 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2576
2577 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2578 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2579 break;
2580 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002581 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002582 {
2583 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2584 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2585 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2586
2587 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2588 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2589 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2590 break;
2591
2592 }
you.chen35020192022-05-06 11:30:57 +08002593 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002594 {
2595 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2596 {
you.chen35020192022-05-06 11:30:57 +08002597 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2598 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2599 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002600 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2601 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002602 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002603 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002604 }
2605// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2606// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2607 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002608
you.chen35020192022-05-06 11:30:57 +08002609 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2610 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2611 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002612 break;
2613 }
2614 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2615 {
2616 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2617 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2618 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2619 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2620
2621 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2622 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2623 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2624 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2625 break;
2626 }
2627 case LYNQ_WIFI_AUTH_WPA3_PSK:
2628 {
2629 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2630 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002631 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002632 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2633
2634 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2635 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2636 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2637 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2638 break;
2639 }
2640 default:
you.chen35020192022-05-06 11:30:57 +08002641 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002642 RLOGE("auth type [%d] not support now\n", auth);
2643 return -1;
you.chen35020192022-05-06 11:30:57 +08002644 }
2645 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002646 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002647
qs.xiong9fbf74e2023-03-28 13:38:22 +08002648 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002649}
2650
you.chen35020192022-05-06 11:30:57 +08002651int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002652{
you.chen35020192022-05-06 11:30:57 +08002653 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002654 char lynq_auth_alg_str[MAX_RET] = {0};
2655 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002656 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002657 CHECK_IDX(idx, CTRL_AP);
2658
qs.xiong9fbf74e2023-03-28 13:38:22 +08002659 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2660 {
2661 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002662 return -1;
2663 }
2664
qs.xiong9fbf74e2023-03-28 13:38:22 +08002665 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2666 {
2667 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2668 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002669 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002670 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002671 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002672 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002673 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2674 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002675 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002676 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002677 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002678 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002679 else
2680 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002681 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002682 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002683 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002684 }
you.chen35020192022-05-06 11:30:57 +08002685 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002686 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2687 {
2688 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2689 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002690 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002691 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002692 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002693 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2694 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002695 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002696 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002697 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002698 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002699 else
2700 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002701 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002702 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002703 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002704 }
you.chen35020192022-05-06 11:30:57 +08002705 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002706
2707 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2708 {
2709 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2710 return -1;
2711 }
2712
2713 if (memcmp(lynq_auth_str,"1",1) == 0 )
2714 {
2715 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2716 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002717 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002718 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2719 {
2720 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2721 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002722 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 }
2724 else
2725 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002726 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002727 *auth = -1;
2728 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002729
you.chen6c2dd9c2022-05-16 17:55:28 +08002730 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002731}
qs.xiong1af5daf2022-03-14 09:12:12 -04002732
you.chenb95401e2023-05-12 19:39:06 +08002733static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2734{
2735 char status[64];
you.chencba13492023-05-19 13:53:43 +08002736 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002737 curr_status_info curr_state;
2738
2739 CHECK_WPA_CTRL(CTRL_AP);
2740
2741 memset(status, 0, sizeof (status));
2742
2743 curr_state.ap = NULL;
2744 curr_state.state = status;
2745
2746 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002747 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002748 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002749 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002750 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002751 {
2752 return 0;
2753 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002754 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002755 {
2756 DO_REQUEST("RECONNECT");
2757 return inner_check_ap_connected(idx, retry_count+1);
2758 }
you.chencba13492023-05-19 13:53:43 +08002759 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002760 {
2761 printf("retry 10 time\n");
2762 return -1;
2763 }
2764 else
2765 {
you.chen6d247052023-06-01 16:39:54 +08002766 if (strcmp(status, STATE_DISCONNECTED) == 0)
2767 {
2768 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2769 DO_REQUEST(LYNQ_WIFI_CMD);
2770 }
you.chenb95401e2023-05-12 19:39:06 +08002771 return inner_check_ap_connected(idx, retry_count+1);
2772 }
2773 }
2774 return -1;
2775}
qs.xiong1af5daf2022-03-14 09:12:12 -04002776
qs.xiongf1b525b2022-03-31 00:58:23 -04002777int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002778{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002779 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002780 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002781
qs.xiongec8bbeb2023-11-20 15:51:45 +08002782 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002783 CHECK_WPA_CTRL(CTRL_AP);
2784
you.chen0df3e7e2023-05-10 15:56:26 +08002785 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002786 {
you.chen0df3e7e2023-05-10 15:56:26 +08002787 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002788 return -1;
2789 }
you.chen35020192022-05-06 11:30:57 +08002790
qs.xiongb37f8c42023-09-13 21:21:58 +08002791
you.chen35020192022-05-06 11:30:57 +08002792 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2793 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2794
you.chenc9928582023-04-24 15:39:37 +08002795 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2796 if (ret != 0)
2797 {
2798 RLOGE("lynq_wifi_ap_start excute script fail");
2799 return -1;
2800 }
2801
you.chenb95401e2023-05-12 19:39:06 +08002802 if (inner_check_ap_connected(idx, 0) != 0)
2803 {
2804 return -1;
2805 }
2806
you.chen0df3e7e2023-05-10 15:56:26 +08002807 check_tether_and_notify();
qs.xiong9d8f3102023-12-07 20:11:37 +08002808/*
qs.xiong44fac672023-08-29 16:15:55 +08002809 if (g_ap_tmp_watcher_pid == 0)
2810 {
2811 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2812 {
2813 g_ap_tmp_watcher_pid = 0;
2814 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2815 return -1;
2816 }
2817 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2818 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002819*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002820 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002821 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002822}
2823
qs.xiongf1b525b2022-03-31 00:58:23 -04002824int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002825{
you.chen35020192022-05-06 11:30:57 +08002826 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002827}
2828
qs.xiongf1b525b2022-03-31 00:58:23 -04002829int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002830{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002831 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002832 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002833
you.chen35020192022-05-06 11:30:57 +08002834 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002835
you.chen35020192022-05-06 11:30:57 +08002836 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002837
you.chen35020192022-05-06 11:30:57 +08002838 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2839
2840 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2841
you.chenc9928582023-04-24 15:39:37 +08002842
2843 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2844 if (ret != 0)
2845 {
2846 RLOGE("lynq_wifi_ap_start excute script fail");
2847 return -1;
2848 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002849/*
qs.xiong44fac672023-08-29 16:15:55 +08002850 g_ap_tmp_watcher_stop_flag = 1;
2851 if (g_ap_tmp_watcher_pid != 0)
2852 pthread_join(g_ap_tmp_watcher_pid, NULL);
2853 g_ap_tmp_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08002854*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002855 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002856 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002857}
qs.xiong1af5daf2022-03-14 09:12:12 -04002858
qs.xiongf1b525b2022-03-31 00:58:23 -04002859int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002860{
you.chen35020192022-05-06 11:30:57 +08002861 char lynq_disable_cmd[128] = {0};
2862 char lynq_select_cmd[128] = {0};
2863 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002864 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002865 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002866
you.chen35020192022-05-06 11:30:57 +08002867 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002868 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2869 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2870
2871 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2872 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2873 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002874
qs.xiong9fbf74e2023-03-28 13:38:22 +08002875 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002876}
2877
qs.xiongf1b525b2022-03-31 00:58:23 -04002878int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002879{
you.chen35020192022-05-06 11:30:57 +08002880 char lynq_disable_cmd[128] = {0};
2881 char lynq_select_cmd[128] = {0};
2882 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002883 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002884 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002885
you.chen35020192022-05-06 11:30:57 +08002886 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002887
you.chen35020192022-05-06 11:30:57 +08002888 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2889 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2890
2891 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2892 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2893 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002894
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002896}
qs.xiongf1b525b2022-03-31 00:58:23 -04002897
you.chen35020192022-05-06 11:30:57 +08002898int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002899{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002901 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002902 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002903 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002904 RLOGD("enter lynq_ap_password_set");
2905 if( password == NULL )
2906 {
2907 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002908 return -1;
2909 }
2910 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002911 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002912 if(pass_len < 8 || pass_len >= 64)
2913 {
2914 RLOGE("[lynq_ap_password_set]input password len not in rage");
2915 return -1;
you.chen35020192022-05-06 11:30:57 +08002916 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002917
you.chen35020192022-05-06 11:30:57 +08002918 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002919
qs.xiong9fbf74e2023-03-28 13:38:22 +08002920 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2921 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002922 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002923 return -1;
2924 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2926 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002927 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2928 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002929 }
2930
you.chen35020192022-05-06 11:30:57 +08002931 CHECK_WPA_CTRL(CTRL_AP);
2932
qs.xiong9fbf74e2023-03-28 13:38:22 +08002933 if (auth == LYNQ_WIFI_AUTH_WEP)
2934 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002935 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002936 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2937 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2938 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2939 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2940 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002941 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2942 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002943 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA_PSK LYNQ_WIFI_AUTH_WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002944 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2945 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2946 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002947 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2948 {
2949
qs.xiongc8d92a62023-03-29 17:36:14 +08002950 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA2_WPA3 LYNQ_WIFI_AUTH_WPA3_PSK\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002951 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002952 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002953 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2954 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2955
2956 }
2957 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002958 {
2959 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002960 return -1;
2961 }
you.chen35020192022-05-06 11:30:57 +08002962
you.chen35020192022-05-06 11:30:57 +08002963 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002964
qs.xiong9fbf74e2023-03-28 13:38:22 +08002965 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002966}
2967
you.chen35020192022-05-06 11:30:57 +08002968int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002969{
you.chen35020192022-05-06 11:30:57 +08002970 FILE * fp;
2971 int len, ret;
2972 int count, index;
2973 char *split_lines[128] = {0};
2974 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002976
you.chen35020192022-05-06 11:30:57 +08002977 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002978
you.chen35020192022-05-06 11:30:57 +08002979 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2980// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981 if (NULL == fp)
2982 {
2983 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002984 return -1;
2985 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002986
you.chen35020192022-05-06 11:30:57 +08002987 buff = alloca(MAX_RET);
2988 fseek(fp, 0, SEEK_SET);
2989 len = fread(buff, 1, MAX_RET, fp);
2990 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002991
qs.xiong9fbf74e2023-03-28 13:38:22 +08002992 for(index=0; index < len; index ++)
2993 {
2994 if (memcmp(buff + index, "network={", 9) != 0)
2995 {
you.chen35020192022-05-06 11:30:57 +08002996 continue;
2997 }
2998 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002999 for (; index < len; index ++ )
3000 {
3001 if (buff[index] != '}')
3002 {
you.chen35020192022-05-06 11:30:57 +08003003 continue;
3004 }
3005 buff[index] = '\0';
3006 break;
3007 }
3008 len = buff + index - p;
3009 }
3010
3011 count = lynq_split(p, len, '\n', split_lines);
3012
3013 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003014 for(index=0; index < count; index++)
3015 {
you.chen35020192022-05-06 11:30:57 +08003016 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003017 if (p != NULL)
3018 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003019 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003020 if (*p == '\"')
3021 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003022 p++;
3023 }
you.chen35020192022-05-06 11:30:57 +08003024 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003025 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3026 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003027 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003028 if (*p == '\"')
3029 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003030 p++;
3031 }
3032 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003033 else
3034 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003035 continue;
you.chen35020192022-05-06 11:30:57 +08003036 }
3037
3038 strcpy(password, p);
3039
qs.xiong9fbf74e2023-03-28 13:38:22 +08003040 while(*password != '\0')
3041 {
3042 if (*password == '\"')
3043 {
you.chen35020192022-05-06 11:30:57 +08003044 *password = '\0';
3045 break;
3046 }
3047 password++;
3048 }
3049 ret = 0;
3050 break;
3051 } //end for(index=0; index < count; index++)
3052
3053 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003054}
3055
you.chen35020192022-05-06 11:30:57 +08003056static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3057 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003058 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003059
qs.xiong9fbf74e2023-03-28 13:38:22 +08003060 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3061 {
you.chen35020192022-05-06 11:30:57 +08003062 return -1;
3063 }
3064
3065 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003066
qs.xiong9fbf74e2023-03-28 13:38:22 +08003067 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3068 {
3069 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003070 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003071 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003072 {
you.chena6cd55a2022-05-08 12:20:18 +08003073 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003074 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003075 }
you.chen70f377f2023-04-14 18:17:09 +08003076 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3077 {
3078 return 0;
3079 }
you.chena6cd55a2022-05-08 12:20:18 +08003080 }
3081 }
you.chen70f377f2023-04-14 18:17:09 +08003082 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3083 {
3084 return 0;
3085 }
3086
qs.xiong9fbf74e2023-03-28 13:38:22 +08003087 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3088 {
you.chen70f377f2023-04-14 18:17:09 +08003089 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003090 return -1;
3091 }
3092 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3093 {
3094
you.chen70f377f2023-04-14 18:17:09 +08003095 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3096 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003097 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3098 {
3099
3100 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003101 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003102 }else
3103 {
you.chen70f377f2023-04-14 18:17:09 +08003104 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003105 *auth = -1;
3106 return -1;
3107 }
you.chen35020192022-05-06 11:30:57 +08003108 return 0;
3109}
3110
3111int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003112{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003113 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003114 int pass_len, net_no, count, index;
3115 char lynq_tmp_cmd[300]={0};
3116 int net_no_list[128];
3117 lynq_wifi_auth_s net_auth;
3118 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003119 if(pass_len < 8 || pass_len >= 64)
3120 {
3121 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003122 return -1;
3123 }
3124
3125 CHECK_IDX(idx, CTRL_STA);
3126
3127 net_no = -1;
3128 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3129
qs.xiong9fbf74e2023-03-28 13:38:22 +08003130 for (index=0; index < count; index++)
3131 {
you.chen35020192022-05-06 11:30:57 +08003132 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003133 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3134 {
you.chen35020192022-05-06 11:30:57 +08003135 net_no = net_no_list[index];
3136 break;
3137 }
3138 }
3139
qs.xiong9fbf74e2023-03-28 13:38:22 +08003140 if (net_no < 0)
3141 {
you.chen35020192022-05-06 11:30:57 +08003142 return -1;
3143 }
3144
3145 CHECK_WPA_CTRL(CTRL_STA);
3146
3147 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3148
3149 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3150 DO_OK_FAIL_REQUEST(cmd_save_config);
3151
3152 return 0;
3153}
3154
qs.xiongb5dab082023-10-13 14:43:41 +08003155/**
3156* buff data
3157* buff_len size of buff
3158* idx sta
3159* *ap ap info for find ssid && password
3160* password return password
3161*
3162*/
3163static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3164
3165 int ret, network_len, i, ssid_len,curr_auth;
3166 int count, index,org_index;
3167 char *split_lines[128] = {0};
3168 char *p, *ssid, *ssid_end_flag,*ptr;
3169 char tmp_ssid[128]={0};
3170 char tmp_auth[24]={0};
3171
3172 org_index = 0;
3173 network_len = 0;
3174 p = NULL;
3175
3176 CHECK_IDX(idx, CTRL_STA);
3177
3178 while(1){
3179 network_len = 0;
3180 p == NULL;
3181 for(; org_index < buff_len; org_index ++)
3182 {
3183 for(; org_index < buff_len; org_index ++)
3184 {
3185 if (memcmp(buff + org_index, "network={", 9) != 0)
3186 {
3187 continue;
3188 }
3189 p = buff + org_index + 9;
3190
3191 for (; org_index < buff_len; org_index ++ )
3192 {
3193 if (buff[org_index] != '}')
3194 {
3195 continue;
3196 }
3197 buff[org_index] = '\0';
3198 break;
3199 }
3200 network_len = buff + org_index - p;
3201 break;
3202 }
3203
3204 if (p == NULL)
3205 {
3206 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3207 return -1;
3208 }
3209
3210 ssid = strstr(p, "ssid=");
3211 if (ssid != NULL) {
3212 ssid += strlen("ssid=");
3213 if (ssid[0] == '\"')
3214 {
3215 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3216 {
qs.xiongb5dab082023-10-13 14:43:41 +08003217 break;
3218 }
qs.xiongb5dab082023-10-13 14:43:41 +08003219 }
3220 else
3221 {
3222 ssid_end_flag = strstr(ssid, "\n");
3223 if (ssid_end_flag != NULL)
3224 {
3225 ssid_len = (ssid_end_flag - ssid) / 2;
3226 for(i=0; i<ssid_len; i++)
3227 {
3228 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3229 }
3230 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3231 {
qs.xiongb5dab082023-10-13 14:43:41 +08003232 break;
3233 }
3234 }
3235 }
3236 }
3237
3238 }
3239
3240 if (org_index >= buff_len || NULL == p || network_len <= 0)
3241 {
3242
3243 if (buff != NULL)
3244 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3245 return -1;
3246 }
3247
3248 count = lynq_split(p, network_len, '\n', split_lines);
3249 ret = -1;
3250 for( index=0; index < count; index++ )
3251 {
3252 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003253 if(p != NULL)
3254 {
3255 p += 9;
3256 if(memcmp(p,"SAE",3) == 0)
3257 {
3258 curr_auth = 5;
3259 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3260 {
3261 curr_auth = 4;
3262 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3263 {
3264 curr_auth = 3;
3265 }else if(memcmp(p,"NONE",4) == 0 )
3266 {
3267 curr_auth = 0;
3268 }else{
3269 curr_auth = 1;
3270 }
qs.xiongb5dab082023-10-13 14:43:41 +08003271 if( curr_auth < 1 || curr_auth > 6)
3272 {
3273 ret = -1;
3274 }
3275 break;
3276 }
3277 }
3278 if( curr_auth == 0)
3279 {
3280 return 0;
3281 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3282 {
3283 for(index=0; index < count; index++)
3284 {
3285 /*get psw info*/
3286
3287 p = strstr(split_lines[index], "psk=");
3288 if (p != NULL)
3289 {
3290 p += 4;
3291 if (*p == '\"')
3292 {
3293 p++;
3294 }
3295 }
3296 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3297 {
3298 p += 9;
3299 if (*p == '\"')
3300 {
3301 p++;
3302 }
3303 }
3304 else
3305 {
3306 continue;
3307 }
3308
3309 if (*p == '\"')
3310 p++;
3311 strncpy(password, p, 64);
3312 p = password;
3313 while(password - p < 64 && *password != '\0')
3314 {
3315 if (*password == '\"')
3316 {
3317 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003318 ret = 0;
3319 break;
3320 }
3321 password++;
3322 }
3323 break;
3324 }
3325 break;
3326 }
3327 }
3328
3329 return ret;
3330}
3331
3332
3333
you.chen35020192022-05-06 11:30:57 +08003334int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3335
3336 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003337 int len, ret;
3338 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003339 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003340
qs.xiongb5dab082023-10-13 14:43:41 +08003341 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003342 CHECK_IDX(idx, CTRL_STA);
3343
qs.xiong9fbf74e2023-03-28 13:38:22 +08003344 if (NULL == password)
3345 {
3346 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003347 return -1;
3348 }
3349
you.chen35020192022-05-06 11:30:57 +08003350 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003351 if (NULL == fp)
3352 {
3353 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003354 return -1;
3355 }
3356
you.chen6d247052023-06-01 16:39:54 +08003357 fseek(fp, 0, SEEK_END);
3358 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003359 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003360
qs.xiongb5dab082023-10-13 14:43:41 +08003361 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003362 {
3363 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3364 return -1;
3365 }
3366
you.chen35020192022-05-06 11:30:57 +08003367 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003368 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003369 fclose(fp);
3370
qs.xiongb5dab082023-10-13 14:43:41 +08003371
3372 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3373
3374 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003375 {
qs.xiongb04dc852024-03-27 21:55:32 +08003376 //RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
qs.xiongb5dab082023-10-13 14:43:41 +08003377 free(info_buff);
3378 return 0;
you.chen35020192022-05-06 11:30:57 +08003379 }
qs.xiongb5dab082023-10-13 14:43:41 +08003380 else{
3381 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003382 return -1;
3383 }
3384
you.chen35020192022-05-06 11:30:57 +08003385}
3386
qs.xiongb5dab082023-10-13 14:43:41 +08003387
you.chen35020192022-05-06 11:30:57 +08003388static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3389{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003390 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003391
qs.xiong9fbf74e2023-03-28 13:38:22 +08003392 if (sta_ssid == NULL)
3393 {
3394 RLOGE("sta_ssid is null\n");
3395 return -1;
you.chen35020192022-05-06 11:30:57 +08003396 }
3397
qs.xiong9fbf74e2023-03-28 13:38:22 +08003398 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003399
3400 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3401
3402 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3403// DO_OK_FAIL_REQUEST(cmd_save_config);
3404
qs.xiong9fbf74e2023-03-28 13:38:22 +08003405 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003406
3407}
3408
you.chen35020192022-05-06 11:30:57 +08003409static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003410{
you.chen35020192022-05-06 11:30:57 +08003411 char lynq_disable_cmd[128]={0};
3412 char lynq_select_cmd[128]={0};
3413
3414 CHECK_WPA_CTRL(CTRL_STA);
3415
qs.xiong9fbf74e2023-03-28 13:38:22 +08003416 if (save != 0)
3417 {
you.chenc29444e2022-06-07 18:01:16 +08003418 if (start_flag != 0)
3419 {
3420 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3421 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3422 }
3423 else
3424 {
3425 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3426 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3427 }
you.chen35020192022-05-06 11:30:57 +08003428 DO_OK_FAIL_REQUEST(cmd_save_config);
3429 }
3430
qs.xiong9fbf74e2023-03-28 13:38:22 +08003431 if (start_flag == 0)
3432 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003433 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003434 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3435 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003436 else
3437 {
you.chen35020192022-05-06 11:30:57 +08003438 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3439 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3440 }
3441
3442 return 0;
3443}
3444
3445int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3446{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003447 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003448 CHECK_IDX(idx, CTRL_STA);
3449
you.chen6c2dd9c2022-05-16 17:55:28 +08003450 curr_status_info curr_state;
3451 ap_info_s ap_info;
3452 curr_state.ap = &ap_info;
3453 curr_state.state = NULL;
3454
qs.xiong9fbf74e2023-03-28 13:38:22 +08003455 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3456 {
you.chend2fef3f2023-02-13 10:50:35 +08003457 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003458 return 0;
3459 }
3460
3461 return -1;
you.chen35020192022-05-06 11:30:57 +08003462}
3463
3464int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3465{
qs.xiong5d716d22023-09-20 20:08:39 +08003466 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003467 scan_info_s *scan_list = NULL;
3468 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003469 int scan_len=0;
3470 int save_len=0;
3471 int best_index = -1;
3472 int best_scan_index = -1;
3473 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003474 int i, j, ret;
3475
3476 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003477
3478 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003479 if (info == NULL)
3480 {
you.chen35020192022-05-06 11:30:57 +08003481 return -1;
3482 }
3483
3484 curr_status_info curr_state;
3485 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003486 char status[64];
you.chen35020192022-05-06 11:30:57 +08003487
you.chen9ac66392022-08-06 17:01:16 +08003488 memset(&ap_info, 0, sizeof (ap_info));
3489 memset(status, 0, sizeof (status));
3490
3491 curr_state.ap = &ap_info;
3492 curr_state.state = status;
3493
qs.xiong9fbf74e2023-03-28 13:38:22 +08003494 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3495 {
you.chen35020192022-05-06 11:30:57 +08003496 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003497 if (strcmp(status, STATE_COMPLETED) == 0)
3498 {
3499 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003500 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003501 }
3502 else
3503 {
3504 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003505 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003506 }
you.chen593621d2023-04-27 17:52:44 +08003507 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003508 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003509 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003510 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003511 return 0;
3512 }
3513
you.chen9ac66392022-08-06 17:01:16 +08003514 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003515 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003516 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3517 {
you.chen9ac66392022-08-06 17:01:16 +08003518 if (NULL != scan_list)
3519 {
3520 free(scan_list);
3521 }
you.chen35020192022-05-06 11:30:57 +08003522 return -1;
3523 }
3524
qs.xiong9fbf74e2023-03-28 13:38:22 +08003525 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3526 {
you.chen9ac66392022-08-06 17:01:16 +08003527 if (NULL != scan_list)
3528 {
3529 free(scan_list);
3530 }
3531 if (NULL != save_list)
3532 {
3533 free(save_list);
3534 }
you.chen35020192022-05-06 11:30:57 +08003535 return -1;
3536 }
3537
qs.xiong9fbf74e2023-03-28 13:38:22 +08003538 for (i=0; i < save_len; i++)
3539 {
3540 for (j=0; j < scan_len; j++)
3541 {
you.chen35020192022-05-06 11:30:57 +08003542 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003543 && save_list[i].base_info.auth == scan_list[j].auth)
3544 {
3545 if (best_rssi == 0)
3546 {
you.chen9ac66392022-08-06 17:01:16 +08003547 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003548 best_rssi = scan_list[j].rssi;
3549 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003550 else if (best_rssi > scan_list[j].rssi)
3551 {
you.chen35020192022-05-06 11:30:57 +08003552 best_index = i;
3553 best_scan_index = j;
3554 best_rssi = scan_list[j].rssi;
3555 }
you.chend2fef3f2023-02-13 10:50:35 +08003556 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 +08003557 break;
3558 }
3559 }
3560 }
3561
qs.xiong9fbf74e2023-03-28 13:38:22 +08003562 if (best_index >= 0)
3563 {
you.chen35020192022-05-06 11:30:57 +08003564 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003565 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 +08003566 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003567 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003568 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003569 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003570 }
3571
you.chen9ac66392022-08-06 17:01:16 +08003572 if (NULL != scan_list)
3573 {
3574 free(scan_list);
3575 }
3576 if (NULL != save_list)
3577 {
3578 free(save_list);
3579 }
3580
qs.xiong5d716d22023-09-20 20:08:39 +08003581 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003582 return ret;
you.chen35020192022-05-06 11:30:57 +08003583}
3584
3585static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3586{
qs.xiongc8d92a62023-03-29 17:36:14 +08003587 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003588 char lynq_ket_mgmt_cmd[64]={0};
3589 char lynq_pairwise_cmd[64]={0};
3590 char lynq_psk_cmd[64]={0};
3591
3592 CHECK_WPA_CTRL(CTRL_STA);
3593
qs.xiong9fbf74e2023-03-28 13:38:22 +08003594 switch(auth)
3595 {
3596 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003597 {
3598 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003599
you.chen35020192022-05-06 11:30:57 +08003600 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003601// DO_OK_FAIL_REQUEST(cmd_save_config);
3602 break;
3603 }
3604 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003605 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003606 {
3607 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3608 {
you.chen35020192022-05-06 11:30:57 +08003609 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3610 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003611 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3612 {
you.chena6cd55a2022-05-08 12:20:18 +08003613 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003614 }
3615 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3616 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003617
you.chen35020192022-05-06 11:30:57 +08003618 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3619 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3620 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003621
qs.xiong9fbf74e2023-03-28 13:38:22 +08003622 if (password != NULL)
3623 {
you.chen35020192022-05-06 11:30:57 +08003624 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3625 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003626 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003627 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003628
you.chen35020192022-05-06 11:30:57 +08003629// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003630 break;
3631 }
3632 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3633 {
qs.xiong3e506812023-04-06 11:08:48 +08003634 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003635 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003636 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3637 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3638
qs.xiong3e506812023-04-06 11:08:48 +08003639 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003640 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3641 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3642 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3643
3644 break;
3645 }
3646 case LYNQ_WIFI_AUTH_WPA3_PSK:
3647 {
qs.xiong3e506812023-04-06 11:08:48 +08003648 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003649 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003650 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3651 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3652
qs.xiongb37f8c42023-09-13 21:21:58 +08003653 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003654 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3655 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3656 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3657
3658 break;
3659 }
3660 default:
3661 return -1;
you.chen35020192022-05-06 11:30:57 +08003662 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003663
qs.xiong9fbf74e2023-03-28 13:38:22 +08003664 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003665}
qs.xiong7a105ce2022-03-02 09:43:11 -05003666
you.chen35020192022-05-06 11:30:57 +08003667static int inner_get_curr_net_no(int interface) {
3668 curr_status_info curr_state;
3669 curr_state.ap = NULL;
3670 curr_state.state = NULL;
3671
qs.xiong9fbf74e2023-03-28 13:38:22 +08003672 if (0 != inner_get_status_info(interface, &curr_state))
3673 {
you.chen35020192022-05-06 11:30:57 +08003674 return -1;
3675 }
3676
3677 return curr_state.net_no;
3678}
3679
3680int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003681{
you.chen35020192022-05-06 11:30:57 +08003682 int net_no;
3683 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003684
you.chen35020192022-05-06 11:30:57 +08003685 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003686
qs.xiong9fbf74e2023-03-28 13:38:22 +08003687 if (net_no < 0)
3688 {
you.chen35020192022-05-06 11:30:57 +08003689 return -1;
3690 }
3691
3692 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003693}
3694
you.chenb95401e2023-05-12 19:39:06 +08003695int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
qs.xiong7a105ce2022-03-02 09:43:11 -05003696{
you.chen35020192022-05-06 11:30:57 +08003697 int count, net_no, index;
3698 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003699 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003700 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003701 curr_status_info curr_state;
3702 ap_info_s ap_info;
3703 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003704
qs.xiong9fbf74e2023-03-28 13:38:22 +08003705 if (ssid == NULL || *ssid == '\0')
3706 {
3707 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003708 return -1;
3709 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003710
qs.xiong9fbf74e2023-03-28 13:38:22 +08003711 if (LYNQ_WIFI_AUTH_OPEN != auth)
3712 {
3713 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003714 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003715 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003716 return -1;
3717 }
3718 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003719
you.chen70f377f2023-04-14 18:17:09 +08003720
3721 pthread_mutex_lock(&s_global_check_mutex);
3722 if (s_sta_status != INNER_STA_STATUS_INIT)
3723 {
3724 s_sta_status = INNER_STA_STATUS_CANCEL;
3725 pthread_cond_signal(&s_global_check_cond);
3726 }
3727 pthread_mutex_unlock(&s_global_check_mutex);
3728
you.chen35020192022-05-06 11:30:57 +08003729 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003730 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003731
3732 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003733 memset(&ap_info, 0, sizeof (ap_info));
3734 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003735
you.chen70f377f2023-04-14 18:17:09 +08003736 curr_state.ap = &ap_info;
3737 curr_state.state = status;
3738
3739 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003740 {
you.chen70f377f2023-04-14 18:17:09 +08003741 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3742 {
3743 net_no = curr_state.net_no;
3744 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3745 && strcmp(ap_info.psw, psw) == 0)
3746 {
3747 RLOGD("already connected\n");
3748
3749 pthread_mutex_lock(&s_global_check_mutex);
3750 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003751 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003752 pthread_cond_signal(&s_global_check_cond);
3753 pthread_mutex_unlock(&s_global_check_mutex);
3754 return 0;
3755 }
you.chen35020192022-05-06 11:30:57 +08003756 }
3757 }
3758
you.chen70f377f2023-04-14 18:17:09 +08003759 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003760 {
you.chen70f377f2023-04-14 18:17:09 +08003761 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3762
3763 for (index=0; index < count; index++)
3764 {
3765 net_auth = -1;
3766 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3767 {
3768 net_no = net_no_list[index];
3769 break;
3770 }
you.chen35020192022-05-06 11:30:57 +08003771 }
3772
you.chen70f377f2023-04-14 18:17:09 +08003773 if (net_no < 0)
3774 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003775 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3776 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3777 {
3778 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3779 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3780 DO_OK_FAIL_REQUEST(rm_net_cmd);
3781 }
you.chen70f377f2023-04-14 18:17:09 +08003782 net_no = lynq_add_network(CTRL_STA);
3783 if (net_no == -1)
3784 {
3785 return -1;
3786 }
3787
3788 RLOGD("net no is %d\n", net_no);
3789 if (0 != inner_set_sta_ssid(net_no, ssid))
3790 {
3791 return -1;
3792 }
you.chen35020192022-05-06 11:30:57 +08003793 }
3794 }
3795
qs.xiong9fbf74e2023-03-28 13:38:22 +08003796 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3797 {
you.chen35020192022-05-06 11:30:57 +08003798 return -1;
3799 }
3800
you.chen70f377f2023-04-14 18:17:09 +08003801
3802 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003803 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003804 usleep(200*1000);
3805
qs.xiong09560402023-10-27 21:58:55 +08003806 pthread_mutex_lock(&s_global_check_mutex);
3807 lynq_sta_removeElement(net_no);
3808 pthread_mutex_unlock(&s_global_check_mutex);
3809
you.chen70f377f2023-04-14 18:17:09 +08003810 ret = inner_sta_start_stop(net_no, 1, 1);
3811
3812 pthread_mutex_lock(&s_global_check_mutex);
3813 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003814 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003815 strcpy(s_sta_current_connecting_ssid, ssid);
3816 struct timeval now;
3817 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003818 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003819 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3820 pthread_cond_signal(&s_global_check_cond);
3821 pthread_mutex_unlock(&s_global_check_mutex);
3822 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003823}
3824
you.chenb95401e2023-05-12 19:39:06 +08003825int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3826{
3827 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3828}
3829
you.chen35020192022-05-06 11:30:57 +08003830int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003831{
you.chen35020192022-05-06 11:30:57 +08003832 ap_info_s ap;
3833 curr_status_info curr_state;
3834 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003835
qs.xiong9fbf74e2023-03-28 13:38:22 +08003836 if (ssid == NULL || *ssid == '\0')
3837 {
3838 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003839 return -1;
3840 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003841
you.chen35020192022-05-06 11:30:57 +08003842 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003843
you.chen35020192022-05-06 11:30:57 +08003844 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003845 curr_state.state = NULL;
3846
qs.xiong9fbf74e2023-03-28 13:38:22 +08003847 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3848 {
you.chen35020192022-05-06 11:30:57 +08003849 return 0;
3850 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003851
qs.xiong9fbf74e2023-03-28 13:38:22 +08003852 if (strcmp(ap.ap_ssid, ssid) != 0)
3853 {
you.chen35020192022-05-06 11:30:57 +08003854 return 0;
3855 }
3856
you.chen70f377f2023-04-14 18:17:09 +08003857 pthread_mutex_lock(&s_global_check_mutex);
3858 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3859 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003860 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003861}
qs.xiong97fa59b2022-04-07 05:41:29 -04003862
qs.xiongc93bf2b2023-08-25 10:22:08 +08003863int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3864{
qs.xiong09560402023-10-27 21:58:55 +08003865 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003866 ap_info_s ap;
3867 curr_status_info curr_state;
3868 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003869 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003870
3871 if (ssid == NULL || *ssid == '\0')
3872 {
3873 RLOGE("input ssid is NULL\n");
3874 return -1;
3875 }
3876
3877 CHECK_IDX(idx, CTRL_STA);
3878
3879
3880 curr_state.ap = &ap;
3881 curr_state.state = NULL;
3882
3883 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3884 {
3885 return 0;
3886 }
3887
3888 if (strcmp(ap.ap_ssid, ssid) != 0)
3889 {
3890 return 0;
3891 }
3892
3893 pthread_mutex_lock(&s_global_check_mutex);
3894 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003895 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3896 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3897 {
3898 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_net[%d] is %d,current disconnet network is %d",i,g_history_disconnect_net[i],curr_state.net_no);
3899 if( g_history_disconnect_net[i] == curr_state.net_no)
3900 {
3901 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3902 check_history_idx_flag = 1;
3903 break;
3904 }
3905 }
3906 if ( check_history_idx_flag == 0)
3907 {
3908 RLOGD("current need add ap idx is %d ,g_history_disconnect_valid_num is %d line %d",curr_state.net_no,g_history_disconnect_valid_num,__LINE__);
3909 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3910 g_history_disconnect_valid_num++;
3911 }
3912 RLOGD("%s %d",__func__,__LINE__);
3913 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003914 pthread_mutex_unlock(&s_global_check_mutex);
3915 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3916
3917}
3918
3919
you.chena6cd55a2022-05-08 12:20:18 +08003920int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3921{
qs.xiong1b5e3472023-11-27 17:42:20 +08003922 RLOGD("enter %s %d func",__func__,__LINE__);
qs.xiongad2f89d2023-01-18 13:17:41 +08003923 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3924 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003925
you.chen35020192022-05-06 11:30:57 +08003926 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003927 CHECK_WPA_CTRL(CTRL_STA);
3928
you.chenc9928582023-04-24 15:39:37 +08003929 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3930 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003931 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003932 RLOGE("lynq_wifi_sta_start excute script fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08003933 return -1;
3934 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003935
qs.xiongad2f89d2023-01-18 13:17:41 +08003936 system(lynq_enable_sta_cmd);
3937 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003938 pthread_mutex_lock(&s_global_check_mutex);
3939 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003940 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003941 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08003942 RLOGD("end %s %d func",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003943 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003944}
3945
you.chen6d247052023-06-01 16:39:54 +08003946static int inner_get_status_info_state (int interface, char *state) {
3947 curr_status_info curr_state;
3948 curr_state.ap = NULL;
3949 curr_state.state = state;
3950 return inner_get_status_info(interface, &curr_state);
3951}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003952
3953int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3954{
3955
qs.xiongb37f8c42023-09-13 21:21:58 +08003956 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3957 int tmp_open_idx[128];
3958 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003959
qs.xiongb37f8c42023-09-13 21:21:58 +08003960 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003961 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003962 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3963 pthread_mutex_unlock(&s_global_check_mutex);
3964 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003965 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003966 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003967 }
3968
qs.xiongb37f8c42023-09-13 21:21:58 +08003969 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003970 return 0;
3971}
3972
3973
you.chen35020192022-05-06 11:30:57 +08003974int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003975{
qs.xiongec8bbeb2023-11-20 15:51:45 +08003976 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003977 int i=0;
3978 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003979
you.chena6cd55a2022-05-08 12:20:18 +08003980 CHECK_IDX(idx, CTRL_STA);
3981 CHECK_WPA_CTRL(CTRL_STA);
3982
you.chen6d247052023-06-01 16:39:54 +08003983 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003984 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003985
3986 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3987 if (ret != 0)
3988 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003989 RLOGE("lynq_wifi_sta_stop excute script fail %s %d",__func__,__LINE__);
you.chenc9928582023-04-24 15:39:37 +08003990 return -1;
3991 }
3992
you.chen6d247052023-06-01 16:39:54 +08003993 for (i=0; i < 30; i++) // to check if sta is realy stoped
3994 {
3995 if (inner_get_status_info_state(idx, state) != 0)
3996 {
3997 break;
3998 }
3999
4000 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
4001 {
4002 break;
4003 }
qs.xiong1b5e3472023-11-27 17:42:20 +08004004 RLOGD("lynq_wifi_sta_stop curr state %s %s %d", state,__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004005 usleep(SLEEP_TIME_ON_IDLE);
4006 }
qs.xiongb37f8c42023-09-13 21:21:58 +08004007 pthread_mutex_lock(&s_global_check_mutex);
4008 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
4009 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004010 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08004011 return 0;
4012// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04004013}
qs.xiongfcc914b2023-07-06 21:16:20 +08004014int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
4015{
4016 char LYNQ_DISABLE_CMD[128]={0};
4017 CHECK_IDX(idx, CTRL_STA);
4018 CHECK_WPA_CTRL(CTRL_STA);
4019 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
4020 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
4021 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
4022 return 0;
4023}
qs.xiong7a105ce2022-03-02 09:43:11 -05004024
you.chen35020192022-05-06 11:30:57 +08004025//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
4026// int i, count;
4027// char *p;
4028// const char * FLAG_SSID = "ssid=";
4029// const char * FLAG_SBSID = "bssid=";
4030// const char * FLAG_KEY_MGMT = "key_mgmt=";
4031// const char * FLAG_FREQ = "freq=";
4032// char lynq_sta_cmd[MAX_CMD];
4033// char *split_lines[128] = {0};
4034
4035// CHECK_WPA_CTRL(CTRL_AP);
4036
4037// sprintf(lynq_sta_cmd, "STA %s", bssid);
4038
4039// DO_REQUEST(lynq_sta_cmd);
4040
4041// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4042
4043// for(i=0; i < count; i++) {
4044// p = strstr(split_lines[i], FLAG_SSID);
4045// if (p != NULL) {
4046// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
4047// continue;
4048// }
4049// }
4050
4051// lynq_get_interface_ip(idx, ap->ap_ip);
4052// lynq_ap_password_set(idx, ap->psw);
4053
4054// return 0;
4055//}
4056
4057static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4058 curr_status_info curr_state;
4059 curr_state.ap = ap;
4060 curr_state.state = NULL;
4061 return inner_get_status_info(interface, &curr_state);
4062}
4063
4064int 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 -04004065{
qs.xiong5071c802023-09-06 14:04:15 +08004066 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004067 int index, line_count;
4068 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004069 const char *lynq_first_sta_cmd = "STA-FIRST";
4070 char lynq_next_sta_cmd[MAX_CMD];
4071 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004072 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004073
you.chen35020192022-05-06 11:30:57 +08004074 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004075
you.chen35020192022-05-06 11:30:57 +08004076 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004077
you.chenb95401e2023-05-12 19:39:06 +08004078 // ap_info_s * tmp_ap;
4079 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004080 if (ap == NULL || list == NULL || len == NULL)
4081 {
4082 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004083 return -1;
4084 }
4085
you.chenb95401e2023-05-12 19:39:06 +08004086 // ap = &tmp_ap;
4087 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004088 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004089 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004090
you.chenb95401e2023-05-12 19:39:06 +08004091 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004092 {
you.chenb95401e2023-05-12 19:39:06 +08004093 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004094 return -1;
4095 }
4096
4097 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4098 lynq_ap_password_get(idx, (*ap)->psw);
4099
you.chen35020192022-05-06 11:30:57 +08004100 DO_REQUEST(lynq_first_sta_cmd);
4101
4102 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004103 while (reply_len > 0)
4104 {
4105 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004106 {
you.chen35020192022-05-06 11:30:57 +08004107 break;
4108 }
4109 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4110 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4111 strcpy(bssid[index], split_lines[0]);
4112 index++;
4113 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4114 reply_len = MAX_RET;
4115 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004116 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004117 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004118 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004119 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004120 break;
4121 }
4122 }
4123
4124 *len = index;
4125
4126 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004127 for (index=0; index < *len; index++)
4128 {
you.chend2fef3f2023-02-13 10:50:35 +08004129 dev_info = &(*list)[index];
4130 memset(dev_info, 0, sizeof(device_info_s));
4131 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4132 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4133 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4134 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004135 free(bssid[index]);
4136 }
qs.xiong5071c802023-09-06 14:04:15 +08004137 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004138 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004139}
4140
you.chen35020192022-05-06 11:30:57 +08004141int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004142{
you.chen35020192022-05-06 11:30:57 +08004143 int i, count, index, count_words;
4144 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4145 char *split_lines[128] = {0};
4146 char *split_words[128] = {0};
4147 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004148
qs.xiong9fbf74e2023-03-28 13:38:22 +08004149 if (list == NULL || len == NULL)
4150 {
you.chen35020192022-05-06 11:30:57 +08004151 return -1;
4152 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004153
you.chen9ac66392022-08-06 17:01:16 +08004154 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4155 {
4156 usleep(100 * 1000);
4157 }
4158
you.chen35020192022-05-06 11:30:57 +08004159 CHECK_IDX(idx, CTRL_STA);
4160
4161 CHECK_WPA_CTRL(CTRL_STA);
4162
4163 DO_REQUEST(lynq_scan_result_cmd);
4164
4165 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4166 *len = count - 1;
4167 *list = malloc(sizeof (scan_info_s) * *len);
4168
4169 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004170 for (index=0; index <count_words; index++)
4171 {
4172 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004173 }
4174
qs.xiong9fbf74e2023-03-28 13:38:22 +08004175 for(index = 1;index < count; index++)
4176 {
4177 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004178 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004179 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4180 if (count_words < 4)
4181 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004182 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004183 //bssid / frequency / signal level / flags / ssid
4184 p = (*list) + index - 1;
4185 strcpy(p->mac, split_words[0]);
4186 p->band = convert_band_from_freq(atoi(split_words[1]));
4187 p->rssi = -1 * atoi( split_words[2]);
4188 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004189 if (count_words == 4) // ssid hided
4190 {
4191 p->ssid[0] = '\0';
4192 }
4193 else
4194 {
4195 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4196 }
you.chen35020192022-05-06 11:30:57 +08004197 }
4198
4199 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004200}
qs.xiong97fa59b2022-04-07 05:41:29 -04004201
you.chen35020192022-05-06 11:30:57 +08004202int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4203{
4204 int count, net_no, index;
4205 int net_no_list[128];
4206 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004207
you.chen35020192022-05-06 11:30:57 +08004208 char lynq_remove_cmd[MAX_CMD];
4209
qs.xiong9fbf74e2023-03-28 13:38:22 +08004210 if (ssid == NULL || *ssid == '\0')
4211 {
4212 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004213 return -1;
4214 }
4215
4216 CHECK_IDX(idx, CTRL_STA);
4217
4218 CHECK_WPA_CTRL(CTRL_STA);
4219
4220 net_no = -1;
4221 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4222
qs.xiong9fbf74e2023-03-28 13:38:22 +08004223 for (index=0; index < count; index++)
4224 {
you.chen35020192022-05-06 11:30:57 +08004225 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004226 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4227 {
you.chen35020192022-05-06 11:30:57 +08004228 net_no = net_no_list[index];
4229 break;
4230 }
4231 }
4232
qs.xiong9fbf74e2023-03-28 13:38:22 +08004233 if (net_no < 0)
4234 {
you.chen35020192022-05-06 11:30:57 +08004235 return 0;
4236 }
4237
4238 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4239
4240 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004241
4242 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4243
4244 pthread_mutex_lock(&s_global_check_mutex);
4245 lynq_sta_removeElement(net_no);
4246 pthread_mutex_unlock(&s_global_check_mutex);
4247
4248 RLOGD("%s %d",__func__,__LINE__);
4249 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004250 DO_OK_FAIL_REQUEST(cmd_save_config);
4251
4252 return 0;
4253}
4254
4255int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004256{
you.chend2fef3f2023-02-13 10:50:35 +08004257 int count, index;
you.chen35020192022-05-06 11:30:57 +08004258 int net_no_list[128];
4259 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004260 RLOGD("enter lynq_get_sta_saved_ap api\n");
4261 if (list == NULL || len == NULL)
4262 {
4263 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004264 return -1;
4265 }
4266
4267 CHECK_IDX(idx, CTRL_STA);
4268
4269// CHECK_WPA_CTRL(CTRL_STA);
4270
4271 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004272 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004273
you.chen057aac42023-04-13 14:06:58 +08004274 if (count < 0)
4275 {
4276 RLOGE("list network fail");
4277 return count;
4278 }
4279 else if (count == 0)
4280 {
4281 *list = NULL;
4282 *len = 0;
4283 return 0;
4284 }
4285
you.chen35020192022-05-06 11:30:57 +08004286 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004287 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004288 *len = count;
4289
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 for (index=0; index < count; index++)
4291 {
you.chen35020192022-05-06 11:30:57 +08004292 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004293 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004294 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004295 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004296 {
you.chen35020192022-05-06 11:30:57 +08004297 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4298 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004299 else
you.chen057aac42023-04-13 14:06:58 +08004300 {
you.chen35020192022-05-06 11:30:57 +08004301 (*list)[index].base_info.band = -1;
4302 }
you.chen057aac42023-04-13 14:06:58 +08004303 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004304 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004305 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004306 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004307 return 0;
4308}
4309
4310int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4311{
you.chen0c9bee22023-10-25 13:03:14 +08004312 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004313 {
you.chen0c9bee22023-10-25 13:03:14 +08004314 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4315 g_sta_fake_scan_finish_flag = 1;
4316 return 0;
4317 }
4318 else if (g_sta_conncet_status_flag != 0)
4319 {
4320 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004321 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004322 }
you.chen0c9bee22023-10-25 13:03:14 +08004323
qs.xiongc8d92a62023-03-29 17:36:14 +08004324 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004325 const char *lynq_scan_cmd = "SCAN";
4326
4327 CHECK_IDX(idx, CTRL_STA);
4328
4329 CHECK_WPA_CTRL(CTRL_STA);
4330
you.chen0df3e7e2023-05-10 15:56:26 +08004331 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4332 {
4333 RLOGD("tmp clear scanlist");
4334 system(clean_last_re);
4335 }
you.chen9ac66392022-08-06 17:01:16 +08004336 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004337 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004338 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4339 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004340 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004341 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4342 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004343 g_sta_scan_finish_flag = 1;
4344 return -1;
4345 }
you.chen35020192022-05-06 11:30:57 +08004346
4347 return 0;
4348}
4349
4350int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004351 if (cb == NULL)
4352 {
4353 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004354 return -1;
4355 }
4356
you.chen6d247052023-06-01 16:39:54 +08004357 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004358 g_ap_callback_priv = priv;
4359 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004360 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004361
you.chen6d247052023-06-01 16:39:54 +08004362 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004363 if (g_ap_watcher_pid == 0 )
4364 {
4365 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4366 {
4367 g_ap_watcher_pid = 0;
4368 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4369 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4370 return -1;
4371 }
4372 }
4373
4374 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004375 RLOGD("creat APWatcherTheradProc succeed");
you.chene9d00032023-04-24 13:55:29 +08004376
you.chen35020192022-05-06 11:30:57 +08004377 return 0;
4378}
4379
4380int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004381 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004382 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004383 if (g_ap_callback_priv == priv)
4384 {
you.chen35020192022-05-06 11:30:57 +08004385 g_ap_callback_func = NULL;
4386 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004387 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004388 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004389 return 0;
4390 }
you.chen6d247052023-06-01 16:39:54 +08004391 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004392 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004393 return -1;
4394}
4395
4396int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004397 if (cb == NULL)
4398 {
4399 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004400 return -1;
4401 }
4402
you.chen6d247052023-06-01 16:39:54 +08004403 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004404 g_sta_callback_priv = priv;
4405 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004406 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004407
you.chen6d247052023-06-01 16:39:54 +08004408 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004409 if (g_sta_watcher_pid == 0 ) {
4410 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4411 {
4412 g_sta_watcher_pid = 0;
4413 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4414 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4415 return -1;
4416 }
4417 }
4418
4419 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004420 RLOGD("creat STAWatcherTheradProc succeed");
you.chen35020192022-05-06 11:30:57 +08004421 return 0;
4422}
4423
4424int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004425 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004426 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004427 if (g_sta_callback_priv == priv)
4428 {
you.chen35020192022-05-06 11:30:57 +08004429 g_sta_callback_func = NULL;
4430 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004431 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004432 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004433 return 0;
4434 }
you.chen6d247052023-06-01 16:39:54 +08004435 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004436 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004437 return -1;
4438}
4439
qs.xiongfcc914b2023-07-06 21:16:20 +08004440int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4441 if (cb == NULL)
4442 {
4443 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4444 return -1;
4445 }
4446 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4447 g_sta_auto_callback_priv = priv;
4448 g_sta_auto_callback_func = cb;
4449 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4450 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4451 if (g_sta_auto_watcher_pid == 0 ) {
4452 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4453 {
4454 g_sta_auto_watcher_pid = 0;
4455 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4456 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4457 return -1;
4458 }
4459 }
4460 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004461 RLOGD("creat STAWatcherTheradProc succeed");
qs.xiongfcc914b2023-07-06 21:16:20 +08004462 return 0;
4463}
4464int lynq_unreg_sta_auto_event_callback(void * priv) {
4465 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4466 if (g_sta_auto_callback_priv == priv)
4467 {
4468 g_sta_auto_watcher_stop_flag = 1;
4469 if (g_sta_auto_watcher_pid != 0)
4470 {
4471 pthread_join(g_sta_auto_watcher_pid, NULL);
4472 }
4473 g_sta_auto_watcher_pid = 0;
4474 g_sta_auto_callback_func = NULL;
4475 g_sta_auto_callback_priv = NULL;
4476 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4477 return 0;
4478 }
4479 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4480 return -1;
4481}
you.chen35020192022-05-06 11:30:57 +08004482int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4483{
4484 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004485 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004486 CHECK_IDX(idx, CTRL_AP);
4487
qs.xiong9fbf74e2023-03-28 13:38:22 +08004488 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4489 {
you.chen35020192022-05-06 11:30:57 +08004490 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4491 return 0;
4492 }
4493
qs.xiong9fbf74e2023-03-28 13:38:22 +08004494 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4495 {
you.chen35020192022-05-06 11:30:57 +08004496 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4497 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004498 else
4499 {
you.chen35020192022-05-06 11:30:57 +08004500 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4501 }
4502
4503 return 0;
4504}
4505
4506int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4507 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004508 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004509 CHECK_IDX(idx, CTRL_STA);
4510
qs.xiong9fbf74e2023-03-28 13:38:22 +08004511 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4512 {
you.chen35020192022-05-06 11:30:57 +08004513 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4514 return 0;
4515 }
4516
qs.xiong9fbf74e2023-03-28 13:38:22 +08004517 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4518 {
you.chen35020192022-05-06 11:30:57 +08004519 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4520 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004521 else
4522 {
you.chen35020192022-05-06 11:30:57 +08004523 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4524 }
4525
4526 return 0;
4527}
4528
4529int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4530// CHECK_IDX(idx, CTRL_AP);
4531// int ret = 0;
4532// size_t reply_len = MAX_RET;
4533// char cmd_reply[MAX_RET]={0};
4534// const char * cmd_str = "GET country";
4535// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4536// do{
4537// if (NULL == s_lynq_wpa_ctrl) {
4538// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4539// if (NULL == s_lynq_wpa_ctrl ) {
4540// printf("wpa_ctrl_open fail\n");
4541// return -1;
4542// }
4543// }
4544// }while(0);
4545
4546// do {
4547// reply_len = MAX_RET;
4548// cmd_reply[0] = '\0';
4549// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004550// 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 +08004551// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004552// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004553// return ret;
4554// }
4555// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004556// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004557// }while(0);
4558
4559 FILE *fp;
4560 size_t i = 0;
4561 char lynq_cmd_ret[MAX_RET]={0};
4562
4563// CHECK_IDX(idx, CTRL_AP);
4564
4565 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004566 {
4567 perror("popen error!");
4568 return -1;
4569 }
you.chen35020192022-05-06 11:30:57 +08004570 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4571 {
4572 perror("fread fail!");
4573 return -1;
4574 }
4575
qs.xiong9fbf74e2023-03-28 13:38:22 +08004576 for(i=0; i < strlen(lynq_cmd_ret); i++)
4577 {
4578 if (lynq_cmd_ret[i] == ' ')
4579 {
you.chen35020192022-05-06 11:30:57 +08004580 lynq_cmd_ret[i] = '\0';
4581 break;
4582 }
4583 }
4584
4585 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004586 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004587
4588 int ret=pclose(fp);
4589 if(ret==-1)
4590 {
4591 perror("close file faild");
4592 }
4593
4594 return 0;
4595}
4596
qs.xiong44fac672023-08-29 16:15:55 +08004597
you.chen705a7ef2023-06-01 22:06:45 +08004598static int check_and_init_uci_config(char * country_code)
4599{
4600 FILE * fp;
4601 int is_different = 0;
4602 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4603 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4604 const char * commit_uci_cmd ="uci commit";
4605 char set_country_cmd[MAX_CMD];
4606 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004607
you.chen705a7ef2023-06-01 22:06:45 +08004608 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004609
you.chen705a7ef2023-06-01 22:06:45 +08004610 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004611 {
you.chen705a7ef2023-06-01 22:06:45 +08004612 if (0 != system(create_uci_cmd))
4613 {
4614 RLOGE("creat_uci_cmd fail");
4615 return -1;
4616 }
4617 is_different = 1;
4618 }
4619
4620 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4621 {
4622 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004623 return -1;
4624 }
4625
you.chen705a7ef2023-06-01 22:06:45 +08004626 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004627 {
you.chen705a7ef2023-06-01 22:06:45 +08004628 RLOGE("fread fail!");
4629 fclose(fp);
4630 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004631 }
4632
you.chen705a7ef2023-06-01 22:06:45 +08004633 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4634 {
qs.xiong44fac672023-08-29 16:15:55 +08004635 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
you.chen705a7ef2023-06-01 22:06:45 +08004636 is_different = 1;
4637 }
4638
4639 fclose(fp);
4640
4641 if (is_different)
4642 {
4643 if ( 0 != system(set_country_cmd))
4644 {
4645 RLOGE("set_country_cmd fail");
4646 return -1;
4647 }
4648 if (0 != system(commit_uci_cmd))
4649 {
4650 RLOGE("commmit fail");
4651 }
4652 }
4653
4654 return is_different;
4655}
4656
4657int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4658 char check_current_code[10];
4659 const char * support_country[] = {"CN", "EU"};
4660
4661 int ret,is_different, i, cc_count;
4662
4663 if (country_code == NULL || country_code[0] == '\0')
4664 {
4665 RLOGE("bad country code\n");
4666 return -1;
4667 }
4668
4669 cc_count = sizeof (support_country) / sizeof (char*);
4670 for(i=0; i < cc_count; i++)
4671 {
4672 if (strcmp(support_country[i], country_code) == 0)
4673 {
4674 break;
4675 }
4676 }
4677
4678 if (i >= cc_count)
4679 {
4680 RLOGE("unspported country code %s\n", country_code);
4681 return -1;
4682 }
4683
4684 is_different = check_and_init_uci_config(country_code);
4685 if( is_different < 0 )
4686 {
4687 RLOGE("init set uci fail\n");
4688 return -1;
4689 }
4690
4691 ret = lynq_get_country_code(idx,check_current_code);
4692 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4693 {
4694 ret = lynq_wifi_disable();
4695 if(ret != 0 )
4696 {
4697 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4698 return -1;
4699 }
4700 }
4701
4702 return 0;
you.chen35020192022-05-06 11:30:57 +08004703}
4704
4705int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4706{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004707 RLOGD("enter lynq_get_connect_ap_mac\n");
4708 if (mac == NULL)
4709 {
4710 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004711 return -1;
4712 }
4713
4714 CHECK_IDX(idx, CTRL_STA);
4715 ap_info_s ap;
4716 ap.ap_mac[0] = '\0';
4717
qs.xiong9fbf74e2023-03-28 13:38:22 +08004718 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4719 {
you.chen35020192022-05-06 11:30:57 +08004720 return -1;
4721 }
4722 strcpy(mac, ap.ap_mac);
4723
4724 return 0;
4725}
4726
4727int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4728{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004729 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004730 struct ifaddrs *ifaddr_header, *ifaddr;
4731 struct in_addr * ifa;
4732 const char * ifaName = "wlan0";
4733 if (ip == NULL)
4734 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004735 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004736 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004737 }
you.chenf58b3c92022-06-21 16:53:48 +08004738
qs.xiong9fbf74e2023-03-28 13:38:22 +08004739 if (idx == 1)
4740 {
you.chen0df3e7e2023-05-10 15:56:26 +08004741 ifaName = inner_get_ap_interface_name();
4742 if (ifaName == NULL)
4743 {
4744 RLOGE("[lynq_get_interface_ip] ap name get fail");
4745 return -1;
4746 }
you.chen9ac66392022-08-06 17:01:16 +08004747 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004748 else if (idx != 0)
4749 {
you.chen35020192022-05-06 11:30:57 +08004750 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004751 }
you.chen35020192022-05-06 11:30:57 +08004752
you.chen9ac66392022-08-06 17:01:16 +08004753 if (getifaddrs(&ifaddr_header) == -1)
4754 {
you.chen35020192022-05-06 11:30:57 +08004755 perror("getifaddrs");
4756 return -1;
4757 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004758 }
you.chen35020192022-05-06 11:30:57 +08004759
4760
you.chen9ac66392022-08-06 17:01:16 +08004761 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4762 {
4763 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004764 continue;
you.chen9ac66392022-08-06 17:01:16 +08004765 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4766 {
4767 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4768 {
4769 // is a valid IP4 Address
4770 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4771 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004772 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004773 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004774 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004775 return 0;
4776 }
4777 }
4778 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004779 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004780 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004781 return -1;
you.chen35020192022-05-06 11:30:57 +08004782}
4783
4784int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4785{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004786 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004787 int count;
4788 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004789 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004790 char *split_words[128] = {0};
4791 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4792
4793 CHECK_WPA_CTRL(idx);
4794
4795 DO_REQUEST(lynq_get_mac_cmd);
4796
qs.xiong9fbf74e2023-03-28 13:38:22 +08004797 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4798 {
4799 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004800 return -1;
4801 }
4802
4803 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4804
qs.xiong9fbf74e2023-03-28 13:38:22 +08004805 if (count < 2)
4806 {
you.chen35020192022-05-06 11:30:57 +08004807 return -1;
4808 }
4809
qs.xiong9fbf74e2023-03-28 13:38:22 +08004810 for (i=0; i < strlen(split_words[1]); i++ )
4811 {
4812 if (split_words[1][i] != ' ')
4813 {
you.chen35020192022-05-06 11:30:57 +08004814 break;
4815 }
4816 }
4817
qs.xiongdd6e44c2023-08-08 15:02:53 +08004818 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004819
4820 return 0;
4821}
4822
4823int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4824{
4825// int count;
4826// char *split_words[128] = {0};
4827// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4828
4829// if (rssi == NULL) {
4830// return -1;
4831// }
4832
4833// CHECK_IDX(idx, CTRL_STA);
4834
4835// CHECK_WPA_CTRL(CTRL_STA);
4836
4837// DO_REQUEST(lynq_get_rssi_cmd);
4838
4839// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4840// return -1;
4841// }
4842
4843// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4844
4845// if (count < 2) {
4846// return -1;
4847// }
4848
4849// *rssi = atoi(split_words[1]) * -1;
4850
you.chen35020192022-05-06 11:30:57 +08004851 char lynq_cmd_ret[MAX_RET]={0};
4852
qs.xiongff0ae0f2022-10-11 15:47:14 +08004853/*******change other cmd to get rssi*******
4854 *
4855 *wl rssi ---> wl -i wlan0 rssi
4856 *
4857 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004858 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004859 {
you.chen23c4a5f2023-04-12 16:46:00 +08004860 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004861 return -1;
4862 }
you.chen9f17e4d2022-06-06 17:18:18 +08004863 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004864/****** if got rssi is 0,means sta didn't connected any device****/
4865 if(*rssi == 0)
4866 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004867 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004868 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004869 }
you.chen35020192022-05-06 11:30:57 +08004870
4871 return 0;
4872}
4873
4874int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4875{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004876 RLOGD("enter lynq_get_connect_ap_band\n");
4877 if (band == NULL)
4878 {
you.chen35020192022-05-06 11:30:57 +08004879 return -1;
4880 }
4881
4882 CHECK_IDX(idx, CTRL_STA);
4883 ap_info_s ap;
4884 ap.band = -1;
4885
qs.xiong9fbf74e2023-03-28 13:38:22 +08004886 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4887 {
you.chen35020192022-05-06 11:30:57 +08004888 return -1;
4889 }
4890 *band = ap.band;
4891
4892 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004893}
you.chenf58b3c92022-06-21 16:53:48 +08004894
4895int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4896{
you.chenb95401e2023-05-12 19:39:06 +08004897 int ret;
4898 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004899 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004900
4901 if (ip == NULL)
4902 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004903 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004904 return -1;
4905 }
4906
4907 CHECK_IDX(idx, CTRL_STA);
4908
qs.xionge4cbf1c2023-02-28 18:22:49 +08004909 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004910 {
4911 return -1;
4912 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004913
you.chenb95401e2023-05-12 19:39:06 +08004914 ip[0] = '\0';
4915 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4916 if (ret != 0)
4917 {
4918 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
you.chenb95401e2023-05-12 19:39:06 +08004919 }
4920
4921 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4922 {
4923 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004924 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004925 if (ret != 0)
4926 {
4927 ip[0] = '\0';
4928 return 0;
4929 }
4930 else
4931 {
4932 p = strchr(ip, '\n');
4933 if (p != NULL)
4934 {
4935 *p = '\0';
4936 }
4937 }
4938 }
4939 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004940}
4941
qs.xionge02a5252023-09-20 14:00:21 +08004942int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4943{
4944 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4945 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4946}
4947
qs.xiong026c5c72022-10-17 11:15:45 +08004948int lynq_ap_connect_num(int sta_number)
4949{
4950 char lynq_limit_cmd[32]={0};
4951 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004952 if((sta_number < 1 ) && (sta_number > 15))
4953 {
4954 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004955 return -1;
4956 }
4957 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4958 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004959 if(ret != 0)
4960 {
4961 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004962 }
4963 return 0;
4964}
you.chenf58b3c92022-06-21 16:53:48 +08004965
qs.xiong77905552022-10-17 11:19:57 +08004966int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4967{
4968
4969 char lynq_wifi_acs_cmd[128]={0};
4970 char lynq_cmd_mode[128]={0};
4971 char lynq_cmd_slect[128]={0};
4972
qs.xiong9fbf74e2023-03-28 13:38:22 +08004973 if((acs_mode != 2) && (acs_mode != 5))
4974 {
qs.xiong77905552022-10-17 11:19:57 +08004975 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4976 }
4977
qs.xiong9fbf74e2023-03-28 13:38:22 +08004978 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4979 {
qs.xiong77905552022-10-17 11:19:57 +08004980 return -1;
4981 }
4982
4983 CHECK_IDX(idx, CTRL_AP);
4984
4985 CHECK_WPA_CTRL(CTRL_AP);
4986
4987 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4988 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4989 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4990
4991 DO_OK_FAIL_REQUEST(cmd_disconnect);
4992 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4993 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4994 DO_OK_FAIL_REQUEST(cmd_save_config);
4995 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4996
4997 return 0;
4998}
you.chen0f5c6432022-11-07 18:31:14 +08004999//you.chen add for tv-box start
5000static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
5001 FILE *fp;
5002 //printf("to exec cmd:%s\n", str_cmd);
5003 if((fp=popen(str_cmd,"r"))==NULL)
5004 {
5005 perror("popen error!");
5006 return -1;
5007 }
5008 if((fread(str_cmd_ret,max_len,1,fp))<0)
5009 {
5010 perror("fread fail!");
5011 fclose(fp);
5012 return -1;
5013 }
5014 fclose(fp);
5015 return 0;
5016}
5017
5018static int get_netmask_length(const char* mask)
5019{
5020 int masklen=0, i=0;
5021 int netmask=0;
5022
5023 if(mask == NULL)
5024 {
5025 return 0;
5026 }
5027
5028 struct in_addr ip_addr;
5029 if( inet_aton(mask, &ip_addr) )
5030 {
5031 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08005032 }else
5033 {
you.chen0f5c6432022-11-07 18:31:14 +08005034 netmask = 0;
5035 return 0;
5036 }
5037
5038 while(0 == (netmask & 0x01) && i<32)
5039 {
5040 i++;
5041 netmask = netmask>>1;
5042 }
5043 masklen = 32-i;
5044 return masklen;
5045}
5046
5047static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
5048 int mask_len;
5049 char *p;
5050 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08005051 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5052 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005053 return -1;
5054 p = strstr(str_cmd_ret, "Mask:");
5055 if (p == NULL)
5056 return -1;
5057 mask_len = get_netmask_length(p + 5);
5058 if (mask_len == 0)
5059 return -1;
5060 p = strstr(str_cmd_ret, "inet addr:");
5061 if (p == NULL)
5062 return -1;
5063 strcpy(tmp, p + 10);
5064 p = strstr(tmp, " ");
5065 if (p != NULL)
5066 *p = '\0';
5067 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5068 return 0;
5069}
5070
5071static void GBWWatchThreadProc() {
5072 int i,n, nloop, nmax, ncheckcount, nidlecount;
5073 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5074 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5075 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5076 char *results[16] = {0};
5077 char str_cmd[256] = {0};
5078 char str_cmd_ret[128] = {0};
5079 char dest_ip[32] = {0};
5080 lastAP1Bytes = lastAP2Bytes = 0;
5081 lastAP1Drop = lastAP2Drop = 0;
5082 lastAP1Speed = lastAP2Speed = 0;
5083 setAP1Speed = 50;
5084 setAP2Speed = 80;
5085 nloop = 0;
5086 nmax = 6;
5087 ncheckcount = nidlecount = 0;
5088
you.chen0df3e7e2023-05-10 15:56:26 +08005089 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005090 {
5091 RLOGE("------gbw thread run\n");
5092 return;
5093 }
5094
qs.xiong9fbf74e2023-03-28 13:38:22 +08005095 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005096 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5097 while (dest_ip[0] == '\0') {
5098 sleep(1);
5099 str_cmd_ret[0] = '\0';
5100 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5101 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5102 if (str_cmd_ret[n] == '\n'){
5103 str_cmd_ret[n] = '\0';
5104 break;
5105 }
5106 }
5107 if (str_cmd_ret[0] != '\0')
5108 {
5109 strcpy(dest_ip, str_cmd_ret);
5110 }
5111 }
5112
you.chenc9928582023-04-24 15:39:37 +08005113 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5114 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5115 system_call_v("tc class add dev %s parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000", s_ap_iterface_name);
you.chen0f5c6432022-11-07 18:31:14 +08005116 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5117 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005118 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005119 return;
5120 }
you.chenc9928582023-04-24 15:39:37 +08005121 system_call_v("tc filter add dev %s parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", s_ap_iterface_name, str_cmd_ret);
5122 system_call_v("tc class add dev %s parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000", s_ap_iterface_name);
5123 system_call_v("tc filter add dev %s parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", s_ap_iterface_name, dest_ip);
you.chen0f5c6432022-11-07 18:31:14 +08005124
5125 while (1) {
5126 sleep(1);
5127 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005128 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5129 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5130 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005131 continue;
5132 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005133 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005134 if (n > 9) {
5135 if (strcmp(results[1], "Sent") == 0) {
5136 currAP1Bytes = atoll(results[2]);
5137 }
5138 if (strcmp(results[6], "(dropped") == 0) {
5139 currAP1Drop = atoi(results[7]);
5140 }
5141 }
5142
5143 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005144 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5145 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5146 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005147 continue;
5148 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005149 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005150 if (n > 9) {
5151 if (strcmp(results[1], "Sent") == 0) {
5152 currAP2Bytes = atoll(results[2]);
5153 }
5154 if (strcmp(results[6], "(dropped") == 0) {
5155 currAP2Drop = atoi(results[7]);
5156 }
5157 }
5158
5159 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5160 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5161 lastAP1Bytes = currAP1Bytes;
5162 lastAP2Bytes = currAP2Bytes;
5163 continue;
5164 }
5165
5166 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5167 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5168 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5169 lastAP1Speed = currAP1Speed;
5170 lastAP2Speed = currAP2Speed;
5171 lastAP1Bytes = currAP1Bytes;
5172 lastAP2Bytes = currAP2Bytes;
5173
5174 currSetAP1Speed = setAP1Speed;
5175 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5176 ncheckcount++;
5177 if (ncheckcount > 3) {
5178 ncheckcount = 0;
5179 currSetAP1Speed = 5;
5180 }
5181 }
5182 else {
5183 ncheckcount = 0;
5184 if (currAP1Speed < 5)
5185 nidlecount++;
5186 else
5187 nidlecount = 0;
5188
5189 }
5190
5191 if (nidlecount > 60 ){
5192 currSetAP1Speed = 50;
5193 }
5194
5195 if (currSetAP1Speed != setAP1Speed) {
5196 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005197 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5198 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005199 }
5200 }
5201}
5202
5203int enableGBW(const char* mac) {
5204 int i,len;
5205 char get_ipaddr_cmd[128]={0};
5206 ap_info_s *ap;
5207 device_info_s * list;
5208
5209 if (mac == NULL || g_gbw_enabled == 1)
5210 return -1;
5211 len = strlen(mac);
5212 g_gbw_mac = malloc(len + 1);
5213 for(i=0;i<len;i++) {
5214 if (mac[i] >= 'A' && mac[i] <= 'Z')
5215 {
5216 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5217 }
5218 else
5219 g_gbw_mac[i] = mac[i];
5220 }
5221 g_gbw_mac[i] = '\0';
5222 g_gbw_enabled = 1;
5223
5224 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5225 if (system(get_ipaddr_cmd) == 0) {
5226 //startGBW();
5227 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5228 for (i=0;i<len;i++) {
5229 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5230 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5231 startGBW();
5232 }
5233 free(ap);
5234 free(list);
5235 }
5236 }
5237 return 0;
5238}
5239
5240int disableGBW() {
5241 stopGBW();
5242 free(g_gbw_mac);
5243 g_gbw_mac = NULL;
5244 g_gbw_enabled = 1;
5245 return 0;
5246}
5247
5248static int startGBW() {
5249 if (g_gbw_watcher_pid != 0) {
5250 stopGBW();
5251 }
5252 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5253}
5254
5255static int stopGBW() {
5256 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005257 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005258 pthread_cancel(g_gbw_watcher_pid);
5259 pthread_join(g_gbw_watcher_pid, &retval);
5260 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005261 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5262 if (s_ap_iterface_name[0] != '\0')
5263 {
5264 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5265 system(cmd);
5266 }
you.chen0f5c6432022-11-07 18:31:14 +08005267}
5268//you.chen add for tv-box end