blob: dbc9ef1aea8e9020efcadda357789a4d43359669 [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.xiongfcc914b2023-07-06 21:16:20 +0800107// add for auto connect
108static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800109
110static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800111
you.chen0f5c6432022-11-07 18:31:14 +0800112//you.chen add for tv-box start
113volatile int g_gbw_enabled = 0;
114char * g_gbw_mac = NULL;
115pthread_t g_gbw_watcher_pid = 0;
116static int startGBW();
117static int stopGBW();
118//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800119
120typedef struct __curr_status_info {
121 ap_info_s *ap;
122 char * state;
123 int net_no;
124}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400125
you.chen70f377f2023-04-14 18:17:09 +0800126typedef enum {
127 INNER_STA_STATUS_INIT = 0,
128 INNER_STA_STATUS_CONNECTING,
129 INNER_STA_STATUS_ASSOCIATING,
130 INNER_STA_STATUS_ASSOCIATED,
131 INNER_STA_STATUS_CONNECTED,
132 INNER_STA_STATUS_DISCONNECTING,
133 INNER_STA_STATUS_DISCONNECTED,
134 INNER_STA_STATUS_CANCEL,
135}inner_sta_status_s;
136
137static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
138static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800139volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800140static error_number_s s_sta_error_number = -1;
141static char s_sta_current_connecting_ssid[64] = {0};
142static struct timespec s_sta_connect_timeout;
143const int MAX_CONNNECT_TIME = 15; // second
144pthread_t g_global_watcher_pid = 0;
145static int s_service_invoke_timeout_cnt=0;
146const int FAKE_MAX_INT_VALUE = 99999;
147
qs.xiong09560402023-10-27 21:58:55 +0800148static void print_disconnect_list()
149{
150 int i;
151 for( i = 0; i < g_history_disconnect_valid_num; i++ )
152 {
153 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__);
154 }
155
156 return;
157}
158
159// idex ----> history_disconnect_list[x] index
160static int removeElement(int idex)
161{
162 RLOGD("into removeElement");
163 if( index < 0 )
164 {
165 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
166 return -1;
167 }
168 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]);
169 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
170 g_history_disconnect_valid_num --;
171 RLOGD("end removeElement");
172 return 0;
173}
174static int check_history_disconenct_ap_list(int val)
175{
176 print_disconnect_list();
177 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__);
178 int i;
179 for( i = 0; i < g_history_disconnect_valid_num; i++)
180 {
181 if( val == g_history_disconnect_net[i] )
182 {
183 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
184 RLOGD("end check_history_disconenct_ap_list && return network index");
185 return i;
186 }
187 }
188 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__);
189 return -1;
190}
191
192
193static void lynq_sta_removeElement(int net_no)
194{
195 int ret;
196
197 ret = check_history_disconenct_ap_list(net_no);
198 if( ret == -1 )
199 {
200 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
201 return;
202 }else
203 {
204 ret = removeElement(ret);
205 if( ret == 0 )
206 {
207 RLOGD("removeElement pass %s %d",__func__,__LINE__);
208 return;
209 }
210 }
211
212 return;
213}
214
you.chen70f377f2023-04-14 18:17:09 +0800215static void notify_service_invoke_fail(int error)
216{
qs.xiong17579bb2024-03-11 19:08:06 +0800217 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen70f377f2023-04-14 18:17:09 +0800218 pthread_mutex_lock(&s_global_check_mutex);
219 if (error == -2) //timeout
220 {
221 s_service_invoke_timeout_cnt++;
222 if (s_service_invoke_timeout_cnt > 10)
223 {
224 pthread_cond_signal(&s_global_check_cond);
225 }
226 }
227 else if (error == -1)
228 {
229 // check if can connect wpa service
230 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
231 if (lynq_wpa_ctrl == NULL)
232 {
233 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
234 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800235 }else
236 {
237 wpa_ctrl_close(lynq_wpa_ctrl);
238 }
you.chen70f377f2023-04-14 18:17:09 +0800239 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
240 if (lynq_wpa_ctrl == NULL)
241 {
242 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
243 pthread_cond_signal(&s_global_check_cond);
qs.xiong17579bb2024-03-11 19:08:06 +0800244 }else
245 {
246 wpa_ctrl_close(lynq_wpa_ctrl);
you.chen70f377f2023-04-14 18:17:09 +0800247 }
you.chen70f377f2023-04-14 18:17:09 +0800248 }
249
250 pthread_mutex_unlock(&s_global_check_mutex);
251}
252
you.chenc9928582023-04-24 15:39:37 +0800253static int system_call_v(const char * fmt, ...)
254{
255 char str_cmd[256] = {0};
256 va_list args;
257 va_start(args, fmt);
258 vsprintf(str_cmd, fmt, args);
259 va_end(args);
260 printf("system call----------%s\n", str_cmd);
261 return system(str_cmd);
262}
263
you.chen0df3e7e2023-05-10 15:56:26 +0800264static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
265
266static const char * inner_get_ap_interface_name()
267{
268 char * p;
269 char cmd[128]={0};
270
271 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
272 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
273 {
274 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
275 return NULL;
276 }
277 p = strchr(s_ap_iterface_name, ' ');
278 if (NULL != p)
279 {
280 *p = '\0';
281 }
282 p = strchr(s_ap_iterface_name, '\n');
283 if (NULL != p)
284 {
285 *p = '\0';
286 }
287 if (s_ap_iterface_name[0] == '\0')
288 {
289 return NULL;
290 }
291
292 return s_ap_iterface_name;
293}
294
you.chen70f377f2023-04-14 18:17:09 +0800295static void check_tether_and_notify()
296{
297 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800298 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 +0800299 {
300 return;
301 }
302 pthread_mutex_lock(&s_global_check_mutex);
303 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
304 pthread_cond_signal(&s_global_check_cond);
305 pthread_mutex_unlock(&s_global_check_mutex);
306}
307
you.chend2fef3f2023-02-13 10:50:35 +0800308static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
309 char *reply, size_t *reply_len,
310 void (*msg_cb)(char *msg, size_t len))
311{
312 int ret;
313 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800314 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800315 return -1;
316 }
317 pthread_mutex_lock(&ctrl->mutex);
318 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
319 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800320 if (ret != 0)
321 {
322 notify_service_invoke_fail(ret);
323 }
you.chend2fef3f2023-02-13 10:50:35 +0800324 return ret;
325}
326
327static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
328 int repeat_cnt;
329 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
330 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800331 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800332 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
333 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
334// printf("wait enable finish\n");
335 usleep(500 * 1000);
336 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
337 }
338 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800339 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800340 goto out_addr;
341 }
342 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
343 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
344 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800345 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800346 goto out_addr;
347 }
348 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
349 }
350 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
351out_addr:
352 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
353 return lynq_wpa_ctrl;
354}
355
qs.xiong97fa59b2022-04-07 05:41:29 -0400356#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400357{\
you.chen35020192022-05-06 11:30:57 +0800358 perror((str));\
359 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400360}
361
you.chen35020192022-05-06 11:30:57 +0800362#define CHECK_IDX(idx, type) do { \
363 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
364 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800365 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800366 return -1; \
367 } \
368 }while (0)
369
370#define CHECK_WPA_CTRL(index) int ret = 0;\
371 size_t reply_len = MAX_RET; \
372 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800373 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800374 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800375 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
376 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800377 }while(0)
378
379#define DO_REQUEST(cmd_str) do { \
380 reply_len = MAX_RET;\
381 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800382 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800383 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 +0800384 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800385 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800386 return ret; \
387 } \
388 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800389 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800390 }while(0)
391
392#define DO_OK_FAIL_REQUEST(cmd_str) do { \
393 DO_REQUEST(cmd_str); \
394 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800395 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800396 return -1; \
397 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800398 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800399 return -1; \
400 } \
401 }while (0)
402
403
you.chenf711c8a2023-04-13 13:49:45 +0800404static int check_connection(struct wpa_ctrl * wpa_ctrl)
405{
406 size_t reply_len = MAX_RET;
407 char cmd_reply[MAX_RET]={0};
408 int ret;
409
410 RLOGD("check_connection [%p]", wpa_ctrl);
411 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
412
413 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
414 {
415 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 +0800416 if (ret != 0)
417 {
418 notify_service_invoke_fail(ret);
419 }
you.chenf711c8a2023-04-13 13:49:45 +0800420 return -1;
421 }
422
423 return 0;
424}
425
426/**
427 * @brief check_pending_msg
428 * @param lynq_wpa_ctrl
429 * @return 1 has msg, 0 no msg, -1 error
430 */
431static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
432{
433 int ret;
434
435 if (*pp_lynq_wpa_ctrl == NULL) // need connect
436 {
437 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
438 if (*pp_lynq_wpa_ctrl == NULL)
439 {
440 usleep(SLEEP_TIME_ON_IDLE);
441 return -1;
442 }
443
444 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
445 if (ret == 0) // attach success
446 {
447 *started_flag = 1;
448 }
449 else
450 {
you.chen70f377f2023-04-14 18:17:09 +0800451 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800452 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
453 *pp_lynq_wpa_ctrl = NULL;
454 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800455 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800456 usleep(SLEEP_TIME_ON_IDLE);
457 return -1;
458 }
459 }
460
461 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
462 if ( ret == 0) // no pending messages
463 {
464 usleep(SLEEP_TIME_ON_IDLE);
465 *idle_count += 1;
466 if (*idle_count > MAX_IDLE_COUNT)
467 {
468 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
469 {
470 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
471 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
472 *pp_lynq_wpa_ctrl = NULL;
473 *idle_count = 0;
474 return -1;
475 }
476 *idle_count = 0;
477 }
478 return 0;
479 }
480 else if ( ret == -1) // on error
481 {
482 RLOGE("[wifi error]sta wpa_ctrl_pending");
483 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
484 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
485 *pp_lynq_wpa_ctrl = NULL;
486 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800487 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800488 return -1;
489 }
490
491 *idle_count = 0;
492 return 1;
493}
494
you.chen6d247052023-06-01 16:39:54 +0800495static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
496{
497 pthread_mutex_lock(&s_ap_callback_mutex);
498 if (g_ap_callback_func != NULL)
499 g_ap_callback_func(g_ap_callback_priv, status);
500 pthread_mutex_unlock(&s_ap_callback_mutex);
501}
502
you.chen35020192022-05-06 11:30:57 +0800503static void APWatcherThreadProc() {
504 size_t len = MAX_RET;
505 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800506 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800507 char *ptr = NULL;
508 char mac[32] = {0};
509 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800510
you.chen6c2dd9c2022-05-16 17:55:28 +0800511 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800512 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800513
qs.xiong9fbf74e2023-03-28 13:38:22 +0800514 while (g_ap_watcher_stop_flag == 0)
515 {
you.chenf711c8a2023-04-13 13:49:45 +0800516 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
517 {
you.chen70f377f2023-04-14 18:17:09 +0800518 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
519 {
520 check_tether_and_notify();
521 }
522
you.chen35020192022-05-06 11:30:57 +0800523 continue;
524 }
you.chenf711c8a2023-04-13 13:49:45 +0800525
you.chen6c2dd9c2022-05-16 17:55:28 +0800526 memset(msg_notify, 0, MAX_RET);
527 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800528 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800529 {
you.chen35020192022-05-06 11:30:57 +0800530 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800531 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800532 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800533 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800534 {
you.chen6d247052023-06-01 16:39:54 +0800535 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800536 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
537 if( ptr != NULL)
538 {
539 ptr += strlen("AP-STA-DISCONNECTED ");
540 memcpy(mac,ptr,17);
541 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
542 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
543 system(cmd);
544 }
545
qs.xiong9fbf74e2023-03-28 13:38:22 +0800546 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800547 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800548 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
549 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800550 {
you.chen0f5c6432022-11-07 18:31:14 +0800551 stopGBW();
552 }
553 }
you.chen35020192022-05-06 11:30:57 +0800554 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800555 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800556 {
you.chen6d247052023-06-01 16:39:54 +0800557 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800558 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800559 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800560 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
561 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800562 {
you.chen0f5c6432022-11-07 18:31:14 +0800563 startGBW();
564 }
565 }
you.chen35020192022-05-06 11:30:57 +0800566 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800567 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800568 {
569 RLOGD("APWatcherThreadProc ap------> service error");
570 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
571 }
572 else
573 {
574 RLOGD("APWatcherThreadProc ap------> going on check next msg");
575 }
you.chenf711c8a2023-04-13 13:49:45 +0800576 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800577 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
578 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800579 if (lynq_wpa_ctrl != NULL)
580 {
you.chen92fd5d32022-05-25 10:09:47 +0800581 wpa_ctrl_detach(lynq_wpa_ctrl);
582 wpa_ctrl_close(lynq_wpa_ctrl);
583 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400584}
585
you.chen70f377f2023-04-14 18:17:09 +0800586static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
587{
588 char * p;
589 const char * try_associat_flag = "Trying to associate";
590 const char * associated_flag = "Associated with ";
591
592 pthread_mutex_lock(&s_global_check_mutex);
593 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
594 {
595 pthread_mutex_unlock(&s_global_check_mutex);
596 return;
597 }
598
you.chen6e724162023-10-19 19:10:01 +0800599 // youchen@2023-10-17 add for "not notify connect fail directly" begin
600 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
601 {
602 s_sta_error_number = error_num;
603 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
604 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
605 pthread_cond_signal(&s_global_check_cond);
606 pthread_mutex_unlock(&s_global_check_mutex);
607 return;
608 }
609 // youchen@2023-10-17 add for "not notify connect fail directly" end
610
you.chen70f377f2023-04-14 18:17:09 +0800611 if (state == LYNQ_WIFI_STATUS_EGNORE)
612 {
613 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
614 {
615 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
616 }
617 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
618 {
619 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
620 }
621 }
622 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
623 {
624 s_sta_error_number = error_num;
625 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
626 {
627 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800628 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 +0800629 pthread_cond_signal(&s_global_check_cond);
630 }
631 }
632 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
633 {
634 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800635 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 +0800636 pthread_cond_signal(&s_global_check_cond);
637 }
638 pthread_mutex_unlock(&s_global_check_mutex);
639}
640
qs.xiongb37f8c42023-09-13 21:21:58 +0800641static int lynq_split(char * str, int len, char delimiter, char * results[]);
642static inline char inner_convert_char(char in);
643static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
644static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
645
646
647
qs.xiong44fac672023-08-29 16:15:55 +0800648/*
649just tmp add for fix sta connect ap fail check ap connect info
650return 0 --->Current no sta device connect this AP
651*/
652static int lynq_connected_ap_sta_status() {
653
654 FILE *fp;
655 size_t i = 0;
656 int ret;
657 char lynq_cmd_ret[MAX_RET]={0};
658
659 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
660 {
661 perror("popen error!");
662 return -1;
663 }
664 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
665 {
666 perror("fread fail!");
667 ret=pclose(fp);
668 if(ret == -1)
669 perror("close file faild");
670 return -1;
671 }
672 if( strlen(lynq_cmd_ret) < MAC_LEN)
673 {
674 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
675 ret=pclose(fp);
676 if(ret==-1)
677 {
678 perror("close file faild");
679 }
680 return 0;
681 }else{
682 ret=pclose(fp);
683 if(ret==-1)
684 {
685 perror("close file faild");
686 }
687 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
688 return 1;
689 }
690}
691
692/*
693 just tmp add for fix sta connect ap fail; check fw status
694 return 1 ----> fw status error; need wl down/up
695*/
696static int check_current_fw_status() {
697
698 FILE *fp;
699 FILE *fp1;
700 size_t i = 0;
701 int ret;
702 char lynq_cmd_ret_2g[MAX_RET]={0};
703 char lynq_cmd_ret_5g[MAX_RET]={0};
704
705 const char * fw_status = "0x0096"; //0x0096 is normal fw status
706
707 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
708 {
709 perror("popen error!");
710 return -1;
711 }
712 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
713 {
714 perror("fread fail!");
715 if(pclose(fp) == -1)
716 perror("close fp file faild");
717 return -1;
718 }
719
720 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
721 {
722 perror("popen error!");
723 if(pclose(fp) == -1)
724 perror("clsoe fp file faild");
725 return -1;
726 }
727 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
728 {
729 perror("fread fail!");
730 if(pclose(fp1) == -1)
731 perror("clsoe fp1 file faild");
732 if(pclose(fp) == -1)
733 perror("clsoe fp file faild");
734 return -1;
735 }
736
737 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
738 {
739 ret=pclose(fp);
740 if(ret==-1)
741 {
742 perror("close fp file faild");
743 }
744 ret=pclose(fp1);
745 if(ret==-1)
746 {
747 perror("close fp1 file faild");
748 }
749 return 0;
750 }else
751 {
752 ret=pclose(fp);
753 if(ret==-1)
754 {
755 perror("close file faild");
756 }
757 if(pclose(fp1) == -1)
758 {
759 perror("clsoe file fp1 faild");
760 }
761 RLOGD("current fw status --error--");
762 return 1;
763 }
764}
765
qs.xiong1d4263a2023-09-06 10:46:23 +0800766/*
767eg: wl counters info
768sh-3.2# wl counters
769counters_version 30
770datalen 1648
771Slice_index: 0
772reinitreason_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)
773reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
774
775check reinit status
776return 0 ===> fw did wl reinit cmd
777*/
778static int check_current_reinit_info()
779{
780 FILE *fp;
781 int ret;
782 char lynq_cmd_ret[MAX_RET]={0};
783 char * dest;
784 char destid[3]={0};
785 if((fp=popen("wl counters","r"))==NULL)
786 {
787 perror("popen error!");
788 return -1;
789 }
790 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
791 {
792 perror("fread fail!");
793 if(pclose(fp) == -1)
794 {
795 perror("close fp file faild");
796 }
797 return -1;
798 }
799 dest = strstr(lynq_cmd_ret,"reinit ");
800 if(dest != NULL)
801 {
802 dest +=strlen("reinit ");
803 RLOGD("current get dest str is %s",dest);
804 memcpy(destid,dest,2);
805 ret = atoi(destid);
806 RLOGD("get current wl counters cmd return counts is %d",ret);
807 if( ret != 0 )
808 {
809 RLOGD("current fw did run cmd wl reinit");
810 if( pclose(fp) == -1 )
811 {
812 perror("close fp file faild");
813 }
814 return 0;
815 }
816 }
817 if( pclose(fp) == -1 )
818 {
819 perror("close fp file faild");
820 }
821 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
822 return -1;
823}
824
qs.xiong44fac672023-08-29 16:15:55 +0800825static void APTmpWatcherThreadProc() {
826
827 int i = 0;
828 int delytime = 300;
829 g_ap_tmp_watcher_stop_flag = 0;
830
831 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
832 while(1)
833 {
834 sleep(1);
835 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800836 if ( (i % 30) == 0 )
837 {
838 if ( check_current_reinit_info() == 0 )
839 {
840 system("wl reset_cnts");
841 system("wl down");
842 system("wl up");
843 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
844 }
845 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800846 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800847 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800848 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800849 {
850 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
851 {
852 system("wl down");
853 system("wl up");
854 }
qs.xiong44fac672023-08-29 16:15:55 +0800855 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800856 }
857 if ( i == delytime )
858 {
qs.xiong44fac672023-08-29 16:15:55 +0800859 i = 0;
860 }
861 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
862 {
863 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
864 return;
865 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800866
qs.xiong44fac672023-08-29 16:15:55 +0800867 }
868
869}
870
qs.xiongf0128b12023-06-29 17:29:39 +0800871static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
872{
873 char LYNQ_DISABLE_CMD[128]={0};
874
875 CHECK_IDX(idx, CTRL_STA);
876 CHECK_WPA_CTRL(CTRL_STA);
877
878 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800879 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800880 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800881
qs.xiongf0128b12023-06-29 17:29:39 +0800882 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800883
qs.xiongf0128b12023-06-29 17:29:39 +0800884}
885
qs.xiongb37f8c42023-09-13 21:21:58 +0800886static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
887{
888 char LYNQ_ENABLE_CMD[128]={0};
889
890 CHECK_IDX(idx, CTRL_STA);
891 CHECK_WPA_CTRL(CTRL_STA);
892
893
894 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800895 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800896 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
897
898 return 0;
899
900}
901
902static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
903{
904
905 int index,networkid;
906
907 for ( index = 0; index < len ;index++)
908 {
909 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800910 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800911 {
912 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
913 }
914 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
915 }
916 return 0;
917
918}
919
920
921/*
922 dis_net_list user disconnect list
923*/
924static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
925{
qs.xiong09560402023-10-27 21:58:55 +0800926 RLOGD("enter %s %d",__func__,__LINE__);
927 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800928 int count,ncount,index;
929 int flag = 0;
930 int merge_index = 0;
931 int net_no_list[128];
932
qs.xiong09560402023-10-27 21:58:55 +0800933 for(ncount = 0;ncount < valid_num; ncount++ )
934 {
935 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
936 }
937
qs.xiongb37f8c42023-09-13 21:21:58 +0800938 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
939 for( count = 0; count < index; count++)
940 {
941 for(ncount = 0; ncount < valid_num; ncount++)
942 {
qs.xiong09560402023-10-27 21:58:55 +0800943 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800944 if(net_no_list[count] == dis_net_list[ncount])
945 {
qs.xiong09560402023-10-27 21:58:55 +0800946 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800947 flag = 1;
948 break;
949 }
950 }
951 if( flag != 1 )
952 {
953 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800954 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 +0800955 merge_index ++;
956 }
957 flag = 0;
958 }
959 * outlen =merge_index;
960 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
961 return;
962}
qs.xiongf0128b12023-06-29 17:29:39 +0800963
qs.xiong455c30b2023-04-12 11:40:02 +0800964void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
965{
966 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800967 char *wpanetid;
968 char destid[3] = {0};
969 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800970 *error = LYNQ_WAIT_CONNECT_ACTIVE;
971 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
972 {
973 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
974 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
975 return;
976 }
977
qs.xiong20202422023-09-06 18:01:18 +0800978 if (strstr(modify, "Trying to associate") != NULL)
979 {
980 RLOGD("Current sta is Trying to associate");
981 *state = LYNQ_WIFI_STATUS_EGNORE;
982 g_sta_conncet_status_flag = 1;
983 return;
984 }
985
qs.xiong455c30b2023-04-12 11:40:02 +0800986 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
987 {
988 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800989 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800990 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800991 return;
992 }
993
qs.xiong1e81dfa2023-09-27 15:52:37 +0800994 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
995 {
996 *error = LYNQ_PSW_ERROR;
997 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
998 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
999 g_sta_conncet_status_flag = 0;
1000 return;
1001 }
qs.xiong455c30b2023-04-12 11:40:02 +08001002 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1003 {
qs.xiongf0128b12023-06-29 17:29:39 +08001004 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1005 wpanetid = strstr(modify,"id=");
1006 if ( wpanetid != NULL )
1007 {
1008 wpanetid +=strlen("id=");
1009 memcpy(destid,wpanetid,2);
1010 tmpdisid = atoi(destid);
1011
1012 }
qs.xiong455c30b2023-04-12 11:40:02 +08001013 pReason = strstr(modify, "reason=");
1014 if (pReason != NULL)
1015 {
1016 pReason += strlen("reason=");
1017 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1018 {
1019 *error = LYNQ_TIME_OUT;
1020 }
1021 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1022 {
1023 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001024 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001025 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001026 // you.chen@2023-10-17 only disable network during autoconnect
1027 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 +08001028 {
1029 RLOGE("stop wlan0 network %d fail",tmpdisid);
1030 }
qs.xiong455c30b2023-04-12 11:40:02 +08001031 }
1032 else
1033 {
1034 *error = LYNQ_UNSPECIFIED_REASON;
1035 }
qs.xiong455c30b2023-04-12 11:40:02 +08001036 }
1037 else
1038 {
1039 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001040 }
qs.xiongf0128b12023-06-29 17:29:39 +08001041 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001042 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001043 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001044
1045 }
1046
1047 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1048 {
1049 *error = LYNQ_NOT_FIND_AP;
1050 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1051 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001052 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001053 return;
1054 }
1055
1056
1057 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1058 {
1059 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1060 pReason = strstr(modify, "status_code=");
1061 if (pReason != NULL)
1062 {
1063 pReason += strlen("status_code=");
1064 if (memcmp(pReason, "17", 2) == 0)
1065 {
1066 *error = LYNQ_AP_UNABLE_HANDLE;
1067 }
1068 else if (memcmp(pReason, "1",1) == 0)
1069 {
1070 *error = LYNQ_UNSPECIFIED_REASON;
1071 }
1072 else
1073 {
1074 *error = LYNQ_UNSPECIFIED_REASON;
1075 }
1076
1077 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1078 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 +08001079 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001080 return;
1081 }
1082 else
1083 {
1084 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1085 *error = LYNQ_UNSPECIFIED_REASON;
1086 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1087 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1088 return;
1089 }
1090 }
1091
1092 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1093 {
1094 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1095 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1096 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1097 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1098 return;
1099 }
1100
1101 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1102 {
1103 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1104 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1105 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1106 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1107 return;
1108 }
1109
qs.xiongdf637b22023-10-26 19:30:07 +08001110 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1111 if (strstr(modify, "Association request to the driver failed") != NULL)
1112 {
1113 RLOGD("Association request to the driver failed --- recover");
1114 system("wl down");
1115 system("wl up");
1116 *error = LYNQ_UNSPECIFIED_REASON;
1117 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1118 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1119 return;
1120 }
1121 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1122
1123
you.chen32cb31e2023-04-13 14:05:45 +08001124 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001125 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001126 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001127 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1128 return;
1129
1130}
1131
qs.xiongfcc914b2023-07-06 21:16:20 +08001132void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1133{
1134 char *pReason;
1135 char *wpanetid;
1136 char destid[3];
1137 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1138 *networkid = -1;
1139 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1140 {
1141 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1142 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1143 return;
1144 }
1145 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1146 {
1147 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1148 wpanetid = strstr(modify,"id=");
1149 if ( wpanetid != NULL )
1150 {
1151 wpanetid +=strlen("id=");
1152 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1153 if (memcpy(destid,wpanetid,2) != NULL)
1154 {
1155 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1156 *networkid = atoi(destid);
1157 RLOGD("get networkid is %d\n",*networkid);
1158 }
1159 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1160 }
1161 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1162 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1163 return;
1164 }
1165 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1166 {
1167 wpanetid = strstr(modify,"id=");
1168 if ( wpanetid != NULL )
1169 {
1170 wpanetid +=strlen("id=");
1171 if (memcpy(destid,wpanetid,2) != NULL)
1172 {
1173 *networkid = atoi(destid);
1174 RLOGD("get networkid is %d\n",*networkid);
1175 }
1176 }
1177 pReason = strstr(modify, "reason=");
1178 if (pReason != NULL)
1179 {
1180 pReason += strlen("reason=");
1181 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1182 {
1183 *error = LYNQ_TIME_OUT;
1184 }
1185 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1186 {
1187 *error = LYNQ_PSW_ERROR;
1188 }
1189 else
1190 {
1191 *error = LYNQ_UNSPECIFIED_REASON;
1192 }
1193 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1194 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1195 return;
1196 }
1197 else
1198 {
1199 *error = LYNQ_UNSPECIFIED_REASON;
1200 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1201 return;
1202 }
1203 }
1204 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1205 {
1206 wpanetid = strstr(modify,"id=");
1207 if ( wpanetid != NULL )
1208 {
1209 wpanetid +=strlen("id=");
1210 if (memcpy(destid,wpanetid,2) != NULL)
1211 {
1212 *networkid = atoi(destid);
1213 RLOGD("get networkid is %d\n",*networkid);
1214 }
1215 }
1216 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1217 pReason = strstr(modify, "status_code=");
1218 if (pReason != NULL)
1219 {
1220 pReason += strlen("status_code=");
1221 if (memcmp(pReason, "17", 2) == 0)
1222 {
1223 *error = LYNQ_AP_UNABLE_HANDLE;
1224 }
1225 else if (memcmp(pReason, "1",1) == 0)
1226 {
1227 *error = LYNQ_UNSPECIFIED_REASON;
1228 }
1229 else
1230 {
1231 *error = LYNQ_UNSPECIFIED_REASON;
1232 }
1233 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1234 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1235 return;
1236 }
1237 else
1238 {
1239 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1240 *error = LYNQ_UNSPECIFIED_REASON;
1241 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001242 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001243 return;
1244 }
1245 }
1246 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1247 {
1248 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1249 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1250 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1251 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1252 return;
1253 }
1254 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1255 {
1256 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1257 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1258 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1259 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1260 return;
1261 }
1262 RLOGD("EVENT : %s\n", modify);
1263 *error = LYNQ_UNSPECIFIED_REASON;
1264 *state = LYNQ_WIFI_STATUS_EGNORE;
1265 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1266 return;
1267}
you.chen70f377f2023-04-14 18:17:09 +08001268static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1269{
you.chen6d247052023-06-01 16:39:54 +08001270 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001271 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1272 {
1273 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1274 g_sta_callback_func(g_sta_callback_priv, state, error);
1275 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1276 }
you.chen6d247052023-06-01 16:39:54 +08001277 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001278}
qs.xiongfcc914b2023-07-06 21:16:20 +08001279static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1280{
1281 pthread_mutex_lock(&s_sta_callback_mutex);
1282 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1283 {
1284 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1285 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1286 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1287 }
1288 pthread_mutex_unlock(&s_sta_callback_mutex);
1289}
you.chen70f377f2023-04-14 18:17:09 +08001290
you.chen35020192022-05-06 11:30:57 +08001291static void STAWatcherThreadProc() {
1292 size_t len = MAX_RET;
1293 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001294 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001295 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001296 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001297
you.chen6c2dd9c2022-05-16 17:55:28 +08001298 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001299 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001300
you.chen70f377f2023-04-14 18:17:09 +08001301 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001302 while (g_sta_watcher_stop_flag == 0)
1303 {
you.chenf711c8a2023-04-13 13:49:45 +08001304 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001305 {
you.chen35020192022-05-06 11:30:57 +08001306 continue;
1307 }
you.chenf711c8a2023-04-13 13:49:45 +08001308
you.chen6c2dd9c2022-05-16 17:55:28 +08001309 memset(msg_notify, 0, MAX_RET);
1310 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001311 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001312 {
you.chen35020192022-05-06 11:30:57 +08001313 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001314 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1315 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001316 {
you.chen35020192022-05-06 11:30:57 +08001317 g_sta_scan_finish_flag = 1;
1318 }
1319
qs.xiong9fbf74e2023-03-28 13:38:22 +08001320 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001321 {
you.chen35020192022-05-06 11:30:57 +08001322 continue;
1323 }
qs.xiong455c30b2023-04-12 11:40:02 +08001324 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001325 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1326 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1327 {
1328 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1329 }
you.chen0c9bee22023-10-25 13:03:14 +08001330 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1331 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1332 {
1333 RLOGD("donot report scan result during in call connect manual");
1334 }
you.chen6e724162023-10-19 19:10:01 +08001335 else
1336 {
1337 notify_connect_status(state, error);
1338 }
1339 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001340
1341 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001342 {
you.chen70f377f2023-04-14 18:17:09 +08001343 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001344 if (last_state != state)
1345 {
1346 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1347 {
1348 system_call_v("%s %s", sta_status_change_script, "connect");
1349 }
1350 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1351 {
1352 system_call_v("%s %s", sta_status_change_script, "disconnect");
1353 }
1354 }
qs.xiong09560402023-10-27 21:58:55 +08001355
you.chenc9928582023-04-24 15:39:37 +08001356 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001357 if (g_sta_fake_scan_finish_flag == 1)
1358 {
1359 g_sta_fake_scan_finish_flag = 0;
1360 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1361 }
you.chen32cb31e2023-04-13 14:05:45 +08001362 }
you.chen35020192022-05-06 11:30:57 +08001363 }
1364 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001365 if (lynq_wpa_ctrl != NULL)
1366 {
you.chen92fd5d32022-05-25 10:09:47 +08001367 wpa_ctrl_detach(lynq_wpa_ctrl);
1368 wpa_ctrl_close(lynq_wpa_ctrl);
1369 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001370}
qs.xiongfcc914b2023-07-06 21:16:20 +08001371static void STAAutoWatcherThreadProc() {
1372 size_t len = MAX_RET;
1373 char msg_notify[MAX_RET];
1374 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001375 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001376 int idle_count = 0;
1377 int networkid;
1378 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1379 g_sta_auto_watcher_stop_flag = 0;
1380 RLOGD("STAAutoWatcherThreadProc thread started ------");
1381 while (g_sta_auto_watcher_stop_flag == 0)
1382 {
1383 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1384 {
1385 continue;
1386 }
1387 memset(msg_notify, 0, MAX_RET);
1388 len = MAX_RET;
1389 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1390 {
1391 msg_notify[len+1] = '\0';
1392 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1393 if (strstr(msg_notify, state_scan_result) != NULL)
1394 {
1395 g_sta_auto_scan_finish_flag = 1;
1396 }
1397 if (g_sta_auto_callback_func == NULL)
1398 {
1399 continue;
1400 }
1401 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1402 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001403 }
1404 }
1405 if (lynq_wpa_ctrl != NULL)
1406 {
1407 wpa_ctrl_detach(lynq_wpa_ctrl);
1408 wpa_ctrl_close(lynq_wpa_ctrl);
1409 }
1410}
qs.xiongf1b525b2022-03-31 00:58:23 -04001411
you.chen70f377f2023-04-14 18:17:09 +08001412// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1413static void GlobalWatcherThreadProc()
1414{
1415 int ret, connect_timeout, service_abnormal;
1416 error_number_s error_num = -1;
1417 inner_sta_status_s sta_status;
1418 scan_info_s *scan_list = NULL;
1419 int i, scan_len=0;
1420 char connecting_ssid[64];
1421 struct timeval now;
1422
1423 RLOGD("GlobalWatcherThreadProc start to run");
1424
1425 while (1)
1426 {
1427 pthread_mutex_lock(&s_global_check_mutex);
1428 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1429 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1430 {
1431 pthread_mutex_unlock(&s_global_check_mutex);
1432 usleep(50*1000);
1433 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1434 continue;
1435 }
1436
1437 connect_timeout = 0;
1438 service_abnormal = 0;
1439 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1440 {
1441 while (1)
1442 {
1443 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1444 if (ret == ETIME)
1445 {
1446 connect_timeout = 1;
1447 }
1448 else if (ret != 0)
1449 {
1450 gettimeofday(&now,NULL);
1451 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1452 {
1453 usleep(SLEEP_TIME_ON_IDLE);
1454 continue;
1455 }
1456 connect_timeout = 1;
1457 }
1458 sta_status = s_sta_status;
1459 error_num = s_sta_error_number;
1460 s_sta_status = INNER_STA_STATUS_INIT;
1461 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1462 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1463 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1464 break;
1465 }
1466 }
1467 if (s_service_invoke_timeout_cnt > 10)
1468 {
1469 service_abnormal = 1;
1470 s_service_invoke_timeout_cnt = 0;
1471 }
1472 pthread_mutex_unlock(&s_global_check_mutex);
1473
1474 if (service_abnormal == 1)
1475 {
1476 sleep(1);
1477 RLOGE("wpa service is abnormal info app to exit");
1478 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001479
1480 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1481
you.chen70f377f2023-04-14 18:17:09 +08001482 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1483 }
1484
1485 if (sta_status == INNER_STA_STATUS_CANCEL)
1486 {
1487 continue;
1488 }
1489 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1490 {
1491 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1492 }
1493 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1494 {
1495 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1496 {
1497 for(i=0; i < scan_len;i++)
1498 {
1499 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1500 {
1501 error_num = LYNQ_AUTH_ERROR;
1502 break;
1503 }
1504 }
1505 free(scan_list);
1506 }
1507 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1508 }
1509 else if (connect_timeout == 0)
1510 {
1511 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1512 }
1513 else // wait timeout
1514 {
1515 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1516 {
1517 ; // wpa service abnormal
1518 }
1519 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1520 {
1521 RLOGD("GlobalWatcherThreadProc notify connected");
1522 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1523 }
1524 else
1525 {
1526 RLOGD("GlobalWatcherThreadProc notify timeout");
1527 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1528 }
1529 }
1530 } // while (1)
1531}
1532
qs.xiong1af5daf2022-03-14 09:12:12 -04001533int lynq_wifi_enable(void)
1534{
you.chen35020192022-05-06 11:30:57 +08001535 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001536 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001537 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001538 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1539
qs.xiong9fbf74e2023-03-28 13:38:22 +08001540 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1541 {
you.chend2fef3f2023-02-13 10:50:35 +08001542 goto out_enable;
1543 }
1544
you.chenc9928582023-04-24 15:39:37 +08001545 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001546 if (ret != 0)
1547 {
1548 //printf("service state %d\n", ret);
1549 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001550 ret = -1;
1551 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001552 }
lhfe8da902022-10-11 18:55:36 +08001553
you.chen70f377f2023-04-14 18:17:09 +08001554 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1555 {
1556 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1557 if(ret<0)
1558 {
1559 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1560 ret = -1;
1561 goto out_enable;
1562 }
1563 }
1564
you.chend2fef3f2023-02-13 10:50:35 +08001565 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1566 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1567 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1568 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1569out_enable:
1570 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001571 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001572}
1573
qs.xiong1af5daf2022-03-14 09:12:12 -04001574int lynq_wifi_disable(void)
1575{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001576 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001577 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001578 g_ap_watcher_stop_flag = 1;
1579 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001580 g_sta_auto_watcher_stop_flag = 1;
qs.xiong9d8f3102023-12-07 20:11:37 +08001581/* g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001582 if (g_ap_watcher_pid != 0)
1583 pthread_join(g_ap_watcher_pid, NULL);
qs.xiong9d8f3102023-12-07 20:11:37 +08001584*/
you.chen35020192022-05-06 11:30:57 +08001585 if (g_sta_watcher_pid != 0)
1586 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001587 if (g_sta_auto_watcher_pid != 0)
1588 pthread_join(g_sta_auto_watcher_pid, NULL);
qs.xiong17579bb2024-03-11 19:08:06 +08001589 if ( g_lynq_wpa_ctrl[0] != NULL)
1590 {
1591 if(g_lynq_wpa_ctrl[0]->ctrl != NULL)
1592 {
1593 wpa_ctrl_close(g_lynq_wpa_ctrl[0]->ctrl);
1594 }
1595 free(g_lynq_wpa_ctrl[0]);
1596 g_lynq_wpa_ctrl[0] = NULL;
1597 }
1598
1599 if ( g_lynq_wpa_ctrl[1] != NULL )
1600 {
1601 if( g_lynq_wpa_ctrl[1]->ctrl != NULL )
1602 {
1603 wpa_ctrl_close(g_lynq_wpa_ctrl[1]->ctrl);
1604 }
1605 free(g_lynq_wpa_ctrl[1]);
1606 g_lynq_wpa_ctrl[1] = NULL;
1607 }
qs.xiong44fac672023-08-29 16:15:55 +08001608 if (g_ap_tmp_watcher_pid != 0)
1609 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001610 g_ap_watcher_pid = 0;
1611 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001612 g_sta_auto_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08001613// g_ap_tmp_watcher_pid = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +08001614 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001615 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001616 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1617 return 0;
1618}
1619
1620static inline char inner_convert_char(char in)
1621{
1622 if (in >= '0' && in <= '9')
1623 {
1624 return in - '0';
1625 }
1626 else if (in >= 'a' && in <= 'f')
1627 {
1628 return in - 'a' + 10;
1629 }
1630 else if (in >= 'A' && in <= 'F')
1631 {
1632 return in - 'A' + 10;
1633 }
1634 else
1635 {
1636 return '\xff';
1637 }
1638}
1639
1640static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1641{
1642 char *p;
1643 size_t pos = 0;
1644 if (NULL == out_ssid)
1645 return;
1646 //printf("input ssid=[%s]\n", ssid);
1647 memset(out_ssid, 0, out_ssid_len);
1648 if (NULL == ssid)
1649 return;
1650 p = strchr(ssid, '\\');
1651 if (NULL == p)
1652 {
1653 strncpy(out_ssid, ssid, out_ssid_len);
1654 //printf(" first %s\n", out_ssid);
1655 }
1656 else
1657 {
1658 pos = p - ssid;
1659 memcpy(out_ssid, ssid, pos);
1660 //printf("pos %lu -- %s\n", pos, out_ssid);
1661 for(; pos < out_ssid_len; pos ++)
1662 {
1663 if (p[0] == '\0')
1664 {
1665 //printf(" out %s\n", out_ssid);
1666 return;
1667 }
1668 else if (p[0] != '\\')
1669 {
1670 out_ssid[pos] = p[0];
1671 p += 1;
1672 }
1673 else if (p[1] == 'x' || p[1] == 'X')
1674 {
1675 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1676 p += 4;
1677 }
1678 else if (p[1] == '\\')
1679 {
1680 out_ssid[pos] = '\\';
1681 p += 2;
1682 }
1683 else if (p[1] == 't')
1684 {
1685 out_ssid[pos] = '\t';
1686 p += 2;
1687 }
1688 else if (p[1] == 'r')
1689 {
1690 out_ssid[pos] = '\r';
1691 p += 2;
1692 }
1693 else if (p[1] == 'n')
1694 {
1695 out_ssid[pos] = '\n';
1696 p += 2;
you.chen34983432023-12-21 20:39:06 +08001697 }
1698 else
1699 {
1700 out_ssid[pos] = p[1];
1701 p += 2;
you.chend2fef3f2023-02-13 10:50:35 +08001702 }//todo find a better way to convert?
1703 }
1704 }
1705 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001706}
qs.xiong1af5daf2022-03-14 09:12:12 -04001707
you.chen35020192022-05-06 11:30:57 +08001708static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001709 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001710 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001711 RLOGD("enter inner_get_param");
1712 if (out_put == NULL)
1713 {
1714 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001715 return -1;
1716 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001717 if (param_name == NULL)
1718 {
1719 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001720 return -1;
1721 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001722 if (param_name[0] == '\0')
1723 {
1724 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001725 return -1;
1726 }
1727
1728 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1729
1730 CHECK_WPA_CTRL(interface);
1731
1732 DO_REQUEST(lynq_cmd_get);
1733
qs.xiong9fbf74e2023-03-28 13:38:22 +08001734 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1735 {
1736 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001737 return -1;
1738 }
1739
you.chena6fa5b22022-05-18 10:28:19 +08001740// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001741 if (strcmp(param_name, "ssid") == 0)
1742 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001743 if (cmd_reply[0] == '\"')
1744 {
you.chend2fef3f2023-02-13 10:50:35 +08001745 ssid_len = reply_len - 1;
1746 memcpy(out_put, cmd_reply + 1, ssid_len);
1747 if (out_put[ssid_len-1] == '\"')
1748 {
1749 out_put[ssid_len-1] = '\0';
1750 }
1751 else
1752 {
1753 out_put[ssid_len] = '\0';
1754 }
1755 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 else
1757 {
you.chend2fef3f2023-02-13 10:50:35 +08001758 ssid_len = reply_len / 2;
1759 for(i=0; i<ssid_len; i++)
1760 {
1761 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1762 }
1763 out_put[ssid_len] = '\0';
1764 }
1765 }
1766 else
1767 {
1768 memcpy(out_put, cmd_reply, reply_len + 1);
1769 }
you.chen35020192022-05-06 11:30:57 +08001770 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001771}
qs.xiong1af5daf2022-03-14 09:12:12 -04001772
you.chen35020192022-05-06 11:30:57 +08001773static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1774 int ret = 0;
1775 char * end = str + len - 1;
1776 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 while(str < end)
1778 {
1779 if (*str == delimiter)
1780 {
you.chen35020192022-05-06 11:30:57 +08001781 *str++ = '\0';
1782 results[ret++] = str;
1783 continue;
1784 }
1785 str++;
1786 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 if (*str == delimiter)
1788 {
you.chen35020192022-05-06 11:30:57 +08001789 *str = '\0';
1790 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001791
you.chen6ed36a62023-04-27 17:51:56 +08001792 results[ret] = NULL;
1793
you.chen35020192022-05-06 11:30:57 +08001794 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001795}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001796/*
1797 *add func to get conencted STA device ip from dnsmasq ap0.lease
1798 *return 0 means get ip success
1799 */
1800static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1801{
1802 char * p;
1803 int ret;
1804 char cmd[256]={0};
1805 if (NULL == mac || NULL == ip)
1806 return -1;
1807 memset(ip, 0, ip_len);
1808 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1809 ret = exec_cmd(cmd, ip, ip_len);
1810 if( ret == 0 )
1811 {
1812 p = strchr(ip, '\n');
1813 if (NULL != p)
1814 {
1815 *p = '\0';
1816 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1817 return ret;
1818 }else
1819 {
1820 ret = -1;
1821 }
1822 }
1823 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1824 return ret;
1825
1826}
qs.xiong7a105ce2022-03-02 09:43:11 -05001827
you.chend2fef3f2023-02-13 10:50:35 +08001828static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1829{
1830 char * p;
1831 int ret = 0;
1832 char cmd[256]={0};
1833 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001834 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001835 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001836 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001837 ret = exec_cmd(cmd, ip, ip_len);
1838 p = strchr(ip, '\n');
1839 if (NULL != p)
1840 {
1841 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001842 }else
1843 {
1844 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001845 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001846 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001847 return ret;
1848}
1849
you.chend2fef3f2023-02-13 10:50:35 +08001850static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001851 struct in_addr addr ={0};
1852 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001853 char cmd[64] = {0};
1854 char * p;
1855 int ret;
you.chen35020192022-05-06 11:30:57 +08001856
qs.xiong9fbf74e2023-03-28 13:38:22 +08001857 if (ip == NULL || *ip == '\0' || hostname == NULL)
1858 {
1859 RLOGE("ip == NULL or hostname == NULL");
1860 return -1;
you.chen35020192022-05-06 11:30:57 +08001861 }
1862
you.chend2fef3f2023-02-13 10:50:35 +08001863 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 if (inet_aton(ip, &addr) == 0)
1865 {
you.chen35020192022-05-06 11:30:57 +08001866 printf("---inet_aton fail\n");
1867 return -1;
1868 }
1869
1870 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1871
qs.xiong9fbf74e2023-03-28 13:38:22 +08001872 if (ht == NULL)
1873 {
you.chen186d3c32023-05-18 14:19:46 +08001874 hostname[0] = '\0';
1875 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1876 ret = exec_cmd(cmd, hostname, 32);
1877 if (ret == 0)
1878 {
1879 p = strchr(hostname, '\n');
1880 if (p != NULL)
1881 {
1882 *p = '\0';
1883 }
1884 return 0;
1885 }
1886 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001887 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001888 herror(NULL);
1889 return -1;
1890 }
1891
1892 strcpy(hostname, ht->h_name);
1893
1894 return 0;
1895}
1896
1897static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1898{
1899 int count, index, words_count;
1900 char * split_lines[128]= {0};
1901 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001902 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001903 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001904 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001905
1906 CHECK_WPA_CTRL(ap_sta);
1907
1908 DO_REQUEST(lynq_wifi_list_networks);
1909
1910 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1911
1912 //@todo check ssid field to compatible
1913
1914 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001915 for(index=1; index < count; index++)
1916 {
you.chen35020192022-05-06 11:30:57 +08001917 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001918 if (words_count > 2)
1919 {
you.chend2fef3f2023-02-13 10:50:35 +08001920 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001921 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1922 {
you.chen35020192022-05-06 11:30:57 +08001923 net_no_list[ret++] = atoi(split_words[0]);
1924 }
1925 }
1926 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001927 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001928 return ret;
1929}
1930
1931static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001932 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001933 CHECK_WPA_CTRL(ap_sta);
1934 const char *lynq_wifi_add_network = "ADD_NETWORK";
1935
qs.xiong9fbf74e2023-03-28 13:38:22 +08001936 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001937 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001938 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1939 {
1940 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001941 return -1;
1942 }
1943
qs.xiong9fbf74e2023-03-28 13:38:22 +08001944 for(i=0;i<reply_len;i++)
1945 {
1946 if(cmd_reply[i] == '\n')
1947 {
you.chen35020192022-05-06 11:30:57 +08001948 cmd_reply[i] = '\0';
1949 break;
1950 }
1951 }
1952 return atoi(cmd_reply);
1953}
you.chena6cd55a2022-05-08 12:20:18 +08001954
you.chen35020192022-05-06 11:30:57 +08001955static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1956{
1957 int count, index;
1958 int net_no_list[128];
1959
qs.xiong9fbf74e2023-03-28 13:38:22 +08001960 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001961 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001962 for (index=0; index < count; index++)
1963 {
1964 if (net_no_list[index] == net_no)
1965 {
you.chen35020192022-05-06 11:30:57 +08001966 return 0;
1967 }
1968 }
1969
1970 if (count >= 1)
1971 index = net_no_list[count - 1];
1972 else
1973 index = -1;
1974
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 while (index < net_no )
1976 {
you.chen35020192022-05-06 11:30:57 +08001977 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001978 if (index >= net_no)
1979 { // required network no created
1980 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001981 return 0;
1982 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001983 else if( index < 0)
1984 {
1985 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001986 return -1;
1987 }
you.chen35020192022-05-06 11:30:57 +08001988 }
1989
1990 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001991 {
1992 RLOGE("[lynq_check_network_number] network index < 0");
1993 return -1;
1994 }
1995 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001996 return 0;
1997}
1998
1999static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08002000 if (freq > 5000 && freq < 6000)
2001 {
you.chen35020192022-05-06 11:30:57 +08002002 return LYNQ_WIFI_5G_band;
2003 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 else if (freq > 2000 && freq < 3000)
2005 {
you.chen35020192022-05-06 11:30:57 +08002006 return LYNQ_WIFI_2G_band;
2007 }
2008 return LYNQ_WIFI_2_and_5G_band;
2009}
2010
2011static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002012 if (key_mgmt != NULL)
2013 {
2014 if (memcmp( key_mgmt, "NONE", 4) == 0)
2015 {
you.chen35020192022-05-06 11:30:57 +08002016 return LYNQ_WIFI_AUTH_OPEN;
2017 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002018 else if (memcmp( key_mgmt, "WEP", 3) == 0)
2019 {
you.chen35020192022-05-06 11:30:57 +08002020 return LYNQ_WIFI_AUTH_WEP;
2021 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2023 {
you.chen35020192022-05-06 11:30:57 +08002024 return LYNQ_WIFI_AUTH_WPA_PSK;
2025 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002026 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2027 {
you.chen35020192022-05-06 11:30:57 +08002028 return LYNQ_WIFI_AUTH_WPA2_PSK;
2029 }
2030 }
2031
2032 return -1;
2033}
2034
2035static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002036 if (flag != NULL)
2037 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002038 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 +08002039 {
2040 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002041 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002042 {
2043 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2044 }else if (strstr( flag, "WPA2-PSK") != NULL)
2045 {
you.chen35020192022-05-06 11:30:57 +08002046 return LYNQ_WIFI_AUTH_WPA2_PSK;
2047 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002048 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002049 {
you.chen35020192022-05-06 11:30:57 +08002050 return LYNQ_WIFI_AUTH_WPA_PSK;
2051 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002052 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002053 {
you.chen35020192022-05-06 11:30:57 +08002054 return LYNQ_WIFI_AUTH_WEP;
2055 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002056 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002057 {
you.chen35020192022-05-06 11:30:57 +08002058 return LYNQ_WIFI_AUTH_OPEN;
2059 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002060 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002061 {
you.chend2fef3f2023-02-13 10:50:35 +08002062 return LYNQ_WIFI_AUTH_OPEN;
2063 }
qs.xiong46f41562023-07-11 21:06:47 +08002064 else
2065 {
2066 RLOGD("convert_max_auth_from_flag not-found auth mode");
2067 }
you.chen35020192022-05-06 11:30:57 +08002068 }
2069
2070 return -1;
2071}
2072
2073static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2074 switch (bw) {
2075 case 10:
2076 return LYNQ_WIFI_BANDWIDTH_HT10;
2077 break;
2078 case 20:
2079 return LYNQ_WIFI_BANDWIDTH_HT20;
2080 break;
2081 case 40:
2082 return LYNQ_WIFI_BANDWIDTH_HT40;
2083 break;
2084 case 80:
2085 return LYNQ_WIFI_BANDWIDTH_HT80;
2086 break;
2087 default:
2088 break;
2089 }
2090
2091 return -1;
2092}
2093
you.chen70f377f2023-04-14 18:17:09 +08002094static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002095static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2096 int i, count;
2097 char *p;
2098 const char *lynq_status_cmd = "STATUS";
2099 const char * FLAG_SSID = "ssid=";
2100 const char * FLAG_SBSID = "bssid=";
2101 const char * FLAG_KEY_MGMT = "key_mgmt=";
2102 const char * FLAG_FREQ = "freq=";
2103 const char * FLAG_STATE = "wpa_state=";
2104 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002105 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002106 char *split_lines[128] = {0};
2107
2108 CHECK_WPA_CTRL(interface);
2109
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 if (curr_state == NULL)
2111 {
2112 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002113 return -1;
2114 }
2115
2116 DO_REQUEST(lynq_status_cmd);
2117
2118 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2119
2120 curr_state->net_no = -1;
2121 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002122 for(i=0; i < count; i++)
2123 {
2124 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002125 {
you.chen35020192022-05-06 11:30:57 +08002126 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002127 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002128 {
you.chend2fef3f2023-02-13 10:50:35 +08002129 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002130 ret = 0;
2131 continue;
2132 }
you.chenf58b3c92022-06-21 16:53:48 +08002133 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002134 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002135 {
you.chend2fef3f2023-02-13 10:50:35 +08002136 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 +08002137 ret = 0;
2138 continue;
2139 }
you.chen35020192022-05-06 11:30:57 +08002140 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002142 {
you.chen450d0172022-07-15 17:56:48 +08002143 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002145 ret = 0;
2146 continue;
2147 }
2148 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002149 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002150 {
you.chen35020192022-05-06 11:30:57 +08002151 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2152 ret = 0;
2153 continue;
2154 }
you.chend2fef3f2023-02-13 10:50:35 +08002155 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002157 {
you.chend2fef3f2023-02-13 10:50:35 +08002158 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2159 ret = 0;
2160 continue;
2161 }
you.chen35020192022-05-06 11:30:57 +08002162 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002163 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002164 {
you.chen35020192022-05-06 11:30:57 +08002165 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002166 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002167 {
you.chen35020192022-05-06 11:30:57 +08002168 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2169 ret = 0;
2170 continue;
2171 }
2172
2173 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002174 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002175 {
you.chen35020192022-05-06 11:30:57 +08002176 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002177 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002178 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002179 }
2180 }
2181
you.chen70f377f2023-04-14 18:17:09 +08002182 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2183 {
2184 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2185 }
2186
you.chen35020192022-05-06 11:30:57 +08002187 return ret;
2188}
2189
qs.xiongf1b525b2022-03-31 00:58:23 -04002190int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002191{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002192 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002193 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002194
qs.xiong9fbf74e2023-03-28 13:38:22 +08002195 if (ap_ssid == NULL)
2196 {
2197 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002198 return -1;
2199 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002200 else
2201 {
2202 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002203 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002204
qs.xiong9fbf74e2023-03-28 13:38:22 +08002205 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2206 {
2207 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002208 return -1;
2209 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002210
you.chen35020192022-05-06 11:30:57 +08002211 CHECK_IDX(idx, CTRL_AP);
2212
2213 CHECK_WPA_CTRL(CTRL_AP);
2214
2215 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2216
2217 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2218 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002219
qs.xiong1b5e3472023-11-27 17:42:20 +08002220 RLOGD("[lynq_wifi_ap_ssid_set] set ssid succeed %d",__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002221 return 0;
you.chen35020192022-05-06 11:30:57 +08002222
qs.xiong7a105ce2022-03-02 09:43:11 -05002223}
2224
you.chen35020192022-05-06 11:30:57 +08002225int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002226{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002227 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002228 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002229 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002230}
2231
qs.xiongc9c79f72022-10-17 15:27:18 +08002232/*****
2233 *frequency <------>channel
2234 *
2235 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2236 *
2237 *
2238 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2239 *
2240 *
2241 * */
2242static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002243 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};
2244 int i;
2245 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2246
qs.xiong69a332b2022-12-02 09:58:57 +08002247 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002248 {
2249 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002250 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002251 }
qs.xiongc00b6032022-11-29 16:28:03 +08002252
2253 if(i == arr_len)
2254 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002255 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002256 return -1;
2257 }
qs.xiongc00b6032022-11-29 16:28:03 +08002258
qs.xiongc9c79f72022-10-17 15:27:18 +08002259 return 0;
2260}
qs.xiong13673462023-02-21 19:12:54 +08002261
2262static int lynq_check_frequencyby_country_code(int input_frequency)
2263{
2264 char str_cnc[]="CN";
2265 char str_dest[20]="";
2266
2267 if( lynq_get_country_code(1,str_dest) != 0 )
2268 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002269 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002270 return -1;
2271 }
2272 if( strncmp(str_dest,str_cnc,2) != 0 )
2273 {
2274 return 0;
2275 }else if( 2473 < input_frequency && input_frequency < 5744)
2276 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002277 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002278 return -1;
2279 }
2280 return 0;
2281}
qs.xiongf1b525b2022-03-31 00:58:23 -04002282int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002283{
qs.xiongc00b6032022-11-29 16:28:03 +08002284 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002285 char lynq_wifi_frequency_cmd[128]={0};
2286 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002287 char lynq_cmd_slect[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002288 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002289 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002290 check = lynq_check_set_frequency(lynq_wifi_frequency);
2291 if(check != 0)
2292 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002293 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002294 return -1;
you.chen35020192022-05-06 11:30:57 +08002295 }
qs.xiong13673462023-02-21 19:12:54 +08002296 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2297 if(check != 0)
2298 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002299 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002300 return -1;
2301 }
2302
qs.xiongc00b6032022-11-29 16:28:03 +08002303 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2304 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002305 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002306 return -1;
2307 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002308
you.chen35020192022-05-06 11:30:57 +08002309 CHECK_IDX(idx, CTRL_AP);
2310
2311 CHECK_WPA_CTRL(CTRL_AP);
2312
2313 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2314 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2315 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2316
you.chen6c2dd9c2022-05-16 17:55:28 +08002317 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002318 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2319 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2320 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002321
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002323}
2324
qs.xiongf1b525b2022-03-31 00:58:23 -04002325int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002326{
you.chen35020192022-05-06 11:30:57 +08002327 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002328 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002329 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002330
qs.xiong9fbf74e2023-03-28 13:38:22 +08002331 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2332 {
2333 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002334 return -1;
2335 }
2336 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002337
qs.xiong9fbf74e2023-03-28 13:38:22 +08002338 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002339}
2340
qs.xiongf1b525b2022-03-31 00:58:23 -04002341int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2342{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002343 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002344 CHECK_IDX(idx, CTRL_AP);
2345 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 case LYNQ_WIFI_BANDWIDTH_HT10:
2347 {
2348 RLOGE("bandwith [%d] not support now\n", bandwidth);
2349 return -1;
2350 }
2351 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002352 {
2353 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2354 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002355 if (system(lynq_cmd_bandwith) != 0 )
2356 {
2357 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002358 return -1;
2359 }
2360 system("wl up");
2361 break;
2362 }
2363 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002364 {
qs.xiong10379192023-02-21 13:19:42 +08002365 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002366 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2367 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002368 if (system(lynq_cmd_bandwith) != 0 )
2369 {
2370 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002371 return -1;
2372 }
2373 system("wl up");
2374 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002375 }
you.chen35020192022-05-06 11:30:57 +08002376 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002377 {
qs.xiong10379192023-02-21 13:19:42 +08002378 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002379 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002380 if (system(lynq_cmd_bandwith) != 0 )
2381 {
2382 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002383 return -1;
2384 }
2385 system("wl up");
2386 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002387 }
2388 default:
you.chen35020192022-05-06 11:30:57 +08002389 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 RLOGE("auth type [%d] not support now\n", bandwidth);
2391 return -1;
you.chen35020192022-05-06 11:30:57 +08002392 }
2393 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002394
2395
you.chen35020192022-05-06 11:30:57 +08002396 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002397}
you.chen35020192022-05-06 11:30:57 +08002398
qs.xiongf1b525b2022-03-31 00:58:23 -04002399int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2400{
you.chen35020192022-05-06 11:30:57 +08002401 int count = 0;
2402 int index = 0;
2403 char *split_words[128] = {0};
2404 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002406 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002407
you.chen35020192022-05-06 11:30:57 +08002408 CHECK_WPA_CTRL(CTRL_AP);
2409
2410 DO_REQUEST(lynq_chanspec_cmd);
2411
2412 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2413 for(;index < count; index++) {
2414 if (strncmp(split_words[index], "bw", 2) != 0) {
2415 continue;
2416 }
2417
2418 index++;
2419 if (index >= count) {
2420 return -1;
2421 }
2422
qs.xiong9fbf74e2023-03-28 13:38:22 +08002423 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002424 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2425 return 0;
2426 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002427 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002428 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002429}
qs.xiong0fb469a2022-04-14 03:50:45 -04002430
qs.xiongf1b525b2022-03-31 00:58:23 -04002431int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002432{
you.chen35020192022-05-06 11:30:57 +08002433 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002434 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002435 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002436
you.chen35020192022-05-06 11:30:57 +08002437 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002438
qs.xiong9fbf74e2023-03-28 13:38:22 +08002439 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2440 {
you.chen35020192022-05-06 11:30:57 +08002441 return -1;
2442 }
2443
2444 system("wl down");
2445 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002446 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002447 return -1;
2448 }
2449 system("wl up");
2450 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002451}
qs.xiong0fb469a2022-04-14 03:50:45 -04002452
qs.xiongf1b525b2022-03-31 00:58:23 -04002453int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002454{
you.chen35020192022-05-06 11:30:57 +08002455 int count = 0;
2456 int index = 0;
2457 char *split_words[128] = {0};
2458 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002459 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002460 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002461
you.chen35020192022-05-06 11:30:57 +08002462 CHECK_WPA_CTRL(CTRL_AP);
2463
2464 DO_REQUEST(lynq_chanspec_cmd);
2465
2466 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002467 for(;index < count; index++)
2468 {
2469 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002470 if (strncmp(split_words[index], "channel", 2) != 0) {
2471 continue;
2472 }
2473
2474 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002475 if (index >= count)
2476 {
you.chen35020192022-05-06 11:30:57 +08002477 return -1;
2478 }
2479
2480 *channel = atoi(split_words[index]);
2481 return 0;
2482 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002483 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002484 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002485}
2486
2487
you.chen35020192022-05-06 11:30:57 +08002488int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002489{
you.chen6c2dd9c2022-05-16 17:55:28 +08002490 char ssid[MAX_CMD] = {0};
2491 int freq = 0;
2492 char lynq_auth_cmd[64]={0};
2493 char lynq_auth_alg_cmd[64]={0};
2494 char lynq_psk_cmd[64]={0};
2495 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002496 char lynq_ieee80211_cmd[64]={0};
2497 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002498 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002499 CHECK_IDX(idx, CTRL_AP);
2500
you.chen6c2dd9c2022-05-16 17:55:28 +08002501 CHECK_WPA_CTRL(CTRL_AP);
2502
qs.xiong9fbf74e2023-03-28 13:38:22 +08002503 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2504 {
2505 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002506 return -1;
2507 }
2508
you.chen92fd5d32022-05-25 10:09:47 +08002509 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002510 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002511 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002512 return 0;
2513 }
2514 else {
2515 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2516 ssid[0] = '\0';
2517 }
2518 lynq_wifi_ap_frequency_get(idx, &freq);
2519
2520 DO_OK_FAIL_REQUEST(cmd_disconnect);
2521 DO_OK_FAIL_REQUEST(cmd_remove_all);
2522 if (ssid[0] != '\0') {
2523 lynq_wifi_ap_ssid_set(idx, ssid);
2524 }
2525 if (freq != 0) {
2526 lynq_wifi_ap_frequency_set(idx, freq);
2527 }
2528 }
2529 }
you.chen35020192022-05-06 11:30:57 +08002530
qs.xiong9fbf74e2023-03-28 13:38:22 +08002531 switch(auth){
2532 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002533 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002534 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002535 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002536 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002537 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002538 break;
2539 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002540 case LYNQ_WIFI_AUTH_WEP:
2541 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002542 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002543 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002544 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002545 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2546
2547 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2548 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2549 break;
2550 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002551 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002552 {
2553 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2554 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2555 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2556
2557 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2558 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2559 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2560 break;
2561
2562 }
you.chen35020192022-05-06 11:30:57 +08002563 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002564 {
2565 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2566 {
you.chen35020192022-05-06 11:30:57 +08002567 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2568 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2569 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002570 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2571 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002572 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002573 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002574 }
2575// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2576// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2577 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002578
you.chen35020192022-05-06 11:30:57 +08002579 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2580 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2581 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002582 break;
2583 }
2584 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2585 {
2586 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2587 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2588 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2589 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2590
2591 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2592 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2593 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2594 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2595 break;
2596 }
2597 case LYNQ_WIFI_AUTH_WPA3_PSK:
2598 {
2599 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2600 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002601 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002602 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2603
2604 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2605 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2606 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2607 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2608 break;
2609 }
2610 default:
you.chen35020192022-05-06 11:30:57 +08002611 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002612 RLOGE("auth type [%d] not support now\n", auth);
2613 return -1;
you.chen35020192022-05-06 11:30:57 +08002614 }
2615 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002616 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002617
qs.xiong9fbf74e2023-03-28 13:38:22 +08002618 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002619}
2620
you.chen35020192022-05-06 11:30:57 +08002621int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002622{
you.chen35020192022-05-06 11:30:57 +08002623 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002624 char lynq_auth_alg_str[MAX_RET] = {0};
2625 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002626 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002627 CHECK_IDX(idx, CTRL_AP);
2628
qs.xiong9fbf74e2023-03-28 13:38:22 +08002629 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2630 {
2631 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002632 return -1;
2633 }
2634
qs.xiong9fbf74e2023-03-28 13:38:22 +08002635 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2636 {
2637 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2638 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002639 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002640 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002641 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002642 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002643 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2644 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002645 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002646 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002647 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002648 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002649 else
2650 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002651 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002652 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002653 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002654 }
you.chen35020192022-05-06 11:30:57 +08002655 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002656 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2657 {
2658 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2659 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002660 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002661 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002662 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002663 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2664 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002665 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002666 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002667 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002668 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002669 else
2670 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002671 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002672 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002673 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002674 }
you.chen35020192022-05-06 11:30:57 +08002675 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002676
2677 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2678 {
2679 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2680 return -1;
2681 }
2682
2683 if (memcmp(lynq_auth_str,"1",1) == 0 )
2684 {
2685 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2686 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002687 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002688 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2689 {
2690 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2691 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002692 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002693 }
2694 else
2695 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002696 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002697 *auth = -1;
2698 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002699
you.chen6c2dd9c2022-05-16 17:55:28 +08002700 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002701}
qs.xiong1af5daf2022-03-14 09:12:12 -04002702
you.chenb95401e2023-05-12 19:39:06 +08002703static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2704{
2705 char status[64];
you.chencba13492023-05-19 13:53:43 +08002706 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002707 curr_status_info curr_state;
2708
2709 CHECK_WPA_CTRL(CTRL_AP);
2710
2711 memset(status, 0, sizeof (status));
2712
2713 curr_state.ap = NULL;
2714 curr_state.state = status;
2715
2716 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002717 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002718 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002719 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002720 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002721 {
2722 return 0;
2723 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002724 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002725 {
2726 DO_REQUEST("RECONNECT");
2727 return inner_check_ap_connected(idx, retry_count+1);
2728 }
you.chencba13492023-05-19 13:53:43 +08002729 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002730 {
2731 printf("retry 10 time\n");
2732 return -1;
2733 }
2734 else
2735 {
you.chen6d247052023-06-01 16:39:54 +08002736 if (strcmp(status, STATE_DISCONNECTED) == 0)
2737 {
2738 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2739 DO_REQUEST(LYNQ_WIFI_CMD);
2740 }
you.chenb95401e2023-05-12 19:39:06 +08002741 return inner_check_ap_connected(idx, retry_count+1);
2742 }
2743 }
2744 return -1;
2745}
qs.xiong1af5daf2022-03-14 09:12:12 -04002746
qs.xiongf1b525b2022-03-31 00:58:23 -04002747int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002748{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002749 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002750 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002751
qs.xiongec8bbeb2023-11-20 15:51:45 +08002752 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002753 CHECK_WPA_CTRL(CTRL_AP);
2754
you.chen0df3e7e2023-05-10 15:56:26 +08002755 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002756 {
you.chen0df3e7e2023-05-10 15:56:26 +08002757 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002758 return -1;
2759 }
you.chen35020192022-05-06 11:30:57 +08002760
qs.xiongb37f8c42023-09-13 21:21:58 +08002761
you.chen35020192022-05-06 11:30:57 +08002762 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2763 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2764
you.chenc9928582023-04-24 15:39:37 +08002765 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2766 if (ret != 0)
2767 {
2768 RLOGE("lynq_wifi_ap_start excute script fail");
2769 return -1;
2770 }
2771
you.chenb95401e2023-05-12 19:39:06 +08002772 if (inner_check_ap_connected(idx, 0) != 0)
2773 {
2774 return -1;
2775 }
2776
you.chen0df3e7e2023-05-10 15:56:26 +08002777 check_tether_and_notify();
qs.xiong9d8f3102023-12-07 20:11:37 +08002778/*
qs.xiong44fac672023-08-29 16:15:55 +08002779 if (g_ap_tmp_watcher_pid == 0)
2780 {
2781 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2782 {
2783 g_ap_tmp_watcher_pid = 0;
2784 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2785 return -1;
2786 }
2787 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2788 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002789*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002790 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002791 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002792}
2793
qs.xiongf1b525b2022-03-31 00:58:23 -04002794int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002795{
you.chen35020192022-05-06 11:30:57 +08002796 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002797}
2798
qs.xiongf1b525b2022-03-31 00:58:23 -04002799int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002800{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002801 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002802 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002803
you.chen35020192022-05-06 11:30:57 +08002804 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002805
you.chen35020192022-05-06 11:30:57 +08002806 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002807
you.chen35020192022-05-06 11:30:57 +08002808 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2809
2810 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2811
you.chenc9928582023-04-24 15:39:37 +08002812
2813 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2814 if (ret != 0)
2815 {
2816 RLOGE("lynq_wifi_ap_start excute script fail");
2817 return -1;
2818 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002819/*
qs.xiong44fac672023-08-29 16:15:55 +08002820 g_ap_tmp_watcher_stop_flag = 1;
2821 if (g_ap_tmp_watcher_pid != 0)
2822 pthread_join(g_ap_tmp_watcher_pid, NULL);
2823 g_ap_tmp_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08002824*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002825 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002826 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002827}
qs.xiong1af5daf2022-03-14 09:12:12 -04002828
qs.xiongf1b525b2022-03-31 00:58:23 -04002829int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002830{
you.chen35020192022-05-06 11:30:57 +08002831 char lynq_disable_cmd[128] = {0};
2832 char lynq_select_cmd[128] = {0};
2833 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002834 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002835 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002836
you.chen35020192022-05-06 11:30:57 +08002837 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002838 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2839 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2840
2841 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2842 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2843 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002844
qs.xiong9fbf74e2023-03-28 13:38:22 +08002845 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002846}
2847
qs.xiongf1b525b2022-03-31 00:58:23 -04002848int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002849{
you.chen35020192022-05-06 11:30:57 +08002850 char lynq_disable_cmd[128] = {0};
2851 char lynq_select_cmd[128] = {0};
2852 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002853 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002854 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002855
you.chen35020192022-05-06 11:30:57 +08002856 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002857
you.chen35020192022-05-06 11:30:57 +08002858 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2859 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2860
2861 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2862 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2863 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002864
qs.xiong9fbf74e2023-03-28 13:38:22 +08002865 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002866}
qs.xiongf1b525b2022-03-31 00:58:23 -04002867
you.chen35020192022-05-06 11:30:57 +08002868int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002869{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002870 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002871 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002872 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002873 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002874 RLOGD("enter lynq_ap_password_set");
2875 if( password == NULL )
2876 {
2877 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002878 return -1;
2879 }
2880 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002881 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002882 if(pass_len < 8 || pass_len >= 64)
2883 {
2884 RLOGE("[lynq_ap_password_set]input password len not in rage");
2885 return -1;
you.chen35020192022-05-06 11:30:57 +08002886 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002887
you.chen35020192022-05-06 11:30:57 +08002888 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002889
qs.xiong9fbf74e2023-03-28 13:38:22 +08002890 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2891 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002892 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002893 return -1;
2894 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2896 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002897 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2898 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002899 }
2900
you.chen35020192022-05-06 11:30:57 +08002901 CHECK_WPA_CTRL(CTRL_AP);
2902
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 if (auth == LYNQ_WIFI_AUTH_WEP)
2904 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002905 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002906 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2907 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2908 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2909 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2910 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002911 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2912 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002913 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 +08002914 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2915 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2916 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2918 {
2919
qs.xiongc8d92a62023-03-29 17:36:14 +08002920 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 +08002921 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002922 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002923 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2924 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2925
2926 }
2927 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002928 {
2929 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002930 return -1;
2931 }
you.chen35020192022-05-06 11:30:57 +08002932
you.chen35020192022-05-06 11:30:57 +08002933 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002934
qs.xiong9fbf74e2023-03-28 13:38:22 +08002935 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002936}
2937
you.chen35020192022-05-06 11:30:57 +08002938int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002939{
you.chen35020192022-05-06 11:30:57 +08002940 FILE * fp;
2941 int len, ret;
2942 int count, index;
2943 char *split_lines[128] = {0};
2944 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002945 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002946
you.chen35020192022-05-06 11:30:57 +08002947 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002948
you.chen35020192022-05-06 11:30:57 +08002949 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2950// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002951 if (NULL == fp)
2952 {
2953 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002954 return -1;
2955 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002956
you.chen35020192022-05-06 11:30:57 +08002957 buff = alloca(MAX_RET);
2958 fseek(fp, 0, SEEK_SET);
2959 len = fread(buff, 1, MAX_RET, fp);
2960 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002961
qs.xiong9fbf74e2023-03-28 13:38:22 +08002962 for(index=0; index < len; index ++)
2963 {
2964 if (memcmp(buff + index, "network={", 9) != 0)
2965 {
you.chen35020192022-05-06 11:30:57 +08002966 continue;
2967 }
2968 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002969 for (; index < len; index ++ )
2970 {
2971 if (buff[index] != '}')
2972 {
you.chen35020192022-05-06 11:30:57 +08002973 continue;
2974 }
2975 buff[index] = '\0';
2976 break;
2977 }
2978 len = buff + index - p;
2979 }
2980
2981 count = lynq_split(p, len, '\n', split_lines);
2982
2983 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002984 for(index=0; index < count; index++)
2985 {
you.chen35020192022-05-06 11:30:57 +08002986 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002987 if (p != NULL)
2988 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002989 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 if (*p == '\"')
2991 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002992 p++;
2993 }
you.chen35020192022-05-06 11:30:57 +08002994 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002995 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2996 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002997 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002998 if (*p == '\"')
2999 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003000 p++;
3001 }
3002 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003003 else
3004 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003005 continue;
you.chen35020192022-05-06 11:30:57 +08003006 }
3007
3008 strcpy(password, p);
3009
qs.xiong9fbf74e2023-03-28 13:38:22 +08003010 while(*password != '\0')
3011 {
3012 if (*password == '\"')
3013 {
you.chen35020192022-05-06 11:30:57 +08003014 *password = '\0';
3015 break;
3016 }
3017 password++;
3018 }
3019 ret = 0;
3020 break;
3021 } //end for(index=0; index < count; index++)
3022
3023 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003024}
3025
you.chen35020192022-05-06 11:30:57 +08003026static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3027 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003028 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003029
qs.xiong9fbf74e2023-03-28 13:38:22 +08003030 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3031 {
you.chen35020192022-05-06 11:30:57 +08003032 return -1;
3033 }
3034
3035 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003036
qs.xiong9fbf74e2023-03-28 13:38:22 +08003037 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3038 {
3039 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003040 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003041 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003042 {
you.chena6cd55a2022-05-08 12:20:18 +08003043 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003044 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003045 }
you.chen70f377f2023-04-14 18:17:09 +08003046 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3047 {
3048 return 0;
3049 }
you.chena6cd55a2022-05-08 12:20:18 +08003050 }
3051 }
you.chen70f377f2023-04-14 18:17:09 +08003052 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3053 {
3054 return 0;
3055 }
3056
qs.xiong9fbf74e2023-03-28 13:38:22 +08003057 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3058 {
you.chen70f377f2023-04-14 18:17:09 +08003059 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003060 return -1;
3061 }
3062 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3063 {
3064
you.chen70f377f2023-04-14 18:17:09 +08003065 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3066 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003067 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3068 {
3069
3070 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003071 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003072 }else
3073 {
you.chen70f377f2023-04-14 18:17:09 +08003074 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003075 *auth = -1;
3076 return -1;
3077 }
you.chen35020192022-05-06 11:30:57 +08003078 return 0;
3079}
3080
3081int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003082{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003083 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003084 int pass_len, net_no, count, index;
3085 char lynq_tmp_cmd[300]={0};
3086 int net_no_list[128];
3087 lynq_wifi_auth_s net_auth;
3088 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003089 if(pass_len < 8 || pass_len >= 64)
3090 {
3091 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003092 return -1;
3093 }
3094
3095 CHECK_IDX(idx, CTRL_STA);
3096
3097 net_no = -1;
3098 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3099
qs.xiong9fbf74e2023-03-28 13:38:22 +08003100 for (index=0; index < count; index++)
3101 {
you.chen35020192022-05-06 11:30:57 +08003102 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003103 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3104 {
you.chen35020192022-05-06 11:30:57 +08003105 net_no = net_no_list[index];
3106 break;
3107 }
3108 }
3109
qs.xiong9fbf74e2023-03-28 13:38:22 +08003110 if (net_no < 0)
3111 {
you.chen35020192022-05-06 11:30:57 +08003112 return -1;
3113 }
3114
3115 CHECK_WPA_CTRL(CTRL_STA);
3116
3117 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3118
3119 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3120 DO_OK_FAIL_REQUEST(cmd_save_config);
3121
3122 return 0;
3123}
3124
qs.xiongb5dab082023-10-13 14:43:41 +08003125/**
3126* buff data
3127* buff_len size of buff
3128* idx sta
3129* *ap ap info for find ssid && password
3130* password return password
3131*
3132*/
3133static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3134
3135 int ret, network_len, i, ssid_len,curr_auth;
3136 int count, index,org_index;
3137 char *split_lines[128] = {0};
3138 char *p, *ssid, *ssid_end_flag,*ptr;
3139 char tmp_ssid[128]={0};
3140 char tmp_auth[24]={0};
3141
3142 org_index = 0;
3143 network_len = 0;
3144 p = NULL;
3145
3146 CHECK_IDX(idx, CTRL_STA);
3147
3148 while(1){
3149 network_len = 0;
3150 p == NULL;
3151 for(; org_index < buff_len; org_index ++)
3152 {
3153 for(; org_index < buff_len; org_index ++)
3154 {
3155 if (memcmp(buff + org_index, "network={", 9) != 0)
3156 {
3157 continue;
3158 }
3159 p = buff + org_index + 9;
3160
3161 for (; org_index < buff_len; org_index ++ )
3162 {
3163 if (buff[org_index] != '}')
3164 {
3165 continue;
3166 }
3167 buff[org_index] = '\0';
3168 break;
3169 }
3170 network_len = buff + org_index - p;
3171 break;
3172 }
3173
3174 if (p == NULL)
3175 {
3176 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3177 return -1;
3178 }
3179
3180 ssid = strstr(p, "ssid=");
3181 if (ssid != NULL) {
3182 ssid += strlen("ssid=");
3183 if (ssid[0] == '\"')
3184 {
3185 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3186 {
qs.xiongb5dab082023-10-13 14:43:41 +08003187 break;
3188 }
qs.xiongb5dab082023-10-13 14:43:41 +08003189 }
3190 else
3191 {
3192 ssid_end_flag = strstr(ssid, "\n");
3193 if (ssid_end_flag != NULL)
3194 {
3195 ssid_len = (ssid_end_flag - ssid) / 2;
3196 for(i=0; i<ssid_len; i++)
3197 {
3198 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3199 }
3200 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3201 {
qs.xiongb5dab082023-10-13 14:43:41 +08003202 break;
3203 }
3204 }
3205 }
3206 }
3207
3208 }
3209
3210 if (org_index >= buff_len || NULL == p || network_len <= 0)
3211 {
3212
3213 if (buff != NULL)
3214 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3215 return -1;
3216 }
3217
3218 count = lynq_split(p, network_len, '\n', split_lines);
3219 ret = -1;
3220 for( index=0; index < count; index++ )
3221 {
3222 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003223 if(p != NULL)
3224 {
3225 p += 9;
3226 if(memcmp(p,"SAE",3) == 0)
3227 {
3228 curr_auth = 5;
3229 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3230 {
3231 curr_auth = 4;
3232 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3233 {
3234 curr_auth = 3;
3235 }else if(memcmp(p,"NONE",4) == 0 )
3236 {
3237 curr_auth = 0;
3238 }else{
3239 curr_auth = 1;
3240 }
qs.xiongb5dab082023-10-13 14:43:41 +08003241 if( curr_auth < 1 || curr_auth > 6)
3242 {
3243 ret = -1;
3244 }
3245 break;
3246 }
3247 }
3248 if( curr_auth == 0)
3249 {
3250 return 0;
3251 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3252 {
3253 for(index=0; index < count; index++)
3254 {
3255 /*get psw info*/
3256
3257 p = strstr(split_lines[index], "psk=");
3258 if (p != NULL)
3259 {
3260 p += 4;
3261 if (*p == '\"')
3262 {
3263 p++;
3264 }
3265 }
3266 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3267 {
3268 p += 9;
3269 if (*p == '\"')
3270 {
3271 p++;
3272 }
3273 }
3274 else
3275 {
3276 continue;
3277 }
3278
3279 if (*p == '\"')
3280 p++;
3281 strncpy(password, p, 64);
3282 p = password;
3283 while(password - p < 64 && *password != '\0')
3284 {
3285 if (*password == '\"')
3286 {
3287 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003288 ret = 0;
3289 break;
3290 }
3291 password++;
3292 }
3293 break;
3294 }
3295 break;
3296 }
3297 }
3298
3299 return ret;
3300}
3301
3302
3303
you.chen35020192022-05-06 11:30:57 +08003304int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3305
3306 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003307 int len, ret;
3308 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003309 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003310
qs.xiongb5dab082023-10-13 14:43:41 +08003311 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003312 CHECK_IDX(idx, CTRL_STA);
3313
qs.xiong9fbf74e2023-03-28 13:38:22 +08003314 if (NULL == password)
3315 {
3316 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003317 return -1;
3318 }
3319
you.chen35020192022-05-06 11:30:57 +08003320 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003321 if (NULL == fp)
3322 {
3323 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003324 return -1;
3325 }
3326
you.chen6d247052023-06-01 16:39:54 +08003327 fseek(fp, 0, SEEK_END);
3328 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003329 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003330
qs.xiongb5dab082023-10-13 14:43:41 +08003331 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003332 {
3333 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3334 return -1;
3335 }
3336
you.chen35020192022-05-06 11:30:57 +08003337 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003338 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003339 fclose(fp);
3340
qs.xiongb5dab082023-10-13 14:43:41 +08003341
3342 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3343
3344 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003345 {
qs.xiongb5dab082023-10-13 14:43:41 +08003346 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3347 free(info_buff);
3348 return 0;
you.chen35020192022-05-06 11:30:57 +08003349 }
qs.xiongb5dab082023-10-13 14:43:41 +08003350 else{
3351 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003352 return -1;
3353 }
3354
you.chen35020192022-05-06 11:30:57 +08003355}
3356
qs.xiongb5dab082023-10-13 14:43:41 +08003357
you.chen35020192022-05-06 11:30:57 +08003358static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3359{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003360 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003361
qs.xiong9fbf74e2023-03-28 13:38:22 +08003362 if (sta_ssid == NULL)
3363 {
3364 RLOGE("sta_ssid is null\n");
3365 return -1;
you.chen35020192022-05-06 11:30:57 +08003366 }
3367
qs.xiong9fbf74e2023-03-28 13:38:22 +08003368 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003369
3370 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3371
3372 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3373// DO_OK_FAIL_REQUEST(cmd_save_config);
3374
qs.xiong9fbf74e2023-03-28 13:38:22 +08003375 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003376
3377}
3378
you.chen35020192022-05-06 11:30:57 +08003379static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003380{
you.chen35020192022-05-06 11:30:57 +08003381 char lynq_disable_cmd[128]={0};
3382 char lynq_select_cmd[128]={0};
3383
3384 CHECK_WPA_CTRL(CTRL_STA);
3385
qs.xiong9fbf74e2023-03-28 13:38:22 +08003386 if (save != 0)
3387 {
you.chenc29444e2022-06-07 18:01:16 +08003388 if (start_flag != 0)
3389 {
3390 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3391 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3392 }
3393 else
3394 {
3395 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3396 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3397 }
you.chen35020192022-05-06 11:30:57 +08003398 DO_OK_FAIL_REQUEST(cmd_save_config);
3399 }
3400
qs.xiong9fbf74e2023-03-28 13:38:22 +08003401 if (start_flag == 0)
3402 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003403 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003404 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3405 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003406 else
3407 {
you.chen35020192022-05-06 11:30:57 +08003408 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3409 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3410 }
3411
3412 return 0;
3413}
3414
3415int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3416{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003417 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003418 CHECK_IDX(idx, CTRL_STA);
3419
you.chen6c2dd9c2022-05-16 17:55:28 +08003420 curr_status_info curr_state;
3421 ap_info_s ap_info;
3422 curr_state.ap = &ap_info;
3423 curr_state.state = NULL;
3424
qs.xiong9fbf74e2023-03-28 13:38:22 +08003425 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3426 {
you.chend2fef3f2023-02-13 10:50:35 +08003427 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003428 return 0;
3429 }
3430
3431 return -1;
you.chen35020192022-05-06 11:30:57 +08003432}
3433
3434int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3435{
qs.xiong5d716d22023-09-20 20:08:39 +08003436 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003437 scan_info_s *scan_list = NULL;
3438 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003439 int scan_len=0;
3440 int save_len=0;
3441 int best_index = -1;
3442 int best_scan_index = -1;
3443 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003444 int i, j, ret;
3445
3446 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003447
3448 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003449 if (info == NULL)
3450 {
you.chen35020192022-05-06 11:30:57 +08003451 return -1;
3452 }
3453
3454 curr_status_info curr_state;
3455 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003456 char status[64];
you.chen35020192022-05-06 11:30:57 +08003457
you.chen9ac66392022-08-06 17:01:16 +08003458 memset(&ap_info, 0, sizeof (ap_info));
3459 memset(status, 0, sizeof (status));
3460
3461 curr_state.ap = &ap_info;
3462 curr_state.state = status;
3463
qs.xiong9fbf74e2023-03-28 13:38:22 +08003464 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3465 {
you.chen35020192022-05-06 11:30:57 +08003466 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003467 if (strcmp(status, STATE_COMPLETED) == 0)
3468 {
3469 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003470 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003471 }
3472 else
3473 {
3474 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003475 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003476 }
you.chen593621d2023-04-27 17:52:44 +08003477 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003478 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003479 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003480 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003481 return 0;
3482 }
3483
you.chen9ac66392022-08-06 17:01:16 +08003484 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003485 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003486 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3487 {
you.chen9ac66392022-08-06 17:01:16 +08003488 if (NULL != scan_list)
3489 {
3490 free(scan_list);
3491 }
you.chen35020192022-05-06 11:30:57 +08003492 return -1;
3493 }
3494
qs.xiong9fbf74e2023-03-28 13:38:22 +08003495 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3496 {
you.chen9ac66392022-08-06 17:01:16 +08003497 if (NULL != scan_list)
3498 {
3499 free(scan_list);
3500 }
3501 if (NULL != save_list)
3502 {
3503 free(save_list);
3504 }
you.chen35020192022-05-06 11:30:57 +08003505 return -1;
3506 }
3507
qs.xiong9fbf74e2023-03-28 13:38:22 +08003508 for (i=0; i < save_len; i++)
3509 {
3510 for (j=0; j < scan_len; j++)
3511 {
you.chen35020192022-05-06 11:30:57 +08003512 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003513 && save_list[i].base_info.auth == scan_list[j].auth)
3514 {
3515 if (best_rssi == 0)
3516 {
you.chen9ac66392022-08-06 17:01:16 +08003517 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003518 best_rssi = scan_list[j].rssi;
3519 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003520 else if (best_rssi > scan_list[j].rssi)
3521 {
you.chen35020192022-05-06 11:30:57 +08003522 best_index = i;
3523 best_scan_index = j;
3524 best_rssi = scan_list[j].rssi;
3525 }
you.chend2fef3f2023-02-13 10:50:35 +08003526 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 +08003527 break;
3528 }
3529 }
3530 }
3531
qs.xiong9fbf74e2023-03-28 13:38:22 +08003532 if (best_index >= 0)
3533 {
you.chen35020192022-05-06 11:30:57 +08003534 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003535 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 +08003536 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003537 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003538 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003539 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003540 }
3541
you.chen9ac66392022-08-06 17:01:16 +08003542 if (NULL != scan_list)
3543 {
3544 free(scan_list);
3545 }
3546 if (NULL != save_list)
3547 {
3548 free(save_list);
3549 }
3550
qs.xiong5d716d22023-09-20 20:08:39 +08003551 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003552 return ret;
you.chen35020192022-05-06 11:30:57 +08003553}
3554
3555static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3556{
qs.xiongc8d92a62023-03-29 17:36:14 +08003557 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003558 char lynq_ket_mgmt_cmd[64]={0};
3559 char lynq_pairwise_cmd[64]={0};
3560 char lynq_psk_cmd[64]={0};
3561
3562 CHECK_WPA_CTRL(CTRL_STA);
3563
qs.xiong9fbf74e2023-03-28 13:38:22 +08003564 switch(auth)
3565 {
3566 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003567 {
3568 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003569
you.chen35020192022-05-06 11:30:57 +08003570 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003571// DO_OK_FAIL_REQUEST(cmd_save_config);
3572 break;
3573 }
3574 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003575 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003576 {
3577 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3578 {
you.chen35020192022-05-06 11:30:57 +08003579 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3580 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003581 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3582 {
you.chena6cd55a2022-05-08 12:20:18 +08003583 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003584 }
3585 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3586 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003587
you.chen35020192022-05-06 11:30:57 +08003588 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3589 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3590 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003591
qs.xiong9fbf74e2023-03-28 13:38:22 +08003592 if (password != NULL)
3593 {
you.chen35020192022-05-06 11:30:57 +08003594 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3595 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003596 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003597 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003598
you.chen35020192022-05-06 11:30:57 +08003599// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003600 break;
3601 }
3602 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3603 {
qs.xiong3e506812023-04-06 11:08:48 +08003604 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003605 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003606 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3607 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3608
qs.xiong3e506812023-04-06 11:08:48 +08003609 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003610 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3611 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3612 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3613
3614 break;
3615 }
3616 case LYNQ_WIFI_AUTH_WPA3_PSK:
3617 {
qs.xiong3e506812023-04-06 11:08:48 +08003618 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003619 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003620 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3621 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3622
qs.xiongb37f8c42023-09-13 21:21:58 +08003623 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003624 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3625 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3626 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3627
3628 break;
3629 }
3630 default:
3631 return -1;
you.chen35020192022-05-06 11:30:57 +08003632 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003633
qs.xiong9fbf74e2023-03-28 13:38:22 +08003634 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003635}
qs.xiong7a105ce2022-03-02 09:43:11 -05003636
you.chen35020192022-05-06 11:30:57 +08003637static int inner_get_curr_net_no(int interface) {
3638 curr_status_info curr_state;
3639 curr_state.ap = NULL;
3640 curr_state.state = NULL;
3641
qs.xiong9fbf74e2023-03-28 13:38:22 +08003642 if (0 != inner_get_status_info(interface, &curr_state))
3643 {
you.chen35020192022-05-06 11:30:57 +08003644 return -1;
3645 }
3646
3647 return curr_state.net_no;
3648}
3649
3650int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003651{
you.chen35020192022-05-06 11:30:57 +08003652 int net_no;
3653 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003654
you.chen35020192022-05-06 11:30:57 +08003655 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003656
qs.xiong9fbf74e2023-03-28 13:38:22 +08003657 if (net_no < 0)
3658 {
you.chen35020192022-05-06 11:30:57 +08003659 return -1;
3660 }
3661
3662 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003663}
3664
you.chenb95401e2023-05-12 19:39:06 +08003665int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
qs.xiong7a105ce2022-03-02 09:43:11 -05003666{
you.chen35020192022-05-06 11:30:57 +08003667 int count, net_no, index;
3668 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003669 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003670 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003671 curr_status_info curr_state;
3672 ap_info_s ap_info;
3673 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003674
qs.xiong9fbf74e2023-03-28 13:38:22 +08003675 if (ssid == NULL || *ssid == '\0')
3676 {
3677 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003678 return -1;
3679 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003680
qs.xiong9fbf74e2023-03-28 13:38:22 +08003681 if (LYNQ_WIFI_AUTH_OPEN != auth)
3682 {
3683 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003684 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003685 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003686 return -1;
3687 }
3688 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003689
you.chen70f377f2023-04-14 18:17:09 +08003690
3691 pthread_mutex_lock(&s_global_check_mutex);
3692 if (s_sta_status != INNER_STA_STATUS_INIT)
3693 {
3694 s_sta_status = INNER_STA_STATUS_CANCEL;
3695 pthread_cond_signal(&s_global_check_cond);
3696 }
3697 pthread_mutex_unlock(&s_global_check_mutex);
3698
you.chen35020192022-05-06 11:30:57 +08003699 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003700 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003701
3702 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003703 memset(&ap_info, 0, sizeof (ap_info));
3704 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003705
you.chen70f377f2023-04-14 18:17:09 +08003706 curr_state.ap = &ap_info;
3707 curr_state.state = status;
3708
3709 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003710 {
you.chen70f377f2023-04-14 18:17:09 +08003711 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3712 {
3713 net_no = curr_state.net_no;
3714 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3715 && strcmp(ap_info.psw, psw) == 0)
3716 {
3717 RLOGD("already connected\n");
3718
3719 pthread_mutex_lock(&s_global_check_mutex);
3720 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003721 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003722 pthread_cond_signal(&s_global_check_cond);
3723 pthread_mutex_unlock(&s_global_check_mutex);
3724 return 0;
3725 }
you.chen35020192022-05-06 11:30:57 +08003726 }
3727 }
3728
you.chen70f377f2023-04-14 18:17:09 +08003729 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003730 {
you.chen70f377f2023-04-14 18:17:09 +08003731 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3732
3733 for (index=0; index < count; index++)
3734 {
3735 net_auth = -1;
3736 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3737 {
3738 net_no = net_no_list[index];
3739 break;
3740 }
you.chen35020192022-05-06 11:30:57 +08003741 }
3742
you.chen70f377f2023-04-14 18:17:09 +08003743 if (net_no < 0)
3744 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003745 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3746 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3747 {
3748 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3749 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3750 DO_OK_FAIL_REQUEST(rm_net_cmd);
3751 }
you.chen70f377f2023-04-14 18:17:09 +08003752 net_no = lynq_add_network(CTRL_STA);
3753 if (net_no == -1)
3754 {
3755 return -1;
3756 }
3757
3758 RLOGD("net no is %d\n", net_no);
3759 if (0 != inner_set_sta_ssid(net_no, ssid))
3760 {
3761 return -1;
3762 }
you.chen35020192022-05-06 11:30:57 +08003763 }
3764 }
3765
qs.xiong9fbf74e2023-03-28 13:38:22 +08003766 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3767 {
you.chen35020192022-05-06 11:30:57 +08003768 return -1;
3769 }
3770
you.chen70f377f2023-04-14 18:17:09 +08003771
3772 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003773 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003774 usleep(200*1000);
3775
qs.xiong09560402023-10-27 21:58:55 +08003776 pthread_mutex_lock(&s_global_check_mutex);
3777 lynq_sta_removeElement(net_no);
3778 pthread_mutex_unlock(&s_global_check_mutex);
3779
you.chen70f377f2023-04-14 18:17:09 +08003780 ret = inner_sta_start_stop(net_no, 1, 1);
3781
3782 pthread_mutex_lock(&s_global_check_mutex);
3783 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003784 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003785 strcpy(s_sta_current_connecting_ssid, ssid);
3786 struct timeval now;
3787 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003788 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003789 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3790 pthread_cond_signal(&s_global_check_cond);
3791 pthread_mutex_unlock(&s_global_check_mutex);
3792 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003793}
3794
you.chenb95401e2023-05-12 19:39:06 +08003795int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3796{
3797 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3798}
3799
you.chen35020192022-05-06 11:30:57 +08003800int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003801{
you.chen35020192022-05-06 11:30:57 +08003802 ap_info_s ap;
3803 curr_status_info curr_state;
3804 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003805
qs.xiong9fbf74e2023-03-28 13:38:22 +08003806 if (ssid == NULL || *ssid == '\0')
3807 {
3808 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003809 return -1;
3810 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003811
you.chen35020192022-05-06 11:30:57 +08003812 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003813
you.chen35020192022-05-06 11:30:57 +08003814 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003815 curr_state.state = NULL;
3816
qs.xiong9fbf74e2023-03-28 13:38:22 +08003817 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3818 {
you.chen35020192022-05-06 11:30:57 +08003819 return 0;
3820 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003821
qs.xiong9fbf74e2023-03-28 13:38:22 +08003822 if (strcmp(ap.ap_ssid, ssid) != 0)
3823 {
you.chen35020192022-05-06 11:30:57 +08003824 return 0;
3825 }
3826
you.chen70f377f2023-04-14 18:17:09 +08003827 pthread_mutex_lock(&s_global_check_mutex);
3828 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3829 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003830 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003831}
qs.xiong97fa59b2022-04-07 05:41:29 -04003832
qs.xiongc93bf2b2023-08-25 10:22:08 +08003833int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3834{
qs.xiong09560402023-10-27 21:58:55 +08003835 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003836 ap_info_s ap;
3837 curr_status_info curr_state;
3838 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003839 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003840
3841 if (ssid == NULL || *ssid == '\0')
3842 {
3843 RLOGE("input ssid is NULL\n");
3844 return -1;
3845 }
3846
3847 CHECK_IDX(idx, CTRL_STA);
3848
3849
3850 curr_state.ap = &ap;
3851 curr_state.state = NULL;
3852
3853 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3854 {
3855 return 0;
3856 }
3857
3858 if (strcmp(ap.ap_ssid, ssid) != 0)
3859 {
3860 return 0;
3861 }
3862
3863 pthread_mutex_lock(&s_global_check_mutex);
3864 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003865 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3866 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3867 {
3868 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);
3869 if( g_history_disconnect_net[i] == curr_state.net_no)
3870 {
3871 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3872 check_history_idx_flag = 1;
3873 break;
3874 }
3875 }
3876 if ( check_history_idx_flag == 0)
3877 {
3878 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__);
3879 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3880 g_history_disconnect_valid_num++;
3881 }
3882 RLOGD("%s %d",__func__,__LINE__);
3883 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003884 pthread_mutex_unlock(&s_global_check_mutex);
3885 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3886
3887}
3888
3889
you.chena6cd55a2022-05-08 12:20:18 +08003890int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3891{
qs.xiong1b5e3472023-11-27 17:42:20 +08003892 RLOGD("enter %s %d func",__func__,__LINE__);
qs.xiongad2f89d2023-01-18 13:17:41 +08003893 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3894 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003895
you.chen35020192022-05-06 11:30:57 +08003896 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003897 CHECK_WPA_CTRL(CTRL_STA);
3898
you.chenc9928582023-04-24 15:39:37 +08003899 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3900 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003901 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003902 RLOGE("lynq_wifi_sta_start excute script fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08003903 return -1;
3904 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003905
qs.xiongad2f89d2023-01-18 13:17:41 +08003906 system(lynq_enable_sta_cmd);
3907 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003908 pthread_mutex_lock(&s_global_check_mutex);
3909 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003910 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003911 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08003912 RLOGD("end %s %d func",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003913 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003914}
3915
you.chen6d247052023-06-01 16:39:54 +08003916static int inner_get_status_info_state (int interface, char *state) {
3917 curr_status_info curr_state;
3918 curr_state.ap = NULL;
3919 curr_state.state = state;
3920 return inner_get_status_info(interface, &curr_state);
3921}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003922
3923int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3924{
3925
qs.xiongb37f8c42023-09-13 21:21:58 +08003926 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3927 int tmp_open_idx[128];
3928 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003929
qs.xiongb37f8c42023-09-13 21:21:58 +08003930 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003931 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003932 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3933 pthread_mutex_unlock(&s_global_check_mutex);
3934 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003935 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003936 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003937 }
3938
qs.xiongb37f8c42023-09-13 21:21:58 +08003939 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003940 return 0;
3941}
3942
3943
you.chen35020192022-05-06 11:30:57 +08003944int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003945{
qs.xiongec8bbeb2023-11-20 15:51:45 +08003946 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003947 int i=0;
3948 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003949
you.chena6cd55a2022-05-08 12:20:18 +08003950 CHECK_IDX(idx, CTRL_STA);
3951 CHECK_WPA_CTRL(CTRL_STA);
3952
you.chen6d247052023-06-01 16:39:54 +08003953 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003954 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003955
3956 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3957 if (ret != 0)
3958 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003959 RLOGE("lynq_wifi_sta_stop excute script fail %s %d",__func__,__LINE__);
you.chenc9928582023-04-24 15:39:37 +08003960 return -1;
3961 }
3962
you.chen6d247052023-06-01 16:39:54 +08003963 for (i=0; i < 30; i++) // to check if sta is realy stoped
3964 {
3965 if (inner_get_status_info_state(idx, state) != 0)
3966 {
3967 break;
3968 }
3969
3970 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3971 {
3972 break;
3973 }
qs.xiong1b5e3472023-11-27 17:42:20 +08003974 RLOGD("lynq_wifi_sta_stop curr state %s %s %d", state,__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003975 usleep(SLEEP_TIME_ON_IDLE);
3976 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003977 pthread_mutex_lock(&s_global_check_mutex);
3978 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3979 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08003980 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003981 return 0;
3982// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003983}
qs.xiongfcc914b2023-07-06 21:16:20 +08003984int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3985{
3986 char LYNQ_DISABLE_CMD[128]={0};
3987 CHECK_IDX(idx, CTRL_STA);
3988 CHECK_WPA_CTRL(CTRL_STA);
3989 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3990 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3991 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3992 return 0;
3993}
qs.xiong7a105ce2022-03-02 09:43:11 -05003994
you.chen35020192022-05-06 11:30:57 +08003995//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3996// int i, count;
3997// char *p;
3998// const char * FLAG_SSID = "ssid=";
3999// const char * FLAG_SBSID = "bssid=";
4000// const char * FLAG_KEY_MGMT = "key_mgmt=";
4001// const char * FLAG_FREQ = "freq=";
4002// char lynq_sta_cmd[MAX_CMD];
4003// char *split_lines[128] = {0};
4004
4005// CHECK_WPA_CTRL(CTRL_AP);
4006
4007// sprintf(lynq_sta_cmd, "STA %s", bssid);
4008
4009// DO_REQUEST(lynq_sta_cmd);
4010
4011// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4012
4013// for(i=0; i < count; i++) {
4014// p = strstr(split_lines[i], FLAG_SSID);
4015// if (p != NULL) {
4016// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
4017// continue;
4018// }
4019// }
4020
4021// lynq_get_interface_ip(idx, ap->ap_ip);
4022// lynq_ap_password_set(idx, ap->psw);
4023
4024// return 0;
4025//}
4026
4027static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4028 curr_status_info curr_state;
4029 curr_state.ap = ap;
4030 curr_state.state = NULL;
4031 return inner_get_status_info(interface, &curr_state);
4032}
4033
4034int 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 -04004035{
qs.xiong5071c802023-09-06 14:04:15 +08004036 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004037 int index, line_count;
4038 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004039 const char *lynq_first_sta_cmd = "STA-FIRST";
4040 char lynq_next_sta_cmd[MAX_CMD];
4041 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004042 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004043
you.chen35020192022-05-06 11:30:57 +08004044 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004045
you.chen35020192022-05-06 11:30:57 +08004046 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004047
you.chenb95401e2023-05-12 19:39:06 +08004048 // ap_info_s * tmp_ap;
4049 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004050 if (ap == NULL || list == NULL || len == NULL)
4051 {
4052 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004053 return -1;
4054 }
4055
you.chenb95401e2023-05-12 19:39:06 +08004056 // ap = &tmp_ap;
4057 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004058 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004059 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004060
you.chenb95401e2023-05-12 19:39:06 +08004061 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004062 {
you.chenb95401e2023-05-12 19:39:06 +08004063 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004064 return -1;
4065 }
4066
4067 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4068 lynq_ap_password_get(idx, (*ap)->psw);
4069
you.chen35020192022-05-06 11:30:57 +08004070 DO_REQUEST(lynq_first_sta_cmd);
4071
4072 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004073 while (reply_len > 0)
4074 {
4075 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004076 {
you.chen35020192022-05-06 11:30:57 +08004077 break;
4078 }
4079 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4080 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4081 strcpy(bssid[index], split_lines[0]);
4082 index++;
4083 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4084 reply_len = MAX_RET;
4085 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004086 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 +08004087 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004088 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004089 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004090 break;
4091 }
4092 }
4093
4094 *len = index;
4095
4096 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004097 for (index=0; index < *len; index++)
4098 {
you.chend2fef3f2023-02-13 10:50:35 +08004099 dev_info = &(*list)[index];
4100 memset(dev_info, 0, sizeof(device_info_s));
4101 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4102 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4103 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4104 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004105 free(bssid[index]);
4106 }
qs.xiong5071c802023-09-06 14:04:15 +08004107 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004108 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004109}
4110
you.chen35020192022-05-06 11:30:57 +08004111int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004112{
you.chen35020192022-05-06 11:30:57 +08004113 int i, count, index, count_words;
4114 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4115 char *split_lines[128] = {0};
4116 char *split_words[128] = {0};
4117 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004118
qs.xiong9fbf74e2023-03-28 13:38:22 +08004119 if (list == NULL || len == NULL)
4120 {
you.chen35020192022-05-06 11:30:57 +08004121 return -1;
4122 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004123
you.chen9ac66392022-08-06 17:01:16 +08004124 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4125 {
4126 usleep(100 * 1000);
4127 }
4128
you.chen35020192022-05-06 11:30:57 +08004129 CHECK_IDX(idx, CTRL_STA);
4130
4131 CHECK_WPA_CTRL(CTRL_STA);
4132
4133 DO_REQUEST(lynq_scan_result_cmd);
4134
4135 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4136 *len = count - 1;
4137 *list = malloc(sizeof (scan_info_s) * *len);
4138
4139 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004140 for (index=0; index <count_words; index++)
4141 {
4142 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004143 }
4144
qs.xiong9fbf74e2023-03-28 13:38:22 +08004145 for(index = 1;index < count; index++)
4146 {
4147 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004148 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004149 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4150 if (count_words < 4)
4151 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004152 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004153 //bssid / frequency / signal level / flags / ssid
4154 p = (*list) + index - 1;
4155 strcpy(p->mac, split_words[0]);
4156 p->band = convert_band_from_freq(atoi(split_words[1]));
4157 p->rssi = -1 * atoi( split_words[2]);
4158 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004159 if (count_words == 4) // ssid hided
4160 {
4161 p->ssid[0] = '\0';
4162 }
4163 else
4164 {
4165 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4166 }
you.chen35020192022-05-06 11:30:57 +08004167 }
4168
4169 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004170}
qs.xiong97fa59b2022-04-07 05:41:29 -04004171
you.chen35020192022-05-06 11:30:57 +08004172int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4173{
4174 int count, net_no, index;
4175 int net_no_list[128];
4176 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004177
you.chen35020192022-05-06 11:30:57 +08004178 char lynq_remove_cmd[MAX_CMD];
4179
qs.xiong9fbf74e2023-03-28 13:38:22 +08004180 if (ssid == NULL || *ssid == '\0')
4181 {
4182 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004183 return -1;
4184 }
4185
4186 CHECK_IDX(idx, CTRL_STA);
4187
4188 CHECK_WPA_CTRL(CTRL_STA);
4189
4190 net_no = -1;
4191 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4192
qs.xiong9fbf74e2023-03-28 13:38:22 +08004193 for (index=0; index < count; index++)
4194 {
you.chen35020192022-05-06 11:30:57 +08004195 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004196 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4197 {
you.chen35020192022-05-06 11:30:57 +08004198 net_no = net_no_list[index];
4199 break;
4200 }
4201 }
4202
qs.xiong9fbf74e2023-03-28 13:38:22 +08004203 if (net_no < 0)
4204 {
you.chen35020192022-05-06 11:30:57 +08004205 return 0;
4206 }
4207
4208 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4209
4210 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004211
4212 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4213
4214 pthread_mutex_lock(&s_global_check_mutex);
4215 lynq_sta_removeElement(net_no);
4216 pthread_mutex_unlock(&s_global_check_mutex);
4217
4218 RLOGD("%s %d",__func__,__LINE__);
4219 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004220 DO_OK_FAIL_REQUEST(cmd_save_config);
4221
4222 return 0;
4223}
4224
4225int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004226{
you.chend2fef3f2023-02-13 10:50:35 +08004227 int count, index;
you.chen35020192022-05-06 11:30:57 +08004228 int net_no_list[128];
4229 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004230 RLOGD("enter lynq_get_sta_saved_ap api\n");
4231 if (list == NULL || len == NULL)
4232 {
4233 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004234 return -1;
4235 }
4236
4237 CHECK_IDX(idx, CTRL_STA);
4238
4239// CHECK_WPA_CTRL(CTRL_STA);
4240
4241 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004242 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004243
you.chen057aac42023-04-13 14:06:58 +08004244 if (count < 0)
4245 {
4246 RLOGE("list network fail");
4247 return count;
4248 }
4249 else if (count == 0)
4250 {
4251 *list = NULL;
4252 *len = 0;
4253 return 0;
4254 }
4255
you.chen35020192022-05-06 11:30:57 +08004256 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004257 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004258 *len = count;
4259
qs.xiong9fbf74e2023-03-28 13:38:22 +08004260 for (index=0; index < count; index++)
4261 {
you.chen35020192022-05-06 11:30:57 +08004262 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004263 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004264 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004265 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004266 {
you.chen35020192022-05-06 11:30:57 +08004267 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4268 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004269 else
you.chen057aac42023-04-13 14:06:58 +08004270 {
you.chen35020192022-05-06 11:30:57 +08004271 (*list)[index].base_info.band = -1;
4272 }
you.chen057aac42023-04-13 14:06:58 +08004273 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004274 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004275 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004276 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004277 return 0;
4278}
4279
4280int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4281{
you.chen0c9bee22023-10-25 13:03:14 +08004282 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004283 {
you.chen0c9bee22023-10-25 13:03:14 +08004284 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4285 g_sta_fake_scan_finish_flag = 1;
4286 return 0;
4287 }
4288 else if (g_sta_conncet_status_flag != 0)
4289 {
4290 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004291 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004292 }
you.chen0c9bee22023-10-25 13:03:14 +08004293
qs.xiongc8d92a62023-03-29 17:36:14 +08004294 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004295 const char *lynq_scan_cmd = "SCAN";
4296
4297 CHECK_IDX(idx, CTRL_STA);
4298
4299 CHECK_WPA_CTRL(CTRL_STA);
4300
you.chen0df3e7e2023-05-10 15:56:26 +08004301 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4302 {
4303 RLOGD("tmp clear scanlist");
4304 system(clean_last_re);
4305 }
you.chen9ac66392022-08-06 17:01:16 +08004306 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004307 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004308 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4309 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004310 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004311 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4312 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004313 g_sta_scan_finish_flag = 1;
4314 return -1;
4315 }
you.chen35020192022-05-06 11:30:57 +08004316
4317 return 0;
4318}
4319
4320int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004321 if (cb == NULL)
4322 {
4323 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004324 return -1;
4325 }
4326
you.chen6d247052023-06-01 16:39:54 +08004327 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004328 g_ap_callback_priv = priv;
4329 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004330 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004331
you.chen6d247052023-06-01 16:39:54 +08004332 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004333 if (g_ap_watcher_pid == 0 )
4334 {
4335 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4336 {
4337 g_ap_watcher_pid = 0;
4338 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4339 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4340 return -1;
4341 }
4342 }
4343
4344 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004345 RLOGD("creat APWatcherTheradProc succeed");
you.chene9d00032023-04-24 13:55:29 +08004346
you.chen35020192022-05-06 11:30:57 +08004347 return 0;
4348}
4349
4350int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004351 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004352 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004353 if (g_ap_callback_priv == priv)
4354 {
you.chen35020192022-05-06 11:30:57 +08004355 g_ap_callback_func = NULL;
4356 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004357 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004358 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004359 return 0;
4360 }
you.chen6d247052023-06-01 16:39:54 +08004361 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004362 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004363 return -1;
4364}
4365
4366int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004367 if (cb == NULL)
4368 {
4369 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004370 return -1;
4371 }
4372
you.chen6d247052023-06-01 16:39:54 +08004373 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004374 g_sta_callback_priv = priv;
4375 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004376 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004377
you.chen6d247052023-06-01 16:39:54 +08004378 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004379 if (g_sta_watcher_pid == 0 ) {
4380 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4381 {
4382 g_sta_watcher_pid = 0;
4383 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4384 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4385 return -1;
4386 }
4387 }
4388
4389 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004390 RLOGD("creat STAWatcherTheradProc succeed");
you.chen35020192022-05-06 11:30:57 +08004391 return 0;
4392}
4393
4394int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004395 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004396 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004397 if (g_sta_callback_priv == priv)
4398 {
you.chen35020192022-05-06 11:30:57 +08004399 g_sta_callback_func = NULL;
4400 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004401 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004402 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004403 return 0;
4404 }
you.chen6d247052023-06-01 16:39:54 +08004405 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004406 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004407 return -1;
4408}
4409
qs.xiongfcc914b2023-07-06 21:16:20 +08004410int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4411 if (cb == NULL)
4412 {
4413 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4414 return -1;
4415 }
4416 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4417 g_sta_auto_callback_priv = priv;
4418 g_sta_auto_callback_func = cb;
4419 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4420 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4421 if (g_sta_auto_watcher_pid == 0 ) {
4422 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4423 {
4424 g_sta_auto_watcher_pid = 0;
4425 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4426 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4427 return -1;
4428 }
4429 }
4430 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004431 RLOGD("creat STAWatcherTheradProc succeed");
qs.xiongfcc914b2023-07-06 21:16:20 +08004432 return 0;
4433}
4434int lynq_unreg_sta_auto_event_callback(void * priv) {
4435 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4436 if (g_sta_auto_callback_priv == priv)
4437 {
4438 g_sta_auto_watcher_stop_flag = 1;
4439 if (g_sta_auto_watcher_pid != 0)
4440 {
4441 pthread_join(g_sta_auto_watcher_pid, NULL);
4442 }
4443 g_sta_auto_watcher_pid = 0;
4444 g_sta_auto_callback_func = NULL;
4445 g_sta_auto_callback_priv = NULL;
4446 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4447 return 0;
4448 }
4449 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4450 return -1;
4451}
you.chen35020192022-05-06 11:30:57 +08004452int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4453{
4454 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004455 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004456 CHECK_IDX(idx, CTRL_AP);
4457
qs.xiong9fbf74e2023-03-28 13:38:22 +08004458 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4459 {
you.chen35020192022-05-06 11:30:57 +08004460 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4461 return 0;
4462 }
4463
qs.xiong9fbf74e2023-03-28 13:38:22 +08004464 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4465 {
you.chen35020192022-05-06 11:30:57 +08004466 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4467 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004468 else
4469 {
you.chen35020192022-05-06 11:30:57 +08004470 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4471 }
4472
4473 return 0;
4474}
4475
4476int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4477 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004478 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004479 CHECK_IDX(idx, CTRL_STA);
4480
qs.xiong9fbf74e2023-03-28 13:38:22 +08004481 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4482 {
you.chen35020192022-05-06 11:30:57 +08004483 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4484 return 0;
4485 }
4486
qs.xiong9fbf74e2023-03-28 13:38:22 +08004487 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4488 {
you.chen35020192022-05-06 11:30:57 +08004489 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4490 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004491 else
4492 {
you.chen35020192022-05-06 11:30:57 +08004493 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4494 }
4495
4496 return 0;
4497}
4498
4499int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4500// CHECK_IDX(idx, CTRL_AP);
4501// int ret = 0;
4502// size_t reply_len = MAX_RET;
4503// char cmd_reply[MAX_RET]={0};
4504// const char * cmd_str = "GET country";
4505// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4506// do{
4507// if (NULL == s_lynq_wpa_ctrl) {
4508// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4509// if (NULL == s_lynq_wpa_ctrl ) {
4510// printf("wpa_ctrl_open fail\n");
4511// return -1;
4512// }
4513// }
4514// }while(0);
4515
4516// do {
4517// reply_len = MAX_RET;
4518// cmd_reply[0] = '\0';
4519// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004520// 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 +08004521// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004522// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004523// return ret;
4524// }
4525// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004526// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004527// }while(0);
4528
4529 FILE *fp;
4530 size_t i = 0;
4531 char lynq_cmd_ret[MAX_RET]={0};
4532
4533// CHECK_IDX(idx, CTRL_AP);
4534
4535 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004536 {
4537 perror("popen error!");
4538 return -1;
4539 }
you.chen35020192022-05-06 11:30:57 +08004540 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4541 {
4542 perror("fread fail!");
4543 return -1;
4544 }
4545
qs.xiong9fbf74e2023-03-28 13:38:22 +08004546 for(i=0; i < strlen(lynq_cmd_ret); i++)
4547 {
4548 if (lynq_cmd_ret[i] == ' ')
4549 {
you.chen35020192022-05-06 11:30:57 +08004550 lynq_cmd_ret[i] = '\0';
4551 break;
4552 }
4553 }
4554
4555 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004556 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004557
4558 int ret=pclose(fp);
4559 if(ret==-1)
4560 {
4561 perror("close file faild");
4562 }
4563
4564 return 0;
4565}
4566
qs.xiong44fac672023-08-29 16:15:55 +08004567
you.chen705a7ef2023-06-01 22:06:45 +08004568static int check_and_init_uci_config(char * country_code)
4569{
4570 FILE * fp;
4571 int is_different = 0;
4572 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4573 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4574 const char * commit_uci_cmd ="uci commit";
4575 char set_country_cmd[MAX_CMD];
4576 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004577
you.chen705a7ef2023-06-01 22:06:45 +08004578 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004579
you.chen705a7ef2023-06-01 22:06:45 +08004580 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004581 {
you.chen705a7ef2023-06-01 22:06:45 +08004582 if (0 != system(create_uci_cmd))
4583 {
4584 RLOGE("creat_uci_cmd fail");
4585 return -1;
4586 }
4587 is_different = 1;
4588 }
4589
4590 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4591 {
4592 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004593 return -1;
4594 }
4595
you.chen705a7ef2023-06-01 22:06:45 +08004596 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004597 {
you.chen705a7ef2023-06-01 22:06:45 +08004598 RLOGE("fread fail!");
4599 fclose(fp);
4600 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004601 }
4602
you.chen705a7ef2023-06-01 22:06:45 +08004603 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4604 {
qs.xiong44fac672023-08-29 16:15:55 +08004605 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 +08004606 is_different = 1;
4607 }
4608
4609 fclose(fp);
4610
4611 if (is_different)
4612 {
4613 if ( 0 != system(set_country_cmd))
4614 {
4615 RLOGE("set_country_cmd fail");
4616 return -1;
4617 }
4618 if (0 != system(commit_uci_cmd))
4619 {
4620 RLOGE("commmit fail");
4621 }
4622 }
4623
4624 return is_different;
4625}
4626
4627int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4628 char check_current_code[10];
4629 const char * support_country[] = {"CN", "EU"};
4630
4631 int ret,is_different, i, cc_count;
4632
4633 if (country_code == NULL || country_code[0] == '\0')
4634 {
4635 RLOGE("bad country code\n");
4636 return -1;
4637 }
4638
4639 cc_count = sizeof (support_country) / sizeof (char*);
4640 for(i=0; i < cc_count; i++)
4641 {
4642 if (strcmp(support_country[i], country_code) == 0)
4643 {
4644 break;
4645 }
4646 }
4647
4648 if (i >= cc_count)
4649 {
4650 RLOGE("unspported country code %s\n", country_code);
4651 return -1;
4652 }
4653
4654 is_different = check_and_init_uci_config(country_code);
4655 if( is_different < 0 )
4656 {
4657 RLOGE("init set uci fail\n");
4658 return -1;
4659 }
4660
4661 ret = lynq_get_country_code(idx,check_current_code);
4662 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4663 {
4664 ret = lynq_wifi_disable();
4665 if(ret != 0 )
4666 {
4667 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4668 return -1;
4669 }
4670 }
4671
4672 return 0;
you.chen35020192022-05-06 11:30:57 +08004673}
4674
4675int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4676{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004677 RLOGD("enter lynq_get_connect_ap_mac\n");
4678 if (mac == NULL)
4679 {
4680 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004681 return -1;
4682 }
4683
4684 CHECK_IDX(idx, CTRL_STA);
4685 ap_info_s ap;
4686 ap.ap_mac[0] = '\0';
4687
qs.xiong9fbf74e2023-03-28 13:38:22 +08004688 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4689 {
you.chen35020192022-05-06 11:30:57 +08004690 return -1;
4691 }
4692 strcpy(mac, ap.ap_mac);
4693
4694 return 0;
4695}
4696
4697int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4698{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004699 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004700 struct ifaddrs *ifaddr_header, *ifaddr;
4701 struct in_addr * ifa;
4702 const char * ifaName = "wlan0";
4703 if (ip == NULL)
4704 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004705 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004706 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004707 }
you.chenf58b3c92022-06-21 16:53:48 +08004708
qs.xiong9fbf74e2023-03-28 13:38:22 +08004709 if (idx == 1)
4710 {
you.chen0df3e7e2023-05-10 15:56:26 +08004711 ifaName = inner_get_ap_interface_name();
4712 if (ifaName == NULL)
4713 {
4714 RLOGE("[lynq_get_interface_ip] ap name get fail");
4715 return -1;
4716 }
you.chen9ac66392022-08-06 17:01:16 +08004717 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004718 else if (idx != 0)
4719 {
you.chen35020192022-05-06 11:30:57 +08004720 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004721 }
you.chen35020192022-05-06 11:30:57 +08004722
you.chen9ac66392022-08-06 17:01:16 +08004723 if (getifaddrs(&ifaddr_header) == -1)
4724 {
you.chen35020192022-05-06 11:30:57 +08004725 perror("getifaddrs");
4726 return -1;
4727 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004728 }
you.chen35020192022-05-06 11:30:57 +08004729
4730
you.chen9ac66392022-08-06 17:01:16 +08004731 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4732 {
4733 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004734 continue;
you.chen9ac66392022-08-06 17:01:16 +08004735 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4736 {
4737 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4738 {
4739 // is a valid IP4 Address
4740 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4741 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004742 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004743 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004744 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004745 return 0;
4746 }
4747 }
4748 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004749 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004750 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004751 return -1;
you.chen35020192022-05-06 11:30:57 +08004752}
4753
4754int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4755{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004756 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004757 int count;
4758 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004759 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004760 char *split_words[128] = {0};
4761 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4762
4763 CHECK_WPA_CTRL(idx);
4764
4765 DO_REQUEST(lynq_get_mac_cmd);
4766
qs.xiong9fbf74e2023-03-28 13:38:22 +08004767 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4768 {
4769 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004770 return -1;
4771 }
4772
4773 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4774
qs.xiong9fbf74e2023-03-28 13:38:22 +08004775 if (count < 2)
4776 {
you.chen35020192022-05-06 11:30:57 +08004777 return -1;
4778 }
4779
qs.xiong9fbf74e2023-03-28 13:38:22 +08004780 for (i=0; i < strlen(split_words[1]); i++ )
4781 {
4782 if (split_words[1][i] != ' ')
4783 {
you.chen35020192022-05-06 11:30:57 +08004784 break;
4785 }
4786 }
4787
qs.xiongdd6e44c2023-08-08 15:02:53 +08004788 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004789
4790 return 0;
4791}
4792
4793int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4794{
4795// int count;
4796// char *split_words[128] = {0};
4797// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4798
4799// if (rssi == NULL) {
4800// return -1;
4801// }
4802
4803// CHECK_IDX(idx, CTRL_STA);
4804
4805// CHECK_WPA_CTRL(CTRL_STA);
4806
4807// DO_REQUEST(lynq_get_rssi_cmd);
4808
4809// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4810// return -1;
4811// }
4812
4813// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4814
4815// if (count < 2) {
4816// return -1;
4817// }
4818
4819// *rssi = atoi(split_words[1]) * -1;
4820
you.chen35020192022-05-06 11:30:57 +08004821 char lynq_cmd_ret[MAX_RET]={0};
4822
qs.xiongff0ae0f2022-10-11 15:47:14 +08004823/*******change other cmd to get rssi*******
4824 *
4825 *wl rssi ---> wl -i wlan0 rssi
4826 *
4827 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004828 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004829 {
you.chen23c4a5f2023-04-12 16:46:00 +08004830 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004831 return -1;
4832 }
you.chen9f17e4d2022-06-06 17:18:18 +08004833 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004834/****** if got rssi is 0,means sta didn't connected any device****/
4835 if(*rssi == 0)
4836 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004837 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004838 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004839 }
you.chen35020192022-05-06 11:30:57 +08004840
4841 return 0;
4842}
4843
4844int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4845{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004846 RLOGD("enter lynq_get_connect_ap_band\n");
4847 if (band == NULL)
4848 {
you.chen35020192022-05-06 11:30:57 +08004849 return -1;
4850 }
4851
4852 CHECK_IDX(idx, CTRL_STA);
4853 ap_info_s ap;
4854 ap.band = -1;
4855
qs.xiong9fbf74e2023-03-28 13:38:22 +08004856 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4857 {
you.chen35020192022-05-06 11:30:57 +08004858 return -1;
4859 }
4860 *band = ap.band;
4861
4862 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004863}
you.chenf58b3c92022-06-21 16:53:48 +08004864
4865int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4866{
you.chenb95401e2023-05-12 19:39:06 +08004867 int ret;
4868 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004869 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004870
4871 if (ip == NULL)
4872 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004873 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004874 return -1;
4875 }
4876
4877 CHECK_IDX(idx, CTRL_STA);
4878
qs.xionge4cbf1c2023-02-28 18:22:49 +08004879 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004880 {
4881 return -1;
4882 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004883
you.chenb95401e2023-05-12 19:39:06 +08004884 ip[0] = '\0';
4885 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4886 if (ret != 0)
4887 {
4888 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
you.chenb95401e2023-05-12 19:39:06 +08004889 }
4890
4891 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4892 {
4893 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004894 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004895 if (ret != 0)
4896 {
4897 ip[0] = '\0';
4898 return 0;
4899 }
4900 else
4901 {
4902 p = strchr(ip, '\n');
4903 if (p != NULL)
4904 {
4905 *p = '\0';
4906 }
4907 }
4908 }
4909 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004910}
4911
qs.xionge02a5252023-09-20 14:00:21 +08004912int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4913{
4914 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4915 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4916}
4917
qs.xiong026c5c72022-10-17 11:15:45 +08004918int lynq_ap_connect_num(int sta_number)
4919{
4920 char lynq_limit_cmd[32]={0};
4921 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004922 if((sta_number < 1 ) && (sta_number > 15))
4923 {
4924 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004925 return -1;
4926 }
4927 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4928 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004929 if(ret != 0)
4930 {
4931 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004932 }
4933 return 0;
4934}
you.chenf58b3c92022-06-21 16:53:48 +08004935
qs.xiong77905552022-10-17 11:19:57 +08004936int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4937{
4938
4939 char lynq_wifi_acs_cmd[128]={0};
4940 char lynq_cmd_mode[128]={0};
4941 char lynq_cmd_slect[128]={0};
4942
qs.xiong9fbf74e2023-03-28 13:38:22 +08004943 if((acs_mode != 2) && (acs_mode != 5))
4944 {
qs.xiong77905552022-10-17 11:19:57 +08004945 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4946 }
4947
qs.xiong9fbf74e2023-03-28 13:38:22 +08004948 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4949 {
qs.xiong77905552022-10-17 11:19:57 +08004950 return -1;
4951 }
4952
4953 CHECK_IDX(idx, CTRL_AP);
4954
4955 CHECK_WPA_CTRL(CTRL_AP);
4956
4957 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4958 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4959 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4960
4961 DO_OK_FAIL_REQUEST(cmd_disconnect);
4962 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4963 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4964 DO_OK_FAIL_REQUEST(cmd_save_config);
4965 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4966
4967 return 0;
4968}
you.chen0f5c6432022-11-07 18:31:14 +08004969//you.chen add for tv-box start
4970static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4971 FILE *fp;
4972 //printf("to exec cmd:%s\n", str_cmd);
4973 if((fp=popen(str_cmd,"r"))==NULL)
4974 {
4975 perror("popen error!");
4976 return -1;
4977 }
4978 if((fread(str_cmd_ret,max_len,1,fp))<0)
4979 {
4980 perror("fread fail!");
4981 fclose(fp);
4982 return -1;
4983 }
4984 fclose(fp);
4985 return 0;
4986}
4987
4988static int get_netmask_length(const char* mask)
4989{
4990 int masklen=0, i=0;
4991 int netmask=0;
4992
4993 if(mask == NULL)
4994 {
4995 return 0;
4996 }
4997
4998 struct in_addr ip_addr;
4999 if( inet_aton(mask, &ip_addr) )
5000 {
5001 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08005002 }else
5003 {
you.chen0f5c6432022-11-07 18:31:14 +08005004 netmask = 0;
5005 return 0;
5006 }
5007
5008 while(0 == (netmask & 0x01) && i<32)
5009 {
5010 i++;
5011 netmask = netmask>>1;
5012 }
5013 masklen = 32-i;
5014 return masklen;
5015}
5016
5017static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
5018 int mask_len;
5019 char *p;
5020 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08005021 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5022 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005023 return -1;
5024 p = strstr(str_cmd_ret, "Mask:");
5025 if (p == NULL)
5026 return -1;
5027 mask_len = get_netmask_length(p + 5);
5028 if (mask_len == 0)
5029 return -1;
5030 p = strstr(str_cmd_ret, "inet addr:");
5031 if (p == NULL)
5032 return -1;
5033 strcpy(tmp, p + 10);
5034 p = strstr(tmp, " ");
5035 if (p != NULL)
5036 *p = '\0';
5037 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5038 return 0;
5039}
5040
5041static void GBWWatchThreadProc() {
5042 int i,n, nloop, nmax, ncheckcount, nidlecount;
5043 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5044 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5045 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5046 char *results[16] = {0};
5047 char str_cmd[256] = {0};
5048 char str_cmd_ret[128] = {0};
5049 char dest_ip[32] = {0};
5050 lastAP1Bytes = lastAP2Bytes = 0;
5051 lastAP1Drop = lastAP2Drop = 0;
5052 lastAP1Speed = lastAP2Speed = 0;
5053 setAP1Speed = 50;
5054 setAP2Speed = 80;
5055 nloop = 0;
5056 nmax = 6;
5057 ncheckcount = nidlecount = 0;
5058
you.chen0df3e7e2023-05-10 15:56:26 +08005059 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005060 {
5061 RLOGE("------gbw thread run\n");
5062 return;
5063 }
5064
qs.xiong9fbf74e2023-03-28 13:38:22 +08005065 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005066 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5067 while (dest_ip[0] == '\0') {
5068 sleep(1);
5069 str_cmd_ret[0] = '\0';
5070 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5071 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5072 if (str_cmd_ret[n] == '\n'){
5073 str_cmd_ret[n] = '\0';
5074 break;
5075 }
5076 }
5077 if (str_cmd_ret[0] != '\0')
5078 {
5079 strcpy(dest_ip, str_cmd_ret);
5080 }
5081 }
5082
you.chenc9928582023-04-24 15:39:37 +08005083 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5084 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5085 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 +08005086 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5087 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005088 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005089 return;
5090 }
you.chenc9928582023-04-24 15:39:37 +08005091 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);
5092 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);
5093 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 +08005094
5095 while (1) {
5096 sleep(1);
5097 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005098 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5099 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5100 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005101 continue;
5102 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005103 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005104 if (n > 9) {
5105 if (strcmp(results[1], "Sent") == 0) {
5106 currAP1Bytes = atoll(results[2]);
5107 }
5108 if (strcmp(results[6], "(dropped") == 0) {
5109 currAP1Drop = atoi(results[7]);
5110 }
5111 }
5112
5113 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005114 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5115 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5116 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005117 continue;
5118 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005119 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005120 if (n > 9) {
5121 if (strcmp(results[1], "Sent") == 0) {
5122 currAP2Bytes = atoll(results[2]);
5123 }
5124 if (strcmp(results[6], "(dropped") == 0) {
5125 currAP2Drop = atoi(results[7]);
5126 }
5127 }
5128
5129 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5130 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5131 lastAP1Bytes = currAP1Bytes;
5132 lastAP2Bytes = currAP2Bytes;
5133 continue;
5134 }
5135
5136 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5137 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5138 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5139 lastAP1Speed = currAP1Speed;
5140 lastAP2Speed = currAP2Speed;
5141 lastAP1Bytes = currAP1Bytes;
5142 lastAP2Bytes = currAP2Bytes;
5143
5144 currSetAP1Speed = setAP1Speed;
5145 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5146 ncheckcount++;
5147 if (ncheckcount > 3) {
5148 ncheckcount = 0;
5149 currSetAP1Speed = 5;
5150 }
5151 }
5152 else {
5153 ncheckcount = 0;
5154 if (currAP1Speed < 5)
5155 nidlecount++;
5156 else
5157 nidlecount = 0;
5158
5159 }
5160
5161 if (nidlecount > 60 ){
5162 currSetAP1Speed = 50;
5163 }
5164
5165 if (currSetAP1Speed != setAP1Speed) {
5166 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005167 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5168 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005169 }
5170 }
5171}
5172
5173int enableGBW(const char* mac) {
5174 int i,len;
5175 char get_ipaddr_cmd[128]={0};
5176 ap_info_s *ap;
5177 device_info_s * list;
5178
5179 if (mac == NULL || g_gbw_enabled == 1)
5180 return -1;
5181 len = strlen(mac);
5182 g_gbw_mac = malloc(len + 1);
5183 for(i=0;i<len;i++) {
5184 if (mac[i] >= 'A' && mac[i] <= 'Z')
5185 {
5186 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5187 }
5188 else
5189 g_gbw_mac[i] = mac[i];
5190 }
5191 g_gbw_mac[i] = '\0';
5192 g_gbw_enabled = 1;
5193
5194 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5195 if (system(get_ipaddr_cmd) == 0) {
5196 //startGBW();
5197 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5198 for (i=0;i<len;i++) {
5199 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5200 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5201 startGBW();
5202 }
5203 free(ap);
5204 free(list);
5205 }
5206 }
5207 return 0;
5208}
5209
5210int disableGBW() {
5211 stopGBW();
5212 free(g_gbw_mac);
5213 g_gbw_mac = NULL;
5214 g_gbw_enabled = 1;
5215 return 0;
5216}
5217
5218static int startGBW() {
5219 if (g_gbw_watcher_pid != 0) {
5220 stopGBW();
5221 }
5222 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5223}
5224
5225static int stopGBW() {
5226 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005227 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005228 pthread_cancel(g_gbw_watcher_pid);
5229 pthread_join(g_gbw_watcher_pid, &retval);
5230 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005231 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5232 if (s_ap_iterface_name[0] != '\0')
5233 {
5234 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5235 system(cmd);
5236 }
you.chen0f5c6432022-11-07 18:31:14 +08005237}
5238//you.chen add for tv-box end