blob: fe1465cb3bc38f75ae24de7789472ef098bbfc02 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen0c9bee22023-10-25 13:03:14 +080057volatile int g_sta_fake_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
qs.xiong6ad0e822023-11-24 17:53:30 +080070
you.chen35020192022-05-06 11:30:57 +080071//const char * CTRL_PATH="/var/run/wpa_supplicant";
72const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
73//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
74const char * cmd_list_networks = "LIST_NETWORKS";
75const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080076const char * cmd_disconnect = "DISCONNECT";
77const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080078const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080079const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080080const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080081const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080082
you.chenf711c8a2023-04-13 13:49:45 +080083const char * cmd_ping = "PING";
84const char * rsp_pong = "PONG";
85const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
86const int MAX_IDLE_COUNT = 600; // 60s
87
you.chenc9928582023-04-24 15:39:37 +080088const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
89const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
90const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
91const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
92const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
93
94static char s_ap_iterface_name[64] = {0};
95
you.chend2fef3f2023-02-13 10:50:35 +080096struct local_wpa_ctrl{
97 struct wpa_ctrl *ctrl;
98 pthread_mutex_t mutex;
99};
100
qs.xiong09560402023-10-27 21:58:55 +0800101volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800102int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800103
you.chend2fef3f2023-02-13 10:50:35 +0800104static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800105static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
106static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiong8362e222024-03-22 11:20:56 +0800107static pthread_mutex_t s_run_cmd_func_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xionga8cbe222024-04-15 16:42:13 +0800108static pthread_mutex_t s_func_run_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiong8362e222024-03-22 11:20:56 +0800109
qs.xiongfcc914b2023-07-06 21:16:20 +0800110// add for auto connect
111static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800112
113static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800114
you.chen0f5c6432022-11-07 18:31:14 +0800115//you.chen add for tv-box start
116volatile int g_gbw_enabled = 0;
117char * g_gbw_mac = NULL;
118pthread_t g_gbw_watcher_pid = 0;
119static int startGBW();
120static int stopGBW();
121//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800122
123typedef struct __curr_status_info {
124 ap_info_s *ap;
125 char * state;
126 int net_no;
127}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400128
you.chen70f377f2023-04-14 18:17:09 +0800129typedef enum {
130 INNER_STA_STATUS_INIT = 0,
131 INNER_STA_STATUS_CONNECTING,
132 INNER_STA_STATUS_ASSOCIATING,
133 INNER_STA_STATUS_ASSOCIATED,
134 INNER_STA_STATUS_CONNECTED,
135 INNER_STA_STATUS_DISCONNECTING,
136 INNER_STA_STATUS_DISCONNECTED,
137 INNER_STA_STATUS_CANCEL,
138}inner_sta_status_s;
139
140static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
141static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800142volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800143static error_number_s s_sta_error_number = -1;
144static char s_sta_current_connecting_ssid[64] = {0};
145static struct timespec s_sta_connect_timeout;
146const int MAX_CONNNECT_TIME = 15; // second
147pthread_t g_global_watcher_pid = 0;
148static int s_service_invoke_timeout_cnt=0;
149const int FAKE_MAX_INT_VALUE = 99999;
150
qs.xiong09560402023-10-27 21:58:55 +0800151static void print_disconnect_list()
152{
153 int i;
154 for( i = 0; i < g_history_disconnect_valid_num; i++ )
155 {
156 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__);
157 }
158
159 return;
160}
161
162// idex ----> history_disconnect_list[x] index
163static int removeElement(int idex)
164{
165 RLOGD("into removeElement");
166 if( index < 0 )
167 {
168 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
169 return -1;
170 }
171 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]);
172 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
173 g_history_disconnect_valid_num --;
174 RLOGD("end removeElement");
175 return 0;
176}
177static int check_history_disconenct_ap_list(int val)
178{
179 print_disconnect_list();
180 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__);
181 int i;
182 for( i = 0; i < g_history_disconnect_valid_num; i++)
183 {
184 if( val == g_history_disconnect_net[i] )
185 {
186 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
187 RLOGD("end check_history_disconenct_ap_list && return network index");
188 return i;
189 }
190 }
191 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__);
192 return -1;
193}
194
195
196static void lynq_sta_removeElement(int net_no)
197{
198 int ret;
199
200 ret = check_history_disconenct_ap_list(net_no);
201 if( ret == -1 )
202 {
203 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
204 return;
205 }else
206 {
207 ret = removeElement(ret);
208 if( ret == 0 )
209 {
210 RLOGD("removeElement pass %s %d",__func__,__LINE__);
211 return;
212 }
213 }
214
215 return;
216}
217
you.chen70f377f2023-04-14 18:17:09 +0800218static void notify_service_invoke_fail(int error)
219{
qs.xiong17579bb2024-03-11 19:08:06 +0800220 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen70f377f2023-04-14 18:17:09 +0800221 pthread_mutex_lock(&s_global_check_mutex);
222 if (error == -2) //timeout
223 {
224 s_service_invoke_timeout_cnt++;
225 if (s_service_invoke_timeout_cnt > 10)
226 {
227 pthread_cond_signal(&s_global_check_cond);
228 }
229 }
230 else if (error == -1)
231 {
232 // check if can connect wpa service
233 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
234 if (lynq_wpa_ctrl == NULL)
235 {
236 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
237 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800238 }else
239 {
240 wpa_ctrl_close(lynq_wpa_ctrl);
241 }
you.chen70f377f2023-04-14 18:17:09 +0800242 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
243 if (lynq_wpa_ctrl == NULL)
244 {
245 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
246 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800247 }else
248 {
249 wpa_ctrl_close(lynq_wpa_ctrl);
you.chen70f377f2023-04-14 18:17:09 +0800250 }
you.chen70f377f2023-04-14 18:17:09 +0800251 }
252
253 pthread_mutex_unlock(&s_global_check_mutex);
254}
255
you.chenc9928582023-04-24 15:39:37 +0800256static int system_call_v(const char * fmt, ...)
257{
258 char str_cmd[256] = {0};
259 va_list args;
260 va_start(args, fmt);
261 vsprintf(str_cmd, fmt, args);
262 va_end(args);
263 printf("system call----------%s\n", str_cmd);
264 return system(str_cmd);
265}
266
you.chen0df3e7e2023-05-10 15:56:26 +0800267static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
268
269static const char * inner_get_ap_interface_name()
270{
271 char * p;
272 char cmd[128]={0};
273
274 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
275 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
276 {
277 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
278 return NULL;
279 }
280 p = strchr(s_ap_iterface_name, ' ');
281 if (NULL != p)
282 {
283 *p = '\0';
284 }
285 p = strchr(s_ap_iterface_name, '\n');
286 if (NULL != p)
287 {
288 *p = '\0';
289 }
290 if (s_ap_iterface_name[0] == '\0')
291 {
292 return NULL;
293 }
294
295 return s_ap_iterface_name;
296}
297
you.chen70f377f2023-04-14 18:17:09 +0800298static void check_tether_and_notify()
299{
300 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800301 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 +0800302 {
303 return;
304 }
305 pthread_mutex_lock(&s_global_check_mutex);
306 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
307 pthread_cond_signal(&s_global_check_cond);
308 pthread_mutex_unlock(&s_global_check_mutex);
309}
310
you.chend2fef3f2023-02-13 10:50:35 +0800311static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
312 char *reply, size_t *reply_len,
313 void (*msg_cb)(char *msg, size_t len))
314{
315 int ret;
316 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800317 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800318 return -1;
319 }
320 pthread_mutex_lock(&ctrl->mutex);
321 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
322 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800323 if (ret != 0)
324 {
325 notify_service_invoke_fail(ret);
326 }
you.chend2fef3f2023-02-13 10:50:35 +0800327 return ret;
328}
329
330static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
331 int repeat_cnt;
332 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
333 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800334 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800335 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
336 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
337// printf("wait enable finish\n");
338 usleep(500 * 1000);
339 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
340 }
341 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800342 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800343 goto out_addr;
344 }
345 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
346 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
347 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800348 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800349 goto out_addr;
350 }
351 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
352 }
353 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
354out_addr:
355 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
356 return lynq_wpa_ctrl;
357}
358
qs.xiong97fa59b2022-04-07 05:41:29 -0400359#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400360{\
you.chen35020192022-05-06 11:30:57 +0800361 perror((str));\
362 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400363}
364
you.chen35020192022-05-06 11:30:57 +0800365#define CHECK_IDX(idx, type) do { \
366 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
367 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800368 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800369 return -1; \
370 } \
371 }while (0)
372
373#define CHECK_WPA_CTRL(index) int ret = 0;\
374 size_t reply_len = MAX_RET; \
375 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800376 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800377 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800378 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
379 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800380 }while(0)
381
382#define DO_REQUEST(cmd_str) do { \
383 reply_len = MAX_RET;\
384 cmd_reply[0] = '\0'; \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800385 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800386 { \
qs.xiong86cced62024-09-23 14:08:38 +0800387 RLOGI("to call [%s]\n", cmd_str); \
qs.xiongb04dc852024-03-27 21:55:32 +0800388 }else \
389 { \
qs.xiong86cced62024-09-23 14:08:38 +0800390 RLOGI("to call SET_NETWORK psk ********\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800391 } \
you.chend2fef3f2023-02-13 10:50:35 +0800392 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 +0800393 if (ret != 0) { \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800394 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800395 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800396 RLOGE("call "#cmd_str" fail %d\n", ret); \
qs.xiongb04dc852024-03-27 21:55:32 +0800397 }else \
398 { \
399 RLOGE("call get or set psk fail %d\n",ret); \
400 } \
you.chen35020192022-05-06 11:30:57 +0800401 return ret; \
402 } \
403 cmd_reply[reply_len+1] = '\0'; \
qs.xiong86cced62024-09-23 14:08:38 +0800404 RLOGI("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800405 }while(0)
406
407#define DO_OK_FAIL_REQUEST(cmd_str) do { \
qs.xiong8362e222024-03-22 11:20:56 +0800408 pthread_mutex_lock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800409 DO_REQUEST(cmd_str); \
410 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiongf1ca0be2024-04-10 16:51:47 +0800411 if( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800412 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800413 RLOGE("cmd "#cmd_str" return FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800414 }else \
415 { \
416 RLOGE("cmd SET_NETWORK psk ******** return FAIL\n"); \
417 } \
qs.xiong8362e222024-03-22 11:20:56 +0800418 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800419 return -1; \
420 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiongf1ca0be2024-04-10 16:51:47 +0800421 if ( strstr(cmd_str,"psk") == NULL && strstr(cmd_str,"sae_password") == NULL ) \
qs.xiongb04dc852024-03-27 21:55:32 +0800422 { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800423 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
qs.xiongb04dc852024-03-27 21:55:32 +0800424 }else \
425 { \
426 RLOGE("cmd get or set psk return not OK|FAIL\n"); \
427 } \
qs.xiong8362e222024-03-22 11:20:56 +0800428 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800429 return -1; \
430 } \
qs.xiong8362e222024-03-22 11:20:56 +0800431 pthread_mutex_unlock(&s_run_cmd_func_mutex); \
you.chen35020192022-05-06 11:30:57 +0800432 }while (0)
433
434
you.chenf711c8a2023-04-13 13:49:45 +0800435static int check_connection(struct wpa_ctrl * wpa_ctrl)
436{
437 size_t reply_len = MAX_RET;
438 char cmd_reply[MAX_RET]={0};
439 int ret;
440
qs.xiong86cced62024-09-23 14:08:38 +0800441 RLOGI("check_connection [%p]", wpa_ctrl);
you.chenf711c8a2023-04-13 13:49:45 +0800442 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
443
444 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
445 {
446 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 +0800447 if (ret != 0)
448 {
449 notify_service_invoke_fail(ret);
450 }
you.chenf711c8a2023-04-13 13:49:45 +0800451 return -1;
452 }
453
454 return 0;
455}
456
457/**
458 * @brief check_pending_msg
459 * @param lynq_wpa_ctrl
460 * @return 1 has msg, 0 no msg, -1 error
461 */
462static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
463{
464 int ret;
465
466 if (*pp_lynq_wpa_ctrl == NULL) // need connect
467 {
468 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
469 if (*pp_lynq_wpa_ctrl == NULL)
470 {
471 usleep(SLEEP_TIME_ON_IDLE);
472 return -1;
473 }
474
475 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
476 if (ret == 0) // attach success
477 {
478 *started_flag = 1;
479 }
480 else
481 {
you.chen70f377f2023-04-14 18:17:09 +0800482 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800483 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
484 *pp_lynq_wpa_ctrl = NULL;
485 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800486 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800487 usleep(SLEEP_TIME_ON_IDLE);
488 return -1;
489 }
490 }
491
492 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
493 if ( ret == 0) // no pending messages
494 {
495 usleep(SLEEP_TIME_ON_IDLE);
496 *idle_count += 1;
497 if (*idle_count > MAX_IDLE_COUNT)
498 {
499 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
500 {
501 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
502 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
503 *pp_lynq_wpa_ctrl = NULL;
504 *idle_count = 0;
505 return -1;
506 }
507 *idle_count = 0;
508 }
509 return 0;
510 }
511 else if ( ret == -1) // on error
512 {
513 RLOGE("[wifi error]sta wpa_ctrl_pending");
514 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
515 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
516 *pp_lynq_wpa_ctrl = NULL;
517 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800518 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800519 return -1;
520 }
521
522 *idle_count = 0;
523 return 1;
524}
525
you.chen6d247052023-06-01 16:39:54 +0800526static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
527{
528 pthread_mutex_lock(&s_ap_callback_mutex);
529 if (g_ap_callback_func != NULL)
530 g_ap_callback_func(g_ap_callback_priv, status);
531 pthread_mutex_unlock(&s_ap_callback_mutex);
532}
533
you.chen35020192022-05-06 11:30:57 +0800534static void APWatcherThreadProc() {
535 size_t len = MAX_RET;
536 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800537 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800538 char *ptr = NULL;
539 char mac[32] = {0};
540 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800541
you.chen6c2dd9c2022-05-16 17:55:28 +0800542 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800543 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800544
qs.xiong9fbf74e2023-03-28 13:38:22 +0800545 while (g_ap_watcher_stop_flag == 0)
546 {
you.chenf711c8a2023-04-13 13:49:45 +0800547 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
548 {
you.chen70f377f2023-04-14 18:17:09 +0800549 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
550 {
551 check_tether_and_notify();
552 }
553
you.chen35020192022-05-06 11:30:57 +0800554 continue;
555 }
you.chenf711c8a2023-04-13 13:49:45 +0800556
you.chen6c2dd9c2022-05-16 17:55:28 +0800557 memset(msg_notify, 0, MAX_RET);
558 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800559 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800560 {
you.chen35020192022-05-06 11:30:57 +0800561 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800562 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800563 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800564 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800565 {
you.chen6d247052023-06-01 16:39:54 +0800566 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800567 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
568 if( ptr != NULL)
569 {
570 ptr += strlen("AP-STA-DISCONNECTED ");
571 memcpy(mac,ptr,17);
572 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
573 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
574 system(cmd);
575 }
576
qs.xiong9fbf74e2023-03-28 13:38:22 +0800577 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800578 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800579 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
580 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800581 {
you.chen0f5c6432022-11-07 18:31:14 +0800582 stopGBW();
583 }
584 }
you.chen35020192022-05-06 11:30:57 +0800585 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800586 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800587 {
you.chen6d247052023-06-01 16:39:54 +0800588 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800589 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800590 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800591 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
592 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800593 {
you.chen0f5c6432022-11-07 18:31:14 +0800594 startGBW();
595 }
596 }
you.chen35020192022-05-06 11:30:57 +0800597 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800598 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800599 {
600 RLOGD("APWatcherThreadProc ap------> service error");
601 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
602 }
603 else
604 {
605 RLOGD("APWatcherThreadProc ap------> going on check next msg");
606 }
you.chenf711c8a2023-04-13 13:49:45 +0800607 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800608 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
609 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800610 if (lynq_wpa_ctrl != NULL)
611 {
you.chen92fd5d32022-05-25 10:09:47 +0800612 wpa_ctrl_detach(lynq_wpa_ctrl);
613 wpa_ctrl_close(lynq_wpa_ctrl);
614 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400615}
616
you.chen70f377f2023-04-14 18:17:09 +0800617static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
618{
619 char * p;
620 const char * try_associat_flag = "Trying to associate";
621 const char * associated_flag = "Associated with ";
622
623 pthread_mutex_lock(&s_global_check_mutex);
624 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
625 {
626 pthread_mutex_unlock(&s_global_check_mutex);
627 return;
628 }
629
you.chen6e724162023-10-19 19:10:01 +0800630 // youchen@2023-10-17 add for "not notify connect fail directly" begin
631 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
632 {
633 s_sta_error_number = error_num;
634 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
635 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
636 pthread_cond_signal(&s_global_check_cond);
637 pthread_mutex_unlock(&s_global_check_mutex);
638 return;
639 }
640 // youchen@2023-10-17 add for "not notify connect fail directly" end
641
you.chen70f377f2023-04-14 18:17:09 +0800642 if (state == LYNQ_WIFI_STATUS_EGNORE)
643 {
644 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
645 {
646 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
647 }
648 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
649 {
650 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
651 }
652 }
653 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
654 {
655 s_sta_error_number = error_num;
656 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
657 {
658 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800659 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 +0800660 pthread_cond_signal(&s_global_check_cond);
661 }
662 }
663 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
664 {
665 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800666 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 +0800667 pthread_cond_signal(&s_global_check_cond);
668 }
669 pthread_mutex_unlock(&s_global_check_mutex);
670}
671
qs.xiongb37f8c42023-09-13 21:21:58 +0800672static int lynq_split(char * str, int len, char delimiter, char * results[]);
673static inline char inner_convert_char(char in);
674static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
675static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
676
677
678
qs.xiong44fac672023-08-29 16:15:55 +0800679/*
680just tmp add for fix sta connect ap fail check ap connect info
681return 0 --->Current no sta device connect this AP
682*/
683static int lynq_connected_ap_sta_status() {
684
685 FILE *fp;
686 size_t i = 0;
687 int ret;
688 char lynq_cmd_ret[MAX_RET]={0};
689
690 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
691 {
692 perror("popen error!");
693 return -1;
694 }
695 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
696 {
697 perror("fread fail!");
698 ret=pclose(fp);
699 if(ret == -1)
700 perror("close file faild");
701 return -1;
702 }
703 if( strlen(lynq_cmd_ret) < MAC_LEN)
704 {
705 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
706 ret=pclose(fp);
707 if(ret==-1)
708 {
709 perror("close file faild");
710 }
711 return 0;
712 }else{
713 ret=pclose(fp);
714 if(ret==-1)
715 {
716 perror("close file faild");
717 }
718 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
719 return 1;
720 }
721}
722
723/*
724 just tmp add for fix sta connect ap fail; check fw status
725 return 1 ----> fw status error; need wl down/up
726*/
727static int check_current_fw_status() {
728
729 FILE *fp;
730 FILE *fp1;
731 size_t i = 0;
732 int ret;
733 char lynq_cmd_ret_2g[MAX_RET]={0};
734 char lynq_cmd_ret_5g[MAX_RET]={0};
735
736 const char * fw_status = "0x0096"; //0x0096 is normal fw status
737
738 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
739 {
740 perror("popen error!");
741 return -1;
742 }
743 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
744 {
745 perror("fread fail!");
746 if(pclose(fp) == -1)
747 perror("close fp file faild");
748 return -1;
749 }
750
751 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
752 {
753 perror("popen error!");
754 if(pclose(fp) == -1)
755 perror("clsoe fp file faild");
756 return -1;
757 }
758 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
759 {
760 perror("fread fail!");
761 if(pclose(fp1) == -1)
762 perror("clsoe fp1 file faild");
763 if(pclose(fp) == -1)
764 perror("clsoe fp file faild");
765 return -1;
766 }
767
768 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
769 {
770 ret=pclose(fp);
771 if(ret==-1)
772 {
773 perror("close fp file faild");
774 }
775 ret=pclose(fp1);
776 if(ret==-1)
777 {
778 perror("close fp1 file faild");
779 }
780 return 0;
781 }else
782 {
783 ret=pclose(fp);
784 if(ret==-1)
785 {
786 perror("close file faild");
787 }
788 if(pclose(fp1) == -1)
789 {
790 perror("clsoe file fp1 faild");
791 }
792 RLOGD("current fw status --error--");
793 return 1;
794 }
795}
796
qs.xiong1d4263a2023-09-06 10:46:23 +0800797/*
798eg: wl counters info
799sh-3.2# wl counters
800counters_version 30
801datalen 1648
802Slice_index: 0
803reinitreason_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)
804reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
805
806check reinit status
807return 0 ===> fw did wl reinit cmd
808*/
809static int check_current_reinit_info()
810{
811 FILE *fp;
812 int ret;
813 char lynq_cmd_ret[MAX_RET]={0};
814 char * dest;
815 char destid[3]={0};
816 if((fp=popen("wl counters","r"))==NULL)
817 {
818 perror("popen error!");
819 return -1;
820 }
821 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
822 {
823 perror("fread fail!");
824 if(pclose(fp) == -1)
825 {
826 perror("close fp file faild");
827 }
828 return -1;
829 }
830 dest = strstr(lynq_cmd_ret,"reinit ");
831 if(dest != NULL)
832 {
833 dest +=strlen("reinit ");
834 RLOGD("current get dest str is %s",dest);
835 memcpy(destid,dest,2);
836 ret = atoi(destid);
837 RLOGD("get current wl counters cmd return counts is %d",ret);
838 if( ret != 0 )
839 {
840 RLOGD("current fw did run cmd wl reinit");
841 if( pclose(fp) == -1 )
842 {
843 perror("close fp file faild");
844 }
845 return 0;
846 }
847 }
848 if( pclose(fp) == -1 )
849 {
850 perror("close fp file faild");
851 }
852 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
853 return -1;
854}
855
qs.xiong44fac672023-08-29 16:15:55 +0800856static void APTmpWatcherThreadProc() {
857
858 int i = 0;
859 int delytime = 300;
860 g_ap_tmp_watcher_stop_flag = 0;
861
qs.xiong86cced62024-09-23 14:08:38 +0800862 RLOGI("APTmpWatcherThreadProc ----> ThreadProc start");
qs.xiong44fac672023-08-29 16:15:55 +0800863 while(1)
864 {
865 sleep(1);
866 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800867 if ( (i % 30) == 0 )
868 {
869 if ( check_current_reinit_info() == 0 )
870 {
871 system("wl reset_cnts");
872 system("wl down");
873 system("wl up");
874 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
875 }
876 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800877 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800878 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800879 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800880 {
881 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
882 {
883 system("wl down");
884 system("wl up");
885 }
qs.xiong44fac672023-08-29 16:15:55 +0800886 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800887 }
888 if ( i == delytime )
889 {
qs.xiong44fac672023-08-29 16:15:55 +0800890 i = 0;
891 }
892 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
893 {
894 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
895 return;
896 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800897
qs.xiong44fac672023-08-29 16:15:55 +0800898 }
899
900}
901
qs.xiongf0128b12023-06-29 17:29:39 +0800902static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
903{
904 char LYNQ_DISABLE_CMD[128]={0};
905
906 CHECK_IDX(idx, CTRL_STA);
907 CHECK_WPA_CTRL(CTRL_STA);
908
909 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800910 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800911 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800912
qs.xiongf0128b12023-06-29 17:29:39 +0800913 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800914
qs.xiongf0128b12023-06-29 17:29:39 +0800915}
916
qs.xiongb37f8c42023-09-13 21:21:58 +0800917static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
918{
919 char LYNQ_ENABLE_CMD[128]={0};
920
921 CHECK_IDX(idx, CTRL_STA);
922 CHECK_WPA_CTRL(CTRL_STA);
923
924
925 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800926 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800927 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
928
929 return 0;
930
931}
932
933static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
934{
935
936 int index,networkid;
937
938 for ( index = 0; index < len ;index++)
939 {
940 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800941 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800942 {
943 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
944 }
945 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
946 }
947 return 0;
948
949}
950
951
952/*
953 dis_net_list user disconnect list
954*/
955static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
956{
qs.xiong09560402023-10-27 21:58:55 +0800957 RLOGD("enter %s %d",__func__,__LINE__);
958 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800959 int count,ncount,index;
960 int flag = 0;
961 int merge_index = 0;
962 int net_no_list[128];
963
qs.xiong09560402023-10-27 21:58:55 +0800964 for(ncount = 0;ncount < valid_num; ncount++ )
965 {
966 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
967 }
968
qs.xiongb37f8c42023-09-13 21:21:58 +0800969 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
970 for( count = 0; count < index; count++)
971 {
972 for(ncount = 0; ncount < valid_num; ncount++)
973 {
qs.xiong09560402023-10-27 21:58:55 +0800974 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800975 if(net_no_list[count] == dis_net_list[ncount])
976 {
qs.xiong09560402023-10-27 21:58:55 +0800977 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800978 flag = 1;
979 break;
980 }
981 }
982 if( flag != 1 )
983 {
984 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800985 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 +0800986 merge_index ++;
987 }
988 flag = 0;
989 }
990 * outlen =merge_index;
991 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
992 return;
993}
qs.xiongf0128b12023-06-29 17:29:39 +0800994
qs.xiong455c30b2023-04-12 11:40:02 +0800995void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
996{
997 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800998 char *wpanetid;
999 char destid[3] = {0};
1000 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +08001001 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1002 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1003 {
1004 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1005 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
1006 return;
1007 }
1008
qs.xiong20202422023-09-06 18:01:18 +08001009 if (strstr(modify, "Trying to associate") != NULL)
1010 {
1011 RLOGD("Current sta is Trying to associate");
1012 *state = LYNQ_WIFI_STATUS_EGNORE;
1013 g_sta_conncet_status_flag = 1;
1014 return;
1015 }
1016
qs.xiong455c30b2023-04-12 11:40:02 +08001017 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1018 {
1019 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +08001020 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001021 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001022 return;
1023 }
1024
qs.xiong1e81dfa2023-09-27 15:52:37 +08001025 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
1026 {
1027 *error = LYNQ_PSW_ERROR;
1028 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1029 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
1030 g_sta_conncet_status_flag = 0;
1031 return;
1032 }
qs.xiong455c30b2023-04-12 11:40:02 +08001033 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1034 {
qs.xiongf0128b12023-06-29 17:29:39 +08001035 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1036 wpanetid = strstr(modify,"id=");
1037 if ( wpanetid != NULL )
1038 {
1039 wpanetid +=strlen("id=");
1040 memcpy(destid,wpanetid,2);
1041 tmpdisid = atoi(destid);
1042
1043 }
qs.xiong455c30b2023-04-12 11:40:02 +08001044 pReason = strstr(modify, "reason=");
1045 if (pReason != NULL)
1046 {
1047 pReason += strlen("reason=");
1048 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1049 {
1050 *error = LYNQ_TIME_OUT;
1051 }
1052 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1053 {
1054 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001055 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001056 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001057 // you.chen@2023-10-17 only disable network during autoconnect
1058 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 +08001059 {
1060 RLOGE("stop wlan0 network %d fail",tmpdisid);
1061 }
qs.xiong455c30b2023-04-12 11:40:02 +08001062 }
1063 else
1064 {
1065 *error = LYNQ_UNSPECIFIED_REASON;
1066 }
qs.xiong455c30b2023-04-12 11:40:02 +08001067 }
1068 else
1069 {
1070 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001071 }
qs.xiongf0128b12023-06-29 17:29:39 +08001072 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001073 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001074 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001075
1076 }
1077
1078 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1079 {
1080 *error = LYNQ_NOT_FIND_AP;
1081 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1082 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001083 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001084 return;
1085 }
1086
1087
1088 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1089 {
1090 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1091 pReason = strstr(modify, "status_code=");
1092 if (pReason != NULL)
1093 {
1094 pReason += strlen("status_code=");
1095 if (memcmp(pReason, "17", 2) == 0)
1096 {
1097 *error = LYNQ_AP_UNABLE_HANDLE;
1098 }
1099 else if (memcmp(pReason, "1",1) == 0)
1100 {
1101 *error = LYNQ_UNSPECIFIED_REASON;
1102 }
1103 else
1104 {
1105 *error = LYNQ_UNSPECIFIED_REASON;
1106 }
1107
1108 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1109 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 +08001110 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001111 return;
1112 }
1113 else
1114 {
1115 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1116 *error = LYNQ_UNSPECIFIED_REASON;
1117 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1118 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1119 return;
1120 }
1121 }
1122
1123 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1124 {
1125 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1126 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1127 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1128 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1129 return;
1130 }
1131
1132 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1133 {
1134 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1135 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1136 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1137 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1138 return;
1139 }
1140
qs.xiongdf637b22023-10-26 19:30:07 +08001141 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1142 if (strstr(modify, "Association request to the driver failed") != NULL)
1143 {
1144 RLOGD("Association request to the driver failed --- recover");
1145 system("wl down");
1146 system("wl up");
1147 *error = LYNQ_UNSPECIFIED_REASON;
1148 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1149 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1150 return;
1151 }
1152 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1153
1154
you.chen32cb31e2023-04-13 14:05:45 +08001155 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001156 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001157 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001158 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1159 return;
1160
1161}
1162
qs.xiongfcc914b2023-07-06 21:16:20 +08001163void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1164{
1165 char *pReason;
1166 char *wpanetid;
1167 char destid[3];
1168 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1169 *networkid = -1;
1170 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1171 {
1172 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1173 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1174 return;
1175 }
1176 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1177 {
1178 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1179 wpanetid = strstr(modify,"id=");
1180 if ( wpanetid != NULL )
1181 {
1182 wpanetid +=strlen("id=");
1183 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1184 if (memcpy(destid,wpanetid,2) != NULL)
1185 {
1186 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1187 *networkid = atoi(destid);
1188 RLOGD("get networkid is %d\n",*networkid);
1189 }
1190 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1191 }
1192 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1193 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1194 return;
1195 }
1196 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1197 {
1198 wpanetid = strstr(modify,"id=");
1199 if ( wpanetid != NULL )
1200 {
1201 wpanetid +=strlen("id=");
1202 if (memcpy(destid,wpanetid,2) != NULL)
1203 {
1204 *networkid = atoi(destid);
1205 RLOGD("get networkid is %d\n",*networkid);
1206 }
1207 }
1208 pReason = strstr(modify, "reason=");
1209 if (pReason != NULL)
1210 {
1211 pReason += strlen("reason=");
1212 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1213 {
1214 *error = LYNQ_TIME_OUT;
1215 }
1216 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1217 {
1218 *error = LYNQ_PSW_ERROR;
1219 }
1220 else
1221 {
1222 *error = LYNQ_UNSPECIFIED_REASON;
1223 }
1224 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1225 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1226 return;
1227 }
1228 else
1229 {
1230 *error = LYNQ_UNSPECIFIED_REASON;
1231 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1232 return;
1233 }
1234 }
1235 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1236 {
1237 wpanetid = strstr(modify,"id=");
1238 if ( wpanetid != NULL )
1239 {
1240 wpanetid +=strlen("id=");
1241 if (memcpy(destid,wpanetid,2) != NULL)
1242 {
1243 *networkid = atoi(destid);
1244 RLOGD("get networkid is %d\n",*networkid);
1245 }
1246 }
1247 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1248 pReason = strstr(modify, "status_code=");
1249 if (pReason != NULL)
1250 {
1251 pReason += strlen("status_code=");
1252 if (memcmp(pReason, "17", 2) == 0)
1253 {
1254 *error = LYNQ_AP_UNABLE_HANDLE;
1255 }
1256 else if (memcmp(pReason, "1",1) == 0)
1257 {
1258 *error = LYNQ_UNSPECIFIED_REASON;
1259 }
1260 else
1261 {
1262 *error = LYNQ_UNSPECIFIED_REASON;
1263 }
1264 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1265 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1266 return;
1267 }
1268 else
1269 {
1270 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1271 *error = LYNQ_UNSPECIFIED_REASON;
1272 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001273 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001274 return;
1275 }
1276 }
1277 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1278 {
1279 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1280 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1281 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1282 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1283 return;
1284 }
1285 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1286 {
1287 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1288 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1289 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1290 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1291 return;
1292 }
1293 RLOGD("EVENT : %s\n", modify);
1294 *error = LYNQ_UNSPECIFIED_REASON;
1295 *state = LYNQ_WIFI_STATUS_EGNORE;
1296 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1297 return;
1298}
you.chen70f377f2023-04-14 18:17:09 +08001299static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1300{
you.chen6d247052023-06-01 16:39:54 +08001301 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001302 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1303 {
1304 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1305 g_sta_callback_func(g_sta_callback_priv, state, error);
1306 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1307 }
you.chen6d247052023-06-01 16:39:54 +08001308 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001309}
qs.xiongfcc914b2023-07-06 21:16:20 +08001310static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1311{
1312 pthread_mutex_lock(&s_sta_callback_mutex);
1313 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1314 {
1315 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1316 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1317 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1318 }
1319 pthread_mutex_unlock(&s_sta_callback_mutex);
1320}
you.chen70f377f2023-04-14 18:17:09 +08001321
you.chen35020192022-05-06 11:30:57 +08001322static void STAWatcherThreadProc() {
1323 size_t len = MAX_RET;
1324 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001325 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001326 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001327 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001328
you.chen6c2dd9c2022-05-16 17:55:28 +08001329 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001330 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001331
you.chen70f377f2023-04-14 18:17:09 +08001332 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001333 while (g_sta_watcher_stop_flag == 0)
1334 {
you.chenf711c8a2023-04-13 13:49:45 +08001335 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001336 {
you.chen35020192022-05-06 11:30:57 +08001337 continue;
1338 }
you.chenf711c8a2023-04-13 13:49:45 +08001339
you.chen6c2dd9c2022-05-16 17:55:28 +08001340 memset(msg_notify, 0, MAX_RET);
1341 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001342 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001343 {
you.chen35020192022-05-06 11:30:57 +08001344 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001345 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1346 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001347 {
you.chen35020192022-05-06 11:30:57 +08001348 g_sta_scan_finish_flag = 1;
1349 }
1350
qs.xiong9fbf74e2023-03-28 13:38:22 +08001351 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001352 {
you.chen35020192022-05-06 11:30:57 +08001353 continue;
1354 }
qs.xiong455c30b2023-04-12 11:40:02 +08001355 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001356 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1357 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1358 {
1359 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1360 }
you.chen0c9bee22023-10-25 13:03:14 +08001361 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1362 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1363 {
1364 RLOGD("donot report scan result during in call connect manual");
1365 }
you.chen6e724162023-10-19 19:10:01 +08001366 else
1367 {
1368 notify_connect_status(state, error);
1369 }
1370 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001371
1372 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001373 {
you.chen70f377f2023-04-14 18:17:09 +08001374 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001375 if (last_state != state)
1376 {
1377 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1378 {
1379 system_call_v("%s %s", sta_status_change_script, "connect");
1380 }
1381 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1382 {
1383 system_call_v("%s %s", sta_status_change_script, "disconnect");
1384 }
1385 }
qs.xiong09560402023-10-27 21:58:55 +08001386
you.chenc9928582023-04-24 15:39:37 +08001387 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001388 if (g_sta_fake_scan_finish_flag == 1)
1389 {
1390 g_sta_fake_scan_finish_flag = 0;
1391 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1392 }
you.chen32cb31e2023-04-13 14:05:45 +08001393 }
you.chen35020192022-05-06 11:30:57 +08001394 }
1395 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001396 if (lynq_wpa_ctrl != NULL)
1397 {
you.chen92fd5d32022-05-25 10:09:47 +08001398 wpa_ctrl_detach(lynq_wpa_ctrl);
1399 wpa_ctrl_close(lynq_wpa_ctrl);
1400 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001401}
qs.xiongfcc914b2023-07-06 21:16:20 +08001402static void STAAutoWatcherThreadProc() {
1403 size_t len = MAX_RET;
1404 char msg_notify[MAX_RET];
1405 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001406 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001407 int idle_count = 0;
1408 int networkid;
1409 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1410 g_sta_auto_watcher_stop_flag = 0;
1411 RLOGD("STAAutoWatcherThreadProc thread started ------");
1412 while (g_sta_auto_watcher_stop_flag == 0)
1413 {
1414 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1415 {
1416 continue;
1417 }
1418 memset(msg_notify, 0, MAX_RET);
1419 len = MAX_RET;
1420 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1421 {
1422 msg_notify[len+1] = '\0';
1423 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1424 if (strstr(msg_notify, state_scan_result) != NULL)
1425 {
1426 g_sta_auto_scan_finish_flag = 1;
1427 }
1428 if (g_sta_auto_callback_func == NULL)
1429 {
1430 continue;
1431 }
1432 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1433 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001434 }
1435 }
1436 if (lynq_wpa_ctrl != NULL)
1437 {
1438 wpa_ctrl_detach(lynq_wpa_ctrl);
1439 wpa_ctrl_close(lynq_wpa_ctrl);
1440 }
1441}
qs.xiongf1b525b2022-03-31 00:58:23 -04001442
you.chen70f377f2023-04-14 18:17:09 +08001443// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1444static void GlobalWatcherThreadProc()
1445{
1446 int ret, connect_timeout, service_abnormal;
1447 error_number_s error_num = -1;
1448 inner_sta_status_s sta_status;
1449 scan_info_s *scan_list = NULL;
1450 int i, scan_len=0;
1451 char connecting_ssid[64];
1452 struct timeval now;
1453
1454 RLOGD("GlobalWatcherThreadProc start to run");
1455
1456 while (1)
1457 {
1458 pthread_mutex_lock(&s_global_check_mutex);
1459 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1460 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1461 {
1462 pthread_mutex_unlock(&s_global_check_mutex);
1463 usleep(50*1000);
1464 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1465 continue;
1466 }
1467
1468 connect_timeout = 0;
1469 service_abnormal = 0;
1470 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1471 {
1472 while (1)
1473 {
1474 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1475 if (ret == ETIME)
1476 {
1477 connect_timeout = 1;
1478 }
1479 else if (ret != 0)
1480 {
1481 gettimeofday(&now,NULL);
1482 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1483 {
1484 usleep(SLEEP_TIME_ON_IDLE);
1485 continue;
1486 }
1487 connect_timeout = 1;
1488 }
1489 sta_status = s_sta_status;
1490 error_num = s_sta_error_number;
1491 s_sta_status = INNER_STA_STATUS_INIT;
1492 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1493 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1494 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1495 break;
1496 }
1497 }
1498 if (s_service_invoke_timeout_cnt > 10)
1499 {
1500 service_abnormal = 1;
1501 s_service_invoke_timeout_cnt = 0;
1502 }
1503 pthread_mutex_unlock(&s_global_check_mutex);
1504
1505 if (service_abnormal == 1)
1506 {
1507 sleep(1);
1508 RLOGE("wpa service is abnormal info app to exit");
1509 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001510
1511 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1512
you.chen70f377f2023-04-14 18:17:09 +08001513 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1514 }
1515
1516 if (sta_status == INNER_STA_STATUS_CANCEL)
1517 {
1518 continue;
1519 }
1520 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1521 {
1522 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1523 }
1524 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1525 {
1526 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1527 {
1528 for(i=0; i < scan_len;i++)
1529 {
1530 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1531 {
1532 error_num = LYNQ_AUTH_ERROR;
1533 break;
1534 }
1535 }
1536 free(scan_list);
1537 }
1538 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1539 }
1540 else if (connect_timeout == 0)
1541 {
1542 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1543 }
1544 else // wait timeout
1545 {
1546 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1547 {
1548 ; // wpa service abnormal
1549 }
1550 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1551 {
1552 RLOGD("GlobalWatcherThreadProc notify connected");
1553 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1554 }
1555 else
1556 {
1557 RLOGD("GlobalWatcherThreadProc notify timeout");
1558 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1559 }
1560 }
1561 } // while (1)
1562}
1563
qs.xiong1af5daf2022-03-14 09:12:12 -04001564int lynq_wifi_enable(void)
1565{
you.chen35020192022-05-06 11:30:57 +08001566 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001567 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001568 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001569 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1570
qs.xiong9fbf74e2023-03-28 13:38:22 +08001571 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1572 {
you.chend2fef3f2023-02-13 10:50:35 +08001573 goto out_enable;
1574 }
1575
you.chenc9928582023-04-24 15:39:37 +08001576 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001577 if (ret != 0)
1578 {
1579 //printf("service state %d\n", ret);
1580 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001581 ret = -1;
1582 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001583 }
lhfe8da902022-10-11 18:55:36 +08001584
you.chen70f377f2023-04-14 18:17:09 +08001585 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1586 {
1587 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1588 if(ret<0)
1589 {
1590 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1591 ret = -1;
1592 goto out_enable;
1593 }
1594 }
1595
you.chend2fef3f2023-02-13 10:50:35 +08001596 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1597 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1598 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1599 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1600out_enable:
1601 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001602 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001603}
1604
qs.xiong1af5daf2022-03-14 09:12:12 -04001605int lynq_wifi_disable(void)
1606{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001607 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001608 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001609 g_ap_watcher_stop_flag = 1;
1610 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001611 g_sta_auto_watcher_stop_flag = 1;
qs.xiong9d8f3102023-12-07 20:11:37 +08001612/* g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001613 if (g_ap_watcher_pid != 0)
1614 pthread_join(g_ap_watcher_pid, NULL);
qs.xiong9d8f3102023-12-07 20:11:37 +08001615*/
you.chen35020192022-05-06 11:30:57 +08001616 if (g_sta_watcher_pid != 0)
1617 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001618 if (g_sta_auto_watcher_pid != 0)
1619 pthread_join(g_sta_auto_watcher_pid, NULL);
qs.xiong17579bb2024-03-11 19:08:06 +08001620 if ( g_lynq_wpa_ctrl[0] != NULL)
1621 {
1622 if(g_lynq_wpa_ctrl[0]->ctrl != NULL)
1623 {
1624 wpa_ctrl_close(g_lynq_wpa_ctrl[0]->ctrl);
1625 }
1626 free(g_lynq_wpa_ctrl[0]);
1627 g_lynq_wpa_ctrl[0] = NULL;
1628 }
1629
1630 if ( g_lynq_wpa_ctrl[1] != NULL )
1631 {
1632 if( g_lynq_wpa_ctrl[1]->ctrl != NULL )
1633 {
1634 wpa_ctrl_close(g_lynq_wpa_ctrl[1]->ctrl);
1635 }
1636 free(g_lynq_wpa_ctrl[1]);
1637 g_lynq_wpa_ctrl[1] = NULL;
1638 }
qs.xiong44fac672023-08-29 16:15:55 +08001639 if (g_ap_tmp_watcher_pid != 0)
1640 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001641 g_ap_watcher_pid = 0;
1642 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001643 g_sta_auto_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08001644// g_ap_tmp_watcher_pid = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +08001645 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001646 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001647 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1648 return 0;
1649}
1650
1651static inline char inner_convert_char(char in)
1652{
1653 if (in >= '0' && in <= '9')
1654 {
1655 return in - '0';
1656 }
1657 else if (in >= 'a' && in <= 'f')
1658 {
1659 return in - 'a' + 10;
1660 }
1661 else if (in >= 'A' && in <= 'F')
1662 {
1663 return in - 'A' + 10;
1664 }
1665 else
1666 {
1667 return '\xff';
1668 }
1669}
1670
1671static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1672{
1673 char *p;
1674 size_t pos = 0;
1675 if (NULL == out_ssid)
1676 return;
1677 //printf("input ssid=[%s]\n", ssid);
1678 memset(out_ssid, 0, out_ssid_len);
1679 if (NULL == ssid)
1680 return;
1681 p = strchr(ssid, '\\');
1682 if (NULL == p)
1683 {
1684 strncpy(out_ssid, ssid, out_ssid_len);
1685 //printf(" first %s\n", out_ssid);
1686 }
1687 else
1688 {
1689 pos = p - ssid;
1690 memcpy(out_ssid, ssid, pos);
1691 //printf("pos %lu -- %s\n", pos, out_ssid);
1692 for(; pos < out_ssid_len; pos ++)
1693 {
1694 if (p[0] == '\0')
1695 {
1696 //printf(" out %s\n", out_ssid);
1697 return;
1698 }
1699 else if (p[0] != '\\')
1700 {
1701 out_ssid[pos] = p[0];
1702 p += 1;
1703 }
1704 else if (p[1] == 'x' || p[1] == 'X')
1705 {
1706 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1707 p += 4;
1708 }
1709 else if (p[1] == '\\')
1710 {
1711 out_ssid[pos] = '\\';
1712 p += 2;
1713 }
1714 else if (p[1] == 't')
1715 {
1716 out_ssid[pos] = '\t';
1717 p += 2;
1718 }
1719 else if (p[1] == 'r')
1720 {
1721 out_ssid[pos] = '\r';
1722 p += 2;
1723 }
1724 else if (p[1] == 'n')
1725 {
1726 out_ssid[pos] = '\n';
1727 p += 2;
you.chen34983432023-12-21 20:39:06 +08001728 }
1729 else
1730 {
1731 out_ssid[pos] = p[1];
1732 p += 2;
you.chend2fef3f2023-02-13 10:50:35 +08001733 }//todo find a better way to convert?
1734 }
1735 }
1736 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001737}
qs.xiong1af5daf2022-03-14 09:12:12 -04001738
you.chen35020192022-05-06 11:30:57 +08001739static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001740 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001741 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001742 RLOGD("enter inner_get_param");
1743 if (out_put == NULL)
1744 {
1745 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001746 return -1;
1747 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001748 if (param_name == NULL)
1749 {
1750 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001751 return -1;
1752 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001753 if (param_name[0] == '\0')
1754 {
1755 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001756 return -1;
1757 }
1758
1759 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1760
1761 CHECK_WPA_CTRL(interface);
1762
1763 DO_REQUEST(lynq_cmd_get);
1764
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1766 {
1767 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001768 return -1;
1769 }
1770
you.chena6fa5b22022-05-18 10:28:19 +08001771// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001772 if (strcmp(param_name, "ssid") == 0)
1773 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001774 if (cmd_reply[0] == '\"')
1775 {
you.chend2fef3f2023-02-13 10:50:35 +08001776 ssid_len = reply_len - 1;
1777 memcpy(out_put, cmd_reply + 1, ssid_len);
1778 if (out_put[ssid_len-1] == '\"')
1779 {
1780 out_put[ssid_len-1] = '\0';
1781 }
1782 else
1783 {
1784 out_put[ssid_len] = '\0';
1785 }
1786 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 else
1788 {
you.chend2fef3f2023-02-13 10:50:35 +08001789 ssid_len = reply_len / 2;
1790 for(i=0; i<ssid_len; i++)
1791 {
1792 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1793 }
1794 out_put[ssid_len] = '\0';
1795 }
1796 }
1797 else
1798 {
1799 memcpy(out_put, cmd_reply, reply_len + 1);
1800 }
you.chen35020192022-05-06 11:30:57 +08001801 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001802}
qs.xiong1af5daf2022-03-14 09:12:12 -04001803
you.chen35020192022-05-06 11:30:57 +08001804static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1805 int ret = 0;
1806 char * end = str + len - 1;
1807 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001808 while(str < end)
1809 {
1810 if (*str == delimiter)
1811 {
you.chen35020192022-05-06 11:30:57 +08001812 *str++ = '\0';
1813 results[ret++] = str;
1814 continue;
1815 }
1816 str++;
1817 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001818 if (*str == delimiter)
1819 {
you.chen35020192022-05-06 11:30:57 +08001820 *str = '\0';
1821 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001822
you.chen6ed36a62023-04-27 17:51:56 +08001823 results[ret] = NULL;
1824
you.chen35020192022-05-06 11:30:57 +08001825 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001826}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001827/*
1828 *add func to get conencted STA device ip from dnsmasq ap0.lease
1829 *return 0 means get ip success
1830 */
1831static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1832{
1833 char * p;
1834 int ret;
1835 char cmd[256]={0};
1836 if (NULL == mac || NULL == ip)
1837 return -1;
1838 memset(ip, 0, ip_len);
1839 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1840 ret = exec_cmd(cmd, ip, ip_len);
1841 if( ret == 0 )
1842 {
1843 p = strchr(ip, '\n');
1844 if (NULL != p)
1845 {
1846 *p = '\0';
1847 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1848 return ret;
1849 }else
1850 {
1851 ret = -1;
1852 }
1853 }
1854 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1855 return ret;
1856
1857}
qs.xiong7a105ce2022-03-02 09:43:11 -05001858
you.chend2fef3f2023-02-13 10:50:35 +08001859static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1860{
1861 char * p;
1862 int ret = 0;
1863 char cmd[256]={0};
1864 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001865 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001866 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001867 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001868 ret = exec_cmd(cmd, ip, ip_len);
1869 p = strchr(ip, '\n');
1870 if (NULL != p)
1871 {
1872 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001873 }else
1874 {
1875 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001876 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001877 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001878 return ret;
1879}
1880
you.chend2fef3f2023-02-13 10:50:35 +08001881static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001882 struct in_addr addr ={0};
1883 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001884 char cmd[64] = {0};
1885 char * p;
1886 int ret;
you.chen35020192022-05-06 11:30:57 +08001887
qs.xiong9fbf74e2023-03-28 13:38:22 +08001888 if (ip == NULL || *ip == '\0' || hostname == NULL)
1889 {
1890 RLOGE("ip == NULL or hostname == NULL");
1891 return -1;
you.chen35020192022-05-06 11:30:57 +08001892 }
1893
you.chend2fef3f2023-02-13 10:50:35 +08001894 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001895 if (inet_aton(ip, &addr) == 0)
1896 {
you.chen35020192022-05-06 11:30:57 +08001897 printf("---inet_aton fail\n");
1898 return -1;
1899 }
1900
1901 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1902
qs.xiong9fbf74e2023-03-28 13:38:22 +08001903 if (ht == NULL)
1904 {
you.chen186d3c32023-05-18 14:19:46 +08001905 hostname[0] = '\0';
1906 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1907 ret = exec_cmd(cmd, hostname, 32);
1908 if (ret == 0)
1909 {
1910 p = strchr(hostname, '\n');
1911 if (p != NULL)
1912 {
1913 *p = '\0';
1914 }
1915 return 0;
1916 }
1917 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001918 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001919 herror(NULL);
1920 return -1;
1921 }
1922
1923 strcpy(hostname, ht->h_name);
1924
1925 return 0;
1926}
1927
1928static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1929{
1930 int count, index, words_count;
1931 char * split_lines[128]= {0};
1932 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001933 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001934 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001935 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001936
1937 CHECK_WPA_CTRL(ap_sta);
1938
1939 DO_REQUEST(lynq_wifi_list_networks);
1940
1941 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1942
1943 //@todo check ssid field to compatible
1944
1945 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001946 for(index=1; index < count; index++)
1947 {
you.chen35020192022-05-06 11:30:57 +08001948 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001949 if (words_count > 2)
1950 {
you.chend2fef3f2023-02-13 10:50:35 +08001951 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001952 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1953 {
you.chen35020192022-05-06 11:30:57 +08001954 net_no_list[ret++] = atoi(split_words[0]);
1955 }
1956 }
1957 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001958 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001959 return ret;
1960}
1961
1962static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001963 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001964 CHECK_WPA_CTRL(ap_sta);
1965 const char *lynq_wifi_add_network = "ADD_NETWORK";
1966
qs.xiong9fbf74e2023-03-28 13:38:22 +08001967 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001968 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001969 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1970 {
1971 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001972 return -1;
1973 }
1974
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 for(i=0;i<reply_len;i++)
1976 {
1977 if(cmd_reply[i] == '\n')
1978 {
you.chen35020192022-05-06 11:30:57 +08001979 cmd_reply[i] = '\0';
1980 break;
1981 }
1982 }
1983 return atoi(cmd_reply);
1984}
you.chena6cd55a2022-05-08 12:20:18 +08001985
you.chen35020192022-05-06 11:30:57 +08001986static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1987{
1988 int count, index;
1989 int net_no_list[128];
1990
qs.xiong9fbf74e2023-03-28 13:38:22 +08001991 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001992 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001993 for (index=0; index < count; index++)
1994 {
1995 if (net_no_list[index] == net_no)
1996 {
you.chen35020192022-05-06 11:30:57 +08001997 return 0;
1998 }
1999 }
2000
2001 if (count >= 1)
2002 index = net_no_list[count - 1];
2003 else
2004 index = -1;
2005
qs.xiong9fbf74e2023-03-28 13:38:22 +08002006 while (index < net_no )
2007 {
you.chen35020192022-05-06 11:30:57 +08002008 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002009 if (index >= net_no)
2010 { // required network no created
2011 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08002012 return 0;
2013 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002014 else if( index < 0)
2015 {
2016 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08002017 return -1;
2018 }
you.chen35020192022-05-06 11:30:57 +08002019 }
2020
2021 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 {
2023 RLOGE("[lynq_check_network_number] network index < 0");
2024 return -1;
2025 }
2026 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08002027 return 0;
2028}
2029
2030static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08002031 if (freq > 5000 && freq < 6000)
2032 {
you.chen35020192022-05-06 11:30:57 +08002033 return LYNQ_WIFI_5G_band;
2034 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002035 else if (freq > 2000 && freq < 3000)
2036 {
you.chen35020192022-05-06 11:30:57 +08002037 return LYNQ_WIFI_2G_band;
2038 }
2039 return LYNQ_WIFI_2_and_5G_band;
2040}
2041
2042static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002043 if (key_mgmt != NULL)
2044 {
2045 if (memcmp( key_mgmt, "NONE", 4) == 0)
2046 {
you.chen35020192022-05-06 11:30:57 +08002047 return LYNQ_WIFI_AUTH_OPEN;
2048 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002049 else if (memcmp( key_mgmt, "WEP", 3) == 0)
2050 {
you.chen35020192022-05-06 11:30:57 +08002051 return LYNQ_WIFI_AUTH_WEP;
2052 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002053 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2054 {
you.chen35020192022-05-06 11:30:57 +08002055 return LYNQ_WIFI_AUTH_WPA_PSK;
2056 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002057 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2058 {
you.chen35020192022-05-06 11:30:57 +08002059 return LYNQ_WIFI_AUTH_WPA2_PSK;
2060 }
2061 }
2062
2063 return -1;
2064}
2065
2066static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002067 if (flag != NULL)
2068 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002069 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 +08002070 {
2071 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002072 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002073 {
2074 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2075 }else if (strstr( flag, "WPA2-PSK") != NULL)
2076 {
you.chen35020192022-05-06 11:30:57 +08002077 return LYNQ_WIFI_AUTH_WPA2_PSK;
2078 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002079 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002080 {
you.chen35020192022-05-06 11:30:57 +08002081 return LYNQ_WIFI_AUTH_WPA_PSK;
2082 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002083 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002084 {
you.chen35020192022-05-06 11:30:57 +08002085 return LYNQ_WIFI_AUTH_WEP;
2086 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002087 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002088 {
you.chen35020192022-05-06 11:30:57 +08002089 return LYNQ_WIFI_AUTH_OPEN;
2090 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002091 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002092 {
you.chend2fef3f2023-02-13 10:50:35 +08002093 return LYNQ_WIFI_AUTH_OPEN;
2094 }
qs.xiong46f41562023-07-11 21:06:47 +08002095 else
2096 {
2097 RLOGD("convert_max_auth_from_flag not-found auth mode");
2098 }
you.chen35020192022-05-06 11:30:57 +08002099 }
2100
2101 return -1;
2102}
2103
2104static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2105 switch (bw) {
2106 case 10:
2107 return LYNQ_WIFI_BANDWIDTH_HT10;
2108 break;
2109 case 20:
2110 return LYNQ_WIFI_BANDWIDTH_HT20;
2111 break;
2112 case 40:
2113 return LYNQ_WIFI_BANDWIDTH_HT40;
2114 break;
2115 case 80:
2116 return LYNQ_WIFI_BANDWIDTH_HT80;
2117 break;
2118 default:
2119 break;
2120 }
2121
2122 return -1;
2123}
2124
you.chen70f377f2023-04-14 18:17:09 +08002125static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002126static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2127 int i, count;
2128 char *p;
2129 const char *lynq_status_cmd = "STATUS";
2130 const char * FLAG_SSID = "ssid=";
2131 const char * FLAG_SBSID = "bssid=";
2132 const char * FLAG_KEY_MGMT = "key_mgmt=";
2133 const char * FLAG_FREQ = "freq=";
2134 const char * FLAG_STATE = "wpa_state=";
2135 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002136 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002137 char *split_lines[128] = {0};
2138
2139 CHECK_WPA_CTRL(interface);
2140
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 if (curr_state == NULL)
2142 {
2143 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002144 return -1;
2145 }
2146
2147 DO_REQUEST(lynq_status_cmd);
2148
2149 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2150
2151 curr_state->net_no = -1;
2152 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002153 for(i=0; i < count; i++)
2154 {
2155 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002156 {
you.chen35020192022-05-06 11:30:57 +08002157 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002158 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002159 {
you.chend2fef3f2023-02-13 10:50:35 +08002160 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002161 ret = 0;
2162 continue;
2163 }
you.chenf58b3c92022-06-21 16:53:48 +08002164 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002165 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002166 {
you.chend2fef3f2023-02-13 10:50:35 +08002167 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 +08002168 ret = 0;
2169 continue;
2170 }
you.chen35020192022-05-06 11:30:57 +08002171 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002173 {
you.chen450d0172022-07-15 17:56:48 +08002174 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002175 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002176 ret = 0;
2177 continue;
2178 }
2179 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002180 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002181 {
you.chen35020192022-05-06 11:30:57 +08002182 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2183 ret = 0;
2184 continue;
2185 }
you.chend2fef3f2023-02-13 10:50:35 +08002186 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002187 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002188 {
you.chend2fef3f2023-02-13 10:50:35 +08002189 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2190 ret = 0;
2191 continue;
2192 }
you.chen35020192022-05-06 11:30:57 +08002193 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002194 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002195 {
you.chen35020192022-05-06 11:30:57 +08002196 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002197 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002198 {
you.chen35020192022-05-06 11:30:57 +08002199 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2200 ret = 0;
2201 continue;
2202 }
2203
2204 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002205 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002206 {
you.chen35020192022-05-06 11:30:57 +08002207 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002208 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002209 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002210 }
2211 }
2212
you.chen70f377f2023-04-14 18:17:09 +08002213 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2214 {
2215 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2216 }
2217
you.chen35020192022-05-06 11:30:57 +08002218 return ret;
2219}
2220
qs.xiongf1b525b2022-03-31 00:58:23 -04002221int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002222{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002223 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002224 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002225
qs.xiong9fbf74e2023-03-28 13:38:22 +08002226 if (ap_ssid == NULL)
2227 {
2228 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002229 return -1;
2230 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002231 else
2232 {
2233 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002234 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002235
qs.xiong9fbf74e2023-03-28 13:38:22 +08002236 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2237 {
2238 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002239 return -1;
2240 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002241
you.chen35020192022-05-06 11:30:57 +08002242 CHECK_IDX(idx, CTRL_AP);
2243
2244 CHECK_WPA_CTRL(CTRL_AP);
2245
2246 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2247
2248 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2249 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002250
qs.xiong1b5e3472023-11-27 17:42:20 +08002251 RLOGD("[lynq_wifi_ap_ssid_set] set ssid succeed %d",__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002252 return 0;
you.chen35020192022-05-06 11:30:57 +08002253
qs.xiong7a105ce2022-03-02 09:43:11 -05002254}
2255
you.chen35020192022-05-06 11:30:57 +08002256int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002257{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002258 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002259 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002260 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002261}
2262
qs.xiongc9c79f72022-10-17 15:27:18 +08002263/*****
2264 *frequency <------>channel
2265 *
2266 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2267 *
2268 *
2269 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2270 *
2271 *
2272 * */
2273static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002274 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};
2275 int i;
2276 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2277
qs.xiong69a332b2022-12-02 09:58:57 +08002278 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002279 {
2280 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002281 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002282 }
qs.xiongc00b6032022-11-29 16:28:03 +08002283
2284 if(i == arr_len)
2285 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002286 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002287 return -1;
2288 }
qs.xiongc00b6032022-11-29 16:28:03 +08002289
qs.xiongc9c79f72022-10-17 15:27:18 +08002290 return 0;
2291}
qs.xiong13673462023-02-21 19:12:54 +08002292
2293static int lynq_check_frequencyby_country_code(int input_frequency)
2294{
2295 char str_cnc[]="CN";
2296 char str_dest[20]="";
2297
2298 if( lynq_get_country_code(1,str_dest) != 0 )
2299 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002300 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002301 return -1;
2302 }
2303 if( strncmp(str_dest,str_cnc,2) != 0 )
2304 {
2305 return 0;
2306 }else if( 2473 < input_frequency && input_frequency < 5744)
2307 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002308 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002309 return -1;
2310 }
2311 return 0;
2312}
qs.xiongf1b525b2022-03-31 00:58:23 -04002313int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002314{
qs.xiongc00b6032022-11-29 16:28:03 +08002315 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002316 char lynq_wifi_frequency_cmd[128]={0};
2317 char lynq_cmd_mode[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002318 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002319 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002320 check = lynq_check_set_frequency(lynq_wifi_frequency);
2321 if(check != 0)
2322 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002323 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002324 return -1;
you.chen35020192022-05-06 11:30:57 +08002325 }
qs.xiong13673462023-02-21 19:12:54 +08002326 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2327 if(check != 0)
2328 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002329 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002330 return -1;
2331 }
2332
qs.xiongc00b6032022-11-29 16:28:03 +08002333 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2334 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002336 return -1;
2337 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002338
you.chen35020192022-05-06 11:30:57 +08002339 CHECK_IDX(idx, CTRL_AP);
2340
2341 CHECK_WPA_CTRL(CTRL_AP);
2342
2343 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2344 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002345
you.chen6c2dd9c2022-05-16 17:55:28 +08002346 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002347 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2348 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2349 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002352}
2353
qs.xiongf1b525b2022-03-31 00:58:23 -04002354int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002355{
you.chen35020192022-05-06 11:30:57 +08002356 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002358 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002359
qs.xiong9fbf74e2023-03-28 13:38:22 +08002360 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2361 {
2362 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002363 return -1;
2364 }
2365 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002366
qs.xiong9fbf74e2023-03-28 13:38:22 +08002367 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002368}
2369
qs.xiongf1b525b2022-03-31 00:58:23 -04002370int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2371{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002373 CHECK_IDX(idx, CTRL_AP);
2374 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002375 case LYNQ_WIFI_BANDWIDTH_HT10:
2376 {
2377 RLOGE("bandwith [%d] not support now\n", bandwidth);
2378 return -1;
2379 }
2380 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002381 {
2382 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2383 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002384 if (system(lynq_cmd_bandwith) != 0 )
2385 {
2386 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002387 return -1;
2388 }
2389 system("wl up");
2390 break;
2391 }
2392 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002393 {
qs.xiong10379192023-02-21 13:19:42 +08002394 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002395 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2396 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002397 if (system(lynq_cmd_bandwith) != 0 )
2398 {
2399 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002400 return -1;
2401 }
2402 system("wl up");
2403 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002404 }
you.chen35020192022-05-06 11:30:57 +08002405 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002406 {
qs.xiong10379192023-02-21 13:19:42 +08002407 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002408 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409 if (system(lynq_cmd_bandwith) != 0 )
2410 {
2411 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002412 return -1;
2413 }
2414 system("wl up");
2415 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002416 }
2417 default:
you.chen35020192022-05-06 11:30:57 +08002418 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002419 RLOGE("auth type [%d] not support now\n", bandwidth);
2420 return -1;
you.chen35020192022-05-06 11:30:57 +08002421 }
2422 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002423
2424
you.chen35020192022-05-06 11:30:57 +08002425 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002426}
you.chen35020192022-05-06 11:30:57 +08002427
qs.xiongf1b525b2022-03-31 00:58:23 -04002428int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2429{
you.chen35020192022-05-06 11:30:57 +08002430 int count = 0;
2431 int index = 0;
2432 char *split_words[128] = {0};
2433 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002434 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002435 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002436
you.chen35020192022-05-06 11:30:57 +08002437 CHECK_WPA_CTRL(CTRL_AP);
2438
2439 DO_REQUEST(lynq_chanspec_cmd);
2440
2441 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2442 for(;index < count; index++) {
2443 if (strncmp(split_words[index], "bw", 2) != 0) {
2444 continue;
2445 }
2446
2447 index++;
2448 if (index >= count) {
2449 return -1;
2450 }
2451
qs.xiong9fbf74e2023-03-28 13:38:22 +08002452 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002453 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2454 return 0;
2455 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002456 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002457 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002458}
qs.xiong0fb469a2022-04-14 03:50:45 -04002459
qs.xiongf1b525b2022-03-31 00:58:23 -04002460int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002461{
you.chen35020192022-05-06 11:30:57 +08002462 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002463 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002464 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002465
you.chen35020192022-05-06 11:30:57 +08002466 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002467
qs.xiong9fbf74e2023-03-28 13:38:22 +08002468 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2469 {
you.chen35020192022-05-06 11:30:57 +08002470 return -1;
2471 }
2472
2473 system("wl down");
2474 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002475 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002476 return -1;
2477 }
2478 system("wl up");
2479 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002480}
qs.xiong0fb469a2022-04-14 03:50:45 -04002481
qs.xiongf1b525b2022-03-31 00:58:23 -04002482int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002483{
you.chen35020192022-05-06 11:30:57 +08002484 int count = 0;
2485 int index = 0;
2486 char *split_words[128] = {0};
2487 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002488 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002489 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002490
you.chen35020192022-05-06 11:30:57 +08002491 CHECK_WPA_CTRL(CTRL_AP);
2492
2493 DO_REQUEST(lynq_chanspec_cmd);
2494
2495 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002496 for(;index < count; index++)
2497 {
2498 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002499 if (strncmp(split_words[index], "channel", 2) != 0) {
2500 continue;
2501 }
2502
2503 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002504 if (index >= count)
2505 {
you.chen35020192022-05-06 11:30:57 +08002506 return -1;
2507 }
2508
2509 *channel = atoi(split_words[index]);
2510 return 0;
2511 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002512 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002513 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002514}
2515
2516
you.chen35020192022-05-06 11:30:57 +08002517int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002518{
you.chen6c2dd9c2022-05-16 17:55:28 +08002519 char ssid[MAX_CMD] = {0};
2520 int freq = 0;
2521 char lynq_auth_cmd[64]={0};
2522 char lynq_auth_alg_cmd[64]={0};
2523 char lynq_psk_cmd[64]={0};
2524 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002525 char lynq_ieee80211_cmd[64]={0};
2526 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002527 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002528 CHECK_IDX(idx, CTRL_AP);
2529
you.chen6c2dd9c2022-05-16 17:55:28 +08002530 CHECK_WPA_CTRL(CTRL_AP);
2531
qs.xiong9fbf74e2023-03-28 13:38:22 +08002532 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2533 {
2534 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002535 return -1;
2536 }
2537
you.chen92fd5d32022-05-25 10:09:47 +08002538 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002539 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002540 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002541 return 0;
2542 }
2543 else {
2544 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2545 ssid[0] = '\0';
2546 }
2547 lynq_wifi_ap_frequency_get(idx, &freq);
2548
2549 DO_OK_FAIL_REQUEST(cmd_disconnect);
2550 DO_OK_FAIL_REQUEST(cmd_remove_all);
2551 if (ssid[0] != '\0') {
2552 lynq_wifi_ap_ssid_set(idx, ssid);
2553 }
2554 if (freq != 0) {
2555 lynq_wifi_ap_frequency_set(idx, freq);
2556 }
2557 }
2558 }
you.chen35020192022-05-06 11:30:57 +08002559
qs.xiong9fbf74e2023-03-28 13:38:22 +08002560 switch(auth){
2561 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002562 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002563 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002564 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002565 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002566 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002567 break;
2568 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002569 case LYNQ_WIFI_AUTH_WEP:
2570 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002571 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002572 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002573 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002574 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2575
2576 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2577 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2578 break;
2579 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002580 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002581 {
2582 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2583 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2584 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2585
2586 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2587 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2588 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2589 break;
2590
2591 }
you.chen35020192022-05-06 11:30:57 +08002592 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002593 {
2594 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2595 {
you.chen35020192022-05-06 11:30:57 +08002596 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2597 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2598 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002599 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2600 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002601 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002602 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002603 }
2604// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2605// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2606 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002607
you.chen35020192022-05-06 11:30:57 +08002608 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2609 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2610 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002611 break;
2612 }
2613 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2614 {
2615 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2616 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2617 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2618 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2619
2620 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2621 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2622 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2623 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2624 break;
2625 }
2626 case LYNQ_WIFI_AUTH_WPA3_PSK:
2627 {
2628 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2629 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002630 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002631 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2632
2633 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2634 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2635 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2636 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2637 break;
2638 }
2639 default:
you.chen35020192022-05-06 11:30:57 +08002640 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002641 RLOGE("auth type [%d] not support now\n", auth);
2642 return -1;
you.chen35020192022-05-06 11:30:57 +08002643 }
2644 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002645 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002646
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002648}
2649
you.chen35020192022-05-06 11:30:57 +08002650int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002651{
you.chen35020192022-05-06 11:30:57 +08002652 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002653 char lynq_auth_alg_str[MAX_RET] = {0};
2654 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002655 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002656 CHECK_IDX(idx, CTRL_AP);
2657
qs.xiong9fbf74e2023-03-28 13:38:22 +08002658 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2659 {
2660 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002661 return -1;
2662 }
2663
qs.xiong9fbf74e2023-03-28 13:38:22 +08002664 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2665 {
2666 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2667 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002668 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002669 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002670 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002671 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002672 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2673 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002674 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002675 *auth = LYNQ_WIFI_AUTH_WEP;
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
2679 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002680 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002681 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002682 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002683 }
you.chen35020192022-05-06 11:30:57 +08002684 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002685 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2686 {
2687 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2688 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002689 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002690 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002691 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002692 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2693 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002694 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002695 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002696 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002697 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002698 else
2699 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002700 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002701 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002702 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002703 }
you.chen35020192022-05-06 11:30:57 +08002704 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002705
2706 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2707 {
2708 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2709 return -1;
2710 }
2711
2712 if (memcmp(lynq_auth_str,"1",1) == 0 )
2713 {
2714 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2715 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002716 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002717 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2718 {
2719 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2720 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002721 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002722 }
2723 else
2724 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002725 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002726 *auth = -1;
2727 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002728
you.chen6c2dd9c2022-05-16 17:55:28 +08002729 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002730}
qs.xiong1af5daf2022-03-14 09:12:12 -04002731
you.chenb95401e2023-05-12 19:39:06 +08002732static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2733{
2734 char status[64];
you.chencba13492023-05-19 13:53:43 +08002735 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002736 curr_status_info curr_state;
2737
2738 CHECK_WPA_CTRL(CTRL_AP);
2739
2740 memset(status, 0, sizeof (status));
2741
2742 curr_state.ap = NULL;
2743 curr_state.state = status;
2744
2745 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002746 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002747 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002748 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002749 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002750 {
2751 return 0;
2752 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002753 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002754 {
2755 DO_REQUEST("RECONNECT");
2756 return inner_check_ap_connected(idx, retry_count+1);
2757 }
you.chencba13492023-05-19 13:53:43 +08002758 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002759 {
2760 printf("retry 10 time\n");
2761 return -1;
2762 }
2763 else
2764 {
you.chen6d247052023-06-01 16:39:54 +08002765 if (strcmp(status, STATE_DISCONNECTED) == 0)
2766 {
2767 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2768 DO_REQUEST(LYNQ_WIFI_CMD);
2769 }
you.chenb95401e2023-05-12 19:39:06 +08002770 return inner_check_ap_connected(idx, retry_count+1);
2771 }
2772 }
2773 return -1;
2774}
qs.xiong1af5daf2022-03-14 09:12:12 -04002775
qs.xiongf1b525b2022-03-31 00:58:23 -04002776int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002777{
qs.xiong86cced62024-09-23 14:08:38 +08002778 RLOGI("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002779 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002780
qs.xiongec8bbeb2023-11-20 15:51:45 +08002781 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002782 CHECK_WPA_CTRL(CTRL_AP);
2783
you.chen0df3e7e2023-05-10 15:56:26 +08002784 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002785 {
you.chen0df3e7e2023-05-10 15:56:26 +08002786 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002787 return -1;
2788 }
you.chen35020192022-05-06 11:30:57 +08002789
qs.xiongb37f8c42023-09-13 21:21:58 +08002790
you.chen35020192022-05-06 11:30:57 +08002791 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2792 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2793
you.chenc9928582023-04-24 15:39:37 +08002794 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2795 if (ret != 0)
2796 {
2797 RLOGE("lynq_wifi_ap_start excute script fail");
2798 return -1;
2799 }
2800
you.chenb95401e2023-05-12 19:39:06 +08002801 if (inner_check_ap_connected(idx, 0) != 0)
2802 {
2803 return -1;
2804 }
2805
you.chen0df3e7e2023-05-10 15:56:26 +08002806 check_tether_and_notify();
qs.xiong9d8f3102023-12-07 20:11:37 +08002807/*
qs.xiong44fac672023-08-29 16:15:55 +08002808 if (g_ap_tmp_watcher_pid == 0)
2809 {
2810 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2811 {
2812 g_ap_tmp_watcher_pid = 0;
2813 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2814 return -1;
2815 }
2816 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2817 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002818*/
qs.xiong86cced62024-09-23 14:08:38 +08002819 RLOGI("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002820 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002821}
2822
qs.xiongf1b525b2022-03-31 00:58:23 -04002823int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002824{
you.chen35020192022-05-06 11:30:57 +08002825 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002826}
2827
qs.xiongf1b525b2022-03-31 00:58:23 -04002828int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002829{
qs.xiong86cced62024-09-23 14:08:38 +08002830 RLOGI("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002831 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002832
you.chen35020192022-05-06 11:30:57 +08002833 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002834
you.chen35020192022-05-06 11:30:57 +08002835 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002836
you.chen35020192022-05-06 11:30:57 +08002837 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2838
2839 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2840
you.chenc9928582023-04-24 15:39:37 +08002841
2842 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2843 if (ret != 0)
2844 {
2845 RLOGE("lynq_wifi_ap_start excute script fail");
2846 return -1;
2847 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002848/*
qs.xiong44fac672023-08-29 16:15:55 +08002849 g_ap_tmp_watcher_stop_flag = 1;
2850 if (g_ap_tmp_watcher_pid != 0)
2851 pthread_join(g_ap_tmp_watcher_pid, NULL);
2852 g_ap_tmp_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08002853*/
qs.xiong86cced62024-09-23 14:08:38 +08002854 RLOGI("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002855 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002856}
qs.xiong1af5daf2022-03-14 09:12:12 -04002857
qs.xiongf1b525b2022-03-31 00:58:23 -04002858int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002859{
you.chen35020192022-05-06 11:30:57 +08002860 char lynq_disable_cmd[128] = {0};
2861 char lynq_select_cmd[128] = {0};
2862 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002863 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002864 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002865
you.chen35020192022-05-06 11:30:57 +08002866 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002867 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2868 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2869
2870 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2871 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2872 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002873
qs.xiong9fbf74e2023-03-28 13:38:22 +08002874 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002875}
2876
qs.xiongf1b525b2022-03-31 00:58:23 -04002877int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002878{
you.chen35020192022-05-06 11:30:57 +08002879 char lynq_disable_cmd[128] = {0};
2880 char lynq_select_cmd[128] = {0};
2881 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002882 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002883 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002884
you.chen35020192022-05-06 11:30:57 +08002885 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002886
you.chen35020192022-05-06 11:30:57 +08002887 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2888 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2889
2890 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2891 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2892 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002893
qs.xiong9fbf74e2023-03-28 13:38:22 +08002894 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002895}
qs.xiongf1b525b2022-03-31 00:58:23 -04002896
you.chen35020192022-05-06 11:30:57 +08002897int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002898{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002899 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002900 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002901 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002902 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 RLOGD("enter lynq_ap_password_set");
2904 if( password == NULL )
2905 {
2906 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002907 return -1;
2908 }
2909 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002910 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002911 if(pass_len < 8 || pass_len >= 64)
2912 {
2913 RLOGE("[lynq_ap_password_set]input password len not in rage");
2914 return -1;
you.chen35020192022-05-06 11:30:57 +08002915 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002916
you.chen35020192022-05-06 11:30:57 +08002917 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002918
qs.xiong9fbf74e2023-03-28 13:38:22 +08002919 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2920 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002921 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002922 return -1;
2923 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002924 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2925 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002926 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2927 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002928 }
2929
you.chen35020192022-05-06 11:30:57 +08002930 CHECK_WPA_CTRL(CTRL_AP);
2931
qs.xiong9fbf74e2023-03-28 13:38:22 +08002932 if (auth == LYNQ_WIFI_AUTH_WEP)
2933 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002934 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002935 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2936 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2937 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2938 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2939 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002940 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2941 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002942 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 +08002943 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2944 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2945 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002946 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2947 {
2948
qs.xiongc8d92a62023-03-29 17:36:14 +08002949 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 +08002950 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002951 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002952 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2953 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2954
2955 }
2956 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002957 {
2958 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002959 return -1;
2960 }
you.chen35020192022-05-06 11:30:57 +08002961
you.chen35020192022-05-06 11:30:57 +08002962 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002963
qs.xiong9fbf74e2023-03-28 13:38:22 +08002964 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002965}
2966
you.chen35020192022-05-06 11:30:57 +08002967int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002968{
you.chen35020192022-05-06 11:30:57 +08002969 FILE * fp;
2970 int len, ret;
2971 int count, index;
2972 char *split_lines[128] = {0};
2973 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002974 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002975
you.chen35020192022-05-06 11:30:57 +08002976 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002977
you.chen35020192022-05-06 11:30:57 +08002978 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2979// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002980 if (NULL == fp)
2981 {
2982 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002983 return -1;
2984 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002985
you.chen35020192022-05-06 11:30:57 +08002986 buff = alloca(MAX_RET);
2987 fseek(fp, 0, SEEK_SET);
2988 len = fread(buff, 1, MAX_RET, fp);
2989 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002990
qs.xiong9fbf74e2023-03-28 13:38:22 +08002991 for(index=0; index < len; index ++)
2992 {
2993 if (memcmp(buff + index, "network={", 9) != 0)
2994 {
you.chen35020192022-05-06 11:30:57 +08002995 continue;
2996 }
2997 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002998 for (; index < len; index ++ )
2999 {
3000 if (buff[index] != '}')
3001 {
you.chen35020192022-05-06 11:30:57 +08003002 continue;
3003 }
3004 buff[index] = '\0';
3005 break;
3006 }
3007 len = buff + index - p;
3008 }
3009
3010 count = lynq_split(p, len, '\n', split_lines);
3011
3012 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003013 for(index=0; index < count; index++)
3014 {
you.chen35020192022-05-06 11:30:57 +08003015 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003016 if (p != NULL)
3017 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003018 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 if (*p == '\"')
3020 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003021 p++;
3022 }
you.chen35020192022-05-06 11:30:57 +08003023 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003024 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3025 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003026 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003027 if (*p == '\"')
3028 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003029 p++;
3030 }
3031 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003032 else
3033 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003034 continue;
you.chen35020192022-05-06 11:30:57 +08003035 }
3036
3037 strcpy(password, p);
3038
qs.xiong9fbf74e2023-03-28 13:38:22 +08003039 while(*password != '\0')
3040 {
3041 if (*password == '\"')
3042 {
you.chen35020192022-05-06 11:30:57 +08003043 *password = '\0';
3044 break;
3045 }
3046 password++;
3047 }
3048 ret = 0;
3049 break;
3050 } //end for(index=0; index < count; index++)
3051
3052 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003053}
3054
you.chen35020192022-05-06 11:30:57 +08003055static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3056 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003057 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003058
qs.xiong9fbf74e2023-03-28 13:38:22 +08003059 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3060 {
you.chen35020192022-05-06 11:30:57 +08003061 return -1;
3062 }
3063
3064 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003065
qs.xiong9fbf74e2023-03-28 13:38:22 +08003066 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3067 {
3068 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003069 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003070 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003071 {
you.chena6cd55a2022-05-08 12:20:18 +08003072 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003073 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003074 }
you.chen70f377f2023-04-14 18:17:09 +08003075 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3076 {
3077 return 0;
3078 }
you.chena6cd55a2022-05-08 12:20:18 +08003079 }
3080 }
you.chen70f377f2023-04-14 18:17:09 +08003081 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3082 {
3083 return 0;
3084 }
3085
qs.xiong9fbf74e2023-03-28 13:38:22 +08003086 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3087 {
you.chen70f377f2023-04-14 18:17:09 +08003088 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003089 return -1;
3090 }
3091 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3092 {
3093
you.chen70f377f2023-04-14 18:17:09 +08003094 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3095 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003096 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3097 {
3098
3099 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003100 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003101 }else
3102 {
you.chen70f377f2023-04-14 18:17:09 +08003103 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003104 *auth = -1;
3105 return -1;
3106 }
you.chen35020192022-05-06 11:30:57 +08003107 return 0;
3108}
3109
3110int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003111{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003112 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003113 int pass_len, net_no, count, index;
3114 char lynq_tmp_cmd[300]={0};
3115 int net_no_list[128];
3116 lynq_wifi_auth_s net_auth;
3117 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003118 if(pass_len < 8 || pass_len >= 64)
3119 {
3120 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003121 return -1;
3122 }
3123
3124 CHECK_IDX(idx, CTRL_STA);
3125
3126 net_no = -1;
3127 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3128
qs.xiong9fbf74e2023-03-28 13:38:22 +08003129 for (index=0; index < count; index++)
3130 {
you.chen35020192022-05-06 11:30:57 +08003131 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003132 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3133 {
you.chen35020192022-05-06 11:30:57 +08003134 net_no = net_no_list[index];
3135 break;
3136 }
3137 }
3138
qs.xiong9fbf74e2023-03-28 13:38:22 +08003139 if (net_no < 0)
3140 {
you.chen35020192022-05-06 11:30:57 +08003141 return -1;
3142 }
3143
3144 CHECK_WPA_CTRL(CTRL_STA);
3145
3146 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3147
3148 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3149 DO_OK_FAIL_REQUEST(cmd_save_config);
3150
3151 return 0;
3152}
3153
qs.xiongb5dab082023-10-13 14:43:41 +08003154/**
3155* buff data
3156* buff_len size of buff
3157* idx sta
3158* *ap ap info for find ssid && password
3159* password return password
3160*
3161*/
3162static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3163
3164 int ret, network_len, i, ssid_len,curr_auth;
3165 int count, index,org_index;
3166 char *split_lines[128] = {0};
3167 char *p, *ssid, *ssid_end_flag,*ptr;
3168 char tmp_ssid[128]={0};
3169 char tmp_auth[24]={0};
3170
3171 org_index = 0;
3172 network_len = 0;
3173 p = NULL;
3174
3175 CHECK_IDX(idx, CTRL_STA);
3176
3177 while(1){
3178 network_len = 0;
3179 p == NULL;
3180 for(; org_index < buff_len; org_index ++)
3181 {
3182 for(; org_index < buff_len; org_index ++)
3183 {
3184 if (memcmp(buff + org_index, "network={", 9) != 0)
3185 {
3186 continue;
3187 }
3188 p = buff + org_index + 9;
3189
3190 for (; org_index < buff_len; org_index ++ )
3191 {
3192 if (buff[org_index] != '}')
3193 {
3194 continue;
3195 }
3196 buff[org_index] = '\0';
3197 break;
3198 }
3199 network_len = buff + org_index - p;
3200 break;
3201 }
3202
3203 if (p == NULL)
3204 {
3205 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3206 return -1;
3207 }
3208
3209 ssid = strstr(p, "ssid=");
3210 if (ssid != NULL) {
3211 ssid += strlen("ssid=");
3212 if (ssid[0] == '\"')
3213 {
3214 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3215 {
qs.xiongb5dab082023-10-13 14:43:41 +08003216 break;
3217 }
qs.xiongb5dab082023-10-13 14:43:41 +08003218 }
3219 else
3220 {
3221 ssid_end_flag = strstr(ssid, "\n");
3222 if (ssid_end_flag != NULL)
3223 {
3224 ssid_len = (ssid_end_flag - ssid) / 2;
3225 for(i=0; i<ssid_len; i++)
3226 {
3227 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3228 }
3229 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3230 {
qs.xiongb5dab082023-10-13 14:43:41 +08003231 break;
3232 }
3233 }
3234 }
3235 }
3236
3237 }
3238
3239 if (org_index >= buff_len || NULL == p || network_len <= 0)
3240 {
3241
3242 if (buff != NULL)
3243 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3244 return -1;
3245 }
3246
3247 count = lynq_split(p, network_len, '\n', split_lines);
3248 ret = -1;
3249 for( index=0; index < count; index++ )
3250 {
3251 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003252 if(p != NULL)
3253 {
3254 p += 9;
3255 if(memcmp(p,"SAE",3) == 0)
3256 {
3257 curr_auth = 5;
3258 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3259 {
3260 curr_auth = 4;
3261 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3262 {
3263 curr_auth = 3;
3264 }else if(memcmp(p,"NONE",4) == 0 )
3265 {
3266 curr_auth = 0;
3267 }else{
3268 curr_auth = 1;
3269 }
qs.xiongb5dab082023-10-13 14:43:41 +08003270 if( curr_auth < 1 || curr_auth > 6)
3271 {
3272 ret = -1;
3273 }
3274 break;
3275 }
3276 }
3277 if( curr_auth == 0)
3278 {
3279 return 0;
3280 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3281 {
3282 for(index=0; index < count; index++)
3283 {
3284 /*get psw info*/
3285
3286 p = strstr(split_lines[index], "psk=");
3287 if (p != NULL)
3288 {
3289 p += 4;
3290 if (*p == '\"')
3291 {
3292 p++;
3293 }
3294 }
3295 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3296 {
3297 p += 9;
3298 if (*p == '\"')
3299 {
3300 p++;
3301 }
3302 }
3303 else
3304 {
3305 continue;
3306 }
3307
3308 if (*p == '\"')
3309 p++;
3310 strncpy(password, p, 64);
3311 p = password;
3312 while(password - p < 64 && *password != '\0')
3313 {
3314 if (*password == '\"')
3315 {
3316 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003317 ret = 0;
3318 break;
3319 }
3320 password++;
3321 }
3322 break;
3323 }
3324 break;
3325 }
3326 }
3327
3328 return ret;
3329}
3330
3331
3332
you.chen35020192022-05-06 11:30:57 +08003333int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3334
3335 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003336 int len, ret;
3337 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003338 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003339
qs.xiongb5dab082023-10-13 14:43:41 +08003340 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003341 CHECK_IDX(idx, CTRL_STA);
3342
qs.xiong9fbf74e2023-03-28 13:38:22 +08003343 if (NULL == password)
3344 {
3345 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003346 return -1;
3347 }
3348
you.chen35020192022-05-06 11:30:57 +08003349 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003350 if (NULL == fp)
3351 {
3352 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003353 return -1;
3354 }
3355
you.chen6d247052023-06-01 16:39:54 +08003356 fseek(fp, 0, SEEK_END);
3357 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003358 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003359
qs.xiongb5dab082023-10-13 14:43:41 +08003360 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003361 {
3362 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3363 return -1;
3364 }
3365
you.chen35020192022-05-06 11:30:57 +08003366 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003367 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003368 fclose(fp);
3369
qs.xiongb5dab082023-10-13 14:43:41 +08003370
3371 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3372
3373 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003374 {
qs.xiongb04dc852024-03-27 21:55:32 +08003375 //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 +08003376 free(info_buff);
3377 return 0;
you.chen35020192022-05-06 11:30:57 +08003378 }
qs.xiongb5dab082023-10-13 14:43:41 +08003379 else{
3380 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003381 return -1;
3382 }
3383
you.chen35020192022-05-06 11:30:57 +08003384}
3385
qs.xiongb5dab082023-10-13 14:43:41 +08003386
you.chen35020192022-05-06 11:30:57 +08003387static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3388{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003389 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003390
qs.xiong9fbf74e2023-03-28 13:38:22 +08003391 if (sta_ssid == NULL)
3392 {
3393 RLOGE("sta_ssid is null\n");
3394 return -1;
you.chen35020192022-05-06 11:30:57 +08003395 }
3396
qs.xiong9fbf74e2023-03-28 13:38:22 +08003397 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003398
3399 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3400
3401 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3402// DO_OK_FAIL_REQUEST(cmd_save_config);
3403
qs.xiong9fbf74e2023-03-28 13:38:22 +08003404 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003405
3406}
3407
you.chen35020192022-05-06 11:30:57 +08003408static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003409{
you.chen35020192022-05-06 11:30:57 +08003410 char lynq_disable_cmd[128]={0};
3411 char lynq_select_cmd[128]={0};
3412
3413 CHECK_WPA_CTRL(CTRL_STA);
3414
qs.xiong9fbf74e2023-03-28 13:38:22 +08003415 if (save != 0)
3416 {
you.chenc29444e2022-06-07 18:01:16 +08003417 if (start_flag != 0)
3418 {
3419 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3420 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3421 }
3422 else
3423 {
3424 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3425 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3426 }
you.chen35020192022-05-06 11:30:57 +08003427 DO_OK_FAIL_REQUEST(cmd_save_config);
3428 }
3429
qs.xiong9fbf74e2023-03-28 13:38:22 +08003430 if (start_flag == 0)
3431 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003432 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003433 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3434 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003435 else
3436 {
you.chen35020192022-05-06 11:30:57 +08003437 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3438 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3439 }
3440
3441 return 0;
3442}
3443
3444int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3445{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003446 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003447 CHECK_IDX(idx, CTRL_STA);
3448
you.chen6c2dd9c2022-05-16 17:55:28 +08003449 curr_status_info curr_state;
3450 ap_info_s ap_info;
3451 curr_state.ap = &ap_info;
3452 curr_state.state = NULL;
3453
qs.xiong9fbf74e2023-03-28 13:38:22 +08003454 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3455 {
you.chend2fef3f2023-02-13 10:50:35 +08003456 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003457 return 0;
3458 }
3459
3460 return -1;
you.chen35020192022-05-06 11:30:57 +08003461}
3462
3463int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3464{
qs.xiong5d716d22023-09-20 20:08:39 +08003465 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003466 scan_info_s *scan_list = NULL;
3467 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003468 int scan_len=0;
3469 int save_len=0;
3470 int best_index = -1;
3471 int best_scan_index = -1;
3472 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003473 int i, j, ret;
3474
3475 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003476
3477 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003478 if (info == NULL)
3479 {
you.chen35020192022-05-06 11:30:57 +08003480 return -1;
3481 }
3482
3483 curr_status_info curr_state;
3484 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003485 char status[64];
you.chen35020192022-05-06 11:30:57 +08003486
you.chen9ac66392022-08-06 17:01:16 +08003487 memset(&ap_info, 0, sizeof (ap_info));
3488 memset(status, 0, sizeof (status));
3489
3490 curr_state.ap = &ap_info;
3491 curr_state.state = status;
3492
qs.xiong9fbf74e2023-03-28 13:38:22 +08003493 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3494 {
you.chen35020192022-05-06 11:30:57 +08003495 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003496 if (strcmp(status, STATE_COMPLETED) == 0)
3497 {
3498 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003499 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003500 }
3501 else
3502 {
3503 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003504 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003505 }
you.chen593621d2023-04-27 17:52:44 +08003506 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003507 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003508 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003509 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003510 return 0;
3511 }
3512
you.chen9ac66392022-08-06 17:01:16 +08003513 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003514 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003515 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3516 {
you.chen9ac66392022-08-06 17:01:16 +08003517 if (NULL != scan_list)
3518 {
3519 free(scan_list);
3520 }
you.chen35020192022-05-06 11:30:57 +08003521 return -1;
3522 }
3523
qs.xiong9fbf74e2023-03-28 13:38:22 +08003524 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3525 {
you.chen9ac66392022-08-06 17:01:16 +08003526 if (NULL != scan_list)
3527 {
3528 free(scan_list);
3529 }
3530 if (NULL != save_list)
3531 {
3532 free(save_list);
3533 }
you.chen35020192022-05-06 11:30:57 +08003534 return -1;
3535 }
3536
qs.xiong9fbf74e2023-03-28 13:38:22 +08003537 for (i=0; i < save_len; i++)
3538 {
3539 for (j=0; j < scan_len; j++)
3540 {
you.chen35020192022-05-06 11:30:57 +08003541 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003542 && save_list[i].base_info.auth == scan_list[j].auth)
3543 {
3544 if (best_rssi == 0)
3545 {
you.chen9ac66392022-08-06 17:01:16 +08003546 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003547 best_rssi = scan_list[j].rssi;
3548 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003549 else if (best_rssi > scan_list[j].rssi)
3550 {
you.chen35020192022-05-06 11:30:57 +08003551 best_index = i;
3552 best_scan_index = j;
3553 best_rssi = scan_list[j].rssi;
3554 }
you.chend2fef3f2023-02-13 10:50:35 +08003555 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 +08003556 break;
3557 }
3558 }
3559 }
3560
qs.xiong9fbf74e2023-03-28 13:38:22 +08003561 if (best_index >= 0)
3562 {
you.chen35020192022-05-06 11:30:57 +08003563 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003564 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 +08003565 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003566 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003567 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003568 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003569 }
3570
you.chen9ac66392022-08-06 17:01:16 +08003571 if (NULL != scan_list)
3572 {
3573 free(scan_list);
3574 }
3575 if (NULL != save_list)
3576 {
3577 free(save_list);
3578 }
3579
qs.xiong5d716d22023-09-20 20:08:39 +08003580 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003581 return ret;
you.chen35020192022-05-06 11:30:57 +08003582}
3583
3584static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3585{
qs.xiongc8d92a62023-03-29 17:36:14 +08003586 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003587 char lynq_ket_mgmt_cmd[64]={0};
3588 char lynq_pairwise_cmd[64]={0};
3589 char lynq_psk_cmd[64]={0};
3590
3591 CHECK_WPA_CTRL(CTRL_STA);
3592
qs.xiong9fbf74e2023-03-28 13:38:22 +08003593 switch(auth)
3594 {
3595 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003596 {
3597 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003598
you.chen35020192022-05-06 11:30:57 +08003599 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003600// DO_OK_FAIL_REQUEST(cmd_save_config);
3601 break;
3602 }
3603 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003604 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003605 {
3606 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3607 {
you.chen35020192022-05-06 11:30:57 +08003608 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3609 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003610 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3611 {
you.chena6cd55a2022-05-08 12:20:18 +08003612 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003613 }
3614 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3615 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003616
you.chen35020192022-05-06 11:30:57 +08003617 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3618 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3619 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003620
qs.xiong9fbf74e2023-03-28 13:38:22 +08003621 if (password != NULL)
3622 {
you.chen35020192022-05-06 11:30:57 +08003623 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3624 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003625 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003626 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003627
you.chen35020192022-05-06 11:30:57 +08003628// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003629 break;
3630 }
3631 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3632 {
qs.xiong3e506812023-04-06 11:08:48 +08003633 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003634 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003635 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3636 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3637
qs.xiong3e506812023-04-06 11:08:48 +08003638 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003639 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3640 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3641 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3642
3643 break;
3644 }
3645 case LYNQ_WIFI_AUTH_WPA3_PSK:
3646 {
qs.xiong3e506812023-04-06 11:08:48 +08003647 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003648 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003649 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3650 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3651
qs.xiongb37f8c42023-09-13 21:21:58 +08003652 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003653 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3654 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3655 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3656
3657 break;
3658 }
3659 default:
3660 return -1;
you.chen35020192022-05-06 11:30:57 +08003661 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003662
qs.xiong9fbf74e2023-03-28 13:38:22 +08003663 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003664}
qs.xiong7a105ce2022-03-02 09:43:11 -05003665
you.chen35020192022-05-06 11:30:57 +08003666static int inner_get_curr_net_no(int interface) {
3667 curr_status_info curr_state;
3668 curr_state.ap = NULL;
3669 curr_state.state = NULL;
3670
qs.xiong9fbf74e2023-03-28 13:38:22 +08003671 if (0 != inner_get_status_info(interface, &curr_state))
3672 {
you.chen35020192022-05-06 11:30:57 +08003673 return -1;
3674 }
3675
3676 return curr_state.net_no;
3677}
3678
3679int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003680{
you.chen35020192022-05-06 11:30:57 +08003681 int net_no;
3682 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003683
you.chen35020192022-05-06 11:30:57 +08003684 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003685
qs.xiong9fbf74e2023-03-28 13:38:22 +08003686 if (net_no < 0)
3687 {
you.chen35020192022-05-06 11:30:57 +08003688 return -1;
3689 }
3690
3691 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003692}
3693
qs.xiongfb992712024-03-28 16:32:50 +08003694/*
3695*
3696* ap_type: 0 means unhide ap 1 means hide ap
3697*/
3698int 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 -05003699{
qs.xionga8cbe222024-04-15 16:42:13 +08003700 RLOGD("enter %s %d",__func__,__LINE__);
3701 pthread_mutex_lock(&s_func_run_mutex);
3702 int count, net_no, index,res;
you.chen35020192022-05-06 11:30:57 +08003703 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003704 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003705 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003706 curr_status_info curr_state;
3707 ap_info_s ap_info;
3708 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003709
qs.xionga8cbe222024-04-15 16:42:13 +08003710 res = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003711 if (ssid == NULL || *ssid == '\0')
3712 {
3713 RLOGE("bad ssid\n");
qs.xionga8cbe222024-04-15 16:42:13 +08003714 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003715 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003716
qs.xiong9fbf74e2023-03-28 13:38:22 +08003717 if (LYNQ_WIFI_AUTH_OPEN != auth)
3718 {
3719 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003720 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003721 RLOGE("bad password\n");
qs.xionga8cbe222024-04-15 16:42:13 +08003722 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003723 }
3724 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003725
you.chen70f377f2023-04-14 18:17:09 +08003726
3727 pthread_mutex_lock(&s_global_check_mutex);
3728 if (s_sta_status != INNER_STA_STATUS_INIT)
3729 {
3730 s_sta_status = INNER_STA_STATUS_CANCEL;
3731 pthread_cond_signal(&s_global_check_cond);
3732 }
3733 pthread_mutex_unlock(&s_global_check_mutex);
3734
you.chen35020192022-05-06 11:30:57 +08003735 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003736 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003737
3738 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003739 memset(&ap_info, 0, sizeof (ap_info));
3740 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003741
you.chen70f377f2023-04-14 18:17:09 +08003742 curr_state.ap = &ap_info;
3743 curr_state.state = status;
3744
3745 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003746 {
you.chen70f377f2023-04-14 18:17:09 +08003747 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3748 {
3749 net_no = curr_state.net_no;
3750 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3751 && strcmp(ap_info.psw, psw) == 0)
3752 {
qs.xionga8cbe222024-04-15 16:42:13 +08003753 RLOGD("already connected %s %d\n",__func__,__LINE__);
you.chen70f377f2023-04-14 18:17:09 +08003754 pthread_mutex_lock(&s_global_check_mutex);
3755 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003756 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003757 pthread_cond_signal(&s_global_check_cond);
3758 pthread_mutex_unlock(&s_global_check_mutex);
qs.xionga8cbe222024-04-15 16:42:13 +08003759 res = 0;
3760 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003761 }
you.chen35020192022-05-06 11:30:57 +08003762 }
3763 }
3764
you.chen70f377f2023-04-14 18:17:09 +08003765 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003766 {
you.chen70f377f2023-04-14 18:17:09 +08003767 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3768
3769 for (index=0; index < count; index++)
3770 {
3771 net_auth = -1;
3772 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3773 {
3774 net_no = net_no_list[index];
3775 break;
3776 }
you.chen35020192022-05-06 11:30:57 +08003777 }
3778
you.chen70f377f2023-04-14 18:17:09 +08003779 if (net_no < 0)
3780 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003781 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3782 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3783 {
3784 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3785 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3786 DO_OK_FAIL_REQUEST(rm_net_cmd);
3787 }
you.chen70f377f2023-04-14 18:17:09 +08003788 net_no = lynq_add_network(CTRL_STA);
3789 if (net_no == -1)
3790 {
qs.xionga8cbe222024-04-15 16:42:13 +08003791 RLOGE("add network id failed %s %d\n",__func__,__LINE__);
3792 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003793 }
3794
3795 RLOGD("net no is %d\n", net_no);
3796 if (0 != inner_set_sta_ssid(net_no, ssid))
3797 {
qs.xionga8cbe222024-04-15 16:42:13 +08003798 RLOGE("set network %d ssid failed %s %d\n",net_no,__func__,__LINE__);
3799 goto CONNECT_END;
you.chen70f377f2023-04-14 18:17:09 +08003800 }
you.chen35020192022-05-06 11:30:57 +08003801 }
3802 }
3803
qs.xiong9fbf74e2023-03-28 13:38:22 +08003804 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3805 {
qs.xionga8cbe222024-04-15 16:42:13 +08003806 RLOGE("set inner_set_sta_auth_psw %d ssid failed %s %d\n",net_no,__func__,__LINE__);
3807 goto CONNECT_END;
you.chen35020192022-05-06 11:30:57 +08003808 }
3809
qs.xiongfb992712024-03-28 16:32:50 +08003810//begain 20240328 change for support sta connect hide ap by qs.xiong API-1569
3811 if( ap_type == 1 )
3812 {
3813 char scan_hide_ap_cmd[64];
3814 sprintf(scan_hide_ap_cmd,"SET_NETWORK %d scan_ssid 1",net_no);
3815 RLOGD("current conenct ap is hide ap cmd:%s\n",scan_hide_ap_cmd);
3816 DO_OK_FAIL_REQUEST(scan_hide_ap_cmd);
3817 }
3818//end 20240328 change for support sta connect hide ap by qs.xiong API-1569
you.chen70f377f2023-04-14 18:17:09 +08003819 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003820 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003821 usleep(200*1000);
3822
qs.xiong09560402023-10-27 21:58:55 +08003823 pthread_mutex_lock(&s_global_check_mutex);
3824 lynq_sta_removeElement(net_no);
3825 pthread_mutex_unlock(&s_global_check_mutex);
3826
qs.xionga8cbe222024-04-15 16:42:13 +08003827 res = inner_sta_start_stop(net_no, 1, 1);
you.chen70f377f2023-04-14 18:17:09 +08003828
3829 pthread_mutex_lock(&s_global_check_mutex);
3830 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003831 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003832 strcpy(s_sta_current_connecting_ssid, ssid);
3833 struct timeval now;
3834 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003835 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003836 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3837 pthread_cond_signal(&s_global_check_cond);
3838 pthread_mutex_unlock(&s_global_check_mutex);
qs.xionga8cbe222024-04-15 16:42:13 +08003839
3840CONNECT_END:
3841 pthread_mutex_unlock(&s_func_run_mutex);
3842 RLOGD("end %s %d and return is %d \n",__func__,__LINE__,res);
3843 return res;
3844
qs.xiong7a105ce2022-03-02 09:43:11 -05003845}
3846
qs.xiongfb992712024-03-28 16:32:50 +08003847int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
3848{
3849 return lynq_wifi_sta_connect_common(idx,ssid, auth, psw,timeout,0);
3850}
3851
3852int lynq_wifi_sta_connect_hide(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
3853{
3854 return lynq_wifi_sta_connect_common(idx,ssid, auth, psw,timeout,1);
3855}
3856
you.chenb95401e2023-05-12 19:39:06 +08003857int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3858{
3859 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3860}
3861
you.chen35020192022-05-06 11:30:57 +08003862int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003863{
you.chen35020192022-05-06 11:30:57 +08003864 ap_info_s ap;
3865 curr_status_info curr_state;
3866 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003867
qs.xiong9fbf74e2023-03-28 13:38:22 +08003868 if (ssid == NULL || *ssid == '\0')
3869 {
3870 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003871 return -1;
3872 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003873
you.chen35020192022-05-06 11:30:57 +08003874 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003875
you.chen35020192022-05-06 11:30:57 +08003876 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003877 curr_state.state = NULL;
3878
qs.xiong9fbf74e2023-03-28 13:38:22 +08003879 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3880 {
you.chen35020192022-05-06 11:30:57 +08003881 return 0;
3882 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003883
qs.xiong9fbf74e2023-03-28 13:38:22 +08003884 if (strcmp(ap.ap_ssid, ssid) != 0)
3885 {
you.chen35020192022-05-06 11:30:57 +08003886 return 0;
3887 }
3888
you.chen70f377f2023-04-14 18:17:09 +08003889 pthread_mutex_lock(&s_global_check_mutex);
3890 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3891 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003892 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003893}
qs.xiong97fa59b2022-04-07 05:41:29 -04003894
qs.xiongc93bf2b2023-08-25 10:22:08 +08003895int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3896{
qs.xiong09560402023-10-27 21:58:55 +08003897 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003898 ap_info_s ap;
3899 curr_status_info curr_state;
3900 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003901 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003902
3903 if (ssid == NULL || *ssid == '\0')
3904 {
3905 RLOGE("input ssid is NULL\n");
3906 return -1;
3907 }
3908
3909 CHECK_IDX(idx, CTRL_STA);
3910
3911
3912 curr_state.ap = &ap;
3913 curr_state.state = NULL;
3914
3915 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3916 {
3917 return 0;
3918 }
3919
3920 if (strcmp(ap.ap_ssid, ssid) != 0)
3921 {
3922 return 0;
3923 }
3924
3925 pthread_mutex_lock(&s_global_check_mutex);
3926 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003927 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3928 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3929 {
3930 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);
3931 if( g_history_disconnect_net[i] == curr_state.net_no)
3932 {
3933 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3934 check_history_idx_flag = 1;
3935 break;
3936 }
3937 }
3938 if ( check_history_idx_flag == 0)
3939 {
3940 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__);
3941 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3942 g_history_disconnect_valid_num++;
3943 }
3944 RLOGD("%s %d",__func__,__LINE__);
3945 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003946 pthread_mutex_unlock(&s_global_check_mutex);
3947 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3948
3949}
3950
3951
you.chena6cd55a2022-05-08 12:20:18 +08003952int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3953{
qs.xiong86cced62024-09-23 14:08:38 +08003954 RLOGI("enter %s %d func",__func__,__LINE__);
qs.xiongad2f89d2023-01-18 13:17:41 +08003955 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3956 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003957
you.chen35020192022-05-06 11:30:57 +08003958 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003959 CHECK_WPA_CTRL(CTRL_STA);
3960
you.chenc9928582023-04-24 15:39:37 +08003961 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3962 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003963 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003964 RLOGE("lynq_wifi_sta_start excute script fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08003965 return -1;
3966 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003967
qs.xiongad2f89d2023-01-18 13:17:41 +08003968 system(lynq_enable_sta_cmd);
3969 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003970 pthread_mutex_lock(&s_global_check_mutex);
3971 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003972 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003973 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08003974 RLOGI("end %s %d func",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003975 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003976}
3977
you.chen6d247052023-06-01 16:39:54 +08003978static int inner_get_status_info_state (int interface, char *state) {
3979 curr_status_info curr_state;
3980 curr_state.ap = NULL;
3981 curr_state.state = state;
3982 return inner_get_status_info(interface, &curr_state);
3983}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003984
3985int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3986{
3987
qs.xiongb37f8c42023-09-13 21:21:58 +08003988 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3989 int tmp_open_idx[128];
3990 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003991
qs.xiongb37f8c42023-09-13 21:21:58 +08003992 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003993 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003994 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3995 pthread_mutex_unlock(&s_global_check_mutex);
3996 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003997 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003998 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003999 }
4000
qs.xiongb37f8c42023-09-13 21:21:58 +08004001 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08004002 return 0;
4003}
4004
4005
you.chen35020192022-05-06 11:30:57 +08004006int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04004007{
qs.xiong86cced62024-09-23 14:08:38 +08004008 RLOGI("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004009 int i=0;
4010 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08004011
you.chena6cd55a2022-05-08 12:20:18 +08004012 CHECK_IDX(idx, CTRL_STA);
4013 CHECK_WPA_CTRL(CTRL_STA);
4014
you.chen6d247052023-06-01 16:39:54 +08004015 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08004016 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08004017
4018 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
4019 if (ret != 0)
4020 {
qs.xiong1b5e3472023-11-27 17:42:20 +08004021 RLOGE("lynq_wifi_sta_stop excute script fail %s %d",__func__,__LINE__);
you.chenc9928582023-04-24 15:39:37 +08004022 return -1;
4023 }
4024
you.chen6d247052023-06-01 16:39:54 +08004025 for (i=0; i < 30; i++) // to check if sta is realy stoped
4026 {
4027 if (inner_get_status_info_state(idx, state) != 0)
4028 {
4029 break;
4030 }
4031
4032 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
4033 {
4034 break;
4035 }
qs.xiong1b5e3472023-11-27 17:42:20 +08004036 RLOGD("lynq_wifi_sta_stop curr state %s %s %d", state,__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004037 usleep(SLEEP_TIME_ON_IDLE);
4038 }
qs.xiongb37f8c42023-09-13 21:21:58 +08004039 pthread_mutex_lock(&s_global_check_mutex);
4040 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
4041 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08004042 RLOGI("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08004043 return 0;
4044// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04004045}
qs.xiongfcc914b2023-07-06 21:16:20 +08004046int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
4047{
4048 char LYNQ_DISABLE_CMD[128]={0};
4049 CHECK_IDX(idx, CTRL_STA);
4050 CHECK_WPA_CTRL(CTRL_STA);
4051 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
4052 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
4053 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
4054 return 0;
4055}
qs.xiong7a105ce2022-03-02 09:43:11 -05004056
you.chen35020192022-05-06 11:30:57 +08004057//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
4058// int i, count;
4059// char *p;
4060// const char * FLAG_SSID = "ssid=";
4061// const char * FLAG_SBSID = "bssid=";
4062// const char * FLAG_KEY_MGMT = "key_mgmt=";
4063// const char * FLAG_FREQ = "freq=";
4064// char lynq_sta_cmd[MAX_CMD];
4065// char *split_lines[128] = {0};
4066
4067// CHECK_WPA_CTRL(CTRL_AP);
4068
4069// sprintf(lynq_sta_cmd, "STA %s", bssid);
4070
4071// DO_REQUEST(lynq_sta_cmd);
4072
4073// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4074
4075// for(i=0; i < count; i++) {
4076// p = strstr(split_lines[i], FLAG_SSID);
4077// if (p != NULL) {
4078// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
4079// continue;
4080// }
4081// }
4082
4083// lynq_get_interface_ip(idx, ap->ap_ip);
4084// lynq_ap_password_set(idx, ap->psw);
4085
4086// return 0;
4087//}
4088
4089static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4090 curr_status_info curr_state;
4091 curr_state.ap = ap;
4092 curr_state.state = NULL;
4093 return inner_get_status_info(interface, &curr_state);
4094}
4095
4096int 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 -04004097{
qs.xiong5071c802023-09-06 14:04:15 +08004098 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004099 int index, line_count;
4100 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004101 const char *lynq_first_sta_cmd = "STA-FIRST";
4102 char lynq_next_sta_cmd[MAX_CMD];
4103 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004104 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004105
you.chen35020192022-05-06 11:30:57 +08004106 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004107
you.chen35020192022-05-06 11:30:57 +08004108 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004109
you.chenb95401e2023-05-12 19:39:06 +08004110 // ap_info_s * tmp_ap;
4111 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004112 if (ap == NULL || list == NULL || len == NULL)
4113 {
4114 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004115 return -1;
4116 }
4117
you.chenb95401e2023-05-12 19:39:06 +08004118 // ap = &tmp_ap;
4119 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004120 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004121 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004122
you.chenb95401e2023-05-12 19:39:06 +08004123 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004124 {
you.chenb95401e2023-05-12 19:39:06 +08004125 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004126 return -1;
4127 }
4128
4129 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4130 lynq_ap_password_get(idx, (*ap)->psw);
4131
you.chen35020192022-05-06 11:30:57 +08004132 DO_REQUEST(lynq_first_sta_cmd);
4133
4134 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004135 while (reply_len > 0)
4136 {
4137 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004138 {
you.chen35020192022-05-06 11:30:57 +08004139 break;
4140 }
4141 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4142 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4143 strcpy(bssid[index], split_lines[0]);
4144 index++;
4145 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4146 reply_len = MAX_RET;
4147 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004148 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 +08004149 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004150 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004151 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004152 break;
4153 }
4154 }
4155
4156 *len = index;
4157
4158 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004159 for (index=0; index < *len; index++)
4160 {
you.chend2fef3f2023-02-13 10:50:35 +08004161 dev_info = &(*list)[index];
4162 memset(dev_info, 0, sizeof(device_info_s));
4163 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4164 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4165 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4166 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004167 free(bssid[index]);
4168 }
qs.xiong5071c802023-09-06 14:04:15 +08004169 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004170 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004171}
4172
you.chen35020192022-05-06 11:30:57 +08004173int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004174{
you.chen35020192022-05-06 11:30:57 +08004175 int i, count, index, count_words;
4176 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4177 char *split_lines[128] = {0};
4178 char *split_words[128] = {0};
4179 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004180
qs.xiong9fbf74e2023-03-28 13:38:22 +08004181 if (list == NULL || len == NULL)
4182 {
you.chen35020192022-05-06 11:30:57 +08004183 return -1;
4184 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004185
you.chen9ac66392022-08-06 17:01:16 +08004186 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4187 {
4188 usleep(100 * 1000);
4189 }
4190
you.chen35020192022-05-06 11:30:57 +08004191 CHECK_IDX(idx, CTRL_STA);
4192
4193 CHECK_WPA_CTRL(CTRL_STA);
4194
4195 DO_REQUEST(lynq_scan_result_cmd);
4196
4197 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4198 *len = count - 1;
4199 *list = malloc(sizeof (scan_info_s) * *len);
4200
4201 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004202 for (index=0; index <count_words; index++)
4203 {
4204 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004205 }
4206
qs.xiong9fbf74e2023-03-28 13:38:22 +08004207 for(index = 1;index < count; index++)
4208 {
4209 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004210 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004211 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4212 if (count_words < 4)
4213 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004214 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004215 //bssid / frequency / signal level / flags / ssid
4216 p = (*list) + index - 1;
4217 strcpy(p->mac, split_words[0]);
4218 p->band = convert_band_from_freq(atoi(split_words[1]));
4219 p->rssi = -1 * atoi( split_words[2]);
4220 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004221 if (count_words == 4) // ssid hided
4222 {
4223 p->ssid[0] = '\0';
4224 }
4225 else
4226 {
4227 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4228 }
you.chen35020192022-05-06 11:30:57 +08004229 }
4230
4231 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004232}
qs.xiong97fa59b2022-04-07 05:41:29 -04004233
you.chen35020192022-05-06 11:30:57 +08004234int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4235{
4236 int count, net_no, index;
4237 int net_no_list[128];
4238 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004239
you.chen35020192022-05-06 11:30:57 +08004240 char lynq_remove_cmd[MAX_CMD];
4241
qs.xiong9fbf74e2023-03-28 13:38:22 +08004242 if (ssid == NULL || *ssid == '\0')
4243 {
4244 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004245 return -1;
4246 }
4247
4248 CHECK_IDX(idx, CTRL_STA);
4249
4250 CHECK_WPA_CTRL(CTRL_STA);
4251
4252 net_no = -1;
4253 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4254
qs.xiong9fbf74e2023-03-28 13:38:22 +08004255 for (index=0; index < count; index++)
4256 {
you.chen35020192022-05-06 11:30:57 +08004257 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004258 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4259 {
you.chen35020192022-05-06 11:30:57 +08004260 net_no = net_no_list[index];
4261 break;
4262 }
4263 }
4264
qs.xiong9fbf74e2023-03-28 13:38:22 +08004265 if (net_no < 0)
4266 {
you.chen35020192022-05-06 11:30:57 +08004267 return 0;
4268 }
4269
4270 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4271
4272 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004273
4274 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4275
4276 pthread_mutex_lock(&s_global_check_mutex);
4277 lynq_sta_removeElement(net_no);
4278 pthread_mutex_unlock(&s_global_check_mutex);
4279
4280 RLOGD("%s %d",__func__,__LINE__);
4281 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004282 DO_OK_FAIL_REQUEST(cmd_save_config);
4283
4284 return 0;
4285}
4286
4287int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004288{
you.chend2fef3f2023-02-13 10:50:35 +08004289 int count, index;
you.chen35020192022-05-06 11:30:57 +08004290 int net_no_list[128];
4291 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004292 RLOGD("enter lynq_get_sta_saved_ap api\n");
4293 if (list == NULL || len == NULL)
4294 {
4295 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004296 return -1;
4297 }
4298
4299 CHECK_IDX(idx, CTRL_STA);
4300
4301// CHECK_WPA_CTRL(CTRL_STA);
4302
4303 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004304 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004305
you.chen057aac42023-04-13 14:06:58 +08004306 if (count < 0)
4307 {
4308 RLOGE("list network fail");
4309 return count;
4310 }
4311 else if (count == 0)
4312 {
4313 *list = NULL;
4314 *len = 0;
4315 return 0;
4316 }
4317
you.chen35020192022-05-06 11:30:57 +08004318 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004319 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004320 *len = count;
4321
qs.xiong9fbf74e2023-03-28 13:38:22 +08004322 for (index=0; index < count; index++)
4323 {
you.chen35020192022-05-06 11:30:57 +08004324 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004325 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004326 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004327 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004328 {
you.chen35020192022-05-06 11:30:57 +08004329 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4330 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004331 else
you.chen057aac42023-04-13 14:06:58 +08004332 {
you.chen35020192022-05-06 11:30:57 +08004333 (*list)[index].base_info.band = -1;
4334 }
you.chen057aac42023-04-13 14:06:58 +08004335 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004336 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004337 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004338 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004339 return 0;
4340}
4341
4342int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4343{
you.chen0c9bee22023-10-25 13:03:14 +08004344 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004345 {
you.chen0c9bee22023-10-25 13:03:14 +08004346 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4347 g_sta_fake_scan_finish_flag = 1;
4348 return 0;
4349 }
4350 else if (g_sta_conncet_status_flag != 0)
4351 {
4352 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004353 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004354 }
you.chen0c9bee22023-10-25 13:03:14 +08004355
qs.xiongc8d92a62023-03-29 17:36:14 +08004356 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004357 const char *lynq_scan_cmd = "SCAN";
4358
4359 CHECK_IDX(idx, CTRL_STA);
4360
4361 CHECK_WPA_CTRL(CTRL_STA);
4362
you.chen0df3e7e2023-05-10 15:56:26 +08004363 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4364 {
4365 RLOGD("tmp clear scanlist");
4366 system(clean_last_re);
4367 }
you.chen9ac66392022-08-06 17:01:16 +08004368 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004369 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004370 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4371 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004372 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004373 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4374 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004375 g_sta_scan_finish_flag = 1;
4376 return -1;
4377 }
you.chen35020192022-05-06 11:30:57 +08004378
4379 return 0;
4380}
4381
4382int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004383 if (cb == NULL)
4384 {
4385 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004386 return -1;
4387 }
4388
you.chen6d247052023-06-01 16:39:54 +08004389 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004390 g_ap_callback_priv = priv;
4391 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004392 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004393
you.chen6d247052023-06-01 16:39:54 +08004394 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004395 if (g_ap_watcher_pid == 0 )
4396 {
4397 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4398 {
4399 g_ap_watcher_pid = 0;
4400 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4401 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4402 return -1;
4403 }
4404 }
4405
4406 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004407 RLOGD("creat APWatcherTheradProc succeed");
you.chene9d00032023-04-24 13:55:29 +08004408
you.chen35020192022-05-06 11:30:57 +08004409 return 0;
4410}
4411
4412int lynq_unreg_ap_event_callback(void * priv) {
qs.xiong86cced62024-09-23 14:08:38 +08004413 RLOGI("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004414 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004415 if (g_ap_callback_priv == priv)
4416 {
you.chen35020192022-05-06 11:30:57 +08004417 g_ap_callback_func = NULL;
4418 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004419 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08004420 RLOGI("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004421 return 0;
4422 }
you.chen6d247052023-06-01 16:39:54 +08004423 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004424 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004425 return -1;
4426}
4427
4428int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004429 if (cb == NULL)
4430 {
4431 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004432 return -1;
4433 }
4434
you.chen6d247052023-06-01 16:39:54 +08004435 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004436 g_sta_callback_priv = priv;
4437 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004438 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004439
you.chen6d247052023-06-01 16:39:54 +08004440 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004441 if (g_sta_watcher_pid == 0 ) {
4442 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4443 {
4444 g_sta_watcher_pid = 0;
4445 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4446 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4447 return -1;
4448 }
4449 }
4450
4451 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08004452 RLOGI("creat STAWatcherTheradProc succeed");
you.chen35020192022-05-06 11:30:57 +08004453 return 0;
4454}
4455
4456int lynq_unreg_sta_event_callback(void * priv) {
qs.xiong86cced62024-09-23 14:08:38 +08004457 RLOGI("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004458 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004459 if (g_sta_callback_priv == priv)
4460 {
you.chen35020192022-05-06 11:30:57 +08004461 g_sta_callback_func = NULL;
4462 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004463 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08004464 RLOGI("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004465 return 0;
4466 }
you.chen6d247052023-06-01 16:39:54 +08004467 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004468 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004469 return -1;
4470}
4471
qs.xiongfcc914b2023-07-06 21:16:20 +08004472int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4473 if (cb == NULL)
4474 {
4475 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4476 return -1;
4477 }
4478 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4479 g_sta_auto_callback_priv = priv;
4480 g_sta_auto_callback_func = cb;
4481 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4482 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4483 if (g_sta_auto_watcher_pid == 0 ) {
4484 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4485 {
4486 g_sta_auto_watcher_pid = 0;
4487 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4488 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4489 return -1;
4490 }
4491 }
4492 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong86cced62024-09-23 14:08:38 +08004493 RLOGI("creat STAWatcherTheradProc succeed");
qs.xiongfcc914b2023-07-06 21:16:20 +08004494 return 0;
4495}
4496int lynq_unreg_sta_auto_event_callback(void * priv) {
4497 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4498 if (g_sta_auto_callback_priv == priv)
4499 {
4500 g_sta_auto_watcher_stop_flag = 1;
4501 if (g_sta_auto_watcher_pid != 0)
4502 {
4503 pthread_join(g_sta_auto_watcher_pid, NULL);
4504 }
4505 g_sta_auto_watcher_pid = 0;
4506 g_sta_auto_callback_func = NULL;
4507 g_sta_auto_callback_priv = NULL;
4508 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4509 return 0;
4510 }
4511 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4512 return -1;
4513}
you.chen35020192022-05-06 11:30:57 +08004514int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4515{
4516 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004517 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004518 CHECK_IDX(idx, CTRL_AP);
4519
qs.xiong9fbf74e2023-03-28 13:38:22 +08004520 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4521 {
you.chen35020192022-05-06 11:30:57 +08004522 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4523 return 0;
4524 }
4525
qs.xiong9fbf74e2023-03-28 13:38:22 +08004526 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4527 {
you.chen35020192022-05-06 11:30:57 +08004528 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4529 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004530 else
4531 {
you.chen35020192022-05-06 11:30:57 +08004532 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4533 }
4534
4535 return 0;
4536}
4537
4538int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4539 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004540 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004541 CHECK_IDX(idx, CTRL_STA);
4542
qs.xiong9fbf74e2023-03-28 13:38:22 +08004543 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4544 {
you.chen35020192022-05-06 11:30:57 +08004545 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4546 return 0;
4547 }
4548
qs.xiong9fbf74e2023-03-28 13:38:22 +08004549 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4550 {
you.chen35020192022-05-06 11:30:57 +08004551 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4552 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004553 else
4554 {
you.chen35020192022-05-06 11:30:57 +08004555 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4556 }
4557
4558 return 0;
4559}
4560
4561int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4562// CHECK_IDX(idx, CTRL_AP);
4563// int ret = 0;
4564// size_t reply_len = MAX_RET;
4565// char cmd_reply[MAX_RET]={0};
4566// const char * cmd_str = "GET country";
4567// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4568// do{
4569// if (NULL == s_lynq_wpa_ctrl) {
4570// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4571// if (NULL == s_lynq_wpa_ctrl ) {
4572// printf("wpa_ctrl_open fail\n");
4573// return -1;
4574// }
4575// }
4576// }while(0);
4577
4578// do {
4579// reply_len = MAX_RET;
4580// cmd_reply[0] = '\0';
4581// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004582// 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 +08004583// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004584// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004585// return ret;
4586// }
4587// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004588// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004589// }while(0);
4590
4591 FILE *fp;
4592 size_t i = 0;
4593 char lynq_cmd_ret[MAX_RET]={0};
4594
4595// CHECK_IDX(idx, CTRL_AP);
4596
4597 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004598 {
4599 perror("popen error!");
4600 return -1;
4601 }
you.chen35020192022-05-06 11:30:57 +08004602 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4603 {
4604 perror("fread fail!");
4605 return -1;
4606 }
4607
qs.xiong9fbf74e2023-03-28 13:38:22 +08004608 for(i=0; i < strlen(lynq_cmd_ret); i++)
4609 {
4610 if (lynq_cmd_ret[i] == ' ')
4611 {
you.chen35020192022-05-06 11:30:57 +08004612 lynq_cmd_ret[i] = '\0';
4613 break;
4614 }
4615 }
4616
4617 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004618 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004619
4620 int ret=pclose(fp);
4621 if(ret==-1)
4622 {
4623 perror("close file faild");
4624 }
4625
4626 return 0;
4627}
4628
qs.xiong44fac672023-08-29 16:15:55 +08004629
you.chen705a7ef2023-06-01 22:06:45 +08004630static int check_and_init_uci_config(char * country_code)
4631{
4632 FILE * fp;
4633 int is_different = 0;
4634 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4635 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4636 const char * commit_uci_cmd ="uci commit";
4637 char set_country_cmd[MAX_CMD];
4638 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004639
you.chen705a7ef2023-06-01 22:06:45 +08004640 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004641
you.chen705a7ef2023-06-01 22:06:45 +08004642 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004643 {
you.chen705a7ef2023-06-01 22:06:45 +08004644 if (0 != system(create_uci_cmd))
4645 {
4646 RLOGE("creat_uci_cmd fail");
4647 return -1;
4648 }
4649 is_different = 1;
4650 }
4651
4652 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4653 {
4654 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004655 return -1;
4656 }
4657
you.chen705a7ef2023-06-01 22:06:45 +08004658 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004659 {
you.chen705a7ef2023-06-01 22:06:45 +08004660 RLOGE("fread fail!");
4661 fclose(fp);
4662 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004663 }
4664
you.chen705a7ef2023-06-01 22:06:45 +08004665 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4666 {
qs.xiong44fac672023-08-29 16:15:55 +08004667 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 +08004668 is_different = 1;
4669 }
4670
4671 fclose(fp);
4672
4673 if (is_different)
4674 {
4675 if ( 0 != system(set_country_cmd))
4676 {
4677 RLOGE("set_country_cmd fail");
4678 return -1;
4679 }
4680 if (0 != system(commit_uci_cmd))
4681 {
4682 RLOGE("commmit fail");
4683 }
4684 }
4685
4686 return is_different;
4687}
4688
4689int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4690 char check_current_code[10];
4691 const char * support_country[] = {"CN", "EU"};
4692
4693 int ret,is_different, i, cc_count;
4694
4695 if (country_code == NULL || country_code[0] == '\0')
4696 {
4697 RLOGE("bad country code\n");
4698 return -1;
4699 }
4700
4701 cc_count = sizeof (support_country) / sizeof (char*);
4702 for(i=0; i < cc_count; i++)
4703 {
4704 if (strcmp(support_country[i], country_code) == 0)
4705 {
4706 break;
4707 }
4708 }
4709
4710 if (i >= cc_count)
4711 {
4712 RLOGE("unspported country code %s\n", country_code);
4713 return -1;
4714 }
4715
4716 is_different = check_and_init_uci_config(country_code);
4717 if( is_different < 0 )
4718 {
4719 RLOGE("init set uci fail\n");
4720 return -1;
4721 }
4722
4723 ret = lynq_get_country_code(idx,check_current_code);
4724 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4725 {
4726 ret = lynq_wifi_disable();
4727 if(ret != 0 )
4728 {
4729 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4730 return -1;
4731 }
4732 }
4733
4734 return 0;
you.chen35020192022-05-06 11:30:57 +08004735}
4736
4737int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4738{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004739 RLOGD("enter lynq_get_connect_ap_mac\n");
4740 if (mac == NULL)
4741 {
4742 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004743 return -1;
4744 }
4745
4746 CHECK_IDX(idx, CTRL_STA);
4747 ap_info_s ap;
4748 ap.ap_mac[0] = '\0';
4749
qs.xiong9fbf74e2023-03-28 13:38:22 +08004750 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4751 {
you.chen35020192022-05-06 11:30:57 +08004752 return -1;
4753 }
4754 strcpy(mac, ap.ap_mac);
4755
4756 return 0;
4757}
4758
4759int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4760{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004761 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004762 struct ifaddrs *ifaddr_header, *ifaddr;
4763 struct in_addr * ifa;
4764 const char * ifaName = "wlan0";
4765 if (ip == NULL)
4766 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004767 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004768 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004769 }
you.chenf58b3c92022-06-21 16:53:48 +08004770
qs.xiong9fbf74e2023-03-28 13:38:22 +08004771 if (idx == 1)
4772 {
you.chen0df3e7e2023-05-10 15:56:26 +08004773 ifaName = inner_get_ap_interface_name();
4774 if (ifaName == NULL)
4775 {
4776 RLOGE("[lynq_get_interface_ip] ap name get fail");
4777 return -1;
4778 }
you.chen9ac66392022-08-06 17:01:16 +08004779 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004780 else if (idx != 0)
4781 {
you.chen35020192022-05-06 11:30:57 +08004782 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004783 }
you.chen35020192022-05-06 11:30:57 +08004784
you.chen9ac66392022-08-06 17:01:16 +08004785 if (getifaddrs(&ifaddr_header) == -1)
4786 {
you.chen35020192022-05-06 11:30:57 +08004787 perror("getifaddrs");
4788 return -1;
4789 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004790 }
you.chen35020192022-05-06 11:30:57 +08004791
4792
you.chen9ac66392022-08-06 17:01:16 +08004793 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4794 {
4795 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004796 continue;
you.chen9ac66392022-08-06 17:01:16 +08004797 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4798 {
4799 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4800 {
4801 // is a valid IP4 Address
4802 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4803 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004804 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004805 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004806 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004807 return 0;
4808 }
4809 }
4810 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004811 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004812 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004813 return -1;
you.chen35020192022-05-06 11:30:57 +08004814}
4815
4816int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4817{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004818 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004819 int count;
4820 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004821 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004822 char *split_words[128] = {0};
4823 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4824
4825 CHECK_WPA_CTRL(idx);
4826
4827 DO_REQUEST(lynq_get_mac_cmd);
4828
qs.xiong9fbf74e2023-03-28 13:38:22 +08004829 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4830 {
4831 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004832 return -1;
4833 }
4834
4835 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4836
qs.xiong9fbf74e2023-03-28 13:38:22 +08004837 if (count < 2)
4838 {
you.chen35020192022-05-06 11:30:57 +08004839 return -1;
4840 }
4841
qs.xiong9fbf74e2023-03-28 13:38:22 +08004842 for (i=0; i < strlen(split_words[1]); i++ )
4843 {
4844 if (split_words[1][i] != ' ')
4845 {
you.chen35020192022-05-06 11:30:57 +08004846 break;
4847 }
4848 }
4849
qs.xiongdd6e44c2023-08-08 15:02:53 +08004850 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004851
4852 return 0;
4853}
4854
4855int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4856{
4857// int count;
4858// char *split_words[128] = {0};
4859// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4860
4861// if (rssi == NULL) {
4862// return -1;
4863// }
4864
4865// CHECK_IDX(idx, CTRL_STA);
4866
4867// CHECK_WPA_CTRL(CTRL_STA);
4868
4869// DO_REQUEST(lynq_get_rssi_cmd);
4870
4871// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4872// return -1;
4873// }
4874
4875// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4876
4877// if (count < 2) {
4878// return -1;
4879// }
4880
4881// *rssi = atoi(split_words[1]) * -1;
4882
you.chen35020192022-05-06 11:30:57 +08004883 char lynq_cmd_ret[MAX_RET]={0};
4884
qs.xiongff0ae0f2022-10-11 15:47:14 +08004885/*******change other cmd to get rssi*******
4886 *
4887 *wl rssi ---> wl -i wlan0 rssi
4888 *
4889 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004890 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004891 {
you.chen23c4a5f2023-04-12 16:46:00 +08004892 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004893 return -1;
4894 }
you.chen9f17e4d2022-06-06 17:18:18 +08004895 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004896/****** if got rssi is 0,means sta didn't connected any device****/
4897 if(*rssi == 0)
4898 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004899 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004900 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004901 }
you.chen35020192022-05-06 11:30:57 +08004902
4903 return 0;
4904}
4905
4906int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4907{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004908 RLOGD("enter lynq_get_connect_ap_band\n");
4909 if (band == NULL)
4910 {
you.chen35020192022-05-06 11:30:57 +08004911 return -1;
4912 }
4913
4914 CHECK_IDX(idx, CTRL_STA);
4915 ap_info_s ap;
4916 ap.band = -1;
4917
qs.xiong9fbf74e2023-03-28 13:38:22 +08004918 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4919 {
you.chen35020192022-05-06 11:30:57 +08004920 return -1;
4921 }
4922 *band = ap.band;
4923
4924 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004925}
you.chenf58b3c92022-06-21 16:53:48 +08004926
4927int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4928{
you.chenb95401e2023-05-12 19:39:06 +08004929 int ret;
4930 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004931 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004932
4933 if (ip == NULL)
4934 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004935 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004936 return -1;
4937 }
4938
4939 CHECK_IDX(idx, CTRL_STA);
4940
qs.xionge4cbf1c2023-02-28 18:22:49 +08004941 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004942 {
4943 return -1;
4944 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004945
you.chenb95401e2023-05-12 19:39:06 +08004946 ip[0] = '\0';
4947 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4948 if (ret != 0)
4949 {
4950 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
you.chenb95401e2023-05-12 19:39:06 +08004951 }
4952
4953 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4954 {
4955 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004956 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004957 if (ret != 0)
4958 {
4959 ip[0] = '\0';
4960 return 0;
4961 }
4962 else
4963 {
4964 p = strchr(ip, '\n');
4965 if (p != NULL)
4966 {
4967 *p = '\0';
4968 }
4969 }
4970 }
4971 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004972}
4973
qs.xionge02a5252023-09-20 14:00:21 +08004974int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4975{
4976 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4977 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4978}
4979
qs.xiong026c5c72022-10-17 11:15:45 +08004980int lynq_ap_connect_num(int sta_number)
4981{
4982 char lynq_limit_cmd[32]={0};
4983 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004984 if((sta_number < 1 ) && (sta_number > 15))
4985 {
4986 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004987 return -1;
4988 }
4989 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4990 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004991 if(ret != 0)
4992 {
4993 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004994 }
4995 return 0;
4996}
you.chenf58b3c92022-06-21 16:53:48 +08004997
qs.xiong77905552022-10-17 11:19:57 +08004998int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4999{
5000
5001 char lynq_wifi_acs_cmd[128]={0};
5002 char lynq_cmd_mode[128]={0};
qs.xiong77905552022-10-17 11:19:57 +08005003
qs.xiong9fbf74e2023-03-28 13:38:22 +08005004 if((acs_mode != 2) && (acs_mode != 5))
5005 {
qs.xiong77905552022-10-17 11:19:57 +08005006 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
5007 }
5008
qs.xiong9fbf74e2023-03-28 13:38:22 +08005009 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
5010 {
qs.xiong77905552022-10-17 11:19:57 +08005011 return -1;
5012 }
5013
5014 CHECK_IDX(idx, CTRL_AP);
5015
5016 CHECK_WPA_CTRL(CTRL_AP);
5017
5018 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
5019 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
qs.xiong77905552022-10-17 11:19:57 +08005020
5021 DO_OK_FAIL_REQUEST(cmd_disconnect);
5022 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
5023 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
5024 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong77905552022-10-17 11:19:57 +08005025
5026 return 0;
5027}
you.chen0f5c6432022-11-07 18:31:14 +08005028//you.chen add for tv-box start
5029static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
5030 FILE *fp;
5031 //printf("to exec cmd:%s\n", str_cmd);
5032 if((fp=popen(str_cmd,"r"))==NULL)
5033 {
5034 perror("popen error!");
5035 return -1;
5036 }
5037 if((fread(str_cmd_ret,max_len,1,fp))<0)
5038 {
5039 perror("fread fail!");
5040 fclose(fp);
5041 return -1;
5042 }
5043 fclose(fp);
5044 return 0;
5045}
5046
5047static int get_netmask_length(const char* mask)
5048{
5049 int masklen=0, i=0;
5050 int netmask=0;
5051
5052 if(mask == NULL)
5053 {
5054 return 0;
5055 }
5056
5057 struct in_addr ip_addr;
5058 if( inet_aton(mask, &ip_addr) )
5059 {
5060 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08005061 }else
5062 {
you.chen0f5c6432022-11-07 18:31:14 +08005063 netmask = 0;
5064 return 0;
5065 }
5066
5067 while(0 == (netmask & 0x01) && i<32)
5068 {
5069 i++;
5070 netmask = netmask>>1;
5071 }
5072 masklen = 32-i;
5073 return masklen;
5074}
5075
5076static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
5077 int mask_len;
5078 char *p;
5079 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08005080 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5081 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005082 return -1;
5083 p = strstr(str_cmd_ret, "Mask:");
5084 if (p == NULL)
5085 return -1;
5086 mask_len = get_netmask_length(p + 5);
5087 if (mask_len == 0)
5088 return -1;
5089 p = strstr(str_cmd_ret, "inet addr:");
5090 if (p == NULL)
5091 return -1;
5092 strcpy(tmp, p + 10);
5093 p = strstr(tmp, " ");
5094 if (p != NULL)
5095 *p = '\0';
5096 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5097 return 0;
5098}
5099
5100static void GBWWatchThreadProc() {
5101 int i,n, nloop, nmax, ncheckcount, nidlecount;
5102 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5103 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5104 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5105 char *results[16] = {0};
5106 char str_cmd[256] = {0};
5107 char str_cmd_ret[128] = {0};
5108 char dest_ip[32] = {0};
5109 lastAP1Bytes = lastAP2Bytes = 0;
5110 lastAP1Drop = lastAP2Drop = 0;
5111 lastAP1Speed = lastAP2Speed = 0;
5112 setAP1Speed = 50;
5113 setAP2Speed = 80;
5114 nloop = 0;
5115 nmax = 6;
5116 ncheckcount = nidlecount = 0;
5117
you.chen0df3e7e2023-05-10 15:56:26 +08005118 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005119 {
5120 RLOGE("------gbw thread run\n");
5121 return;
5122 }
5123
qs.xiong9fbf74e2023-03-28 13:38:22 +08005124 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005125 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5126 while (dest_ip[0] == '\0') {
5127 sleep(1);
5128 str_cmd_ret[0] = '\0';
5129 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5130 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5131 if (str_cmd_ret[n] == '\n'){
5132 str_cmd_ret[n] = '\0';
5133 break;
5134 }
5135 }
5136 if (str_cmd_ret[0] != '\0')
5137 {
5138 strcpy(dest_ip, str_cmd_ret);
5139 }
5140 }
5141
you.chenc9928582023-04-24 15:39:37 +08005142 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5143 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5144 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 +08005145 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5146 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005147 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005148 return;
5149 }
you.chenc9928582023-04-24 15:39:37 +08005150 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);
5151 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);
5152 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 +08005153
5154 while (1) {
5155 sleep(1);
5156 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005157 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5158 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5159 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005160 continue;
5161 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005162 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005163 if (n > 9) {
5164 if (strcmp(results[1], "Sent") == 0) {
5165 currAP1Bytes = atoll(results[2]);
5166 }
5167 if (strcmp(results[6], "(dropped") == 0) {
5168 currAP1Drop = atoi(results[7]);
5169 }
5170 }
5171
5172 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005173 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5174 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5175 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005176 continue;
5177 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005178 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005179 if (n > 9) {
5180 if (strcmp(results[1], "Sent") == 0) {
5181 currAP2Bytes = atoll(results[2]);
5182 }
5183 if (strcmp(results[6], "(dropped") == 0) {
5184 currAP2Drop = atoi(results[7]);
5185 }
5186 }
5187
5188 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5189 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5190 lastAP1Bytes = currAP1Bytes;
5191 lastAP2Bytes = currAP2Bytes;
5192 continue;
5193 }
5194
5195 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5196 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5197 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5198 lastAP1Speed = currAP1Speed;
5199 lastAP2Speed = currAP2Speed;
5200 lastAP1Bytes = currAP1Bytes;
5201 lastAP2Bytes = currAP2Bytes;
5202
5203 currSetAP1Speed = setAP1Speed;
5204 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5205 ncheckcount++;
5206 if (ncheckcount > 3) {
5207 ncheckcount = 0;
5208 currSetAP1Speed = 5;
5209 }
5210 }
5211 else {
5212 ncheckcount = 0;
5213 if (currAP1Speed < 5)
5214 nidlecount++;
5215 else
5216 nidlecount = 0;
5217
5218 }
5219
5220 if (nidlecount > 60 ){
5221 currSetAP1Speed = 50;
5222 }
5223
5224 if (currSetAP1Speed != setAP1Speed) {
5225 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005226 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5227 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005228 }
5229 }
5230}
5231
5232int enableGBW(const char* mac) {
5233 int i,len;
5234 char get_ipaddr_cmd[128]={0};
5235 ap_info_s *ap;
5236 device_info_s * list;
5237
5238 if (mac == NULL || g_gbw_enabled == 1)
5239 return -1;
5240 len = strlen(mac);
5241 g_gbw_mac = malloc(len + 1);
5242 for(i=0;i<len;i++) {
5243 if (mac[i] >= 'A' && mac[i] <= 'Z')
5244 {
5245 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5246 }
5247 else
5248 g_gbw_mac[i] = mac[i];
5249 }
5250 g_gbw_mac[i] = '\0';
5251 g_gbw_enabled = 1;
5252
5253 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5254 if (system(get_ipaddr_cmd) == 0) {
5255 //startGBW();
5256 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5257 for (i=0;i<len;i++) {
5258 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5259 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5260 startGBW();
5261 }
5262 free(ap);
5263 free(list);
5264 }
5265 }
5266 return 0;
5267}
5268
5269int disableGBW() {
5270 stopGBW();
5271 free(g_gbw_mac);
5272 g_gbw_mac = NULL;
5273 g_gbw_enabled = 1;
5274 return 0;
5275}
5276
5277static int startGBW() {
5278 if (g_gbw_watcher_pid != 0) {
5279 stopGBW();
5280 }
5281 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5282}
5283
5284static int stopGBW() {
5285 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005286 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005287 pthread_cancel(g_gbw_watcher_pid);
5288 pthread_join(g_gbw_watcher_pid, &retval);
5289 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005290 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5291 if (s_ap_iterface_name[0] != '\0')
5292 {
5293 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5294 system(cmd);
5295 }
you.chen0f5c6432022-11-07 18:31:14 +08005296}
5297//you.chen add for tv-box end