blob: b1d6f7a8558fd4ad5e19ee7e53cd41a8e713e1be [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";
qs.xiong448588f2024-05-21 10:04:04 +080093const char * check_dnsmasq_dhcp_status = "/etc/wg870/scripts/check_dnsmasq_dhcp_status.sh";
you.chenc9928582023-04-24 15:39:37 +080094
95static char s_ap_iterface_name[64] = {0};
96
you.chend2fef3f2023-02-13 10:50:35 +080097struct local_wpa_ctrl{
98 struct wpa_ctrl *ctrl;
99 pthread_mutex_t mutex;
100};
101
qs.xiong09560402023-10-27 21:58:55 +0800102volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800103int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800104
you.chend2fef3f2023-02-13 10:50:35 +0800105static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800106static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
107static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiong8362e222024-03-22 11:20:56 +0800108static pthread_mutex_t s_run_cmd_func_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xionga8cbe222024-04-15 16:42:13 +0800109static pthread_mutex_t s_func_run_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiong8362e222024-03-22 11:20:56 +0800110
qs.xiongfcc914b2023-07-06 21:16:20 +0800111// add for auto connect
112static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800113
114static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800115
you.chen0f5c6432022-11-07 18:31:14 +0800116//you.chen add for tv-box start
117volatile int g_gbw_enabled = 0;
118char * g_gbw_mac = NULL;
119pthread_t g_gbw_watcher_pid = 0;
120static int startGBW();
121static int stopGBW();
122//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800123
124typedef struct __curr_status_info {
125 ap_info_s *ap;
126 char * state;
127 int net_no;
128}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400129
you.chen70f377f2023-04-14 18:17:09 +0800130typedef enum {
131 INNER_STA_STATUS_INIT = 0,
132 INNER_STA_STATUS_CONNECTING,
133 INNER_STA_STATUS_ASSOCIATING,
134 INNER_STA_STATUS_ASSOCIATED,
135 INNER_STA_STATUS_CONNECTED,
136 INNER_STA_STATUS_DISCONNECTING,
137 INNER_STA_STATUS_DISCONNECTED,
138 INNER_STA_STATUS_CANCEL,
139}inner_sta_status_s;
140
141static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
142static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800143volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800144static error_number_s s_sta_error_number = -1;
145static char s_sta_current_connecting_ssid[64] = {0};
146static struct timespec s_sta_connect_timeout;
147const int MAX_CONNNECT_TIME = 15; // second
148pthread_t g_global_watcher_pid = 0;
149static int s_service_invoke_timeout_cnt=0;
150const int FAKE_MAX_INT_VALUE = 99999;
151
qs.xiong09560402023-10-27 21:58:55 +0800152static void print_disconnect_list()
153{
154 int i;
155 for( i = 0; i < g_history_disconnect_valid_num; i++ )
156 {
157 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__);
158 }
159
160 return;
161}
162
163// idex ----> history_disconnect_list[x] index
164static int removeElement(int idex)
165{
166 RLOGD("into removeElement");
167 if( index < 0 )
168 {
169 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
170 return -1;
171 }
172 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]);
173 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
174 g_history_disconnect_valid_num --;
175 RLOGD("end removeElement");
176 return 0;
177}
178static int check_history_disconenct_ap_list(int val)
179{
180 print_disconnect_list();
181 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__);
182 int i;
183 for( i = 0; i < g_history_disconnect_valid_num; i++)
184 {
185 if( val == g_history_disconnect_net[i] )
186 {
187 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
188 RLOGD("end check_history_disconenct_ap_list && return network index");
189 return i;
190 }
191 }
192 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__);
193 return -1;
194}
195
196
197static void lynq_sta_removeElement(int net_no)
198{
199 int ret;
200
201 ret = check_history_disconenct_ap_list(net_no);
202 if( ret == -1 )
203 {
204 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
205 return;
206 }else
207 {
208 ret = removeElement(ret);
209 if( ret == 0 )
210 {
211 RLOGD("removeElement pass %s %d",__func__,__LINE__);
212 return;
213 }
214 }
215
216 return;
217}
218
you.chen70f377f2023-04-14 18:17:09 +0800219static void notify_service_invoke_fail(int error)
220{
qs.xiong17579bb2024-03-11 19:08:06 +0800221 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen70f377f2023-04-14 18:17:09 +0800222 pthread_mutex_lock(&s_global_check_mutex);
223 if (error == -2) //timeout
224 {
225 s_service_invoke_timeout_cnt++;
226 if (s_service_invoke_timeout_cnt > 10)
227 {
228 pthread_cond_signal(&s_global_check_cond);
229 }
230 }
231 else if (error == -1)
232 {
233 // check if can connect wpa service
234 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
235 if (lynq_wpa_ctrl == NULL)
236 {
237 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
238 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800239 }else
240 {
241 wpa_ctrl_close(lynq_wpa_ctrl);
242 }
you.chen70f377f2023-04-14 18:17:09 +0800243 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
244 if (lynq_wpa_ctrl == NULL)
245 {
246 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
247 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800248 }else
249 {
250 wpa_ctrl_close(lynq_wpa_ctrl);
you.chen70f377f2023-04-14 18:17:09 +0800251 }
you.chen70f377f2023-04-14 18:17:09 +0800252 }
253
254 pthread_mutex_unlock(&s_global_check_mutex);
255}
256
you.chenc9928582023-04-24 15:39:37 +0800257static int system_call_v(const char * fmt, ...)
258{
259 char str_cmd[256] = {0};
260 va_list args;
261 va_start(args, fmt);
262 vsprintf(str_cmd, fmt, args);
263 va_end(args);
264 printf("system call----------%s\n", str_cmd);
265 return system(str_cmd);
266}
267
you.chen0df3e7e2023-05-10 15:56:26 +0800268static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
269
270static const char * inner_get_ap_interface_name()
271{
272 char * p;
273 char cmd[128]={0};
274
275 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
276 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
277 {
278 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
279 return NULL;
280 }
281 p = strchr(s_ap_iterface_name, ' ');
282 if (NULL != p)
283 {
284 *p = '\0';
285 }
286 p = strchr(s_ap_iterface_name, '\n');
287 if (NULL != p)
288 {
289 *p = '\0';
290 }
291 if (s_ap_iterface_name[0] == '\0')
292 {
293 return NULL;
294 }
295
296 return s_ap_iterface_name;
297}
298
you.chen70f377f2023-04-14 18:17:09 +0800299static void check_tether_and_notify()
300{
301 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800302 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 +0800303 {
304 return;
305 }
306 pthread_mutex_lock(&s_global_check_mutex);
307 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
308 pthread_cond_signal(&s_global_check_cond);
309 pthread_mutex_unlock(&s_global_check_mutex);
310}
311
you.chend2fef3f2023-02-13 10:50:35 +0800312static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
313 char *reply, size_t *reply_len,
314 void (*msg_cb)(char *msg, size_t len))
315{
316 int ret;
317 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800318 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800319 return -1;
320 }
321 pthread_mutex_lock(&ctrl->mutex);
322 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
323 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800324 if (ret != 0)
325 {
326 notify_service_invoke_fail(ret);
327 }
you.chend2fef3f2023-02-13 10:50:35 +0800328 return ret;
329}
330
331static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
332 int repeat_cnt;
333 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
334 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800335 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800336 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
337 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
338// printf("wait enable finish\n");
339 usleep(500 * 1000);
340 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
341 }
342 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800343 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800344 goto out_addr;
345 }
346 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
347 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
348 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800349 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800350 goto out_addr;
351 }
352 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
353 }
354 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
355out_addr:
356 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
357 return lynq_wpa_ctrl;
358}
359
qs.xiong97fa59b2022-04-07 05:41:29 -0400360#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400361{\
you.chen35020192022-05-06 11:30:57 +0800362 perror((str));\
363 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400364}
365
you.chen35020192022-05-06 11:30:57 +0800366#define CHECK_IDX(idx, type) do { \
367 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
368 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800369 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800370 return -1; \
371 } \
372 }while (0)
373
374#define CHECK_WPA_CTRL(index) int ret = 0;\
375 size_t reply_len = MAX_RET; \
376 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800377 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800378 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800379 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
380 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800381 }while(0)
382
383#define DO_REQUEST(cmd_str) do { \
384 reply_len = MAX_RET;\
385 cmd_reply[0] = '\0'; \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800386 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800387 { \
388 RLOGD("to call [%s]\n", cmd_str); \
389 }else \
390 { \
391 RLOGD("to call SET_NETWORK psk ********\n"); \
392 } \
you.chend2fef3f2023-02-13 10:50:35 +0800393 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 +0800394 if (ret != 0) { \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800395 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800396 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800397 RLOGE("call "#cmd_str" fail %d\n", ret); \
qs.xiongb04dc852024-03-27 21:55:32 +0800398 }else \
399 { \
400 RLOGE("call get or set psk fail %d\n",ret); \
401 } \
you.chen35020192022-05-06 11:30:57 +0800402 return ret; \
403 } \
404 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800405 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800406 }while(0)
407
408#define DO_OK_FAIL_REQUEST(cmd_str) do { \
qs.xiong8362e222024-03-22 11:20:56 +0800409 pthread_mutex_lock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800410 DO_REQUEST(cmd_str); \
411 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiongf1ca0be2024-04-10 16:51:47 +0800412 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800413 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800414 RLOGE("cmd "#cmd_str" return FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800415 }else \
416 { \
417 RLOGE("cmd SET_NETWORK psk ******** return FAIL\n"); \
418 } \
qs.xiong8362e222024-03-22 11:20:56 +0800419 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800420 return -1; \
421 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800422 if ( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800423 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800424 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800425 }else \
426 { \
427 RLOGE("cmd get or set psk return not OK|FAIL\n"); \
428 } \
qs.xiong8362e222024-03-22 11:20:56 +0800429 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800430 return -1; \
431 } \
qs.xiong8362e222024-03-22 11:20:56 +0800432 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800433 }while (0)
434
435
you.chenf711c8a2023-04-13 13:49:45 +0800436static int check_connection(struct wpa_ctrl * wpa_ctrl)
437{
438 size_t reply_len = MAX_RET;
439 char cmd_reply[MAX_RET]={0};
440 int ret;
441
442 RLOGD("check_connection [%p]", wpa_ctrl);
443 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
444
445 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
446 {
447 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 +0800448 if (ret != 0)
449 {
450 notify_service_invoke_fail(ret);
451 }
you.chenf711c8a2023-04-13 13:49:45 +0800452 return -1;
453 }
454
455 return 0;
456}
457
458/**
459 * @brief check_pending_msg
460 * @param lynq_wpa_ctrl
461 * @return 1 has msg, 0 no msg, -1 error
462 */
463static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
464{
465 int ret;
466
467 if (*pp_lynq_wpa_ctrl == NULL) // need connect
468 {
469 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
470 if (*pp_lynq_wpa_ctrl == NULL)
471 {
472 usleep(SLEEP_TIME_ON_IDLE);
473 return -1;
474 }
475
476 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
477 if (ret == 0) // attach success
478 {
479 *started_flag = 1;
480 }
481 else
482 {
you.chen70f377f2023-04-14 18:17:09 +0800483 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800484 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
485 *pp_lynq_wpa_ctrl = NULL;
486 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800487 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800488 usleep(SLEEP_TIME_ON_IDLE);
489 return -1;
490 }
491 }
492
493 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
494 if ( ret == 0) // no pending messages
495 {
496 usleep(SLEEP_TIME_ON_IDLE);
497 *idle_count += 1;
498 if (*idle_count > MAX_IDLE_COUNT)
499 {
500 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
501 {
502 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
503 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
504 *pp_lynq_wpa_ctrl = NULL;
505 *idle_count = 0;
506 return -1;
507 }
508 *idle_count = 0;
509 }
510 return 0;
511 }
512 else if ( ret == -1) // on error
513 {
514 RLOGE("[wifi error]sta wpa_ctrl_pending");
515 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
516 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
517 *pp_lynq_wpa_ctrl = NULL;
518 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800519 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800520 return -1;
521 }
522
523 *idle_count = 0;
524 return 1;
525}
526
qs.xiong448588f2024-05-21 10:04:04 +0800527static void check_dnsmasq_dhcp(void)
528{
529 system_call_v("%s", check_dnsmasq_dhcp_status);
530}
you.chen6d247052023-06-01 16:39:54 +0800531static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
532{
533 pthread_mutex_lock(&s_ap_callback_mutex);
534 if (g_ap_callback_func != NULL)
535 g_ap_callback_func(g_ap_callback_priv, status);
536 pthread_mutex_unlock(&s_ap_callback_mutex);
537}
538
you.chen35020192022-05-06 11:30:57 +0800539static void APWatcherThreadProc() {
540 size_t len = MAX_RET;
541 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800542 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800543 char *ptr = NULL;
544 char mac[32] = {0};
545 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800546
you.chen6c2dd9c2022-05-16 17:55:28 +0800547 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800548 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800549
qs.xiong9fbf74e2023-03-28 13:38:22 +0800550 while (g_ap_watcher_stop_flag == 0)
551 {
you.chenf711c8a2023-04-13 13:49:45 +0800552 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
553 {
you.chen70f377f2023-04-14 18:17:09 +0800554 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
555 {
556 check_tether_and_notify();
557 }
558
you.chen35020192022-05-06 11:30:57 +0800559 continue;
560 }
you.chenf711c8a2023-04-13 13:49:45 +0800561
you.chen6c2dd9c2022-05-16 17:55:28 +0800562 memset(msg_notify, 0, MAX_RET);
563 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800564 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800565 {
you.chen35020192022-05-06 11:30:57 +0800566 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800567 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800568 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800569 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800570 {
you.chen6d247052023-06-01 16:39:54 +0800571 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800572 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
573 if( ptr != NULL)
574 {
575 ptr += strlen("AP-STA-DISCONNECTED ");
576 memcpy(mac,ptr,17);
577 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
578 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
579 system(cmd);
580 }
581
qs.xiong9fbf74e2023-03-28 13:38:22 +0800582 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800583 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800584 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
585 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800586 {
you.chen0f5c6432022-11-07 18:31:14 +0800587 stopGBW();
588 }
589 }
you.chen35020192022-05-06 11:30:57 +0800590 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800591 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800592 {
you.chen6d247052023-06-01 16:39:54 +0800593 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong448588f2024-05-21 10:04:04 +0800594 check_dnsmasq_dhcp();
qs.xiong9fbf74e2023-03-28 13:38:22 +0800595 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800596 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800597 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
598 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800599 {
you.chen0f5c6432022-11-07 18:31:14 +0800600 startGBW();
601 }
602 }
you.chen35020192022-05-06 11:30:57 +0800603 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800604 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800605 {
606 RLOGD("APWatcherThreadProc ap------> service error");
607 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
608 }
609 else
610 {
611 RLOGD("APWatcherThreadProc ap------> going on check next msg");
612 }
you.chenf711c8a2023-04-13 13:49:45 +0800613 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800614 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
615 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800616 if (lynq_wpa_ctrl != NULL)
617 {
you.chen92fd5d32022-05-25 10:09:47 +0800618 wpa_ctrl_detach(lynq_wpa_ctrl);
619 wpa_ctrl_close(lynq_wpa_ctrl);
620 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400621}
622
you.chen70f377f2023-04-14 18:17:09 +0800623static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
624{
625 char * p;
626 const char * try_associat_flag = "Trying to associate";
627 const char * associated_flag = "Associated with ";
628
629 pthread_mutex_lock(&s_global_check_mutex);
630 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
631 {
632 pthread_mutex_unlock(&s_global_check_mutex);
633 return;
634 }
635
you.chen6e724162023-10-19 19:10:01 +0800636 // youchen@2023-10-17 add for "not notify connect fail directly" begin
637 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
638 {
639 s_sta_error_number = error_num;
640 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
641 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
642 pthread_cond_signal(&s_global_check_cond);
643 pthread_mutex_unlock(&s_global_check_mutex);
644 return;
645 }
646 // youchen@2023-10-17 add for "not notify connect fail directly" end
647
you.chen70f377f2023-04-14 18:17:09 +0800648 if (state == LYNQ_WIFI_STATUS_EGNORE)
649 {
650 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
651 {
652 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
653 }
654 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
655 {
656 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
657 }
658 }
659 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
660 {
661 s_sta_error_number = error_num;
662 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
663 {
664 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
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 }
669 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
670 {
671 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800672 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 +0800673 pthread_cond_signal(&s_global_check_cond);
674 }
675 pthread_mutex_unlock(&s_global_check_mutex);
676}
677
qs.xiongb37f8c42023-09-13 21:21:58 +0800678static int lynq_split(char * str, int len, char delimiter, char * results[]);
679static inline char inner_convert_char(char in);
680static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
681static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
682
683
684
qs.xiong44fac672023-08-29 16:15:55 +0800685/*
686just tmp add for fix sta connect ap fail check ap connect info
687return 0 --->Current no sta device connect this AP
688*/
689static int lynq_connected_ap_sta_status() {
690
691 FILE *fp;
692 size_t i = 0;
693 int ret;
694 char lynq_cmd_ret[MAX_RET]={0};
695
696 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
697 {
698 perror("popen error!");
699 return -1;
700 }
701 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
702 {
703 perror("fread fail!");
704 ret=pclose(fp);
705 if(ret == -1)
706 perror("close file faild");
707 return -1;
708 }
709 if( strlen(lynq_cmd_ret) < MAC_LEN)
710 {
711 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
712 ret=pclose(fp);
713 if(ret==-1)
714 {
715 perror("close file faild");
716 }
717 return 0;
718 }else{
719 ret=pclose(fp);
720 if(ret==-1)
721 {
722 perror("close file faild");
723 }
724 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
725 return 1;
726 }
727}
728
729/*
730 just tmp add for fix sta connect ap fail; check fw status
731 return 1 ----> fw status error; need wl down/up
732*/
733static int check_current_fw_status() {
734
735 FILE *fp;
736 FILE *fp1;
737 size_t i = 0;
738 int ret;
739 char lynq_cmd_ret_2g[MAX_RET]={0};
740 char lynq_cmd_ret_5g[MAX_RET]={0};
741
742 const char * fw_status = "0x0096"; //0x0096 is normal fw status
743
744 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
745 {
746 perror("popen error!");
747 return -1;
748 }
749 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
750 {
751 perror("fread fail!");
752 if(pclose(fp) == -1)
753 perror("close fp file faild");
754 return -1;
755 }
756
757 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
758 {
759 perror("popen error!");
760 if(pclose(fp) == -1)
761 perror("clsoe fp file faild");
762 return -1;
763 }
764 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
765 {
766 perror("fread fail!");
767 if(pclose(fp1) == -1)
768 perror("clsoe fp1 file faild");
769 if(pclose(fp) == -1)
770 perror("clsoe fp file faild");
771 return -1;
772 }
773
774 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
775 {
776 ret=pclose(fp);
777 if(ret==-1)
778 {
779 perror("close fp file faild");
780 }
781 ret=pclose(fp1);
782 if(ret==-1)
783 {
784 perror("close fp1 file faild");
785 }
786 return 0;
787 }else
788 {
789 ret=pclose(fp);
790 if(ret==-1)
791 {
792 perror("close file faild");
793 }
794 if(pclose(fp1) == -1)
795 {
796 perror("clsoe file fp1 faild");
797 }
798 RLOGD("current fw status --error--");
799 return 1;
800 }
801}
802
qs.xiong1d4263a2023-09-06 10:46:23 +0800803/*
804eg: wl counters info
805sh-3.2# wl counters
806counters_version 30
807datalen 1648
808Slice_index: 0
809reinitreason_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)
810reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
811
812check reinit status
813return 0 ===> fw did wl reinit cmd
814*/
815static int check_current_reinit_info()
816{
817 FILE *fp;
818 int ret;
819 char lynq_cmd_ret[MAX_RET]={0};
820 char * dest;
821 char destid[3]={0};
822 if((fp=popen("wl counters","r"))==NULL)
823 {
824 perror("popen error!");
825 return -1;
826 }
827 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
828 {
829 perror("fread fail!");
830 if(pclose(fp) == -1)
831 {
832 perror("close fp file faild");
833 }
834 return -1;
835 }
836 dest = strstr(lynq_cmd_ret,"reinit ");
837 if(dest != NULL)
838 {
839 dest +=strlen("reinit ");
840 RLOGD("current get dest str is %s",dest);
841 memcpy(destid,dest,2);
842 ret = atoi(destid);
843 RLOGD("get current wl counters cmd return counts is %d",ret);
844 if( ret != 0 )
845 {
846 RLOGD("current fw did run cmd wl reinit");
847 if( pclose(fp) == -1 )
848 {
849 perror("close fp file faild");
850 }
851 return 0;
852 }
853 }
854 if( pclose(fp) == -1 )
855 {
856 perror("close fp file faild");
857 }
858 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
859 return -1;
860}
861
qs.xiong44fac672023-08-29 16:15:55 +0800862static void APTmpWatcherThreadProc() {
863
864 int i = 0;
865 int delytime = 300;
866 g_ap_tmp_watcher_stop_flag = 0;
867
868 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
869 while(1)
870 {
871 sleep(1);
872 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800873 if ( (i % 30) == 0 )
874 {
875 if ( check_current_reinit_info() == 0 )
876 {
877 system("wl reset_cnts");
878 system("wl down");
879 system("wl up");
880 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
881 }
882 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800883 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800884 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800885 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800886 {
887 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
888 {
889 system("wl down");
890 system("wl up");
891 }
qs.xiong44fac672023-08-29 16:15:55 +0800892 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800893 }
894 if ( i == delytime )
895 {
qs.xiong44fac672023-08-29 16:15:55 +0800896 i = 0;
897 }
898 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
899 {
900 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
901 return;
902 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800903
qs.xiong44fac672023-08-29 16:15:55 +0800904 }
905
906}
907
qs.xiongf0128b12023-06-29 17:29:39 +0800908static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
909{
910 char LYNQ_DISABLE_CMD[128]={0};
911
912 CHECK_IDX(idx, CTRL_STA);
913 CHECK_WPA_CTRL(CTRL_STA);
914
915 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800916 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800917 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800918
qs.xiongf0128b12023-06-29 17:29:39 +0800919 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800920
qs.xiongf0128b12023-06-29 17:29:39 +0800921}
922
qs.xiongb37f8c42023-09-13 21:21:58 +0800923static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
924{
925 char LYNQ_ENABLE_CMD[128]={0};
926
927 CHECK_IDX(idx, CTRL_STA);
928 CHECK_WPA_CTRL(CTRL_STA);
929
930
931 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800932 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800933 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
934
935 return 0;
936
937}
938
939static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
940{
941
942 int index,networkid;
943
944 for ( index = 0; index < len ;index++)
945 {
946 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800947 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800948 {
949 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
950 }
951 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
952 }
953 return 0;
954
955}
956
957
958/*
959 dis_net_list user disconnect list
960*/
961static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
962{
qs.xiong09560402023-10-27 21:58:55 +0800963 RLOGD("enter %s %d",__func__,__LINE__);
964 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800965 int count,ncount,index;
966 int flag = 0;
967 int merge_index = 0;
968 int net_no_list[128];
969
qs.xiong09560402023-10-27 21:58:55 +0800970 for(ncount = 0;ncount < valid_num; ncount++ )
971 {
972 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
973 }
974
qs.xiongb37f8c42023-09-13 21:21:58 +0800975 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
976 for( count = 0; count < index; count++)
977 {
978 for(ncount = 0; ncount < valid_num; ncount++)
979 {
qs.xiong09560402023-10-27 21:58:55 +0800980 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800981 if(net_no_list[count] == dis_net_list[ncount])
982 {
qs.xiong09560402023-10-27 21:58:55 +0800983 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800984 flag = 1;
985 break;
986 }
987 }
988 if( flag != 1 )
989 {
990 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800991 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 +0800992 merge_index ++;
993 }
994 flag = 0;
995 }
996 * outlen =merge_index;
997 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
998 return;
999}
qs.xiongf0128b12023-06-29 17:29:39 +08001000
qs.xiong455c30b2023-04-12 11:40:02 +08001001void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
1002{
1003 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +08001004 char *wpanetid;
1005 char destid[3] = {0};
1006 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +08001007 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1008 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1009 {
1010 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1011 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
1012 return;
1013 }
1014
qs.xiong20202422023-09-06 18:01:18 +08001015 if (strstr(modify, "Trying to associate") != NULL)
1016 {
1017 RLOGD("Current sta is Trying to associate");
1018 *state = LYNQ_WIFI_STATUS_EGNORE;
1019 g_sta_conncet_status_flag = 1;
1020 return;
1021 }
1022
qs.xiong455c30b2023-04-12 11:40:02 +08001023 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1024 {
1025 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +08001026 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001027 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001028 return;
1029 }
1030
qs.xiong1e81dfa2023-09-27 15:52:37 +08001031 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
1032 {
1033 *error = LYNQ_PSW_ERROR;
1034 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1035 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
1036 g_sta_conncet_status_flag = 0;
1037 return;
1038 }
qs.xiong455c30b2023-04-12 11:40:02 +08001039 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1040 {
qs.xiongf0128b12023-06-29 17:29:39 +08001041 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1042 wpanetid = strstr(modify,"id=");
1043 if ( wpanetid != NULL )
1044 {
1045 wpanetid +=strlen("id=");
1046 memcpy(destid,wpanetid,2);
1047 tmpdisid = atoi(destid);
1048
1049 }
qs.xiong455c30b2023-04-12 11:40:02 +08001050 pReason = strstr(modify, "reason=");
1051 if (pReason != NULL)
1052 {
1053 pReason += strlen("reason=");
1054 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1055 {
1056 *error = LYNQ_TIME_OUT;
1057 }
1058 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1059 {
1060 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001061 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001062 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001063 // you.chen@2023-10-17 only disable network during autoconnect
1064 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 +08001065 {
1066 RLOGE("stop wlan0 network %d fail",tmpdisid);
1067 }
qs.xiong455c30b2023-04-12 11:40:02 +08001068 }
1069 else
1070 {
1071 *error = LYNQ_UNSPECIFIED_REASON;
1072 }
qs.xiong455c30b2023-04-12 11:40:02 +08001073 }
1074 else
1075 {
1076 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001077 }
qs.xiongf0128b12023-06-29 17:29:39 +08001078 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001079 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001080 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001081
1082 }
1083
1084 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1085 {
1086 *error = LYNQ_NOT_FIND_AP;
1087 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1088 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001089 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001090 return;
1091 }
1092
1093
1094 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1095 {
1096 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1097 pReason = strstr(modify, "status_code=");
1098 if (pReason != NULL)
1099 {
1100 pReason += strlen("status_code=");
1101 if (memcmp(pReason, "17", 2) == 0)
1102 {
1103 *error = LYNQ_AP_UNABLE_HANDLE;
1104 }
1105 else if (memcmp(pReason, "1",1) == 0)
1106 {
1107 *error = LYNQ_UNSPECIFIED_REASON;
1108 }
1109 else
1110 {
1111 *error = LYNQ_UNSPECIFIED_REASON;
1112 }
1113
1114 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1115 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 +08001116 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001117 return;
1118 }
1119 else
1120 {
1121 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1122 *error = LYNQ_UNSPECIFIED_REASON;
1123 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1124 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1125 return;
1126 }
1127 }
1128
1129 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1130 {
1131 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1132 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1133 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1134 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1135 return;
1136 }
1137
1138 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1139 {
1140 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1141 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1142 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1143 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1144 return;
1145 }
1146
qs.xiongdf637b22023-10-26 19:30:07 +08001147 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1148 if (strstr(modify, "Association request to the driver failed") != NULL)
1149 {
1150 RLOGD("Association request to the driver failed --- recover");
1151 system("wl down");
1152 system("wl up");
1153 *error = LYNQ_UNSPECIFIED_REASON;
1154 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1155 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1156 return;
1157 }
1158 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1159
1160
you.chen32cb31e2023-04-13 14:05:45 +08001161 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001162 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001163 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001164 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1165 return;
1166
1167}
1168
qs.xiongfcc914b2023-07-06 21:16:20 +08001169void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1170{
1171 char *pReason;
1172 char *wpanetid;
1173 char destid[3];
1174 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1175 *networkid = -1;
1176 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1177 {
1178 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1179 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1180 return;
1181 }
1182 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1183 {
1184 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1185 wpanetid = strstr(modify,"id=");
1186 if ( wpanetid != NULL )
1187 {
1188 wpanetid +=strlen("id=");
1189 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1190 if (memcpy(destid,wpanetid,2) != NULL)
1191 {
1192 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1193 *networkid = atoi(destid);
1194 RLOGD("get networkid is %d\n",*networkid);
1195 }
1196 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1197 }
1198 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1199 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1200 return;
1201 }
1202 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1203 {
1204 wpanetid = strstr(modify,"id=");
1205 if ( wpanetid != NULL )
1206 {
1207 wpanetid +=strlen("id=");
1208 if (memcpy(destid,wpanetid,2) != NULL)
1209 {
1210 *networkid = atoi(destid);
1211 RLOGD("get networkid is %d\n",*networkid);
1212 }
1213 }
1214 pReason = strstr(modify, "reason=");
1215 if (pReason != NULL)
1216 {
1217 pReason += strlen("reason=");
1218 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1219 {
1220 *error = LYNQ_TIME_OUT;
1221 }
1222 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1223 {
1224 *error = LYNQ_PSW_ERROR;
1225 }
1226 else
1227 {
1228 *error = LYNQ_UNSPECIFIED_REASON;
1229 }
1230 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1231 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1232 return;
1233 }
1234 else
1235 {
1236 *error = LYNQ_UNSPECIFIED_REASON;
1237 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1238 return;
1239 }
1240 }
1241 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1242 {
1243 wpanetid = strstr(modify,"id=");
1244 if ( wpanetid != NULL )
1245 {
1246 wpanetid +=strlen("id=");
1247 if (memcpy(destid,wpanetid,2) != NULL)
1248 {
1249 *networkid = atoi(destid);
1250 RLOGD("get networkid is %d\n",*networkid);
1251 }
1252 }
1253 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1254 pReason = strstr(modify, "status_code=");
1255 if (pReason != NULL)
1256 {
1257 pReason += strlen("status_code=");
1258 if (memcmp(pReason, "17", 2) == 0)
1259 {
1260 *error = LYNQ_AP_UNABLE_HANDLE;
1261 }
1262 else if (memcmp(pReason, "1",1) == 0)
1263 {
1264 *error = LYNQ_UNSPECIFIED_REASON;
1265 }
1266 else
1267 {
1268 *error = LYNQ_UNSPECIFIED_REASON;
1269 }
1270 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1271 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1272 return;
1273 }
1274 else
1275 {
1276 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1277 *error = LYNQ_UNSPECIFIED_REASON;
1278 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001279 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001280 return;
1281 }
1282 }
1283 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1284 {
1285 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1286 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1287 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1288 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1289 return;
1290 }
1291 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1292 {
1293 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1294 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1295 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1296 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1297 return;
1298 }
1299 RLOGD("EVENT : %s\n", modify);
1300 *error = LYNQ_UNSPECIFIED_REASON;
1301 *state = LYNQ_WIFI_STATUS_EGNORE;
1302 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1303 return;
1304}
you.chen70f377f2023-04-14 18:17:09 +08001305static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1306{
you.chen6d247052023-06-01 16:39:54 +08001307 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001308 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1309 {
1310 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1311 g_sta_callback_func(g_sta_callback_priv, state, error);
1312 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1313 }
you.chen6d247052023-06-01 16:39:54 +08001314 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001315}
qs.xiongfcc914b2023-07-06 21:16:20 +08001316static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1317{
1318 pthread_mutex_lock(&s_sta_callback_mutex);
1319 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1320 {
1321 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1322 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1323 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1324 }
1325 pthread_mutex_unlock(&s_sta_callback_mutex);
1326}
you.chen70f377f2023-04-14 18:17:09 +08001327
you.chen35020192022-05-06 11:30:57 +08001328static void STAWatcherThreadProc() {
1329 size_t len = MAX_RET;
1330 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001331 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001332 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001333 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001334
you.chen6c2dd9c2022-05-16 17:55:28 +08001335 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001336 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001337
you.chen70f377f2023-04-14 18:17:09 +08001338 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001339 while (g_sta_watcher_stop_flag == 0)
1340 {
you.chenf711c8a2023-04-13 13:49:45 +08001341 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001342 {
you.chen35020192022-05-06 11:30:57 +08001343 continue;
1344 }
you.chenf711c8a2023-04-13 13:49:45 +08001345
you.chen6c2dd9c2022-05-16 17:55:28 +08001346 memset(msg_notify, 0, MAX_RET);
1347 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001348 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001349 {
you.chen35020192022-05-06 11:30:57 +08001350 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001351 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1352 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001353 {
you.chen35020192022-05-06 11:30:57 +08001354 g_sta_scan_finish_flag = 1;
1355 }
1356
qs.xiong9fbf74e2023-03-28 13:38:22 +08001357 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001358 {
you.chen35020192022-05-06 11:30:57 +08001359 continue;
1360 }
qs.xiong455c30b2023-04-12 11:40:02 +08001361 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001362 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1363 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1364 {
1365 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1366 }
you.chen0c9bee22023-10-25 13:03:14 +08001367 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1368 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1369 {
1370 RLOGD("donot report scan result during in call connect manual");
1371 }
you.chen6e724162023-10-19 19:10:01 +08001372 else
1373 {
1374 notify_connect_status(state, error);
1375 }
1376 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001377
1378 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001379 {
you.chen70f377f2023-04-14 18:17:09 +08001380 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001381 if (last_state != state)
1382 {
1383 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1384 {
1385 system_call_v("%s %s", sta_status_change_script, "connect");
1386 }
1387 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1388 {
1389 system_call_v("%s %s", sta_status_change_script, "disconnect");
1390 }
1391 }
qs.xiong09560402023-10-27 21:58:55 +08001392
you.chenc9928582023-04-24 15:39:37 +08001393 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001394 if (g_sta_fake_scan_finish_flag == 1)
1395 {
1396 g_sta_fake_scan_finish_flag = 0;
1397 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1398 }
you.chen32cb31e2023-04-13 14:05:45 +08001399 }
you.chen35020192022-05-06 11:30:57 +08001400 }
1401 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001402 if (lynq_wpa_ctrl != NULL)
1403 {
you.chen92fd5d32022-05-25 10:09:47 +08001404 wpa_ctrl_detach(lynq_wpa_ctrl);
1405 wpa_ctrl_close(lynq_wpa_ctrl);
1406 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001407}
qs.xiongfcc914b2023-07-06 21:16:20 +08001408static void STAAutoWatcherThreadProc() {
1409 size_t len = MAX_RET;
1410 char msg_notify[MAX_RET];
1411 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001412 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001413 int idle_count = 0;
1414 int networkid;
1415 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1416 g_sta_auto_watcher_stop_flag = 0;
1417 RLOGD("STAAutoWatcherThreadProc thread started ------");
1418 while (g_sta_auto_watcher_stop_flag == 0)
1419 {
1420 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1421 {
1422 continue;
1423 }
1424 memset(msg_notify, 0, MAX_RET);
1425 len = MAX_RET;
1426 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1427 {
1428 msg_notify[len+1] = '\0';
1429 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1430 if (strstr(msg_notify, state_scan_result) != NULL)
1431 {
1432 g_sta_auto_scan_finish_flag = 1;
1433 }
1434 if (g_sta_auto_callback_func == NULL)
1435 {
1436 continue;
1437 }
1438 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1439 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001440 }
1441 }
1442 if (lynq_wpa_ctrl != NULL)
1443 {
1444 wpa_ctrl_detach(lynq_wpa_ctrl);
1445 wpa_ctrl_close(lynq_wpa_ctrl);
1446 }
1447}
qs.xiongf1b525b2022-03-31 00:58:23 -04001448
you.chen70f377f2023-04-14 18:17:09 +08001449// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1450static void GlobalWatcherThreadProc()
1451{
1452 int ret, connect_timeout, service_abnormal;
1453 error_number_s error_num = -1;
1454 inner_sta_status_s sta_status;
1455 scan_info_s *scan_list = NULL;
1456 int i, scan_len=0;
1457 char connecting_ssid[64];
1458 struct timeval now;
1459
1460 RLOGD("GlobalWatcherThreadProc start to run");
1461
1462 while (1)
1463 {
1464 pthread_mutex_lock(&s_global_check_mutex);
1465 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1466 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1467 {
1468 pthread_mutex_unlock(&s_global_check_mutex);
1469 usleep(50*1000);
1470 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1471 continue;
1472 }
1473
1474 connect_timeout = 0;
1475 service_abnormal = 0;
1476 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1477 {
1478 while (1)
1479 {
1480 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1481 if (ret == ETIME)
1482 {
1483 connect_timeout = 1;
1484 }
1485 else if (ret != 0)
1486 {
1487 gettimeofday(&now,NULL);
1488 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1489 {
1490 usleep(SLEEP_TIME_ON_IDLE);
1491 continue;
1492 }
1493 connect_timeout = 1;
1494 }
1495 sta_status = s_sta_status;
1496 error_num = s_sta_error_number;
1497 s_sta_status = INNER_STA_STATUS_INIT;
1498 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1499 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1500 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1501 break;
1502 }
1503 }
1504 if (s_service_invoke_timeout_cnt > 10)
1505 {
1506 service_abnormal = 1;
1507 s_service_invoke_timeout_cnt = 0;
1508 }
1509 pthread_mutex_unlock(&s_global_check_mutex);
1510
1511 if (service_abnormal == 1)
1512 {
1513 sleep(1);
1514 RLOGE("wpa service is abnormal info app to exit");
1515 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001516
1517 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1518
you.chen70f377f2023-04-14 18:17:09 +08001519 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1520 }
1521
1522 if (sta_status == INNER_STA_STATUS_CANCEL)
1523 {
1524 continue;
1525 }
1526 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1527 {
1528 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1529 }
1530 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1531 {
1532 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1533 {
1534 for(i=0; i < scan_len;i++)
1535 {
1536 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1537 {
1538 error_num = LYNQ_AUTH_ERROR;
1539 break;
1540 }
1541 }
1542 free(scan_list);
1543 }
1544 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1545 }
1546 else if (connect_timeout == 0)
1547 {
1548 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1549 }
1550 else // wait timeout
1551 {
1552 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1553 {
1554 ; // wpa service abnormal
1555 }
1556 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1557 {
1558 RLOGD("GlobalWatcherThreadProc notify connected");
1559 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1560 }
1561 else
1562 {
1563 RLOGD("GlobalWatcherThreadProc notify timeout");
1564 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1565 }
1566 }
1567 } // while (1)
1568}
1569
qs.xiong1af5daf2022-03-14 09:12:12 -04001570int lynq_wifi_enable(void)
1571{
you.chen35020192022-05-06 11:30:57 +08001572 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001573 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001574 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001575 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1576
qs.xiong9fbf74e2023-03-28 13:38:22 +08001577 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1578 {
you.chend2fef3f2023-02-13 10:50:35 +08001579 goto out_enable;
1580 }
1581
you.chenc9928582023-04-24 15:39:37 +08001582 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001583 if (ret != 0)
1584 {
1585 //printf("service state %d\n", ret);
1586 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001587 ret = -1;
1588 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001589 }
lhfe8da902022-10-11 18:55:36 +08001590
you.chen70f377f2023-04-14 18:17:09 +08001591 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1592 {
1593 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1594 if(ret<0)
1595 {
1596 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1597 ret = -1;
1598 goto out_enable;
1599 }
1600 }
1601
you.chend2fef3f2023-02-13 10:50:35 +08001602 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1603 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1604 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1605 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1606out_enable:
1607 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001608 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001609}
1610
qs.xiong1af5daf2022-03-14 09:12:12 -04001611int lynq_wifi_disable(void)
1612{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001613 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001614 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001615 g_ap_watcher_stop_flag = 1;
1616 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001617 g_sta_auto_watcher_stop_flag = 1;
qs.xiong9d8f3102023-12-07 20:11:37 +08001618/* g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001619 if (g_ap_watcher_pid != 0)
1620 pthread_join(g_ap_watcher_pid, NULL);
qs.xiong9d8f3102023-12-07 20:11:37 +08001621*/
you.chen35020192022-05-06 11:30:57 +08001622 if (g_sta_watcher_pid != 0)
1623 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001624 if (g_sta_auto_watcher_pid != 0)
1625 pthread_join(g_sta_auto_watcher_pid, NULL);
qs.xiong17579bb2024-03-11 19:08:06 +08001626 if ( g_lynq_wpa_ctrl[0] != NULL)
1627 {
1628 if(g_lynq_wpa_ctrl[0]->ctrl != NULL)
1629 {
1630 wpa_ctrl_close(g_lynq_wpa_ctrl[0]->ctrl);
1631 }
1632 free(g_lynq_wpa_ctrl[0]);
1633 g_lynq_wpa_ctrl[0] = NULL;
1634 }
1635
1636 if ( g_lynq_wpa_ctrl[1] != NULL )
1637 {
1638 if( g_lynq_wpa_ctrl[1]->ctrl != NULL )
1639 {
1640 wpa_ctrl_close(g_lynq_wpa_ctrl[1]->ctrl);
1641 }
1642 free(g_lynq_wpa_ctrl[1]);
1643 g_lynq_wpa_ctrl[1] = NULL;
1644 }
qs.xiong44fac672023-08-29 16:15:55 +08001645 if (g_ap_tmp_watcher_pid != 0)
1646 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001647 g_ap_watcher_pid = 0;
1648 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001649 g_sta_auto_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08001650// g_ap_tmp_watcher_pid = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +08001651 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001652 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001653 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1654 return 0;
1655}
1656
1657static inline char inner_convert_char(char in)
1658{
1659 if (in >= '0' && in <= '9')
1660 {
1661 return in - '0';
1662 }
1663 else if (in >= 'a' && in <= 'f')
1664 {
1665 return in - 'a' + 10;
1666 }
1667 else if (in >= 'A' && in <= 'F')
1668 {
1669 return in - 'A' + 10;
1670 }
1671 else
1672 {
1673 return '\xff';
1674 }
1675}
1676
1677static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1678{
1679 char *p;
1680 size_t pos = 0;
1681 if (NULL == out_ssid)
1682 return;
1683 //printf("input ssid=[%s]\n", ssid);
1684 memset(out_ssid, 0, out_ssid_len);
1685 if (NULL == ssid)
1686 return;
1687 p = strchr(ssid, '\\');
1688 if (NULL == p)
1689 {
1690 strncpy(out_ssid, ssid, out_ssid_len);
1691 //printf(" first %s\n", out_ssid);
1692 }
1693 else
1694 {
1695 pos = p - ssid;
1696 memcpy(out_ssid, ssid, pos);
1697 //printf("pos %lu -- %s\n", pos, out_ssid);
1698 for(; pos < out_ssid_len; pos ++)
1699 {
1700 if (p[0] == '\0')
1701 {
1702 //printf(" out %s\n", out_ssid);
1703 return;
1704 }
1705 else if (p[0] != '\\')
1706 {
1707 out_ssid[pos] = p[0];
1708 p += 1;
1709 }
1710 else if (p[1] == 'x' || p[1] == 'X')
1711 {
1712 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1713 p += 4;
1714 }
1715 else if (p[1] == '\\')
1716 {
1717 out_ssid[pos] = '\\';
1718 p += 2;
1719 }
1720 else if (p[1] == 't')
1721 {
1722 out_ssid[pos] = '\t';
1723 p += 2;
1724 }
1725 else if (p[1] == 'r')
1726 {
1727 out_ssid[pos] = '\r';
1728 p += 2;
1729 }
1730 else if (p[1] == 'n')
1731 {
1732 out_ssid[pos] = '\n';
1733 p += 2;
you.chen34983432023-12-21 20:39:06 +08001734 }
1735 else
1736 {
1737 out_ssid[pos] = p[1];
1738 p += 2;
you.chend2fef3f2023-02-13 10:50:35 +08001739 }//todo find a better way to convert?
1740 }
1741 }
1742 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001743}
qs.xiong1af5daf2022-03-14 09:12:12 -04001744
you.chen35020192022-05-06 11:30:57 +08001745static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001746 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001747 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001748 RLOGD("enter inner_get_param");
1749 if (out_put == NULL)
1750 {
1751 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001752 return -1;
1753 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001754 if (param_name == NULL)
1755 {
1756 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001757 return -1;
1758 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001759 if (param_name[0] == '\0')
1760 {
1761 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001762 return -1;
1763 }
1764
1765 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1766
1767 CHECK_WPA_CTRL(interface);
1768
1769 DO_REQUEST(lynq_cmd_get);
1770
qs.xiong9fbf74e2023-03-28 13:38:22 +08001771 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1772 {
1773 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001774 return -1;
1775 }
1776
you.chena6fa5b22022-05-18 10:28:19 +08001777// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001778 if (strcmp(param_name, "ssid") == 0)
1779 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001780 if (cmd_reply[0] == '\"')
1781 {
you.chend2fef3f2023-02-13 10:50:35 +08001782 ssid_len = reply_len - 1;
1783 memcpy(out_put, cmd_reply + 1, ssid_len);
1784 if (out_put[ssid_len-1] == '\"')
1785 {
1786 out_put[ssid_len-1] = '\0';
1787 }
1788 else
1789 {
1790 out_put[ssid_len] = '\0';
1791 }
1792 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001793 else
1794 {
you.chend2fef3f2023-02-13 10:50:35 +08001795 ssid_len = reply_len / 2;
1796 for(i=0; i<ssid_len; i++)
1797 {
1798 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1799 }
1800 out_put[ssid_len] = '\0';
1801 }
1802 }
1803 else
1804 {
1805 memcpy(out_put, cmd_reply, reply_len + 1);
1806 }
you.chen35020192022-05-06 11:30:57 +08001807 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001808}
qs.xiong1af5daf2022-03-14 09:12:12 -04001809
you.chen35020192022-05-06 11:30:57 +08001810static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1811 int ret = 0;
1812 char * end = str + len - 1;
1813 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001814 while(str < end)
1815 {
1816 if (*str == delimiter)
1817 {
you.chen35020192022-05-06 11:30:57 +08001818 *str++ = '\0';
1819 results[ret++] = str;
1820 continue;
1821 }
1822 str++;
1823 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001824 if (*str == delimiter)
1825 {
you.chen35020192022-05-06 11:30:57 +08001826 *str = '\0';
1827 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001828
you.chen6ed36a62023-04-27 17:51:56 +08001829 results[ret] = NULL;
1830
you.chen35020192022-05-06 11:30:57 +08001831 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001832}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001833/*
1834 *add func to get conencted STA device ip from dnsmasq ap0.lease
1835 *return 0 means get ip success
1836 */
1837static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1838{
1839 char * p;
1840 int ret;
1841 char cmd[256]={0};
1842 if (NULL == mac || NULL == ip)
1843 return -1;
1844 memset(ip, 0, ip_len);
1845 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1846 ret = exec_cmd(cmd, ip, ip_len);
1847 if( ret == 0 )
1848 {
1849 p = strchr(ip, '\n');
1850 if (NULL != p)
1851 {
1852 *p = '\0';
1853 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1854 return ret;
1855 }else
1856 {
1857 ret = -1;
1858 }
1859 }
1860 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1861 return ret;
1862
1863}
qs.xiong7a105ce2022-03-02 09:43:11 -05001864
you.chend2fef3f2023-02-13 10:50:35 +08001865static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1866{
1867 char * p;
1868 int ret = 0;
1869 char cmd[256]={0};
1870 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001871 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001872 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001873 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001874 ret = exec_cmd(cmd, ip, ip_len);
1875 p = strchr(ip, '\n');
1876 if (NULL != p)
1877 {
1878 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001879 }else
1880 {
1881 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001882 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001883 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001884 return ret;
1885}
1886
you.chend2fef3f2023-02-13 10:50:35 +08001887static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001888 struct in_addr addr ={0};
1889 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001890 char cmd[64] = {0};
1891 char * p;
1892 int ret;
you.chen35020192022-05-06 11:30:57 +08001893
qs.xiong9fbf74e2023-03-28 13:38:22 +08001894 if (ip == NULL || *ip == '\0' || hostname == NULL)
1895 {
1896 RLOGE("ip == NULL or hostname == NULL");
1897 return -1;
you.chen35020192022-05-06 11:30:57 +08001898 }
1899
you.chend2fef3f2023-02-13 10:50:35 +08001900 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001901 if (inet_aton(ip, &addr) == 0)
1902 {
you.chen35020192022-05-06 11:30:57 +08001903 printf("---inet_aton fail\n");
1904 return -1;
1905 }
1906
1907 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1908
qs.xiong9fbf74e2023-03-28 13:38:22 +08001909 if (ht == NULL)
1910 {
you.chen186d3c32023-05-18 14:19:46 +08001911 hostname[0] = '\0';
1912 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1913 ret = exec_cmd(cmd, hostname, 32);
1914 if (ret == 0)
1915 {
1916 p = strchr(hostname, '\n');
1917 if (p != NULL)
1918 {
1919 *p = '\0';
1920 }
1921 return 0;
1922 }
1923 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001924 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001925 herror(NULL);
1926 return -1;
1927 }
1928
1929 strcpy(hostname, ht->h_name);
1930
1931 return 0;
1932}
1933
1934static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1935{
1936 int count, index, words_count;
1937 char * split_lines[128]= {0};
1938 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001939 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001940 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001941 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001942
1943 CHECK_WPA_CTRL(ap_sta);
1944
1945 DO_REQUEST(lynq_wifi_list_networks);
1946
1947 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1948
1949 //@todo check ssid field to compatible
1950
1951 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001952 for(index=1; index < count; index++)
1953 {
you.chen35020192022-05-06 11:30:57 +08001954 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 if (words_count > 2)
1956 {
you.chend2fef3f2023-02-13 10:50:35 +08001957 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001958 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1959 {
you.chen35020192022-05-06 11:30:57 +08001960 net_no_list[ret++] = atoi(split_words[0]);
1961 }
1962 }
1963 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001964 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001965 return ret;
1966}
1967
1968static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001969 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001970 CHECK_WPA_CTRL(ap_sta);
1971 const char *lynq_wifi_add_network = "ADD_NETWORK";
1972
qs.xiong9fbf74e2023-03-28 13:38:22 +08001973 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001974 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1976 {
1977 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001978 return -1;
1979 }
1980
qs.xiong9fbf74e2023-03-28 13:38:22 +08001981 for(i=0;i<reply_len;i++)
1982 {
1983 if(cmd_reply[i] == '\n')
1984 {
you.chen35020192022-05-06 11:30:57 +08001985 cmd_reply[i] = '\0';
1986 break;
1987 }
1988 }
1989 return atoi(cmd_reply);
1990}
you.chena6cd55a2022-05-08 12:20:18 +08001991
you.chen35020192022-05-06 11:30:57 +08001992static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1993{
1994 int count, index;
1995 int net_no_list[128];
1996
qs.xiong9fbf74e2023-03-28 13:38:22 +08001997 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001998 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001999 for (index=0; index < count; index++)
2000 {
2001 if (net_no_list[index] == net_no)
2002 {
you.chen35020192022-05-06 11:30:57 +08002003 return 0;
2004 }
2005 }
2006
2007 if (count >= 1)
2008 index = net_no_list[count - 1];
2009 else
2010 index = -1;
2011
qs.xiong9fbf74e2023-03-28 13:38:22 +08002012 while (index < net_no )
2013 {
you.chen35020192022-05-06 11:30:57 +08002014 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 if (index >= net_no)
2016 { // required network no created
2017 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08002018 return 0;
2019 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002020 else if( index < 0)
2021 {
2022 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08002023 return -1;
2024 }
you.chen35020192022-05-06 11:30:57 +08002025 }
2026
2027 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002028 {
2029 RLOGE("[lynq_check_network_number] network index < 0");
2030 return -1;
2031 }
2032 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08002033 return 0;
2034}
2035
2036static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08002037 if (freq > 5000 && freq < 6000)
2038 {
you.chen35020192022-05-06 11:30:57 +08002039 return LYNQ_WIFI_5G_band;
2040 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002041 else if (freq > 2000 && freq < 3000)
2042 {
you.chen35020192022-05-06 11:30:57 +08002043 return LYNQ_WIFI_2G_band;
2044 }
2045 return LYNQ_WIFI_2_and_5G_band;
2046}
2047
2048static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002049 if (key_mgmt != NULL)
2050 {
2051 if (memcmp( key_mgmt, "NONE", 4) == 0)
2052 {
you.chen35020192022-05-06 11:30:57 +08002053 return LYNQ_WIFI_AUTH_OPEN;
2054 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002055 else if (memcmp( key_mgmt, "WEP", 3) == 0)
2056 {
you.chen35020192022-05-06 11:30:57 +08002057 return LYNQ_WIFI_AUTH_WEP;
2058 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002059 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2060 {
you.chen35020192022-05-06 11:30:57 +08002061 return LYNQ_WIFI_AUTH_WPA_PSK;
2062 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002063 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2064 {
you.chen35020192022-05-06 11:30:57 +08002065 return LYNQ_WIFI_AUTH_WPA2_PSK;
2066 }
2067 }
2068
2069 return -1;
2070}
2071
2072static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002073 if (flag != NULL)
2074 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002075 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 +08002076 {
2077 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002078 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002079 {
2080 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2081 }else if (strstr( flag, "WPA2-PSK") != NULL)
2082 {
you.chen35020192022-05-06 11:30:57 +08002083 return LYNQ_WIFI_AUTH_WPA2_PSK;
2084 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002085 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002086 {
you.chen35020192022-05-06 11:30:57 +08002087 return LYNQ_WIFI_AUTH_WPA_PSK;
2088 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002089 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002090 {
you.chen35020192022-05-06 11:30:57 +08002091 return LYNQ_WIFI_AUTH_WEP;
2092 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002093 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002094 {
you.chen35020192022-05-06 11:30:57 +08002095 return LYNQ_WIFI_AUTH_OPEN;
2096 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002097 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002098 {
you.chend2fef3f2023-02-13 10:50:35 +08002099 return LYNQ_WIFI_AUTH_OPEN;
2100 }
qs.xiong46f41562023-07-11 21:06:47 +08002101 else
2102 {
2103 RLOGD("convert_max_auth_from_flag not-found auth mode");
2104 }
you.chen35020192022-05-06 11:30:57 +08002105 }
2106
2107 return -1;
2108}
2109
2110static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2111 switch (bw) {
2112 case 10:
2113 return LYNQ_WIFI_BANDWIDTH_HT10;
2114 break;
2115 case 20:
2116 return LYNQ_WIFI_BANDWIDTH_HT20;
2117 break;
2118 case 40:
2119 return LYNQ_WIFI_BANDWIDTH_HT40;
2120 break;
2121 case 80:
2122 return LYNQ_WIFI_BANDWIDTH_HT80;
2123 break;
2124 default:
2125 break;
2126 }
2127
2128 return -1;
2129}
2130
you.chen70f377f2023-04-14 18:17:09 +08002131static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002132static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2133 int i, count;
2134 char *p;
2135 const char *lynq_status_cmd = "STATUS";
2136 const char * FLAG_SSID = "ssid=";
2137 const char * FLAG_SBSID = "bssid=";
2138 const char * FLAG_KEY_MGMT = "key_mgmt=";
2139 const char * FLAG_FREQ = "freq=";
2140 const char * FLAG_STATE = "wpa_state=";
2141 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002142 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002143 char *split_lines[128] = {0};
2144
2145 CHECK_WPA_CTRL(interface);
2146
qs.xiong9fbf74e2023-03-28 13:38:22 +08002147 if (curr_state == NULL)
2148 {
2149 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002150 return -1;
2151 }
2152
2153 DO_REQUEST(lynq_status_cmd);
2154
2155 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2156
2157 curr_state->net_no = -1;
2158 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002159 for(i=0; i < count; i++)
2160 {
2161 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002162 {
you.chen35020192022-05-06 11:30:57 +08002163 p = strstr(split_lines[i], FLAG_SBSID);
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 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002167 ret = 0;
2168 continue;
2169 }
you.chenf58b3c92022-06-21 16:53:48 +08002170 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002171 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002172 {
you.chend2fef3f2023-02-13 10:50:35 +08002173 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 +08002174 ret = 0;
2175 continue;
2176 }
you.chen35020192022-05-06 11:30:57 +08002177 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002178 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002179 {
you.chen450d0172022-07-15 17:56:48 +08002180 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002181 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002182 ret = 0;
2183 continue;
2184 }
2185 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002186 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002187 {
you.chen35020192022-05-06 11:30:57 +08002188 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2189 ret = 0;
2190 continue;
2191 }
you.chend2fef3f2023-02-13 10:50:35 +08002192 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002193 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002194 {
you.chend2fef3f2023-02-13 10:50:35 +08002195 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2196 ret = 0;
2197 continue;
2198 }
you.chen35020192022-05-06 11:30:57 +08002199 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002200 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002201 {
you.chen35020192022-05-06 11:30:57 +08002202 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002203 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002204 {
you.chen35020192022-05-06 11:30:57 +08002205 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2206 ret = 0;
2207 continue;
2208 }
2209
2210 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002211 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002212 {
you.chen35020192022-05-06 11:30:57 +08002213 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002214 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002215 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002216 }
2217 }
2218
you.chen70f377f2023-04-14 18:17:09 +08002219 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2220 {
2221 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2222 }
2223
you.chen35020192022-05-06 11:30:57 +08002224 return ret;
2225}
2226
qs.xiongf1b525b2022-03-31 00:58:23 -04002227int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002228{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002229 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002230 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002231
qs.xiong9fbf74e2023-03-28 13:38:22 +08002232 if (ap_ssid == NULL)
2233 {
2234 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002235 return -1;
2236 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002237 else
2238 {
2239 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002240 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002241
qs.xiong9fbf74e2023-03-28 13:38:22 +08002242 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2243 {
2244 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002245 return -1;
2246 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002247
you.chen35020192022-05-06 11:30:57 +08002248 CHECK_IDX(idx, CTRL_AP);
2249
2250 CHECK_WPA_CTRL(CTRL_AP);
2251
2252 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2253
2254 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2255 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002256
qs.xiong1b5e3472023-11-27 17:42:20 +08002257 RLOGD("[lynq_wifi_ap_ssid_set] set ssid succeed %d",__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002258 return 0;
you.chen35020192022-05-06 11:30:57 +08002259
qs.xiong7a105ce2022-03-02 09:43:11 -05002260}
2261
you.chen35020192022-05-06 11:30:57 +08002262int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002263{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002264 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002265 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002266 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002267}
2268
qs.xiongc9c79f72022-10-17 15:27:18 +08002269/*****
2270 *frequency <------>channel
2271 *
2272 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2273 *
2274 *
2275 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2276 *
2277 *
2278 * */
2279static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002280 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};
2281 int i;
2282 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2283
qs.xiong69a332b2022-12-02 09:58:57 +08002284 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002285 {
2286 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002287 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002288 }
qs.xiongc00b6032022-11-29 16:28:03 +08002289
2290 if(i == arr_len)
2291 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002292 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002293 return -1;
2294 }
qs.xiongc00b6032022-11-29 16:28:03 +08002295
qs.xiongc9c79f72022-10-17 15:27:18 +08002296 return 0;
2297}
qs.xiong13673462023-02-21 19:12:54 +08002298
2299static int lynq_check_frequencyby_country_code(int input_frequency)
2300{
2301 char str_cnc[]="CN";
2302 char str_dest[20]="";
2303
2304 if( lynq_get_country_code(1,str_dest) != 0 )
2305 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002306 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002307 return -1;
2308 }
2309 if( strncmp(str_dest,str_cnc,2) != 0 )
2310 {
2311 return 0;
2312 }else if( 2473 < input_frequency && input_frequency < 5744)
2313 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002314 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002315 return -1;
2316 }
2317 return 0;
2318}
qs.xiongf1b525b2022-03-31 00:58:23 -04002319int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002320{
qs.xiongc00b6032022-11-29 16:28:03 +08002321 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002322 char lynq_wifi_frequency_cmd[128]={0};
2323 char lynq_cmd_mode[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002324 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002325 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002326 check = lynq_check_set_frequency(lynq_wifi_frequency);
2327 if(check != 0)
2328 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002329 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002330 return -1;
you.chen35020192022-05-06 11:30:57 +08002331 }
qs.xiong13673462023-02-21 19:12:54 +08002332 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2333 if(check != 0)
2334 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002336 return -1;
2337 }
2338
qs.xiongc00b6032022-11-29 16:28:03 +08002339 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2340 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002341 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002342 return -1;
2343 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002344
you.chen35020192022-05-06 11:30:57 +08002345 CHECK_IDX(idx, CTRL_AP);
2346
2347 CHECK_WPA_CTRL(CTRL_AP);
2348
2349 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2350 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002351
you.chen6c2dd9c2022-05-16 17:55:28 +08002352 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002353 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2354 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2355 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002356
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002358}
2359
qs.xiongf1b525b2022-03-31 00:58:23 -04002360int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002361{
you.chen35020192022-05-06 11:30:57 +08002362 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002363 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002364 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002365
qs.xiong9fbf74e2023-03-28 13:38:22 +08002366 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2367 {
2368 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002369 return -1;
2370 }
2371 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002372
qs.xiong9fbf74e2023-03-28 13:38:22 +08002373 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002374}
2375
qs.xiongf1b525b2022-03-31 00:58:23 -04002376int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2377{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002378 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002379 CHECK_IDX(idx, CTRL_AP);
2380 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002381 case LYNQ_WIFI_BANDWIDTH_HT10:
2382 {
2383 RLOGE("bandwith [%d] not support now\n", bandwidth);
2384 return -1;
2385 }
2386 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002387 {
2388 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2389 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 if (system(lynq_cmd_bandwith) != 0 )
2391 {
2392 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002393 return -1;
2394 }
2395 system("wl up");
2396 break;
2397 }
2398 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002399 {
qs.xiong10379192023-02-21 13:19:42 +08002400 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002401 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2402 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002403 if (system(lynq_cmd_bandwith) != 0 )
2404 {
2405 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002406 return -1;
2407 }
2408 system("wl up");
2409 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002410 }
you.chen35020192022-05-06 11:30:57 +08002411 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002412 {
qs.xiong10379192023-02-21 13:19:42 +08002413 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002414 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002415 if (system(lynq_cmd_bandwith) != 0 )
2416 {
2417 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002418 return -1;
2419 }
2420 system("wl up");
2421 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 }
2423 default:
you.chen35020192022-05-06 11:30:57 +08002424 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002425 RLOGE("auth type [%d] not support now\n", bandwidth);
2426 return -1;
you.chen35020192022-05-06 11:30:57 +08002427 }
2428 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002429
2430
you.chen35020192022-05-06 11:30:57 +08002431 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002432}
you.chen35020192022-05-06 11:30:57 +08002433
qs.xiongf1b525b2022-03-31 00:58:23 -04002434int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2435{
you.chen35020192022-05-06 11:30:57 +08002436 int count = 0;
2437 int index = 0;
2438 char *split_words[128] = {0};
2439 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002440 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002441 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002442
you.chen35020192022-05-06 11:30:57 +08002443 CHECK_WPA_CTRL(CTRL_AP);
2444
2445 DO_REQUEST(lynq_chanspec_cmd);
2446
2447 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2448 for(;index < count; index++) {
2449 if (strncmp(split_words[index], "bw", 2) != 0) {
2450 continue;
2451 }
2452
2453 index++;
2454 if (index >= count) {
2455 return -1;
2456 }
2457
qs.xiong9fbf74e2023-03-28 13:38:22 +08002458 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002459 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2460 return 0;
2461 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002462 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002463 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002464}
qs.xiong0fb469a2022-04-14 03:50:45 -04002465
qs.xiongf1b525b2022-03-31 00:58:23 -04002466int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002467{
you.chen35020192022-05-06 11:30:57 +08002468 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002469 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002470 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002471
you.chen35020192022-05-06 11:30:57 +08002472 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002473
qs.xiong9fbf74e2023-03-28 13:38:22 +08002474 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2475 {
you.chen35020192022-05-06 11:30:57 +08002476 return -1;
2477 }
2478
2479 system("wl down");
2480 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002481 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002482 return -1;
2483 }
2484 system("wl up");
2485 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002486}
qs.xiong0fb469a2022-04-14 03:50:45 -04002487
qs.xiongf1b525b2022-03-31 00:58:23 -04002488int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002489{
you.chen35020192022-05-06 11:30:57 +08002490 int count = 0;
2491 int index = 0;
2492 char *split_words[128] = {0};
2493 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002494 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002495 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002496
you.chen35020192022-05-06 11:30:57 +08002497 CHECK_WPA_CTRL(CTRL_AP);
2498
2499 DO_REQUEST(lynq_chanspec_cmd);
2500
2501 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002502 for(;index < count; index++)
2503 {
2504 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002505 if (strncmp(split_words[index], "channel", 2) != 0) {
2506 continue;
2507 }
2508
2509 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002510 if (index >= count)
2511 {
you.chen35020192022-05-06 11:30:57 +08002512 return -1;
2513 }
2514
2515 *channel = atoi(split_words[index]);
2516 return 0;
2517 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002518 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002519 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002520}
2521
2522
you.chen35020192022-05-06 11:30:57 +08002523int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002524{
you.chen6c2dd9c2022-05-16 17:55:28 +08002525 char ssid[MAX_CMD] = {0};
2526 int freq = 0;
2527 char lynq_auth_cmd[64]={0};
2528 char lynq_auth_alg_cmd[64]={0};
2529 char lynq_psk_cmd[64]={0};
2530 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002531 char lynq_ieee80211_cmd[64]={0};
2532 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002533 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002534 CHECK_IDX(idx, CTRL_AP);
2535
you.chen6c2dd9c2022-05-16 17:55:28 +08002536 CHECK_WPA_CTRL(CTRL_AP);
2537
qs.xiong9fbf74e2023-03-28 13:38:22 +08002538 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2539 {
2540 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002541 return -1;
2542 }
2543
you.chen92fd5d32022-05-25 10:09:47 +08002544 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002545 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002546 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002547 return 0;
2548 }
2549 else {
2550 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2551 ssid[0] = '\0';
2552 }
2553 lynq_wifi_ap_frequency_get(idx, &freq);
2554
2555 DO_OK_FAIL_REQUEST(cmd_disconnect);
2556 DO_OK_FAIL_REQUEST(cmd_remove_all);
2557 if (ssid[0] != '\0') {
2558 lynq_wifi_ap_ssid_set(idx, ssid);
2559 }
2560 if (freq != 0) {
2561 lynq_wifi_ap_frequency_set(idx, freq);
2562 }
2563 }
2564 }
you.chen35020192022-05-06 11:30:57 +08002565
qs.xiong9fbf74e2023-03-28 13:38:22 +08002566 switch(auth){
2567 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002568 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002569 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002570 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002571 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002572 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002573 break;
2574 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002575 case LYNQ_WIFI_AUTH_WEP:
2576 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002577 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002578 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002579 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002580 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2581
2582 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2583 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2584 break;
2585 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002586 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002587 {
2588 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2589 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2590 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2591
2592 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2593 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2594 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2595 break;
2596
2597 }
you.chen35020192022-05-06 11:30:57 +08002598 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002599 {
2600 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2601 {
you.chen35020192022-05-06 11:30:57 +08002602 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2603 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2604 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002605 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2606 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002607 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002608 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002609 }
2610// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2611// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2612 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002613
you.chen35020192022-05-06 11:30:57 +08002614 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2615 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2616 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002617 break;
2618 }
2619 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2620 {
2621 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2622 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2623 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2624 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2625
2626 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2627 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2628 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2629 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2630 break;
2631 }
2632 case LYNQ_WIFI_AUTH_WPA3_PSK:
2633 {
2634 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2635 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002636 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002637 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2638
2639 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2640 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2641 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2642 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2643 break;
2644 }
2645 default:
you.chen35020192022-05-06 11:30:57 +08002646 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 RLOGE("auth type [%d] not support now\n", auth);
2648 return -1;
you.chen35020192022-05-06 11:30:57 +08002649 }
2650 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002651 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002652
qs.xiong9fbf74e2023-03-28 13:38:22 +08002653 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002654}
2655
you.chen35020192022-05-06 11:30:57 +08002656int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002657{
you.chen35020192022-05-06 11:30:57 +08002658 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002659 char lynq_auth_alg_str[MAX_RET] = {0};
2660 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002661 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002662 CHECK_IDX(idx, CTRL_AP);
2663
qs.xiong9fbf74e2023-03-28 13:38:22 +08002664 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2665 {
2666 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002667 return -1;
2668 }
2669
qs.xiong9fbf74e2023-03-28 13:38:22 +08002670 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2671 {
2672 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2673 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002674 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002675 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002676 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002677 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002678 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2679 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002680 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002681 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002682 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002683 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002684 else
2685 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002686 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002687 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002688 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002689 }
you.chen35020192022-05-06 11:30:57 +08002690 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002691 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2692 {
2693 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2694 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002695 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002696 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002697 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002698 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2699 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002700 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002701 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002702 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002703 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002704 else
2705 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002706 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002707 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002708 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002709 }
you.chen35020192022-05-06 11:30:57 +08002710 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002711
2712 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2713 {
2714 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2715 return -1;
2716 }
2717
2718 if (memcmp(lynq_auth_str,"1",1) == 0 )
2719 {
2720 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2721 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002722 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2724 {
2725 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2726 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002727 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002728 }
2729 else
2730 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002731 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002732 *auth = -1;
2733 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002734
you.chen6c2dd9c2022-05-16 17:55:28 +08002735 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002736}
qs.xiong1af5daf2022-03-14 09:12:12 -04002737
you.chenb95401e2023-05-12 19:39:06 +08002738static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2739{
2740 char status[64];
you.chencba13492023-05-19 13:53:43 +08002741 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002742 curr_status_info curr_state;
2743
2744 CHECK_WPA_CTRL(CTRL_AP);
2745
2746 memset(status, 0, sizeof (status));
2747
2748 curr_state.ap = NULL;
2749 curr_state.state = status;
2750
2751 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002752 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002753 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002754 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002755 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002756 {
2757 return 0;
2758 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002759 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002760 {
2761 DO_REQUEST("RECONNECT");
2762 return inner_check_ap_connected(idx, retry_count+1);
2763 }
you.chencba13492023-05-19 13:53:43 +08002764 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002765 {
2766 printf("retry 10 time\n");
2767 return -1;
2768 }
2769 else
2770 {
you.chen6d247052023-06-01 16:39:54 +08002771 if (strcmp(status, STATE_DISCONNECTED) == 0)
2772 {
2773 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2774 DO_REQUEST(LYNQ_WIFI_CMD);
2775 }
you.chenb95401e2023-05-12 19:39:06 +08002776 return inner_check_ap_connected(idx, retry_count+1);
2777 }
2778 }
2779 return -1;
2780}
qs.xiong1af5daf2022-03-14 09:12:12 -04002781
qs.xiongf1b525b2022-03-31 00:58:23 -04002782int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002783{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002784 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002785 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002786
qs.xiongec8bbeb2023-11-20 15:51:45 +08002787 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002788 CHECK_WPA_CTRL(CTRL_AP);
2789
you.chen0df3e7e2023-05-10 15:56:26 +08002790 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002791 {
you.chen0df3e7e2023-05-10 15:56:26 +08002792 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002793 return -1;
2794 }
you.chen35020192022-05-06 11:30:57 +08002795
qs.xiongb37f8c42023-09-13 21:21:58 +08002796
you.chen35020192022-05-06 11:30:57 +08002797 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2798 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2799
you.chenc9928582023-04-24 15:39:37 +08002800 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2801 if (ret != 0)
2802 {
2803 RLOGE("lynq_wifi_ap_start excute script fail");
2804 return -1;
2805 }
2806
you.chenb95401e2023-05-12 19:39:06 +08002807 if (inner_check_ap_connected(idx, 0) != 0)
2808 {
2809 return -1;
2810 }
2811
you.chen0df3e7e2023-05-10 15:56:26 +08002812 check_tether_and_notify();
qs.xiong9d8f3102023-12-07 20:11:37 +08002813/*
qs.xiong44fac672023-08-29 16:15:55 +08002814 if (g_ap_tmp_watcher_pid == 0)
2815 {
2816 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2817 {
2818 g_ap_tmp_watcher_pid = 0;
2819 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2820 return -1;
2821 }
2822 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2823 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002824*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002825 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002826 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002827}
2828
qs.xiongf1b525b2022-03-31 00:58:23 -04002829int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002830{
you.chen35020192022-05-06 11:30:57 +08002831 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002832}
2833
qs.xiongf1b525b2022-03-31 00:58:23 -04002834int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002835{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002836 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002837 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002838
you.chen35020192022-05-06 11:30:57 +08002839 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002840
you.chen35020192022-05-06 11:30:57 +08002841 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002842
you.chen35020192022-05-06 11:30:57 +08002843 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2844
2845 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
qs.xionga6973992024-07-23 15:17:10 +08002846 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08002847
2848 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2849 if (ret != 0)
2850 {
2851 RLOGE("lynq_wifi_ap_start excute script fail");
2852 return -1;
2853 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002854/*
qs.xiong44fac672023-08-29 16:15:55 +08002855 g_ap_tmp_watcher_stop_flag = 1;
2856 if (g_ap_tmp_watcher_pid != 0)
2857 pthread_join(g_ap_tmp_watcher_pid, NULL);
2858 g_ap_tmp_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08002859*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002860 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002861 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002862}
qs.xiong1af5daf2022-03-14 09:12:12 -04002863
qs.xiongf1b525b2022-03-31 00:58:23 -04002864int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002865{
you.chen35020192022-05-06 11:30:57 +08002866 char lynq_disable_cmd[128] = {0};
2867 char lynq_select_cmd[128] = {0};
2868 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002869 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002870 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002871
you.chen35020192022-05-06 11:30:57 +08002872 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002873 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2874 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2875
2876 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2877 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2878 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002879
qs.xiong9fbf74e2023-03-28 13:38:22 +08002880 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002881}
2882
qs.xiongf1b525b2022-03-31 00:58:23 -04002883int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002884{
you.chen35020192022-05-06 11:30:57 +08002885 char lynq_disable_cmd[128] = {0};
2886 char lynq_select_cmd[128] = {0};
2887 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002889 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002890
you.chen35020192022-05-06 11:30:57 +08002891 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002892
you.chen35020192022-05-06 11:30:57 +08002893 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2894 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2895
2896 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2897 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2898 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002899
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002901}
qs.xiongf1b525b2022-03-31 00:58:23 -04002902
you.chen35020192022-05-06 11:30:57 +08002903int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002904{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002905 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002906 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002907 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002908 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002909 RLOGD("enter lynq_ap_password_set");
2910 if( password == NULL )
2911 {
2912 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002913 return -1;
2914 }
2915 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002916 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 if(pass_len < 8 || pass_len >= 64)
2918 {
2919 RLOGE("[lynq_ap_password_set]input password len not in rage");
2920 return -1;
you.chen35020192022-05-06 11:30:57 +08002921 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002922
you.chen35020192022-05-06 11:30:57 +08002923 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002924
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2926 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002927 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002928 return -1;
2929 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002930 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2931 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002932 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2933 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002934 }
2935
you.chen35020192022-05-06 11:30:57 +08002936 CHECK_WPA_CTRL(CTRL_AP);
2937
qs.xiong9fbf74e2023-03-28 13:38:22 +08002938 if (auth == LYNQ_WIFI_AUTH_WEP)
2939 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002940 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002941 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2942 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2943 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2944 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2945 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002946 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2947 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002948 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 +08002949 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2950 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2951 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002952 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2953 {
2954
qs.xiongc8d92a62023-03-29 17:36:14 +08002955 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 +08002956 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002957 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002958 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2959 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2960
2961 }
2962 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002963 {
2964 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002965 return -1;
2966 }
you.chen35020192022-05-06 11:30:57 +08002967
you.chen35020192022-05-06 11:30:57 +08002968 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002969
qs.xiong9fbf74e2023-03-28 13:38:22 +08002970 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002971}
2972
you.chen35020192022-05-06 11:30:57 +08002973int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002974{
you.chen35020192022-05-06 11:30:57 +08002975 FILE * fp;
2976 int len, ret;
2977 int count, index;
2978 char *split_lines[128] = {0};
2979 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002980 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002981
you.chen35020192022-05-06 11:30:57 +08002982 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002983
you.chen35020192022-05-06 11:30:57 +08002984 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2985// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002986 if (NULL == fp)
2987 {
2988 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002989 return -1;
2990 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002991
you.chen35020192022-05-06 11:30:57 +08002992 buff = alloca(MAX_RET);
2993 fseek(fp, 0, SEEK_SET);
2994 len = fread(buff, 1, MAX_RET, fp);
2995 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002996
qs.xiong9fbf74e2023-03-28 13:38:22 +08002997 for(index=0; index < len; index ++)
2998 {
2999 if (memcmp(buff + index, "network={", 9) != 0)
3000 {
you.chen35020192022-05-06 11:30:57 +08003001 continue;
3002 }
3003 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003004 for (; index < len; index ++ )
3005 {
3006 if (buff[index] != '}')
3007 {
you.chen35020192022-05-06 11:30:57 +08003008 continue;
3009 }
3010 buff[index] = '\0';
3011 break;
3012 }
3013 len = buff + index - p;
3014 }
3015
3016 count = lynq_split(p, len, '\n', split_lines);
3017
3018 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 for(index=0; index < count; index++)
3020 {
you.chen35020192022-05-06 11:30:57 +08003021 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003022 if (p != NULL)
3023 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003024 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003025 if (*p == '\"')
3026 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003027 p++;
3028 }
you.chen35020192022-05-06 11:30:57 +08003029 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003030 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3031 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003032 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003033 if (*p == '\"')
3034 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003035 p++;
3036 }
3037 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003038 else
3039 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003040 continue;
you.chen35020192022-05-06 11:30:57 +08003041 }
3042
3043 strcpy(password, p);
3044
qs.xiong9fbf74e2023-03-28 13:38:22 +08003045 while(*password != '\0')
3046 {
3047 if (*password == '\"')
3048 {
you.chen35020192022-05-06 11:30:57 +08003049 *password = '\0';
3050 break;
3051 }
3052 password++;
3053 }
3054 ret = 0;
3055 break;
3056 } //end for(index=0; index < count; index++)
3057
3058 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003059}
3060
you.chen35020192022-05-06 11:30:57 +08003061static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3062 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003063 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003064
qs.xiong9fbf74e2023-03-28 13:38:22 +08003065 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3066 {
you.chen35020192022-05-06 11:30:57 +08003067 return -1;
3068 }
3069
3070 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003071
qs.xiong9fbf74e2023-03-28 13:38:22 +08003072 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3073 {
3074 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003075 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003076 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003077 {
you.chena6cd55a2022-05-08 12:20:18 +08003078 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003079 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003080 }
you.chen70f377f2023-04-14 18:17:09 +08003081 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3082 {
3083 return 0;
3084 }
you.chena6cd55a2022-05-08 12:20:18 +08003085 }
3086 }
you.chen70f377f2023-04-14 18:17:09 +08003087 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3088 {
3089 return 0;
3090 }
3091
qs.xiong9fbf74e2023-03-28 13:38:22 +08003092 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3093 {
you.chen70f377f2023-04-14 18:17:09 +08003094 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003095 return -1;
3096 }
3097 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3098 {
3099
you.chen70f377f2023-04-14 18:17:09 +08003100 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3101 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003102 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3103 {
3104
3105 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003106 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003107 }else
3108 {
you.chen70f377f2023-04-14 18:17:09 +08003109 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003110 *auth = -1;
3111 return -1;
3112 }
you.chen35020192022-05-06 11:30:57 +08003113 return 0;
3114}
3115
3116int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003117{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003118 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003119 int pass_len, net_no, count, index;
3120 char lynq_tmp_cmd[300]={0};
3121 int net_no_list[128];
3122 lynq_wifi_auth_s net_auth;
3123 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003124 if(pass_len < 8 || pass_len >= 64)
3125 {
3126 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003127 return -1;
3128 }
3129
3130 CHECK_IDX(idx, CTRL_STA);
3131
3132 net_no = -1;
3133 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3134
qs.xiong9fbf74e2023-03-28 13:38:22 +08003135 for (index=0; index < count; index++)
3136 {
you.chen35020192022-05-06 11:30:57 +08003137 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003138 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3139 {
you.chen35020192022-05-06 11:30:57 +08003140 net_no = net_no_list[index];
3141 break;
3142 }
3143 }
3144
qs.xiong9fbf74e2023-03-28 13:38:22 +08003145 if (net_no < 0)
3146 {
you.chen35020192022-05-06 11:30:57 +08003147 return -1;
3148 }
3149
3150 CHECK_WPA_CTRL(CTRL_STA);
3151
3152 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3153
3154 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3155 DO_OK_FAIL_REQUEST(cmd_save_config);
3156
3157 return 0;
3158}
3159
qs.xiongb5dab082023-10-13 14:43:41 +08003160/**
3161* buff data
3162* buff_len size of buff
3163* idx sta
3164* *ap ap info for find ssid && password
3165* password return password
3166*
3167*/
3168static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3169
3170 int ret, network_len, i, ssid_len,curr_auth;
3171 int count, index,org_index;
3172 char *split_lines[128] = {0};
3173 char *p, *ssid, *ssid_end_flag,*ptr;
3174 char tmp_ssid[128]={0};
3175 char tmp_auth[24]={0};
3176
3177 org_index = 0;
3178 network_len = 0;
3179 p = NULL;
3180
3181 CHECK_IDX(idx, CTRL_STA);
3182
3183 while(1){
3184 network_len = 0;
3185 p == NULL;
3186 for(; org_index < buff_len; org_index ++)
3187 {
3188 for(; org_index < buff_len; org_index ++)
3189 {
3190 if (memcmp(buff + org_index, "network={", 9) != 0)
3191 {
3192 continue;
3193 }
3194 p = buff + org_index + 9;
3195
3196 for (; org_index < buff_len; org_index ++ )
3197 {
3198 if (buff[org_index] != '}')
3199 {
3200 continue;
3201 }
3202 buff[org_index] = '\0';
3203 break;
3204 }
3205 network_len = buff + org_index - p;
3206 break;
3207 }
3208
3209 if (p == NULL)
3210 {
3211 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3212 return -1;
3213 }
3214
3215 ssid = strstr(p, "ssid=");
3216 if (ssid != NULL) {
3217 ssid += strlen("ssid=");
3218 if (ssid[0] == '\"')
3219 {
3220 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3221 {
qs.xiongb5dab082023-10-13 14:43:41 +08003222 break;
3223 }
qs.xiongb5dab082023-10-13 14:43:41 +08003224 }
3225 else
3226 {
3227 ssid_end_flag = strstr(ssid, "\n");
3228 if (ssid_end_flag != NULL)
3229 {
3230 ssid_len = (ssid_end_flag - ssid) / 2;
3231 for(i=0; i<ssid_len; i++)
3232 {
3233 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3234 }
3235 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3236 {
qs.xiongb5dab082023-10-13 14:43:41 +08003237 break;
3238 }
3239 }
3240 }
3241 }
3242
3243 }
3244
3245 if (org_index >= buff_len || NULL == p || network_len <= 0)
3246 {
3247
3248 if (buff != NULL)
3249 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3250 return -1;
3251 }
3252
3253 count = lynq_split(p, network_len, '\n', split_lines);
3254 ret = -1;
3255 for( index=0; index < count; index++ )
3256 {
3257 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003258 if(p != NULL)
3259 {
3260 p += 9;
3261 if(memcmp(p,"SAE",3) == 0)
3262 {
3263 curr_auth = 5;
3264 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3265 {
3266 curr_auth = 4;
3267 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3268 {
3269 curr_auth = 3;
3270 }else if(memcmp(p,"NONE",4) == 0 )
3271 {
3272 curr_auth = 0;
3273 }else{
3274 curr_auth = 1;
3275 }
qs.xiongb5dab082023-10-13 14:43:41 +08003276 if( curr_auth < 1 || curr_auth > 6)
3277 {
3278 ret = -1;
3279 }
3280 break;
3281 }
3282 }
3283 if( curr_auth == 0)
3284 {
3285 return 0;
3286 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3287 {
3288 for(index=0; index < count; index++)
3289 {
3290 /*get psw info*/
3291
3292 p = strstr(split_lines[index], "psk=");
3293 if (p != NULL)
3294 {
3295 p += 4;
3296 if (*p == '\"')
3297 {
3298 p++;
3299 }
3300 }
3301 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3302 {
3303 p += 9;
3304 if (*p == '\"')
3305 {
3306 p++;
3307 }
3308 }
3309 else
3310 {
3311 continue;
3312 }
3313
3314 if (*p == '\"')
3315 p++;
3316 strncpy(password, p, 64);
3317 p = password;
3318 while(password - p < 64 && *password != '\0')
3319 {
3320 if (*password == '\"')
3321 {
3322 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003323 ret = 0;
3324 break;
3325 }
3326 password++;
3327 }
3328 break;
3329 }
3330 break;
3331 }
3332 }
3333
3334 return ret;
3335}
3336
3337
3338
you.chen35020192022-05-06 11:30:57 +08003339int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3340
3341 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003342 int len, ret;
3343 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003344 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003345
qs.xiongb5dab082023-10-13 14:43:41 +08003346 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003347 CHECK_IDX(idx, CTRL_STA);
3348
qs.xiong9fbf74e2023-03-28 13:38:22 +08003349 if (NULL == password)
3350 {
3351 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003352 return -1;
3353 }
3354
you.chen35020192022-05-06 11:30:57 +08003355 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003356 if (NULL == fp)
3357 {
3358 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003359 return -1;
3360 }
3361
you.chen6d247052023-06-01 16:39:54 +08003362 fseek(fp, 0, SEEK_END);
3363 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003364 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003365
qs.xiongb5dab082023-10-13 14:43:41 +08003366 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003367 {
3368 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3369 return -1;
3370 }
3371
you.chen35020192022-05-06 11:30:57 +08003372 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003373 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003374 fclose(fp);
3375
qs.xiongb5dab082023-10-13 14:43:41 +08003376
3377 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3378
3379 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003380 {
qs.xiongb04dc852024-03-27 21:55:32 +08003381 //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 +08003382 free(info_buff);
3383 return 0;
you.chen35020192022-05-06 11:30:57 +08003384 }
qs.xiongb5dab082023-10-13 14:43:41 +08003385 else{
3386 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003387 return -1;
3388 }
3389
you.chen35020192022-05-06 11:30:57 +08003390}
3391
qs.xiongb5dab082023-10-13 14:43:41 +08003392
you.chen35020192022-05-06 11:30:57 +08003393static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3394{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003395 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003396
qs.xiong9fbf74e2023-03-28 13:38:22 +08003397 if (sta_ssid == NULL)
3398 {
3399 RLOGE("sta_ssid is null\n");
3400 return -1;
you.chen35020192022-05-06 11:30:57 +08003401 }
3402
qs.xiong9fbf74e2023-03-28 13:38:22 +08003403 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003404
3405 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3406
3407 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3408// DO_OK_FAIL_REQUEST(cmd_save_config);
3409
qs.xiong9fbf74e2023-03-28 13:38:22 +08003410 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003411
3412}
3413
you.chen35020192022-05-06 11:30:57 +08003414static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003415{
you.chen35020192022-05-06 11:30:57 +08003416 char lynq_disable_cmd[128]={0};
3417 char lynq_select_cmd[128]={0};
3418
3419 CHECK_WPA_CTRL(CTRL_STA);
3420
qs.xiong9fbf74e2023-03-28 13:38:22 +08003421 if (save != 0)
3422 {
you.chenc29444e2022-06-07 18:01:16 +08003423 if (start_flag != 0)
3424 {
3425 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3426 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3427 }
3428 else
3429 {
3430 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3431 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3432 }
you.chen35020192022-05-06 11:30:57 +08003433 DO_OK_FAIL_REQUEST(cmd_save_config);
3434 }
3435
qs.xiong9fbf74e2023-03-28 13:38:22 +08003436 if (start_flag == 0)
3437 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003438 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003439 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3440 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003441 else
3442 {
you.chen35020192022-05-06 11:30:57 +08003443 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3444 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3445 }
3446
3447 return 0;
3448}
3449
3450int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3451{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003452 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003453 CHECK_IDX(idx, CTRL_STA);
3454
you.chen6c2dd9c2022-05-16 17:55:28 +08003455 curr_status_info curr_state;
3456 ap_info_s ap_info;
3457 curr_state.ap = &ap_info;
3458 curr_state.state = NULL;
3459
qs.xiong9fbf74e2023-03-28 13:38:22 +08003460 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3461 {
you.chend2fef3f2023-02-13 10:50:35 +08003462 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003463 return 0;
3464 }
3465
3466 return -1;
you.chen35020192022-05-06 11:30:57 +08003467}
3468
3469int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3470{
qs.xiong5d716d22023-09-20 20:08:39 +08003471 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003472 scan_info_s *scan_list = NULL;
3473 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003474 int scan_len=0;
3475 int save_len=0;
3476 int best_index = -1;
3477 int best_scan_index = -1;
3478 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003479 int i, j, ret;
3480
3481 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003482
3483 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003484 if (info == NULL)
3485 {
you.chen35020192022-05-06 11:30:57 +08003486 return -1;
3487 }
3488
3489 curr_status_info curr_state;
3490 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003491 char status[64];
you.chen35020192022-05-06 11:30:57 +08003492
you.chen9ac66392022-08-06 17:01:16 +08003493 memset(&ap_info, 0, sizeof (ap_info));
3494 memset(status, 0, sizeof (status));
3495
3496 curr_state.ap = &ap_info;
3497 curr_state.state = status;
3498
qs.xiong9fbf74e2023-03-28 13:38:22 +08003499 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3500 {
you.chen35020192022-05-06 11:30:57 +08003501 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003502 if (strcmp(status, STATE_COMPLETED) == 0)
3503 {
3504 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003505 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003506 }
3507 else
3508 {
3509 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003510 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003511 }
you.chen593621d2023-04-27 17:52:44 +08003512 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003513 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003514 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003515 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003516 return 0;
3517 }
3518
you.chen9ac66392022-08-06 17:01:16 +08003519 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003520 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003521 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3522 {
you.chen9ac66392022-08-06 17:01:16 +08003523 if (NULL != scan_list)
3524 {
3525 free(scan_list);
3526 }
you.chen35020192022-05-06 11:30:57 +08003527 return -1;
3528 }
3529
qs.xiong9fbf74e2023-03-28 13:38:22 +08003530 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3531 {
you.chen9ac66392022-08-06 17:01:16 +08003532 if (NULL != scan_list)
3533 {
3534 free(scan_list);
3535 }
3536 if (NULL != save_list)
3537 {
3538 free(save_list);
3539 }
you.chen35020192022-05-06 11:30:57 +08003540 return -1;
3541 }
3542
qs.xiong9fbf74e2023-03-28 13:38:22 +08003543 for (i=0; i < save_len; i++)
3544 {
3545 for (j=0; j < scan_len; j++)
3546 {
you.chen35020192022-05-06 11:30:57 +08003547 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003548 && save_list[i].base_info.auth == scan_list[j].auth)
3549 {
3550 if (best_rssi == 0)
3551 {
you.chen9ac66392022-08-06 17:01:16 +08003552 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003553 best_rssi = scan_list[j].rssi;
3554 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003555 else if (best_rssi > scan_list[j].rssi)
3556 {
you.chen35020192022-05-06 11:30:57 +08003557 best_index = i;
3558 best_scan_index = j;
3559 best_rssi = scan_list[j].rssi;
3560 }
you.chend2fef3f2023-02-13 10:50:35 +08003561 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 +08003562 break;
3563 }
3564 }
3565 }
3566
qs.xiong9fbf74e2023-03-28 13:38:22 +08003567 if (best_index >= 0)
3568 {
you.chen35020192022-05-06 11:30:57 +08003569 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003570 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 +08003571 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003572 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003573 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003574 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003575 }
3576
you.chen9ac66392022-08-06 17:01:16 +08003577 if (NULL != scan_list)
3578 {
3579 free(scan_list);
3580 }
3581 if (NULL != save_list)
3582 {
3583 free(save_list);
3584 }
3585
qs.xiong5d716d22023-09-20 20:08:39 +08003586 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003587 return ret;
you.chen35020192022-05-06 11:30:57 +08003588}
3589
3590static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3591{
qs.xiongc8d92a62023-03-29 17:36:14 +08003592 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003593 char lynq_ket_mgmt_cmd[64]={0};
3594 char lynq_pairwise_cmd[64]={0};
3595 char lynq_psk_cmd[64]={0};
3596
3597 CHECK_WPA_CTRL(CTRL_STA);
3598
qs.xiong9fbf74e2023-03-28 13:38:22 +08003599 switch(auth)
3600 {
3601 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003602 {
3603 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003604
you.chen35020192022-05-06 11:30:57 +08003605 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003606// DO_OK_FAIL_REQUEST(cmd_save_config);
3607 break;
3608 }
3609 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003610 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003611 {
3612 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3613 {
you.chen35020192022-05-06 11:30:57 +08003614 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3615 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003616 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3617 {
you.chena6cd55a2022-05-08 12:20:18 +08003618 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003619 }
3620 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3621 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003622
you.chen35020192022-05-06 11:30:57 +08003623 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3624 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3625 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003626
qs.xiong9fbf74e2023-03-28 13:38:22 +08003627 if (password != NULL)
3628 {
you.chen35020192022-05-06 11:30:57 +08003629 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3630 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003631 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003632 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003633
you.chen35020192022-05-06 11:30:57 +08003634// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003635 break;
3636 }
3637 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3638 {
qs.xiong3e506812023-04-06 11:08:48 +08003639 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003640 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003641 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3642 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3643
qs.xiong3e506812023-04-06 11:08:48 +08003644 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003645 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3646 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3647 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3648
3649 break;
3650 }
3651 case LYNQ_WIFI_AUTH_WPA3_PSK:
3652 {
qs.xiong3e506812023-04-06 11:08:48 +08003653 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003654 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003655 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3656 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3657
qs.xiongb37f8c42023-09-13 21:21:58 +08003658 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003659 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3660 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3661 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3662
3663 break;
3664 }
3665 default:
3666 return -1;
you.chen35020192022-05-06 11:30:57 +08003667 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003668
qs.xiong9fbf74e2023-03-28 13:38:22 +08003669 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003670}
qs.xiong7a105ce2022-03-02 09:43:11 -05003671
you.chen35020192022-05-06 11:30:57 +08003672static int inner_get_curr_net_no(int interface) {
3673 curr_status_info curr_state;
3674 curr_state.ap = NULL;
3675 curr_state.state = NULL;
3676
qs.xiong9fbf74e2023-03-28 13:38:22 +08003677 if (0 != inner_get_status_info(interface, &curr_state))
3678 {
you.chen35020192022-05-06 11:30:57 +08003679 return -1;
3680 }
3681
3682 return curr_state.net_no;
3683}
3684
3685int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003686{
you.chen35020192022-05-06 11:30:57 +08003687 int net_no;
3688 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003689
you.chen35020192022-05-06 11:30:57 +08003690 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003691
qs.xiong9fbf74e2023-03-28 13:38:22 +08003692 if (net_no < 0)
3693 {
you.chen35020192022-05-06 11:30:57 +08003694 return -1;
3695 }
3696
3697 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003698}
3699
qs.xiongfb992712024-03-28 16:32:50 +08003700/*
3701*
3702* ap_type: 0 means unhide ap 1 means hide ap
3703*/
3704int lynq_wifi_sta_connect_common(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout,int ap_type)
qs.xiong7a105ce2022-03-02 09:43:11 -05003705{
qs.xionga8cbe222024-04-15 16:42:13 +08003706 RLOGD("enter %s %d",__func__,__LINE__);
3707 pthread_mutex_lock(&s_func_run_mutex);
3708 int count, net_no, index,res;
you.chen35020192022-05-06 11:30:57 +08003709 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003710 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003711 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003712 curr_status_info curr_state;
3713 ap_info_s ap_info;
3714 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003715
qs.xionga8cbe222024-04-15 16:42:13 +08003716 res = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003717 if (ssid == NULL || *ssid == '\0')
3718 {
3719 RLOGE("bad ssid\n");
qs.xionga8cbe222024-04-15 16:42:13 +08003720 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003721 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003722
qs.xiong9fbf74e2023-03-28 13:38:22 +08003723 if (LYNQ_WIFI_AUTH_OPEN != auth)
3724 {
3725 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003726 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003727 RLOGE("bad password\n");
qs.xionga8cbe222024-04-15 16:42:13 +08003728 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003729 }
3730 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003731
you.chen70f377f2023-04-14 18:17:09 +08003732
3733 pthread_mutex_lock(&s_global_check_mutex);
3734 if (s_sta_status != INNER_STA_STATUS_INIT)
3735 {
3736 s_sta_status = INNER_STA_STATUS_CANCEL;
3737 pthread_cond_signal(&s_global_check_cond);
3738 }
3739 pthread_mutex_unlock(&s_global_check_mutex);
3740
you.chen35020192022-05-06 11:30:57 +08003741 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003742 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003743
3744 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003745 memset(&ap_info, 0, sizeof (ap_info));
3746 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003747
you.chen70f377f2023-04-14 18:17:09 +08003748 curr_state.ap = &ap_info;
3749 curr_state.state = status;
3750
3751 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003752 {
you.chen70f377f2023-04-14 18:17:09 +08003753 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3754 {
3755 net_no = curr_state.net_no;
3756 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3757 && strcmp(ap_info.psw, psw) == 0)
3758 {
qs.xionga8cbe222024-04-15 16:42:13 +08003759 RLOGD("already connected %s %d\n",__func__,__LINE__);
you.chen70f377f2023-04-14 18:17:09 +08003760 pthread_mutex_lock(&s_global_check_mutex);
3761 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003762 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003763 pthread_cond_signal(&s_global_check_cond);
3764 pthread_mutex_unlock(&s_global_check_mutex);
qs.xionga8cbe222024-04-15 16:42:13 +08003765 res = 0;
3766 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003767 }
you.chen35020192022-05-06 11:30:57 +08003768 }
3769 }
3770
you.chen70f377f2023-04-14 18:17:09 +08003771 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003772 {
you.chen70f377f2023-04-14 18:17:09 +08003773 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3774
3775 for (index=0; index < count; index++)
3776 {
3777 net_auth = -1;
3778 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3779 {
3780 net_no = net_no_list[index];
3781 break;
3782 }
you.chen35020192022-05-06 11:30:57 +08003783 }
3784
you.chen70f377f2023-04-14 18:17:09 +08003785 if (net_no < 0)
3786 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003787 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3788 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3789 {
3790 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3791 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3792 DO_OK_FAIL_REQUEST(rm_net_cmd);
3793 }
you.chen70f377f2023-04-14 18:17:09 +08003794 net_no = lynq_add_network(CTRL_STA);
3795 if (net_no == -1)
3796 {
qs.xionga8cbe222024-04-15 16:42:13 +08003797 RLOGE("add network id failed %s %d\n",__func__,__LINE__);
3798 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003799 }
3800
3801 RLOGD("net no is %d\n", net_no);
3802 if (0 != inner_set_sta_ssid(net_no, ssid))
3803 {
qs.xionga8cbe222024-04-15 16:42:13 +08003804 RLOGE("set network %d ssid failed %s %d\n",net_no,__func__,__LINE__);
3805 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003806 }
you.chen35020192022-05-06 11:30:57 +08003807 }
3808 }
3809
qs.xiong9fbf74e2023-03-28 13:38:22 +08003810 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3811 {
qs.xionga8cbe222024-04-15 16:42:13 +08003812 RLOGE("set inner_set_sta_auth_psw %d ssid failed %s %d\n",net_no,__func__,__LINE__);
3813 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003814 }
3815
qs.xiongfb992712024-03-28 16:32:50 +08003816//begain 20240328 change for support sta connect hide ap by qs.xiong API-1569
3817 if( ap_type == 1 )
3818 {
3819 char scan_hide_ap_cmd[64];
3820 sprintf(scan_hide_ap_cmd,"SET_NETWORK %d scan_ssid 1",net_no);
3821 RLOGD("current conenct ap is hide ap cmd:%s\n",scan_hide_ap_cmd);
3822 DO_OK_FAIL_REQUEST(scan_hide_ap_cmd);
3823 }
3824//end 20240328 change for support sta connect hide ap by qs.xiong API-1569
you.chen70f377f2023-04-14 18:17:09 +08003825 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003826 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003827 usleep(200*1000);
3828
qs.xiong09560402023-10-27 21:58:55 +08003829 pthread_mutex_lock(&s_global_check_mutex);
3830 lynq_sta_removeElement(net_no);
3831 pthread_mutex_unlock(&s_global_check_mutex);
3832
qs.xionga8cbe222024-04-15 16:42:13 +08003833 res = inner_sta_start_stop(net_no, 1, 1);
you.chen70f377f2023-04-14 18:17:09 +08003834
3835 pthread_mutex_lock(&s_global_check_mutex);
3836 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003837 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003838 strcpy(s_sta_current_connecting_ssid, ssid);
3839 struct timeval now;
3840 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003841 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003842 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3843 pthread_cond_signal(&s_global_check_cond);
3844 pthread_mutex_unlock(&s_global_check_mutex);
qs.xionga8cbe222024-04-15 16:42:13 +08003845
3846CONNECT_END:
3847 pthread_mutex_unlock(&s_func_run_mutex);
3848 RLOGD("end %s %d and return is %d \n",__func__,__LINE__,res);
3849 return res;
3850
qs.xiong7a105ce2022-03-02 09:43:11 -05003851}
3852
qs.xiongfb992712024-03-28 16:32:50 +08003853int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
3854{
3855 return lynq_wifi_sta_connect_common(idx,ssid, auth, psw,timeout,0);
3856}
3857
3858int lynq_wifi_sta_connect_hide(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
3859{
3860 return lynq_wifi_sta_connect_common(idx,ssid, auth, psw,timeout,1);
3861}
3862
you.chenb95401e2023-05-12 19:39:06 +08003863int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3864{
3865 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3866}
3867
you.chen35020192022-05-06 11:30:57 +08003868int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003869{
you.chen35020192022-05-06 11:30:57 +08003870 ap_info_s ap;
3871 curr_status_info curr_state;
3872 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003873
qs.xiong9fbf74e2023-03-28 13:38:22 +08003874 if (ssid == NULL || *ssid == '\0')
3875 {
3876 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003877 return -1;
3878 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003879
you.chen35020192022-05-06 11:30:57 +08003880 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003881
you.chen35020192022-05-06 11:30:57 +08003882 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003883 curr_state.state = NULL;
3884
qs.xiong9fbf74e2023-03-28 13:38:22 +08003885 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3886 {
you.chen35020192022-05-06 11:30:57 +08003887 return 0;
3888 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003889
qs.xiong9fbf74e2023-03-28 13:38:22 +08003890 if (strcmp(ap.ap_ssid, ssid) != 0)
3891 {
you.chen35020192022-05-06 11:30:57 +08003892 return 0;
3893 }
3894
you.chen70f377f2023-04-14 18:17:09 +08003895 pthread_mutex_lock(&s_global_check_mutex);
3896 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3897 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003898 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003899}
qs.xiong97fa59b2022-04-07 05:41:29 -04003900
qs.xiongc93bf2b2023-08-25 10:22:08 +08003901int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3902{
qs.xiong09560402023-10-27 21:58:55 +08003903 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003904 ap_info_s ap;
3905 curr_status_info curr_state;
3906 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003907 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003908
3909 if (ssid == NULL || *ssid == '\0')
3910 {
3911 RLOGE("input ssid is NULL\n");
3912 return -1;
3913 }
3914
3915 CHECK_IDX(idx, CTRL_STA);
3916
3917
3918 curr_state.ap = &ap;
3919 curr_state.state = NULL;
3920
3921 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3922 {
3923 return 0;
3924 }
3925
3926 if (strcmp(ap.ap_ssid, ssid) != 0)
3927 {
3928 return 0;
3929 }
3930
3931 pthread_mutex_lock(&s_global_check_mutex);
3932 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003933 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3934 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3935 {
3936 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);
3937 if( g_history_disconnect_net[i] == curr_state.net_no)
3938 {
3939 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3940 check_history_idx_flag = 1;
3941 break;
3942 }
3943 }
3944 if ( check_history_idx_flag == 0)
3945 {
3946 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__);
3947 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3948 g_history_disconnect_valid_num++;
3949 }
3950 RLOGD("%s %d",__func__,__LINE__);
3951 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003952 pthread_mutex_unlock(&s_global_check_mutex);
3953 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3954
3955}
3956
3957
you.chena6cd55a2022-05-08 12:20:18 +08003958int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3959{
qs.xiong1b5e3472023-11-27 17:42:20 +08003960 RLOGD("enter %s %d func",__func__,__LINE__);
qs.xiongad2f89d2023-01-18 13:17:41 +08003961 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3962 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003963
you.chen35020192022-05-06 11:30:57 +08003964 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003965 CHECK_WPA_CTRL(CTRL_STA);
3966
you.chenc9928582023-04-24 15:39:37 +08003967 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3968 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003969 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003970 RLOGE("lynq_wifi_sta_start excute script fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08003971 return -1;
3972 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003973
qs.xiongad2f89d2023-01-18 13:17:41 +08003974 system(lynq_enable_sta_cmd);
3975 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003976 pthread_mutex_lock(&s_global_check_mutex);
3977 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003978 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003979 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08003980 RLOGD("end %s %d func",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003981 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003982}
3983
you.chen6d247052023-06-01 16:39:54 +08003984static int inner_get_status_info_state (int interface, char *state) {
3985 curr_status_info curr_state;
3986 curr_state.ap = NULL;
3987 curr_state.state = state;
3988 return inner_get_status_info(interface, &curr_state);
3989}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003990
3991int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3992{
3993
qs.xiongb37f8c42023-09-13 21:21:58 +08003994 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3995 int tmp_open_idx[128];
3996 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003997
qs.xiongb37f8c42023-09-13 21:21:58 +08003998 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003999 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08004000 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
4001 pthread_mutex_unlock(&s_global_check_mutex);
4002 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08004003 {
qs.xiongb37f8c42023-09-13 21:21:58 +08004004 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08004005 }
4006
qs.xiongb37f8c42023-09-13 21:21:58 +08004007 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08004008 return 0;
4009}
4010
4011
you.chen35020192022-05-06 11:30:57 +08004012int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04004013{
qs.xiongec8bbeb2023-11-20 15:51:45 +08004014 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004015 int i=0;
4016 char state[MAX_CMD];
qs.xionga6973992024-07-23 15:17:10 +08004017 const char * cmd_disable_all_network = "DISABLE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +08004018
you.chena6cd55a2022-05-08 12:20:18 +08004019 CHECK_IDX(idx, CTRL_STA);
4020 CHECK_WPA_CTRL(CTRL_STA);
4021
you.chen6d247052023-06-01 16:39:54 +08004022 DO_OK_FAIL_REQUEST(cmd_disconnect);
qs.xionga6973992024-07-23 15:17:10 +08004023 DO_OK_FAIL_REQUEST(cmd_disable_all_network);
you.chena6cd55a2022-05-08 12:20:18 +08004024 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08004025
4026 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
4027 if (ret != 0)
4028 {
qs.xiong1b5e3472023-11-27 17:42:20 +08004029 RLOGE("lynq_wifi_sta_stop excute script fail %s %d",__func__,__LINE__);
you.chenc9928582023-04-24 15:39:37 +08004030 return -1;
4031 }
4032
you.chen6d247052023-06-01 16:39:54 +08004033 for (i=0; i < 30; i++) // to check if sta is realy stoped
4034 {
4035 if (inner_get_status_info_state(idx, state) != 0)
4036 {
4037 break;
4038 }
4039
4040 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
4041 {
4042 break;
4043 }
qs.xiong1b5e3472023-11-27 17:42:20 +08004044 RLOGD("lynq_wifi_sta_stop curr state %s %s %d", state,__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004045 usleep(SLEEP_TIME_ON_IDLE);
4046 }
qs.xiongb37f8c42023-09-13 21:21:58 +08004047 pthread_mutex_lock(&s_global_check_mutex);
4048 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
4049 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004050 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08004051 return 0;
4052// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04004053}
qs.xiongfcc914b2023-07-06 21:16:20 +08004054int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
4055{
4056 char LYNQ_DISABLE_CMD[128]={0};
4057 CHECK_IDX(idx, CTRL_STA);
4058 CHECK_WPA_CTRL(CTRL_STA);
4059 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
4060 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
4061 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
4062 return 0;
4063}
qs.xiong7a105ce2022-03-02 09:43:11 -05004064
you.chen35020192022-05-06 11:30:57 +08004065//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
4066// int i, count;
4067// char *p;
4068// const char * FLAG_SSID = "ssid=";
4069// const char * FLAG_SBSID = "bssid=";
4070// const char * FLAG_KEY_MGMT = "key_mgmt=";
4071// const char * FLAG_FREQ = "freq=";
4072// char lynq_sta_cmd[MAX_CMD];
4073// char *split_lines[128] = {0};
4074
4075// CHECK_WPA_CTRL(CTRL_AP);
4076
4077// sprintf(lynq_sta_cmd, "STA %s", bssid);
4078
4079// DO_REQUEST(lynq_sta_cmd);
4080
4081// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4082
4083// for(i=0; i < count; i++) {
4084// p = strstr(split_lines[i], FLAG_SSID);
4085// if (p != NULL) {
4086// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
4087// continue;
4088// }
4089// }
4090
4091// lynq_get_interface_ip(idx, ap->ap_ip);
4092// lynq_ap_password_set(idx, ap->psw);
4093
4094// return 0;
4095//}
4096
4097static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4098 curr_status_info curr_state;
4099 curr_state.ap = ap;
4100 curr_state.state = NULL;
4101 return inner_get_status_info(interface, &curr_state);
4102}
4103
4104int 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 -04004105{
qs.xiong5071c802023-09-06 14:04:15 +08004106 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004107 int index, line_count;
4108 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004109 const char *lynq_first_sta_cmd = "STA-FIRST";
4110 char lynq_next_sta_cmd[MAX_CMD];
4111 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004112 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004113
you.chen35020192022-05-06 11:30:57 +08004114 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004115
you.chen35020192022-05-06 11:30:57 +08004116 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004117
you.chenb95401e2023-05-12 19:39:06 +08004118 // ap_info_s * tmp_ap;
4119 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004120 if (ap == NULL || list == NULL || len == NULL)
4121 {
4122 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004123 return -1;
4124 }
4125
you.chenb95401e2023-05-12 19:39:06 +08004126 // ap = &tmp_ap;
4127 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004128 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004129 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004130
you.chenb95401e2023-05-12 19:39:06 +08004131 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004132 {
you.chenb95401e2023-05-12 19:39:06 +08004133 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004134 return -1;
4135 }
4136
4137 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4138 lynq_ap_password_get(idx, (*ap)->psw);
4139
you.chen35020192022-05-06 11:30:57 +08004140 DO_REQUEST(lynq_first_sta_cmd);
4141
4142 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004143 while (reply_len > 0)
4144 {
4145 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004146 {
you.chen35020192022-05-06 11:30:57 +08004147 break;
4148 }
4149 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4150 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4151 strcpy(bssid[index], split_lines[0]);
4152 index++;
4153 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4154 reply_len = MAX_RET;
4155 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004156 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 +08004157 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004158 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004159 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004160 break;
4161 }
4162 }
4163
4164 *len = index;
4165
4166 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004167 for (index=0; index < *len; index++)
4168 {
you.chend2fef3f2023-02-13 10:50:35 +08004169 dev_info = &(*list)[index];
4170 memset(dev_info, 0, sizeof(device_info_s));
4171 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4172 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4173 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4174 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004175 free(bssid[index]);
4176 }
qs.xiong5071c802023-09-06 14:04:15 +08004177 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004178 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004179}
4180
you.chen35020192022-05-06 11:30:57 +08004181int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004182{
you.chen35020192022-05-06 11:30:57 +08004183 int i, count, index, count_words;
4184 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4185 char *split_lines[128] = {0};
4186 char *split_words[128] = {0};
4187 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004188
qs.xiong9fbf74e2023-03-28 13:38:22 +08004189 if (list == NULL || len == NULL)
4190 {
you.chen35020192022-05-06 11:30:57 +08004191 return -1;
4192 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004193
you.chen9ac66392022-08-06 17:01:16 +08004194 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4195 {
4196 usleep(100 * 1000);
4197 }
4198
you.chen35020192022-05-06 11:30:57 +08004199 CHECK_IDX(idx, CTRL_STA);
4200
4201 CHECK_WPA_CTRL(CTRL_STA);
4202
4203 DO_REQUEST(lynq_scan_result_cmd);
4204
4205 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4206 *len = count - 1;
4207 *list = malloc(sizeof (scan_info_s) * *len);
4208
4209 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004210 for (index=0; index <count_words; index++)
4211 {
4212 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004213 }
4214
qs.xiong9fbf74e2023-03-28 13:38:22 +08004215 for(index = 1;index < count; index++)
4216 {
4217 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004218 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004219 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4220 if (count_words < 4)
4221 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004222 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004223 //bssid / frequency / signal level / flags / ssid
4224 p = (*list) + index - 1;
4225 strcpy(p->mac, split_words[0]);
4226 p->band = convert_band_from_freq(atoi(split_words[1]));
4227 p->rssi = -1 * atoi( split_words[2]);
4228 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004229 if (count_words == 4) // ssid hided
4230 {
4231 p->ssid[0] = '\0';
4232 }
4233 else
4234 {
4235 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4236 }
you.chen35020192022-05-06 11:30:57 +08004237 }
4238
4239 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004240}
qs.xiong97fa59b2022-04-07 05:41:29 -04004241
you.chen35020192022-05-06 11:30:57 +08004242int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4243{
4244 int count, net_no, index;
4245 int net_no_list[128];
4246 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004247
you.chen35020192022-05-06 11:30:57 +08004248 char lynq_remove_cmd[MAX_CMD];
4249
qs.xiong9fbf74e2023-03-28 13:38:22 +08004250 if (ssid == NULL || *ssid == '\0')
4251 {
4252 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004253 return -1;
4254 }
4255
4256 CHECK_IDX(idx, CTRL_STA);
4257
4258 CHECK_WPA_CTRL(CTRL_STA);
4259
4260 net_no = -1;
4261 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4262
qs.xiong9fbf74e2023-03-28 13:38:22 +08004263 for (index=0; index < count; index++)
4264 {
you.chen35020192022-05-06 11:30:57 +08004265 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004266 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4267 {
you.chen35020192022-05-06 11:30:57 +08004268 net_no = net_no_list[index];
4269 break;
4270 }
4271 }
4272
qs.xiong9fbf74e2023-03-28 13:38:22 +08004273 if (net_no < 0)
4274 {
you.chen35020192022-05-06 11:30:57 +08004275 return 0;
4276 }
4277
4278 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4279
4280 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004281
4282 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4283
4284 pthread_mutex_lock(&s_global_check_mutex);
4285 lynq_sta_removeElement(net_no);
4286 pthread_mutex_unlock(&s_global_check_mutex);
4287
4288 RLOGD("%s %d",__func__,__LINE__);
4289 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004290 DO_OK_FAIL_REQUEST(cmd_save_config);
4291
4292 return 0;
4293}
4294
4295int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004296{
you.chend2fef3f2023-02-13 10:50:35 +08004297 int count, index;
you.chen35020192022-05-06 11:30:57 +08004298 int net_no_list[128];
4299 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004300 RLOGD("enter lynq_get_sta_saved_ap api\n");
4301 if (list == NULL || len == NULL)
4302 {
4303 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004304 return -1;
4305 }
4306
4307 CHECK_IDX(idx, CTRL_STA);
4308
4309// CHECK_WPA_CTRL(CTRL_STA);
4310
4311 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004312 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004313
you.chen057aac42023-04-13 14:06:58 +08004314 if (count < 0)
4315 {
4316 RLOGE("list network fail");
4317 return count;
4318 }
4319 else if (count == 0)
4320 {
4321 *list = NULL;
4322 *len = 0;
4323 return 0;
4324 }
4325
you.chen35020192022-05-06 11:30:57 +08004326 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004327 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004328 *len = count;
4329
qs.xiong9fbf74e2023-03-28 13:38:22 +08004330 for (index=0; index < count; index++)
4331 {
you.chen35020192022-05-06 11:30:57 +08004332 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004333 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004334 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004335 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004336 {
you.chen35020192022-05-06 11:30:57 +08004337 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4338 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004339 else
you.chen057aac42023-04-13 14:06:58 +08004340 {
you.chen35020192022-05-06 11:30:57 +08004341 (*list)[index].base_info.band = -1;
4342 }
you.chen057aac42023-04-13 14:06:58 +08004343 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004344 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004345 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004346 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004347 return 0;
4348}
4349
4350int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4351{
you.chen0c9bee22023-10-25 13:03:14 +08004352 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004353 {
you.chen0c9bee22023-10-25 13:03:14 +08004354 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4355 g_sta_fake_scan_finish_flag = 1;
4356 return 0;
4357 }
4358 else if (g_sta_conncet_status_flag != 0)
4359 {
4360 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004361 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004362 }
you.chen0c9bee22023-10-25 13:03:14 +08004363
qs.xiongc8d92a62023-03-29 17:36:14 +08004364 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004365 const char *lynq_scan_cmd = "SCAN";
4366
4367 CHECK_IDX(idx, CTRL_STA);
4368
4369 CHECK_WPA_CTRL(CTRL_STA);
4370
you.chen0df3e7e2023-05-10 15:56:26 +08004371 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4372 {
4373 RLOGD("tmp clear scanlist");
4374 system(clean_last_re);
4375 }
you.chen9ac66392022-08-06 17:01:16 +08004376 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004377 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004378 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4379 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004380 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004381 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4382 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004383 g_sta_scan_finish_flag = 1;
4384 return -1;
4385 }
you.chen35020192022-05-06 11:30:57 +08004386
4387 return 0;
4388}
4389
4390int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004391 if (cb == NULL)
4392 {
4393 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004394 return -1;
4395 }
4396
you.chen6d247052023-06-01 16:39:54 +08004397 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004398 g_ap_callback_priv = priv;
4399 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004400 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004401
you.chen6d247052023-06-01 16:39:54 +08004402 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004403 if (g_ap_watcher_pid == 0 )
4404 {
4405 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4406 {
4407 g_ap_watcher_pid = 0;
4408 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4409 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4410 return -1;
4411 }
4412 }
4413
4414 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004415 RLOGD("creat APWatcherTheradProc succeed");
you.chene9d00032023-04-24 13:55:29 +08004416
you.chen35020192022-05-06 11:30:57 +08004417 return 0;
4418}
4419
4420int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004421 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004422 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004423 if (g_ap_callback_priv == priv)
4424 {
you.chen35020192022-05-06 11:30:57 +08004425 g_ap_callback_func = NULL;
4426 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004427 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004428 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004429 return 0;
4430 }
you.chen6d247052023-06-01 16:39:54 +08004431 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004432 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004433 return -1;
4434}
4435
4436int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004437 if (cb == NULL)
4438 {
4439 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004440 return -1;
4441 }
4442
you.chen6d247052023-06-01 16:39:54 +08004443 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004444 g_sta_callback_priv = priv;
4445 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004446 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004447
you.chen6d247052023-06-01 16:39:54 +08004448 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004449 if (g_sta_watcher_pid == 0 ) {
4450 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4451 {
4452 g_sta_watcher_pid = 0;
4453 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4454 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4455 return -1;
4456 }
4457 }
4458
4459 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004460 RLOGD("creat STAWatcherTheradProc succeed");
you.chen35020192022-05-06 11:30:57 +08004461 return 0;
4462}
4463
4464int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004465 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004466 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004467 if (g_sta_callback_priv == priv)
4468 {
you.chen35020192022-05-06 11:30:57 +08004469 g_sta_callback_func = NULL;
4470 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004471 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004472 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004473 return 0;
4474 }
you.chen6d247052023-06-01 16:39:54 +08004475 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004476 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004477 return -1;
4478}
4479
qs.xiongfcc914b2023-07-06 21:16:20 +08004480int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4481 if (cb == NULL)
4482 {
4483 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4484 return -1;
4485 }
4486 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4487 g_sta_auto_callback_priv = priv;
4488 g_sta_auto_callback_func = cb;
4489 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4490 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4491 if (g_sta_auto_watcher_pid == 0 ) {
4492 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4493 {
4494 g_sta_auto_watcher_pid = 0;
4495 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4496 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4497 return -1;
4498 }
4499 }
4500 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004501 RLOGD("creat STAWatcherTheradProc succeed");
qs.xiongfcc914b2023-07-06 21:16:20 +08004502 return 0;
4503}
4504int lynq_unreg_sta_auto_event_callback(void * priv) {
4505 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4506 if (g_sta_auto_callback_priv == priv)
4507 {
4508 g_sta_auto_watcher_stop_flag = 1;
4509 if (g_sta_auto_watcher_pid != 0)
4510 {
4511 pthread_join(g_sta_auto_watcher_pid, NULL);
4512 }
4513 g_sta_auto_watcher_pid = 0;
4514 g_sta_auto_callback_func = NULL;
4515 g_sta_auto_callback_priv = NULL;
4516 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4517 return 0;
4518 }
4519 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4520 return -1;
4521}
you.chen35020192022-05-06 11:30:57 +08004522int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4523{
4524 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004525 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004526 CHECK_IDX(idx, CTRL_AP);
4527
qs.xiong9fbf74e2023-03-28 13:38:22 +08004528 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4529 {
you.chen35020192022-05-06 11:30:57 +08004530 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4531 return 0;
4532 }
4533
qs.xiong9fbf74e2023-03-28 13:38:22 +08004534 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4535 {
you.chen35020192022-05-06 11:30:57 +08004536 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4537 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004538 else
4539 {
you.chen35020192022-05-06 11:30:57 +08004540 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4541 }
4542
4543 return 0;
4544}
4545
4546int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4547 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004548 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004549 CHECK_IDX(idx, CTRL_STA);
4550
qs.xiong9fbf74e2023-03-28 13:38:22 +08004551 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4552 {
you.chen35020192022-05-06 11:30:57 +08004553 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4554 return 0;
4555 }
4556
qs.xiong9fbf74e2023-03-28 13:38:22 +08004557 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4558 {
you.chen35020192022-05-06 11:30:57 +08004559 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4560 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004561 else
4562 {
you.chen35020192022-05-06 11:30:57 +08004563 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4564 }
4565
4566 return 0;
4567}
4568
4569int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4570// CHECK_IDX(idx, CTRL_AP);
4571// int ret = 0;
4572// size_t reply_len = MAX_RET;
4573// char cmd_reply[MAX_RET]={0};
4574// const char * cmd_str = "GET country";
4575// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4576// do{
4577// if (NULL == s_lynq_wpa_ctrl) {
4578// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4579// if (NULL == s_lynq_wpa_ctrl ) {
4580// printf("wpa_ctrl_open fail\n");
4581// return -1;
4582// }
4583// }
4584// }while(0);
4585
4586// do {
4587// reply_len = MAX_RET;
4588// cmd_reply[0] = '\0';
4589// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004590// 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 +08004591// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004592// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004593// return ret;
4594// }
4595// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004596// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004597// }while(0);
4598
4599 FILE *fp;
4600 size_t i = 0;
4601 char lynq_cmd_ret[MAX_RET]={0};
4602
4603// CHECK_IDX(idx, CTRL_AP);
4604
4605 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004606 {
4607 perror("popen error!");
4608 return -1;
4609 }
you.chen35020192022-05-06 11:30:57 +08004610 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4611 {
4612 perror("fread fail!");
4613 return -1;
4614 }
4615
qs.xiong9fbf74e2023-03-28 13:38:22 +08004616 for(i=0; i < strlen(lynq_cmd_ret); i++)
4617 {
4618 if (lynq_cmd_ret[i] == ' ')
4619 {
you.chen35020192022-05-06 11:30:57 +08004620 lynq_cmd_ret[i] = '\0';
4621 break;
4622 }
4623 }
4624
4625 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004626 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004627
4628 int ret=pclose(fp);
4629 if(ret==-1)
4630 {
4631 perror("close file faild");
4632 }
4633
4634 return 0;
4635}
4636
qs.xiong44fac672023-08-29 16:15:55 +08004637
you.chen705a7ef2023-06-01 22:06:45 +08004638static int check_and_init_uci_config(char * country_code)
4639{
4640 FILE * fp;
4641 int is_different = 0;
4642 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4643 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4644 const char * commit_uci_cmd ="uci commit";
4645 char set_country_cmd[MAX_CMD];
4646 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004647
you.chen705a7ef2023-06-01 22:06:45 +08004648 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004649
you.chen705a7ef2023-06-01 22:06:45 +08004650 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004651 {
you.chen705a7ef2023-06-01 22:06:45 +08004652 if (0 != system(create_uci_cmd))
4653 {
4654 RLOGE("creat_uci_cmd fail");
4655 return -1;
4656 }
4657 is_different = 1;
4658 }
4659
4660 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4661 {
4662 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004663 return -1;
4664 }
4665
you.chen705a7ef2023-06-01 22:06:45 +08004666 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004667 {
you.chen705a7ef2023-06-01 22:06:45 +08004668 RLOGE("fread fail!");
4669 fclose(fp);
4670 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004671 }
4672
you.chen705a7ef2023-06-01 22:06:45 +08004673 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4674 {
qs.xiong44fac672023-08-29 16:15:55 +08004675 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 +08004676 is_different = 1;
4677 }
4678
4679 fclose(fp);
4680
4681 if (is_different)
4682 {
4683 if ( 0 != system(set_country_cmd))
4684 {
4685 RLOGE("set_country_cmd fail");
4686 return -1;
4687 }
4688 if (0 != system(commit_uci_cmd))
4689 {
4690 RLOGE("commmit fail");
4691 }
4692 }
4693
4694 return is_different;
4695}
4696
4697int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4698 char check_current_code[10];
4699 const char * support_country[] = {"CN", "EU"};
4700
4701 int ret,is_different, i, cc_count;
4702
4703 if (country_code == NULL || country_code[0] == '\0')
4704 {
4705 RLOGE("bad country code\n");
4706 return -1;
4707 }
4708
4709 cc_count = sizeof (support_country) / sizeof (char*);
4710 for(i=0; i < cc_count; i++)
4711 {
4712 if (strcmp(support_country[i], country_code) == 0)
4713 {
4714 break;
4715 }
4716 }
4717
4718 if (i >= cc_count)
4719 {
4720 RLOGE("unspported country code %s\n", country_code);
4721 return -1;
4722 }
4723
4724 is_different = check_and_init_uci_config(country_code);
4725 if( is_different < 0 )
4726 {
4727 RLOGE("init set uci fail\n");
4728 return -1;
4729 }
4730
4731 ret = lynq_get_country_code(idx,check_current_code);
4732 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4733 {
4734 ret = lynq_wifi_disable();
4735 if(ret != 0 )
4736 {
4737 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4738 return -1;
4739 }
4740 }
4741
4742 return 0;
you.chen35020192022-05-06 11:30:57 +08004743}
4744
4745int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4746{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004747 RLOGD("enter lynq_get_connect_ap_mac\n");
4748 if (mac == NULL)
4749 {
4750 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004751 return -1;
4752 }
4753
4754 CHECK_IDX(idx, CTRL_STA);
4755 ap_info_s ap;
4756 ap.ap_mac[0] = '\0';
4757
qs.xiong9fbf74e2023-03-28 13:38:22 +08004758 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4759 {
you.chen35020192022-05-06 11:30:57 +08004760 return -1;
4761 }
4762 strcpy(mac, ap.ap_mac);
4763
4764 return 0;
4765}
4766
4767int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4768{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004769 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004770 struct ifaddrs *ifaddr_header, *ifaddr;
4771 struct in_addr * ifa;
4772 const char * ifaName = "wlan0";
4773 if (ip == NULL)
4774 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004775 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004776 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004777 }
you.chenf58b3c92022-06-21 16:53:48 +08004778
qs.xiong9fbf74e2023-03-28 13:38:22 +08004779 if (idx == 1)
4780 {
you.chen0df3e7e2023-05-10 15:56:26 +08004781 ifaName = inner_get_ap_interface_name();
4782 if (ifaName == NULL)
4783 {
4784 RLOGE("[lynq_get_interface_ip] ap name get fail");
4785 return -1;
4786 }
you.chen9ac66392022-08-06 17:01:16 +08004787 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004788 else if (idx != 0)
4789 {
you.chen35020192022-05-06 11:30:57 +08004790 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004791 }
you.chen35020192022-05-06 11:30:57 +08004792
you.chen9ac66392022-08-06 17:01:16 +08004793 if (getifaddrs(&ifaddr_header) == -1)
4794 {
you.chen35020192022-05-06 11:30:57 +08004795 perror("getifaddrs");
4796 return -1;
4797 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004798 }
you.chen35020192022-05-06 11:30:57 +08004799
4800
you.chen9ac66392022-08-06 17:01:16 +08004801 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4802 {
4803 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004804 continue;
you.chen9ac66392022-08-06 17:01:16 +08004805 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4806 {
4807 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4808 {
4809 // is a valid IP4 Address
4810 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4811 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004812 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004813 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004814 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004815 return 0;
4816 }
4817 }
4818 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004819 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004820 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004821 return -1;
you.chen35020192022-05-06 11:30:57 +08004822}
4823
4824int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4825{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004826 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004827 int count;
4828 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004829 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004830 char *split_words[128] = {0};
4831 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4832
4833 CHECK_WPA_CTRL(idx);
4834
4835 DO_REQUEST(lynq_get_mac_cmd);
4836
qs.xiong9fbf74e2023-03-28 13:38:22 +08004837 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4838 {
4839 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004840 return -1;
4841 }
4842
4843 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4844
qs.xiong9fbf74e2023-03-28 13:38:22 +08004845 if (count < 2)
4846 {
you.chen35020192022-05-06 11:30:57 +08004847 return -1;
4848 }
4849
qs.xiong9fbf74e2023-03-28 13:38:22 +08004850 for (i=0; i < strlen(split_words[1]); i++ )
4851 {
4852 if (split_words[1][i] != ' ')
4853 {
you.chen35020192022-05-06 11:30:57 +08004854 break;
4855 }
4856 }
4857
qs.xiongdd6e44c2023-08-08 15:02:53 +08004858 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004859
4860 return 0;
4861}
4862
4863int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4864{
4865// int count;
4866// char *split_words[128] = {0};
4867// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4868
4869// if (rssi == NULL) {
4870// return -1;
4871// }
4872
4873// CHECK_IDX(idx, CTRL_STA);
4874
4875// CHECK_WPA_CTRL(CTRL_STA);
4876
4877// DO_REQUEST(lynq_get_rssi_cmd);
4878
4879// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4880// return -1;
4881// }
4882
4883// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4884
4885// if (count < 2) {
4886// return -1;
4887// }
4888
4889// *rssi = atoi(split_words[1]) * -1;
4890
you.chen35020192022-05-06 11:30:57 +08004891 char lynq_cmd_ret[MAX_RET]={0};
4892
qs.xiongff0ae0f2022-10-11 15:47:14 +08004893/*******change other cmd to get rssi*******
4894 *
4895 *wl rssi ---> wl -i wlan0 rssi
4896 *
4897 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004898 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004899 {
you.chen23c4a5f2023-04-12 16:46:00 +08004900 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004901 return -1;
4902 }
you.chen9f17e4d2022-06-06 17:18:18 +08004903 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004904/****** if got rssi is 0,means sta didn't connected any device****/
4905 if(*rssi == 0)
4906 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004907 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004908 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004909 }
you.chen35020192022-05-06 11:30:57 +08004910
4911 return 0;
4912}
4913
4914int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4915{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004916 RLOGD("enter lynq_get_connect_ap_band\n");
4917 if (band == NULL)
4918 {
you.chen35020192022-05-06 11:30:57 +08004919 return -1;
4920 }
4921
4922 CHECK_IDX(idx, CTRL_STA);
4923 ap_info_s ap;
4924 ap.band = -1;
4925
qs.xiong9fbf74e2023-03-28 13:38:22 +08004926 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4927 {
you.chen35020192022-05-06 11:30:57 +08004928 return -1;
4929 }
4930 *band = ap.band;
4931
4932 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004933}
you.chenf58b3c92022-06-21 16:53:48 +08004934
4935int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4936{
you.chenb95401e2023-05-12 19:39:06 +08004937 int ret;
4938 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004939 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004940
4941 if (ip == NULL)
4942 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004943 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004944 return -1;
4945 }
4946
4947 CHECK_IDX(idx, CTRL_STA);
4948
qs.xionge4cbf1c2023-02-28 18:22:49 +08004949 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004950 {
4951 return -1;
4952 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004953
you.chenb95401e2023-05-12 19:39:06 +08004954 ip[0] = '\0';
4955 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4956 if (ret != 0)
4957 {
4958 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
you.chenb95401e2023-05-12 19:39:06 +08004959 }
4960
4961 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4962 {
4963 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004964 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004965 if (ret != 0)
4966 {
4967 ip[0] = '\0';
4968 return 0;
4969 }
4970 else
4971 {
4972 p = strchr(ip, '\n');
4973 if (p != NULL)
4974 {
4975 *p = '\0';
4976 }
4977 }
4978 }
4979 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004980}
4981
qs.xionge02a5252023-09-20 14:00:21 +08004982int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4983{
4984 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4985 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4986}
4987
qs.xiong026c5c72022-10-17 11:15:45 +08004988int lynq_ap_connect_num(int sta_number)
4989{
4990 char lynq_limit_cmd[32]={0};
4991 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004992 if((sta_number < 1 ) && (sta_number > 15))
4993 {
4994 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004995 return -1;
4996 }
4997 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4998 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004999 if(ret != 0)
5000 {
5001 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08005002 }
5003 return 0;
5004}
you.chenf58b3c92022-06-21 16:53:48 +08005005
qs.xiong77905552022-10-17 11:19:57 +08005006int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
5007{
5008
5009 char lynq_wifi_acs_cmd[128]={0};
5010 char lynq_cmd_mode[128]={0};
qs.xiong77905552022-10-17 11:19:57 +08005011
qs.xiong9fbf74e2023-03-28 13:38:22 +08005012 if((acs_mode != 2) && (acs_mode != 5))
5013 {
qs.xiong77905552022-10-17 11:19:57 +08005014 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
5015 }
5016
qs.xiong9fbf74e2023-03-28 13:38:22 +08005017 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
5018 {
qs.xiong77905552022-10-17 11:19:57 +08005019 return -1;
5020 }
5021
5022 CHECK_IDX(idx, CTRL_AP);
5023
5024 CHECK_WPA_CTRL(CTRL_AP);
5025
5026 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
5027 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
qs.xiong77905552022-10-17 11:19:57 +08005028
5029 DO_OK_FAIL_REQUEST(cmd_disconnect);
5030 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
5031 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
5032 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong77905552022-10-17 11:19:57 +08005033
5034 return 0;
5035}
you.chen0f5c6432022-11-07 18:31:14 +08005036//you.chen add for tv-box start
5037static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
5038 FILE *fp;
5039 //printf("to exec cmd:%s\n", str_cmd);
5040 if((fp=popen(str_cmd,"r"))==NULL)
5041 {
5042 perror("popen error!");
5043 return -1;
5044 }
5045 if((fread(str_cmd_ret,max_len,1,fp))<0)
5046 {
5047 perror("fread fail!");
5048 fclose(fp);
5049 return -1;
5050 }
5051 fclose(fp);
5052 return 0;
5053}
5054
5055static int get_netmask_length(const char* mask)
5056{
5057 int masklen=0, i=0;
5058 int netmask=0;
5059
5060 if(mask == NULL)
5061 {
5062 return 0;
5063 }
5064
5065 struct in_addr ip_addr;
5066 if( inet_aton(mask, &ip_addr) )
5067 {
5068 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08005069 }else
5070 {
you.chen0f5c6432022-11-07 18:31:14 +08005071 netmask = 0;
5072 return 0;
5073 }
5074
5075 while(0 == (netmask & 0x01) && i<32)
5076 {
5077 i++;
5078 netmask = netmask>>1;
5079 }
5080 masklen = 32-i;
5081 return masklen;
5082}
5083
5084static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
5085 int mask_len;
5086 char *p;
5087 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08005088 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5089 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005090 return -1;
5091 p = strstr(str_cmd_ret, "Mask:");
5092 if (p == NULL)
5093 return -1;
5094 mask_len = get_netmask_length(p + 5);
5095 if (mask_len == 0)
5096 return -1;
5097 p = strstr(str_cmd_ret, "inet addr:");
5098 if (p == NULL)
5099 return -1;
5100 strcpy(tmp, p + 10);
5101 p = strstr(tmp, " ");
5102 if (p != NULL)
5103 *p = '\0';
5104 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5105 return 0;
5106}
5107
5108static void GBWWatchThreadProc() {
5109 int i,n, nloop, nmax, ncheckcount, nidlecount;
5110 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5111 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5112 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5113 char *results[16] = {0};
5114 char str_cmd[256] = {0};
5115 char str_cmd_ret[128] = {0};
5116 char dest_ip[32] = {0};
5117 lastAP1Bytes = lastAP2Bytes = 0;
5118 lastAP1Drop = lastAP2Drop = 0;
5119 lastAP1Speed = lastAP2Speed = 0;
5120 setAP1Speed = 50;
5121 setAP2Speed = 80;
5122 nloop = 0;
5123 nmax = 6;
5124 ncheckcount = nidlecount = 0;
5125
you.chen0df3e7e2023-05-10 15:56:26 +08005126 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005127 {
5128 RLOGE("------gbw thread run\n");
5129 return;
5130 }
5131
qs.xiong9fbf74e2023-03-28 13:38:22 +08005132 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005133 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5134 while (dest_ip[0] == '\0') {
5135 sleep(1);
5136 str_cmd_ret[0] = '\0';
5137 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5138 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5139 if (str_cmd_ret[n] == '\n'){
5140 str_cmd_ret[n] = '\0';
5141 break;
5142 }
5143 }
5144 if (str_cmd_ret[0] != '\0')
5145 {
5146 strcpy(dest_ip, str_cmd_ret);
5147 }
5148 }
5149
you.chenc9928582023-04-24 15:39:37 +08005150 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5151 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5152 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 +08005153 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5154 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005155 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005156 return;
5157 }
you.chenc9928582023-04-24 15:39:37 +08005158 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);
5159 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);
5160 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 +08005161
5162 while (1) {
5163 sleep(1);
5164 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005165 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5166 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5167 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005168 continue;
5169 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005170 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005171 if (n > 9) {
5172 if (strcmp(results[1], "Sent") == 0) {
5173 currAP1Bytes = atoll(results[2]);
5174 }
5175 if (strcmp(results[6], "(dropped") == 0) {
5176 currAP1Drop = atoi(results[7]);
5177 }
5178 }
5179
5180 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005181 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5182 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5183 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005184 continue;
5185 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005186 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005187 if (n > 9) {
5188 if (strcmp(results[1], "Sent") == 0) {
5189 currAP2Bytes = atoll(results[2]);
5190 }
5191 if (strcmp(results[6], "(dropped") == 0) {
5192 currAP2Drop = atoi(results[7]);
5193 }
5194 }
5195
5196 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5197 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5198 lastAP1Bytes = currAP1Bytes;
5199 lastAP2Bytes = currAP2Bytes;
5200 continue;
5201 }
5202
5203 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5204 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5205 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5206 lastAP1Speed = currAP1Speed;
5207 lastAP2Speed = currAP2Speed;
5208 lastAP1Bytes = currAP1Bytes;
5209 lastAP2Bytes = currAP2Bytes;
5210
5211 currSetAP1Speed = setAP1Speed;
5212 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5213 ncheckcount++;
5214 if (ncheckcount > 3) {
5215 ncheckcount = 0;
5216 currSetAP1Speed = 5;
5217 }
5218 }
5219 else {
5220 ncheckcount = 0;
5221 if (currAP1Speed < 5)
5222 nidlecount++;
5223 else
5224 nidlecount = 0;
5225
5226 }
5227
5228 if (nidlecount > 60 ){
5229 currSetAP1Speed = 50;
5230 }
5231
5232 if (currSetAP1Speed != setAP1Speed) {
5233 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005234 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5235 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005236 }
5237 }
5238}
5239
5240int enableGBW(const char* mac) {
5241 int i,len;
5242 char get_ipaddr_cmd[128]={0};
5243 ap_info_s *ap;
5244 device_info_s * list;
5245
5246 if (mac == NULL || g_gbw_enabled == 1)
5247 return -1;
5248 len = strlen(mac);
5249 g_gbw_mac = malloc(len + 1);
5250 for(i=0;i<len;i++) {
5251 if (mac[i] >= 'A' && mac[i] <= 'Z')
5252 {
5253 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5254 }
5255 else
5256 g_gbw_mac[i] = mac[i];
5257 }
5258 g_gbw_mac[i] = '\0';
5259 g_gbw_enabled = 1;
5260
5261 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5262 if (system(get_ipaddr_cmd) == 0) {
5263 //startGBW();
5264 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5265 for (i=0;i<len;i++) {
5266 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5267 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5268 startGBW();
5269 }
5270 free(ap);
5271 free(list);
5272 }
5273 }
5274 return 0;
5275}
5276
5277int disableGBW() {
5278 stopGBW();
5279 free(g_gbw_mac);
5280 g_gbw_mac = NULL;
5281 g_gbw_enabled = 1;
5282 return 0;
5283}
5284
5285static int startGBW() {
5286 if (g_gbw_watcher_pid != 0) {
5287 stopGBW();
5288 }
5289 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5290}
5291
5292static int stopGBW() {
5293 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005294 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005295 pthread_cancel(g_gbw_watcher_pid);
5296 pthread_join(g_gbw_watcher_pid, &retval);
5297 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005298 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5299 if (s_ap_iterface_name[0] != '\0')
5300 {
5301 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5302 system(cmd);
5303 }
you.chen0f5c6432022-11-07 18:31:14 +08005304}
5305//you.chen add for tv-box end