blob: e99ff0d0fd734c8cb0070aa5c7c0f33febe01b79 [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
70//const char * CTRL_PATH="/var/run/wpa_supplicant";
71const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
72//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
73const char * cmd_list_networks = "LIST_NETWORKS";
74const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080075const char * cmd_disconnect = "DISCONNECT";
76const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080077const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080078const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080079const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080080const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080081
you.chenf711c8a2023-04-13 13:49:45 +080082const char * cmd_ping = "PING";
83const char * rsp_pong = "PONG";
84const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
85const int MAX_IDLE_COUNT = 600; // 60s
86
you.chenc9928582023-04-24 15:39:37 +080087const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
88const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
89const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
90const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
91const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
92
93static char s_ap_iterface_name[64] = {0};
94
you.chend2fef3f2023-02-13 10:50:35 +080095struct local_wpa_ctrl{
96 struct wpa_ctrl *ctrl;
97 pthread_mutex_t mutex;
98};
99
qs.xiong09560402023-10-27 21:58:55 +0800100volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800101int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800102
you.chend2fef3f2023-02-13 10:50:35 +0800103static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800104static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
105static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800106// add for auto connect
107static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800108
109static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800110
you.chen0f5c6432022-11-07 18:31:14 +0800111//you.chen add for tv-box start
112volatile int g_gbw_enabled = 0;
113char * g_gbw_mac = NULL;
114pthread_t g_gbw_watcher_pid = 0;
115static int startGBW();
116static int stopGBW();
117//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800118
119typedef struct __curr_status_info {
120 ap_info_s *ap;
121 char * state;
122 int net_no;
123}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400124
you.chen70f377f2023-04-14 18:17:09 +0800125typedef enum {
126 INNER_STA_STATUS_INIT = 0,
127 INNER_STA_STATUS_CONNECTING,
128 INNER_STA_STATUS_ASSOCIATING,
129 INNER_STA_STATUS_ASSOCIATED,
130 INNER_STA_STATUS_CONNECTED,
131 INNER_STA_STATUS_DISCONNECTING,
132 INNER_STA_STATUS_DISCONNECTED,
133 INNER_STA_STATUS_CANCEL,
134}inner_sta_status_s;
135
136static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
137static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800138volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800139static error_number_s s_sta_error_number = -1;
140static char s_sta_current_connecting_ssid[64] = {0};
141static struct timespec s_sta_connect_timeout;
142const int MAX_CONNNECT_TIME = 15; // second
143pthread_t g_global_watcher_pid = 0;
144static int s_service_invoke_timeout_cnt=0;
145const int FAKE_MAX_INT_VALUE = 99999;
146
qs.xiong09560402023-10-27 21:58:55 +0800147static void print_disconnect_list()
148{
149 int i;
150 for( i = 0; i < g_history_disconnect_valid_num; i++ )
151 {
152 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__);
153 }
154
155 return;
156}
157
158// idex ----> history_disconnect_list[x] index
159static int removeElement(int idex)
160{
161 RLOGD("into removeElement");
162 if( index < 0 )
163 {
164 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
165 return -1;
166 }
167 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]);
168 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
169 g_history_disconnect_valid_num --;
170 RLOGD("end removeElement");
171 return 0;
172}
173static int check_history_disconenct_ap_list(int val)
174{
175 print_disconnect_list();
176 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__);
177 int i;
178 for( i = 0; i < g_history_disconnect_valid_num; i++)
179 {
180 if( val == g_history_disconnect_net[i] )
181 {
182 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
183 RLOGD("end check_history_disconenct_ap_list && return network index");
184 return i;
185 }
186 }
187 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__);
188 return -1;
189}
190
191
192static void lynq_sta_removeElement(int net_no)
193{
194 int ret;
195
196 ret = check_history_disconenct_ap_list(net_no);
197 if( ret == -1 )
198 {
199 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
200 return;
201 }else
202 {
203 ret = removeElement(ret);
204 if( ret == 0 )
205 {
206 RLOGD("removeElement pass %s %d",__func__,__LINE__);
207 return;
208 }
209 }
210
211 return;
212}
213
you.chen70f377f2023-04-14 18:17:09 +0800214static void notify_service_invoke_fail(int error)
215{
216 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
217 pthread_mutex_lock(&s_global_check_mutex);
218 if (error == -2) //timeout
219 {
220 s_service_invoke_timeout_cnt++;
221 if (s_service_invoke_timeout_cnt > 10)
222 {
223 pthread_cond_signal(&s_global_check_cond);
224 }
225 }
226 else if (error == -1)
227 {
228 // check if can connect wpa service
229 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
230 if (lynq_wpa_ctrl == NULL)
231 {
232 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
233 pthread_cond_signal(&s_global_check_cond);
234 }
235 wpa_ctrl_close(lynq_wpa_ctrl);
236 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
237 if (lynq_wpa_ctrl == NULL)
238 {
239 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
240 pthread_cond_signal(&s_global_check_cond);
241 }
242 wpa_ctrl_close(lynq_wpa_ctrl);
243 }
244
245 pthread_mutex_unlock(&s_global_check_mutex);
246}
247
you.chenc9928582023-04-24 15:39:37 +0800248static int system_call_v(const char * fmt, ...)
249{
250 char str_cmd[256] = {0};
251 va_list args;
252 va_start(args, fmt);
253 vsprintf(str_cmd, fmt, args);
254 va_end(args);
255 printf("system call----------%s\n", str_cmd);
256 return system(str_cmd);
257}
258
you.chen0df3e7e2023-05-10 15:56:26 +0800259static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
260
261static const char * inner_get_ap_interface_name()
262{
263 char * p;
264 char cmd[128]={0};
265
266 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
267 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
268 {
269 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
270 return NULL;
271 }
272 p = strchr(s_ap_iterface_name, ' ');
273 if (NULL != p)
274 {
275 *p = '\0';
276 }
277 p = strchr(s_ap_iterface_name, '\n');
278 if (NULL != p)
279 {
280 *p = '\0';
281 }
282 if (s_ap_iterface_name[0] == '\0')
283 {
284 return NULL;
285 }
286
287 return s_ap_iterface_name;
288}
289
you.chen70f377f2023-04-14 18:17:09 +0800290static void check_tether_and_notify()
291{
292 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800293 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 +0800294 {
295 return;
296 }
297 pthread_mutex_lock(&s_global_check_mutex);
298 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
299 pthread_cond_signal(&s_global_check_cond);
300 pthread_mutex_unlock(&s_global_check_mutex);
301}
302
you.chend2fef3f2023-02-13 10:50:35 +0800303static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
304 char *reply, size_t *reply_len,
305 void (*msg_cb)(char *msg, size_t len))
306{
307 int ret;
308 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800310 return -1;
311 }
312 pthread_mutex_lock(&ctrl->mutex);
313 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
314 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800315 if (ret != 0)
316 {
317 notify_service_invoke_fail(ret);
318 }
you.chend2fef3f2023-02-13 10:50:35 +0800319 return ret;
320}
321
322static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
323 int repeat_cnt;
324 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
325 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800326 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800327 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
328 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
329// printf("wait enable finish\n");
330 usleep(500 * 1000);
331 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
332 }
333 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800334 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800335 goto out_addr;
336 }
337 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
338 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
339 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800340 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800341 goto out_addr;
342 }
343 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
344 }
345 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
346out_addr:
347 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
348 return lynq_wpa_ctrl;
349}
350
qs.xiong97fa59b2022-04-07 05:41:29 -0400351#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400352{\
you.chen35020192022-05-06 11:30:57 +0800353 perror((str));\
354 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400355}
356
you.chen35020192022-05-06 11:30:57 +0800357#define CHECK_IDX(idx, type) do { \
358 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
359 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800360 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800361 return -1; \
362 } \
363 }while (0)
364
365#define CHECK_WPA_CTRL(index) int ret = 0;\
366 size_t reply_len = MAX_RET; \
367 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800368 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800369 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800370 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
371 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800372 }while(0)
373
374#define DO_REQUEST(cmd_str) do { \
375 reply_len = MAX_RET;\
376 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800377 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800378 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 +0800379 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800380 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800381 return ret; \
382 } \
383 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800384 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800385 }while(0)
386
387#define DO_OK_FAIL_REQUEST(cmd_str) do { \
388 DO_REQUEST(cmd_str); \
389 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800390 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800391 return -1; \
392 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800393 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800394 return -1; \
395 } \
396 }while (0)
397
398
you.chenf711c8a2023-04-13 13:49:45 +0800399static int check_connection(struct wpa_ctrl * wpa_ctrl)
400{
401 size_t reply_len = MAX_RET;
402 char cmd_reply[MAX_RET]={0};
403 int ret;
404
405 RLOGD("check_connection [%p]", wpa_ctrl);
406 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
407
408 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
409 {
410 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 +0800411 if (ret != 0)
412 {
413 notify_service_invoke_fail(ret);
414 }
you.chenf711c8a2023-04-13 13:49:45 +0800415 return -1;
416 }
417
418 return 0;
419}
420
421/**
422 * @brief check_pending_msg
423 * @param lynq_wpa_ctrl
424 * @return 1 has msg, 0 no msg, -1 error
425 */
426static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
427{
428 int ret;
429
430 if (*pp_lynq_wpa_ctrl == NULL) // need connect
431 {
432 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
433 if (*pp_lynq_wpa_ctrl == NULL)
434 {
435 usleep(SLEEP_TIME_ON_IDLE);
436 return -1;
437 }
438
439 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
440 if (ret == 0) // attach success
441 {
442 *started_flag = 1;
443 }
444 else
445 {
you.chen70f377f2023-04-14 18:17:09 +0800446 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800447 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
448 *pp_lynq_wpa_ctrl = NULL;
449 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800450 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800451 usleep(SLEEP_TIME_ON_IDLE);
452 return -1;
453 }
454 }
455
456 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
457 if ( ret == 0) // no pending messages
458 {
459 usleep(SLEEP_TIME_ON_IDLE);
460 *idle_count += 1;
461 if (*idle_count > MAX_IDLE_COUNT)
462 {
463 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
464 {
465 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
466 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
467 *pp_lynq_wpa_ctrl = NULL;
468 *idle_count = 0;
469 return -1;
470 }
471 *idle_count = 0;
472 }
473 return 0;
474 }
475 else if ( ret == -1) // on error
476 {
477 RLOGE("[wifi error]sta wpa_ctrl_pending");
478 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
479 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
480 *pp_lynq_wpa_ctrl = NULL;
481 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800482 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800483 return -1;
484 }
485
486 *idle_count = 0;
487 return 1;
488}
489
you.chen6d247052023-06-01 16:39:54 +0800490static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
491{
492 pthread_mutex_lock(&s_ap_callback_mutex);
493 if (g_ap_callback_func != NULL)
494 g_ap_callback_func(g_ap_callback_priv, status);
495 pthread_mutex_unlock(&s_ap_callback_mutex);
496}
497
you.chen35020192022-05-06 11:30:57 +0800498static void APWatcherThreadProc() {
499 size_t len = MAX_RET;
500 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800501 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800502
you.chen6c2dd9c2022-05-16 17:55:28 +0800503 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800504 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800505
qs.xiong9fbf74e2023-03-28 13:38:22 +0800506 while (g_ap_watcher_stop_flag == 0)
507 {
you.chenf711c8a2023-04-13 13:49:45 +0800508 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
509 {
you.chen70f377f2023-04-14 18:17:09 +0800510 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
511 {
512 check_tether_and_notify();
513 }
514
you.chen35020192022-05-06 11:30:57 +0800515 continue;
516 }
you.chenf711c8a2023-04-13 13:49:45 +0800517
you.chen6c2dd9c2022-05-16 17:55:28 +0800518 memset(msg_notify, 0, MAX_RET);
519 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800520 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800521 {
you.chen35020192022-05-06 11:30:57 +0800522 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800523 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800524 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800525 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800526 {
you.chen6d247052023-06-01 16:39:54 +0800527 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800528 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800529 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800530 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
531 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800532 {
you.chen0f5c6432022-11-07 18:31:14 +0800533 stopGBW();
534 }
535 }
you.chen35020192022-05-06 11:30:57 +0800536 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800537 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800538 {
you.chen6d247052023-06-01 16:39:54 +0800539 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800540 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800541 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800542 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
543 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800544 {
you.chen0f5c6432022-11-07 18:31:14 +0800545 startGBW();
546 }
547 }
you.chen35020192022-05-06 11:30:57 +0800548 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800549 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800550 {
551 RLOGD("APWatcherThreadProc ap------> service error");
552 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
553 }
554 else
555 {
556 RLOGD("APWatcherThreadProc ap------> going on check next msg");
557 }
you.chenf711c8a2023-04-13 13:49:45 +0800558 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800559 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
560 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800561 if (lynq_wpa_ctrl != NULL)
562 {
you.chen92fd5d32022-05-25 10:09:47 +0800563 wpa_ctrl_detach(lynq_wpa_ctrl);
564 wpa_ctrl_close(lynq_wpa_ctrl);
565 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400566}
567
you.chen70f377f2023-04-14 18:17:09 +0800568static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
569{
570 char * p;
571 const char * try_associat_flag = "Trying to associate";
572 const char * associated_flag = "Associated with ";
573
574 pthread_mutex_lock(&s_global_check_mutex);
575 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
576 {
577 pthread_mutex_unlock(&s_global_check_mutex);
578 return;
579 }
580
you.chen6e724162023-10-19 19:10:01 +0800581 // youchen@2023-10-17 add for "not notify connect fail directly" begin
582 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
583 {
584 s_sta_error_number = error_num;
585 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
586 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
587 pthread_cond_signal(&s_global_check_cond);
588 pthread_mutex_unlock(&s_global_check_mutex);
589 return;
590 }
591 // youchen@2023-10-17 add for "not notify connect fail directly" end
592
you.chen70f377f2023-04-14 18:17:09 +0800593 if (state == LYNQ_WIFI_STATUS_EGNORE)
594 {
595 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
596 {
597 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
598 }
599 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
600 {
601 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
602 }
603 }
604 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
605 {
606 s_sta_error_number = error_num;
607 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
608 {
609 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800610 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 +0800611 pthread_cond_signal(&s_global_check_cond);
612 }
613 }
614 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
615 {
616 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800617 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 +0800618 pthread_cond_signal(&s_global_check_cond);
619 }
620 pthread_mutex_unlock(&s_global_check_mutex);
621}
622
qs.xiongb37f8c42023-09-13 21:21:58 +0800623static int lynq_split(char * str, int len, char delimiter, char * results[]);
624static inline char inner_convert_char(char in);
625static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
626static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
627
628
629
qs.xiong44fac672023-08-29 16:15:55 +0800630/*
631just tmp add for fix sta connect ap fail check ap connect info
632return 0 --->Current no sta device connect this AP
633*/
634static int lynq_connected_ap_sta_status() {
635
636 FILE *fp;
637 size_t i = 0;
638 int ret;
639 char lynq_cmd_ret[MAX_RET]={0};
640
641 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
642 {
643 perror("popen error!");
644 return -1;
645 }
646 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
647 {
648 perror("fread fail!");
649 ret=pclose(fp);
650 if(ret == -1)
651 perror("close file faild");
652 return -1;
653 }
654 if( strlen(lynq_cmd_ret) < MAC_LEN)
655 {
656 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
657 ret=pclose(fp);
658 if(ret==-1)
659 {
660 perror("close file faild");
661 }
662 return 0;
663 }else{
664 ret=pclose(fp);
665 if(ret==-1)
666 {
667 perror("close file faild");
668 }
669 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
670 return 1;
671 }
672}
673
674/*
675 just tmp add for fix sta connect ap fail; check fw status
676 return 1 ----> fw status error; need wl down/up
677*/
678static int check_current_fw_status() {
679
680 FILE *fp;
681 FILE *fp1;
682 size_t i = 0;
683 int ret;
684 char lynq_cmd_ret_2g[MAX_RET]={0};
685 char lynq_cmd_ret_5g[MAX_RET]={0};
686
687 const char * fw_status = "0x0096"; //0x0096 is normal fw status
688
689 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
690 {
691 perror("popen error!");
692 return -1;
693 }
694 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
695 {
696 perror("fread fail!");
697 if(pclose(fp) == -1)
698 perror("close fp file faild");
699 return -1;
700 }
701
702 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
703 {
704 perror("popen error!");
705 if(pclose(fp) == -1)
706 perror("clsoe fp file faild");
707 return -1;
708 }
709 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
710 {
711 perror("fread fail!");
712 if(pclose(fp1) == -1)
713 perror("clsoe fp1 file faild");
714 if(pclose(fp) == -1)
715 perror("clsoe fp file faild");
716 return -1;
717 }
718
719 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
720 {
721 ret=pclose(fp);
722 if(ret==-1)
723 {
724 perror("close fp file faild");
725 }
726 ret=pclose(fp1);
727 if(ret==-1)
728 {
729 perror("close fp1 file faild");
730 }
731 return 0;
732 }else
733 {
734 ret=pclose(fp);
735 if(ret==-1)
736 {
737 perror("close file faild");
738 }
739 if(pclose(fp1) == -1)
740 {
741 perror("clsoe file fp1 faild");
742 }
743 RLOGD("current fw status --error--");
744 return 1;
745 }
746}
747
qs.xiong1d4263a2023-09-06 10:46:23 +0800748/*
749eg: wl counters info
750sh-3.2# wl counters
751counters_version 30
752datalen 1648
753Slice_index: 0
754reinitreason_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)
755reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
756
757check reinit status
758return 0 ===> fw did wl reinit cmd
759*/
760static int check_current_reinit_info()
761{
762 FILE *fp;
763 int ret;
764 char lynq_cmd_ret[MAX_RET]={0};
765 char * dest;
766 char destid[3]={0};
767 if((fp=popen("wl counters","r"))==NULL)
768 {
769 perror("popen error!");
770 return -1;
771 }
772 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
773 {
774 perror("fread fail!");
775 if(pclose(fp) == -1)
776 {
777 perror("close fp file faild");
778 }
779 return -1;
780 }
781 dest = strstr(lynq_cmd_ret,"reinit ");
782 if(dest != NULL)
783 {
784 dest +=strlen("reinit ");
785 RLOGD("current get dest str is %s",dest);
786 memcpy(destid,dest,2);
787 ret = atoi(destid);
788 RLOGD("get current wl counters cmd return counts is %d",ret);
789 if( ret != 0 )
790 {
791 RLOGD("current fw did run cmd wl reinit");
792 if( pclose(fp) == -1 )
793 {
794 perror("close fp file faild");
795 }
796 return 0;
797 }
798 }
799 if( pclose(fp) == -1 )
800 {
801 perror("close fp file faild");
802 }
803 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
804 return -1;
805}
806
qs.xiong44fac672023-08-29 16:15:55 +0800807static void APTmpWatcherThreadProc() {
808
809 int i = 0;
810 int delytime = 300;
811 g_ap_tmp_watcher_stop_flag = 0;
812
813 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
814 while(1)
815 {
816 sleep(1);
817 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800818 if ( (i % 30) == 0 )
819 {
820 if ( check_current_reinit_info() == 0 )
821 {
822 system("wl reset_cnts");
823 system("wl down");
824 system("wl up");
825 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
826 }
827 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800828 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800829 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800830 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800831 {
832 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
833 {
834 system("wl down");
835 system("wl up");
836 }
qs.xiong44fac672023-08-29 16:15:55 +0800837 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800838 }
839 if ( i == delytime )
840 {
qs.xiong44fac672023-08-29 16:15:55 +0800841 i = 0;
842 }
843 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
844 {
845 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
846 return;
847 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800848
qs.xiong44fac672023-08-29 16:15:55 +0800849 }
850
851}
852
qs.xiongf0128b12023-06-29 17:29:39 +0800853static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
854{
855 char LYNQ_DISABLE_CMD[128]={0};
856
857 CHECK_IDX(idx, CTRL_STA);
858 CHECK_WPA_CTRL(CTRL_STA);
859
860 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800861 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800862 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800863
qs.xiongf0128b12023-06-29 17:29:39 +0800864 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800865
qs.xiongf0128b12023-06-29 17:29:39 +0800866}
867
qs.xiongb37f8c42023-09-13 21:21:58 +0800868static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
869{
870 char LYNQ_ENABLE_CMD[128]={0};
871
872 CHECK_IDX(idx, CTRL_STA);
873 CHECK_WPA_CTRL(CTRL_STA);
874
875
876 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800877 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800878 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
879
880 return 0;
881
882}
883
884static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
885{
886
887 int index,networkid;
888
889 for ( index = 0; index < len ;index++)
890 {
891 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800892 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800893 {
894 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
895 }
896 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
897 }
898 return 0;
899
900}
901
902
903/*
904 dis_net_list user disconnect list
905*/
906static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
907{
qs.xiong09560402023-10-27 21:58:55 +0800908 RLOGD("enter %s %d",__func__,__LINE__);
909 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800910 int count,ncount,index;
911 int flag = 0;
912 int merge_index = 0;
913 int net_no_list[128];
914
qs.xiong09560402023-10-27 21:58:55 +0800915 for(ncount = 0;ncount < valid_num; ncount++ )
916 {
917 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
918 }
919
qs.xiongb37f8c42023-09-13 21:21:58 +0800920 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
921 for( count = 0; count < index; count++)
922 {
923 for(ncount = 0; ncount < valid_num; ncount++)
924 {
qs.xiong09560402023-10-27 21:58:55 +0800925 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800926 if(net_no_list[count] == dis_net_list[ncount])
927 {
qs.xiong09560402023-10-27 21:58:55 +0800928 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800929 flag = 1;
930 break;
931 }
932 }
933 if( flag != 1 )
934 {
935 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800936 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 +0800937 merge_index ++;
938 }
939 flag = 0;
940 }
941 * outlen =merge_index;
942 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
943 return;
944}
qs.xiongf0128b12023-06-29 17:29:39 +0800945
qs.xiong455c30b2023-04-12 11:40:02 +0800946void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
947{
948 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800949 char *wpanetid;
950 char destid[3] = {0};
951 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800952 *error = LYNQ_WAIT_CONNECT_ACTIVE;
953 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
954 {
955 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
956 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
957 return;
958 }
959
qs.xiong20202422023-09-06 18:01:18 +0800960 if (strstr(modify, "Trying to associate") != NULL)
961 {
962 RLOGD("Current sta is Trying to associate");
963 *state = LYNQ_WIFI_STATUS_EGNORE;
964 g_sta_conncet_status_flag = 1;
965 return;
966 }
967
qs.xiong455c30b2023-04-12 11:40:02 +0800968 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
969 {
970 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800971 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800972 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800973 return;
974 }
975
qs.xiong1e81dfa2023-09-27 15:52:37 +0800976 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
977 {
978 *error = LYNQ_PSW_ERROR;
979 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
980 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
981 g_sta_conncet_status_flag = 0;
982 return;
983 }
qs.xiong455c30b2023-04-12 11:40:02 +0800984 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
985 {
qs.xiongf0128b12023-06-29 17:29:39 +0800986 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
987 wpanetid = strstr(modify,"id=");
988 if ( wpanetid != NULL )
989 {
990 wpanetid +=strlen("id=");
991 memcpy(destid,wpanetid,2);
992 tmpdisid = atoi(destid);
993
994 }
qs.xiong455c30b2023-04-12 11:40:02 +0800995 pReason = strstr(modify, "reason=");
996 if (pReason != NULL)
997 {
998 pReason += strlen("reason=");
999 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1000 {
1001 *error = LYNQ_TIME_OUT;
1002 }
1003 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1004 {
1005 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001006 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001007 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001008 // you.chen@2023-10-17 only disable network during autoconnect
1009 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 +08001010 {
1011 RLOGE("stop wlan0 network %d fail",tmpdisid);
1012 }
qs.xiong455c30b2023-04-12 11:40:02 +08001013 }
1014 else
1015 {
1016 *error = LYNQ_UNSPECIFIED_REASON;
1017 }
qs.xiong455c30b2023-04-12 11:40:02 +08001018 }
1019 else
1020 {
1021 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001022 }
qs.xiongf0128b12023-06-29 17:29:39 +08001023 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001024 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001025 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001026
1027 }
1028
1029 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1030 {
1031 *error = LYNQ_NOT_FIND_AP;
1032 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1033 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001034 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001035 return;
1036 }
1037
1038
1039 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1040 {
1041 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1042 pReason = strstr(modify, "status_code=");
1043 if (pReason != NULL)
1044 {
1045 pReason += strlen("status_code=");
1046 if (memcmp(pReason, "17", 2) == 0)
1047 {
1048 *error = LYNQ_AP_UNABLE_HANDLE;
1049 }
1050 else if (memcmp(pReason, "1",1) == 0)
1051 {
1052 *error = LYNQ_UNSPECIFIED_REASON;
1053 }
1054 else
1055 {
1056 *error = LYNQ_UNSPECIFIED_REASON;
1057 }
1058
1059 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1060 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 +08001061 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001062 return;
1063 }
1064 else
1065 {
1066 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1067 *error = LYNQ_UNSPECIFIED_REASON;
1068 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1069 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1070 return;
1071 }
1072 }
1073
1074 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1075 {
1076 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1077 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1078 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1079 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1080 return;
1081 }
1082
1083 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1084 {
1085 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1086 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1087 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1088 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1089 return;
1090 }
1091
qs.xiongdf637b22023-10-26 19:30:07 +08001092 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1093 if (strstr(modify, "Association request to the driver failed") != NULL)
1094 {
1095 RLOGD("Association request to the driver failed --- recover");
1096 system("wl down");
1097 system("wl up");
1098 *error = LYNQ_UNSPECIFIED_REASON;
1099 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1100 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1101 return;
1102 }
1103 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1104
1105
you.chen32cb31e2023-04-13 14:05:45 +08001106 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001107 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001108 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001109 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1110 return;
1111
1112}
1113
qs.xiongfcc914b2023-07-06 21:16:20 +08001114void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1115{
1116 char *pReason;
1117 char *wpanetid;
1118 char destid[3];
1119 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1120 *networkid = -1;
1121 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1122 {
1123 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1124 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1125 return;
1126 }
1127 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1128 {
1129 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1130 wpanetid = strstr(modify,"id=");
1131 if ( wpanetid != NULL )
1132 {
1133 wpanetid +=strlen("id=");
1134 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1135 if (memcpy(destid,wpanetid,2) != NULL)
1136 {
1137 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1138 *networkid = atoi(destid);
1139 RLOGD("get networkid is %d\n",*networkid);
1140 }
1141 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1142 }
1143 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1144 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1145 return;
1146 }
1147 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1148 {
1149 wpanetid = strstr(modify,"id=");
1150 if ( wpanetid != NULL )
1151 {
1152 wpanetid +=strlen("id=");
1153 if (memcpy(destid,wpanetid,2) != NULL)
1154 {
1155 *networkid = atoi(destid);
1156 RLOGD("get networkid is %d\n",*networkid);
1157 }
1158 }
1159 pReason = strstr(modify, "reason=");
1160 if (pReason != NULL)
1161 {
1162 pReason += strlen("reason=");
1163 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1164 {
1165 *error = LYNQ_TIME_OUT;
1166 }
1167 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1168 {
1169 *error = LYNQ_PSW_ERROR;
1170 }
1171 else
1172 {
1173 *error = LYNQ_UNSPECIFIED_REASON;
1174 }
1175 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1176 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1177 return;
1178 }
1179 else
1180 {
1181 *error = LYNQ_UNSPECIFIED_REASON;
1182 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1183 return;
1184 }
1185 }
1186 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1187 {
1188 wpanetid = strstr(modify,"id=");
1189 if ( wpanetid != NULL )
1190 {
1191 wpanetid +=strlen("id=");
1192 if (memcpy(destid,wpanetid,2) != NULL)
1193 {
1194 *networkid = atoi(destid);
1195 RLOGD("get networkid is %d\n",*networkid);
1196 }
1197 }
1198 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1199 pReason = strstr(modify, "status_code=");
1200 if (pReason != NULL)
1201 {
1202 pReason += strlen("status_code=");
1203 if (memcmp(pReason, "17", 2) == 0)
1204 {
1205 *error = LYNQ_AP_UNABLE_HANDLE;
1206 }
1207 else if (memcmp(pReason, "1",1) == 0)
1208 {
1209 *error = LYNQ_UNSPECIFIED_REASON;
1210 }
1211 else
1212 {
1213 *error = LYNQ_UNSPECIFIED_REASON;
1214 }
1215 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1216 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1217 return;
1218 }
1219 else
1220 {
1221 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1222 *error = LYNQ_UNSPECIFIED_REASON;
1223 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001224 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001225 return;
1226 }
1227 }
1228 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1229 {
1230 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1231 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1232 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1233 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1234 return;
1235 }
1236 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1237 {
1238 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1239 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1240 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1241 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1242 return;
1243 }
1244 RLOGD("EVENT : %s\n", modify);
1245 *error = LYNQ_UNSPECIFIED_REASON;
1246 *state = LYNQ_WIFI_STATUS_EGNORE;
1247 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1248 return;
1249}
you.chen70f377f2023-04-14 18:17:09 +08001250static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1251{
you.chen6d247052023-06-01 16:39:54 +08001252 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001253 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1254 {
1255 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1256 g_sta_callback_func(g_sta_callback_priv, state, error);
1257 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1258 }
you.chen6d247052023-06-01 16:39:54 +08001259 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001260}
qs.xiongfcc914b2023-07-06 21:16:20 +08001261static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1262{
1263 pthread_mutex_lock(&s_sta_callback_mutex);
1264 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1265 {
1266 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1267 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1268 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1269 }
1270 pthread_mutex_unlock(&s_sta_callback_mutex);
1271}
you.chen70f377f2023-04-14 18:17:09 +08001272
you.chen35020192022-05-06 11:30:57 +08001273static void STAWatcherThreadProc() {
1274 size_t len = MAX_RET;
1275 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001276 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001277 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001278 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001279
you.chen6c2dd9c2022-05-16 17:55:28 +08001280 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001281 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001282
you.chen70f377f2023-04-14 18:17:09 +08001283 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001284 while (g_sta_watcher_stop_flag == 0)
1285 {
you.chenf711c8a2023-04-13 13:49:45 +08001286 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001287 {
you.chen35020192022-05-06 11:30:57 +08001288 continue;
1289 }
you.chenf711c8a2023-04-13 13:49:45 +08001290
you.chen6c2dd9c2022-05-16 17:55:28 +08001291 memset(msg_notify, 0, MAX_RET);
1292 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001293 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001294 {
you.chen35020192022-05-06 11:30:57 +08001295 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001296 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1297 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001298 {
you.chen35020192022-05-06 11:30:57 +08001299 g_sta_scan_finish_flag = 1;
1300 }
1301
qs.xiong9fbf74e2023-03-28 13:38:22 +08001302 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001303 {
you.chen35020192022-05-06 11:30:57 +08001304 continue;
1305 }
qs.xiong455c30b2023-04-12 11:40:02 +08001306 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001307 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1308 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1309 {
1310 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1311 }
you.chen0c9bee22023-10-25 13:03:14 +08001312 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1313 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1314 {
1315 RLOGD("donot report scan result during in call connect manual");
1316 }
you.chen6e724162023-10-19 19:10:01 +08001317 else
1318 {
1319 notify_connect_status(state, error);
1320 }
1321 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001322
1323 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001324 {
you.chen70f377f2023-04-14 18:17:09 +08001325 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001326 if (last_state != state)
1327 {
1328 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1329 {
1330 system_call_v("%s %s", sta_status_change_script, "connect");
1331 }
1332 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1333 {
1334 system_call_v("%s %s", sta_status_change_script, "disconnect");
1335 }
1336 }
qs.xiong09560402023-10-27 21:58:55 +08001337
you.chenc9928582023-04-24 15:39:37 +08001338 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001339 if (g_sta_fake_scan_finish_flag == 1)
1340 {
1341 g_sta_fake_scan_finish_flag = 0;
1342 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1343 }
you.chen32cb31e2023-04-13 14:05:45 +08001344 }
you.chen35020192022-05-06 11:30:57 +08001345 }
1346 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001347 if (lynq_wpa_ctrl != NULL)
1348 {
you.chen92fd5d32022-05-25 10:09:47 +08001349 wpa_ctrl_detach(lynq_wpa_ctrl);
1350 wpa_ctrl_close(lynq_wpa_ctrl);
1351 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001352}
qs.xiongfcc914b2023-07-06 21:16:20 +08001353static void STAAutoWatcherThreadProc() {
1354 size_t len = MAX_RET;
1355 char msg_notify[MAX_RET];
1356 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001357 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001358 int idle_count = 0;
1359 int networkid;
1360 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1361 g_sta_auto_watcher_stop_flag = 0;
1362 RLOGD("STAAutoWatcherThreadProc thread started ------");
1363 while (g_sta_auto_watcher_stop_flag == 0)
1364 {
1365 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1366 {
1367 continue;
1368 }
1369 memset(msg_notify, 0, MAX_RET);
1370 len = MAX_RET;
1371 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1372 {
1373 msg_notify[len+1] = '\0';
1374 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1375 if (strstr(msg_notify, state_scan_result) != NULL)
1376 {
1377 g_sta_auto_scan_finish_flag = 1;
1378 }
1379 if (g_sta_auto_callback_func == NULL)
1380 {
1381 continue;
1382 }
1383 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1384 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001385 }
1386 }
1387 if (lynq_wpa_ctrl != NULL)
1388 {
1389 wpa_ctrl_detach(lynq_wpa_ctrl);
1390 wpa_ctrl_close(lynq_wpa_ctrl);
1391 }
1392}
qs.xiongf1b525b2022-03-31 00:58:23 -04001393
you.chen70f377f2023-04-14 18:17:09 +08001394// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1395static void GlobalWatcherThreadProc()
1396{
1397 int ret, connect_timeout, service_abnormal;
1398 error_number_s error_num = -1;
1399 inner_sta_status_s sta_status;
1400 scan_info_s *scan_list = NULL;
1401 int i, scan_len=0;
1402 char connecting_ssid[64];
1403 struct timeval now;
1404
1405 RLOGD("GlobalWatcherThreadProc start to run");
1406
1407 while (1)
1408 {
1409 pthread_mutex_lock(&s_global_check_mutex);
1410 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1411 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1412 {
1413 pthread_mutex_unlock(&s_global_check_mutex);
1414 usleep(50*1000);
1415 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1416 continue;
1417 }
1418
1419 connect_timeout = 0;
1420 service_abnormal = 0;
1421 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1422 {
1423 while (1)
1424 {
1425 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1426 if (ret == ETIME)
1427 {
1428 connect_timeout = 1;
1429 }
1430 else if (ret != 0)
1431 {
1432 gettimeofday(&now,NULL);
1433 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1434 {
1435 usleep(SLEEP_TIME_ON_IDLE);
1436 continue;
1437 }
1438 connect_timeout = 1;
1439 }
1440 sta_status = s_sta_status;
1441 error_num = s_sta_error_number;
1442 s_sta_status = INNER_STA_STATUS_INIT;
1443 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1444 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1445 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1446 break;
1447 }
1448 }
1449 if (s_service_invoke_timeout_cnt > 10)
1450 {
1451 service_abnormal = 1;
1452 s_service_invoke_timeout_cnt = 0;
1453 }
1454 pthread_mutex_unlock(&s_global_check_mutex);
1455
1456 if (service_abnormal == 1)
1457 {
1458 sleep(1);
1459 RLOGE("wpa service is abnormal info app to exit");
1460 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001461
1462 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1463
you.chen70f377f2023-04-14 18:17:09 +08001464 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1465 }
1466
1467 if (sta_status == INNER_STA_STATUS_CANCEL)
1468 {
1469 continue;
1470 }
1471 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1472 {
1473 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1474 }
1475 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1476 {
1477 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1478 {
1479 for(i=0; i < scan_len;i++)
1480 {
1481 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1482 {
1483 error_num = LYNQ_AUTH_ERROR;
1484 break;
1485 }
1486 }
1487 free(scan_list);
1488 }
1489 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1490 }
1491 else if (connect_timeout == 0)
1492 {
1493 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1494 }
1495 else // wait timeout
1496 {
1497 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1498 {
1499 ; // wpa service abnormal
1500 }
1501 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1502 {
1503 RLOGD("GlobalWatcherThreadProc notify connected");
1504 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1505 }
1506 else
1507 {
1508 RLOGD("GlobalWatcherThreadProc notify timeout");
1509 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1510 }
1511 }
1512 } // while (1)
1513}
1514
qs.xiong1af5daf2022-03-14 09:12:12 -04001515int lynq_wifi_enable(void)
1516{
you.chen35020192022-05-06 11:30:57 +08001517 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001518 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001519 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001520 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1521
qs.xiong9fbf74e2023-03-28 13:38:22 +08001522 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1523 {
you.chend2fef3f2023-02-13 10:50:35 +08001524 goto out_enable;
1525 }
1526
you.chenc9928582023-04-24 15:39:37 +08001527 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001528 if (ret != 0)
1529 {
1530 //printf("service state %d\n", ret);
1531 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001532 ret = -1;
1533 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001534 }
lhfe8da902022-10-11 18:55:36 +08001535
you.chen70f377f2023-04-14 18:17:09 +08001536 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1537 {
1538 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1539 if(ret<0)
1540 {
1541 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1542 ret = -1;
1543 goto out_enable;
1544 }
1545 }
1546
you.chend2fef3f2023-02-13 10:50:35 +08001547 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1548 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1549 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1550 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1551out_enable:
1552 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001553 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001554}
1555
qs.xiong1af5daf2022-03-14 09:12:12 -04001556int lynq_wifi_disable(void)
1557{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001558 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001559 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001560 g_ap_watcher_stop_flag = 1;
1561 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001562 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001563 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001564 if (g_ap_watcher_pid != 0)
1565 pthread_join(g_ap_watcher_pid, NULL);
1566 if (g_sta_watcher_pid != 0)
1567 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001568 if (g_sta_auto_watcher_pid != 0)
1569 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001570 if (g_lynq_wpa_ctrl[0] != NULL)
1571 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1572 if (g_lynq_wpa_ctrl[1] != NULL)
1573 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001574 if (g_ap_tmp_watcher_pid != 0)
1575 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001576 g_ap_watcher_pid = 0;
1577 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001578 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001579 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001580 g_lynq_wpa_ctrl[0] = NULL;
1581 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001582 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001583 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001584 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1585 return 0;
1586}
1587
1588static inline char inner_convert_char(char in)
1589{
1590 if (in >= '0' && in <= '9')
1591 {
1592 return in - '0';
1593 }
1594 else if (in >= 'a' && in <= 'f')
1595 {
1596 return in - 'a' + 10;
1597 }
1598 else if (in >= 'A' && in <= 'F')
1599 {
1600 return in - 'A' + 10;
1601 }
1602 else
1603 {
1604 return '\xff';
1605 }
1606}
1607
1608static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1609{
1610 char *p;
1611 size_t pos = 0;
1612 if (NULL == out_ssid)
1613 return;
1614 //printf("input ssid=[%s]\n", ssid);
1615 memset(out_ssid, 0, out_ssid_len);
1616 if (NULL == ssid)
1617 return;
1618 p = strchr(ssid, '\\');
1619 if (NULL == p)
1620 {
1621 strncpy(out_ssid, ssid, out_ssid_len);
1622 //printf(" first %s\n", out_ssid);
1623 }
1624 else
1625 {
1626 pos = p - ssid;
1627 memcpy(out_ssid, ssid, pos);
1628 //printf("pos %lu -- %s\n", pos, out_ssid);
1629 for(; pos < out_ssid_len; pos ++)
1630 {
1631 if (p[0] == '\0')
1632 {
1633 //printf(" out %s\n", out_ssid);
1634 return;
1635 }
1636 else if (p[0] != '\\')
1637 {
1638 out_ssid[pos] = p[0];
1639 p += 1;
1640 }
1641 else if (p[1] == 'x' || p[1] == 'X')
1642 {
1643 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1644 p += 4;
1645 }
1646 else if (p[1] == '\\')
1647 {
1648 out_ssid[pos] = '\\';
1649 p += 2;
1650 }
1651 else if (p[1] == 't')
1652 {
1653 out_ssid[pos] = '\t';
1654 p += 2;
1655 }
1656 else if (p[1] == 'r')
1657 {
1658 out_ssid[pos] = '\r';
1659 p += 2;
1660 }
1661 else if (p[1] == 'n')
1662 {
1663 out_ssid[pos] = '\n';
1664 p += 2;
1665 }//todo find a better way to convert?
1666 }
1667 }
1668 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001669}
qs.xiong1af5daf2022-03-14 09:12:12 -04001670
you.chen35020192022-05-06 11:30:57 +08001671static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001672 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001673 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001674 RLOGD("enter inner_get_param");
1675 if (out_put == NULL)
1676 {
1677 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001678 return -1;
1679 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001680 if (param_name == NULL)
1681 {
1682 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001683 return -1;
1684 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001685 if (param_name[0] == '\0')
1686 {
1687 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001688 return -1;
1689 }
1690
1691 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1692
1693 CHECK_WPA_CTRL(interface);
1694
1695 DO_REQUEST(lynq_cmd_get);
1696
qs.xiong9fbf74e2023-03-28 13:38:22 +08001697 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1698 {
1699 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001700 return -1;
1701 }
1702
you.chena6fa5b22022-05-18 10:28:19 +08001703// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001704 if (strcmp(param_name, "ssid") == 0)
1705 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001706 if (cmd_reply[0] == '\"')
1707 {
you.chend2fef3f2023-02-13 10:50:35 +08001708 ssid_len = reply_len - 1;
1709 memcpy(out_put, cmd_reply + 1, ssid_len);
1710 if (out_put[ssid_len-1] == '\"')
1711 {
1712 out_put[ssid_len-1] = '\0';
1713 }
1714 else
1715 {
1716 out_put[ssid_len] = '\0';
1717 }
1718 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001719 else
1720 {
you.chend2fef3f2023-02-13 10:50:35 +08001721 ssid_len = reply_len / 2;
1722 for(i=0; i<ssid_len; i++)
1723 {
1724 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1725 }
1726 out_put[ssid_len] = '\0';
1727 }
1728 }
1729 else
1730 {
1731 memcpy(out_put, cmd_reply, reply_len + 1);
1732 }
you.chen35020192022-05-06 11:30:57 +08001733 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001734}
qs.xiong1af5daf2022-03-14 09:12:12 -04001735
you.chen35020192022-05-06 11:30:57 +08001736static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1737 int ret = 0;
1738 char * end = str + len - 1;
1739 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001740 while(str < end)
1741 {
1742 if (*str == delimiter)
1743 {
you.chen35020192022-05-06 11:30:57 +08001744 *str++ = '\0';
1745 results[ret++] = str;
1746 continue;
1747 }
1748 str++;
1749 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001750 if (*str == delimiter)
1751 {
you.chen35020192022-05-06 11:30:57 +08001752 *str = '\0';
1753 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001754
you.chen6ed36a62023-04-27 17:51:56 +08001755 results[ret] = NULL;
1756
you.chen35020192022-05-06 11:30:57 +08001757 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001758}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001759/*
1760 *add func to get conencted STA device ip from dnsmasq ap0.lease
1761 *return 0 means get ip success
1762 */
1763static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1764{
1765 char * p;
1766 int ret;
1767 char cmd[256]={0};
1768 if (NULL == mac || NULL == ip)
1769 return -1;
1770 memset(ip, 0, ip_len);
1771 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1772 ret = exec_cmd(cmd, ip, ip_len);
1773 if( ret == 0 )
1774 {
1775 p = strchr(ip, '\n');
1776 if (NULL != p)
1777 {
1778 *p = '\0';
1779 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1780 return ret;
1781 }else
1782 {
1783 ret = -1;
1784 }
1785 }
1786 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1787 return ret;
1788
1789}
qs.xiong7a105ce2022-03-02 09:43:11 -05001790
you.chend2fef3f2023-02-13 10:50:35 +08001791static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1792{
1793 char * p;
1794 int ret = 0;
1795 char cmd[256]={0};
1796 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001797 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001798 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001799 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001800 ret = exec_cmd(cmd, ip, ip_len);
1801 p = strchr(ip, '\n');
1802 if (NULL != p)
1803 {
1804 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001805 }else
1806 {
1807 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001808 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001809 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001810 return ret;
1811}
1812
you.chend2fef3f2023-02-13 10:50:35 +08001813static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001814 struct in_addr addr ={0};
1815 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001816 char cmd[64] = {0};
1817 char * p;
1818 int ret;
you.chen35020192022-05-06 11:30:57 +08001819
qs.xiong9fbf74e2023-03-28 13:38:22 +08001820 if (ip == NULL || *ip == '\0' || hostname == NULL)
1821 {
1822 RLOGE("ip == NULL or hostname == NULL");
1823 return -1;
you.chen35020192022-05-06 11:30:57 +08001824 }
1825
you.chend2fef3f2023-02-13 10:50:35 +08001826 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001827 if (inet_aton(ip, &addr) == 0)
1828 {
you.chen35020192022-05-06 11:30:57 +08001829 printf("---inet_aton fail\n");
1830 return -1;
1831 }
1832
1833 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1834
qs.xiong9fbf74e2023-03-28 13:38:22 +08001835 if (ht == NULL)
1836 {
you.chen186d3c32023-05-18 14:19:46 +08001837 hostname[0] = '\0';
1838 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1839 ret = exec_cmd(cmd, hostname, 32);
1840 if (ret == 0)
1841 {
1842 p = strchr(hostname, '\n');
1843 if (p != NULL)
1844 {
1845 *p = '\0';
1846 }
1847 return 0;
1848 }
1849 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001850 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001851 herror(NULL);
1852 return -1;
1853 }
1854
1855 strcpy(hostname, ht->h_name);
1856
1857 return 0;
1858}
1859
1860static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1861{
1862 int count, index, words_count;
1863 char * split_lines[128]= {0};
1864 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001865 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001866 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001867 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001868
1869 CHECK_WPA_CTRL(ap_sta);
1870
1871 DO_REQUEST(lynq_wifi_list_networks);
1872
1873 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1874
1875 //@todo check ssid field to compatible
1876
1877 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 for(index=1; index < count; index++)
1879 {
you.chen35020192022-05-06 11:30:57 +08001880 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001881 if (words_count > 2)
1882 {
you.chend2fef3f2023-02-13 10:50:35 +08001883 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001884 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1885 {
you.chen35020192022-05-06 11:30:57 +08001886 net_no_list[ret++] = atoi(split_words[0]);
1887 }
1888 }
1889 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001890 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001891 return ret;
1892}
1893
1894static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001895 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001896 CHECK_WPA_CTRL(ap_sta);
1897 const char *lynq_wifi_add_network = "ADD_NETWORK";
1898
qs.xiong9fbf74e2023-03-28 13:38:22 +08001899 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001900 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001901 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1902 {
1903 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001904 return -1;
1905 }
1906
qs.xiong9fbf74e2023-03-28 13:38:22 +08001907 for(i=0;i<reply_len;i++)
1908 {
1909 if(cmd_reply[i] == '\n')
1910 {
you.chen35020192022-05-06 11:30:57 +08001911 cmd_reply[i] = '\0';
1912 break;
1913 }
1914 }
1915 return atoi(cmd_reply);
1916}
you.chena6cd55a2022-05-08 12:20:18 +08001917
you.chen35020192022-05-06 11:30:57 +08001918static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1919{
1920 int count, index;
1921 int net_no_list[128];
1922
qs.xiong9fbf74e2023-03-28 13:38:22 +08001923 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001924 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001925 for (index=0; index < count; index++)
1926 {
1927 if (net_no_list[index] == net_no)
1928 {
you.chen35020192022-05-06 11:30:57 +08001929 return 0;
1930 }
1931 }
1932
1933 if (count >= 1)
1934 index = net_no_list[count - 1];
1935 else
1936 index = -1;
1937
qs.xiong9fbf74e2023-03-28 13:38:22 +08001938 while (index < net_no )
1939 {
you.chen35020192022-05-06 11:30:57 +08001940 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001941 if (index >= net_no)
1942 { // required network no created
1943 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001944 return 0;
1945 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001946 else if( index < 0)
1947 {
1948 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001949 return -1;
1950 }
you.chen35020192022-05-06 11:30:57 +08001951 }
1952
1953 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001954 {
1955 RLOGE("[lynq_check_network_number] network index < 0");
1956 return -1;
1957 }
1958 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001959 return 0;
1960}
1961
1962static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001963 if (freq > 5000 && freq < 6000)
1964 {
you.chen35020192022-05-06 11:30:57 +08001965 return LYNQ_WIFI_5G_band;
1966 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001967 else if (freq > 2000 && freq < 3000)
1968 {
you.chen35020192022-05-06 11:30:57 +08001969 return LYNQ_WIFI_2G_band;
1970 }
1971 return LYNQ_WIFI_2_and_5G_band;
1972}
1973
1974static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 if (key_mgmt != NULL)
1976 {
1977 if (memcmp( key_mgmt, "NONE", 4) == 0)
1978 {
you.chen35020192022-05-06 11:30:57 +08001979 return LYNQ_WIFI_AUTH_OPEN;
1980 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001981 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1982 {
you.chen35020192022-05-06 11:30:57 +08001983 return LYNQ_WIFI_AUTH_WEP;
1984 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001985 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1986 {
you.chen35020192022-05-06 11:30:57 +08001987 return LYNQ_WIFI_AUTH_WPA_PSK;
1988 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001989 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1990 {
you.chen35020192022-05-06 11:30:57 +08001991 return LYNQ_WIFI_AUTH_WPA2_PSK;
1992 }
1993 }
1994
1995 return -1;
1996}
1997
1998static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001999 if (flag != NULL)
2000 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002001 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 +08002002 {
2003 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002004 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002005 {
2006 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2007 }else if (strstr( flag, "WPA2-PSK") != NULL)
2008 {
you.chen35020192022-05-06 11:30:57 +08002009 return LYNQ_WIFI_AUTH_WPA2_PSK;
2010 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002011 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002012 {
you.chen35020192022-05-06 11:30:57 +08002013 return LYNQ_WIFI_AUTH_WPA_PSK;
2014 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002016 {
you.chen35020192022-05-06 11:30:57 +08002017 return LYNQ_WIFI_AUTH_WEP;
2018 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002019 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002020 {
you.chen35020192022-05-06 11:30:57 +08002021 return LYNQ_WIFI_AUTH_OPEN;
2022 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002023 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002024 {
you.chend2fef3f2023-02-13 10:50:35 +08002025 return LYNQ_WIFI_AUTH_OPEN;
2026 }
qs.xiong46f41562023-07-11 21:06:47 +08002027 else
2028 {
2029 RLOGD("convert_max_auth_from_flag not-found auth mode");
2030 }
you.chen35020192022-05-06 11:30:57 +08002031 }
2032
2033 return -1;
2034}
2035
2036static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2037 switch (bw) {
2038 case 10:
2039 return LYNQ_WIFI_BANDWIDTH_HT10;
2040 break;
2041 case 20:
2042 return LYNQ_WIFI_BANDWIDTH_HT20;
2043 break;
2044 case 40:
2045 return LYNQ_WIFI_BANDWIDTH_HT40;
2046 break;
2047 case 80:
2048 return LYNQ_WIFI_BANDWIDTH_HT80;
2049 break;
2050 default:
2051 break;
2052 }
2053
2054 return -1;
2055}
2056
you.chen70f377f2023-04-14 18:17:09 +08002057static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002058static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2059 int i, count;
2060 char *p;
2061 const char *lynq_status_cmd = "STATUS";
2062 const char * FLAG_SSID = "ssid=";
2063 const char * FLAG_SBSID = "bssid=";
2064 const char * FLAG_KEY_MGMT = "key_mgmt=";
2065 const char * FLAG_FREQ = "freq=";
2066 const char * FLAG_STATE = "wpa_state=";
2067 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002068 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002069 char *split_lines[128] = {0};
2070
2071 CHECK_WPA_CTRL(interface);
2072
qs.xiong9fbf74e2023-03-28 13:38:22 +08002073 if (curr_state == NULL)
2074 {
2075 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002076 return -1;
2077 }
2078
2079 DO_REQUEST(lynq_status_cmd);
2080
2081 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2082
2083 curr_state->net_no = -1;
2084 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002085 for(i=0; i < count; i++)
2086 {
2087 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002088 {
you.chen35020192022-05-06 11:30:57 +08002089 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002090 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002091 {
you.chend2fef3f2023-02-13 10:50:35 +08002092 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002093 ret = 0;
2094 continue;
2095 }
you.chenf58b3c92022-06-21 16:53:48 +08002096 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002097 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002098 {
you.chend2fef3f2023-02-13 10:50:35 +08002099 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 +08002100 ret = 0;
2101 continue;
2102 }
you.chen35020192022-05-06 11:30:57 +08002103 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002104 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002105 {
you.chen450d0172022-07-15 17:56:48 +08002106 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002107 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002108 ret = 0;
2109 continue;
2110 }
2111 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002112 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002113 {
you.chen35020192022-05-06 11:30:57 +08002114 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2115 ret = 0;
2116 continue;
2117 }
you.chend2fef3f2023-02-13 10:50:35 +08002118 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002120 {
you.chend2fef3f2023-02-13 10:50:35 +08002121 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2122 ret = 0;
2123 continue;
2124 }
you.chen35020192022-05-06 11:30:57 +08002125 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002126 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002127 {
you.chen35020192022-05-06 11:30:57 +08002128 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002129 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002130 {
you.chen35020192022-05-06 11:30:57 +08002131 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2132 ret = 0;
2133 continue;
2134 }
2135
2136 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002137 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002138 {
you.chen35020192022-05-06 11:30:57 +08002139 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002140 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002142 }
2143 }
2144
you.chen70f377f2023-04-14 18:17:09 +08002145 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2146 {
2147 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2148 }
2149
you.chen35020192022-05-06 11:30:57 +08002150 return ret;
2151}
2152
qs.xiongf1b525b2022-03-31 00:58:23 -04002153int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002154{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002156 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002157
qs.xiong9fbf74e2023-03-28 13:38:22 +08002158 if (ap_ssid == NULL)
2159 {
2160 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002161 return -1;
2162 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002163 else
2164 {
2165 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002166 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002167
qs.xiong9fbf74e2023-03-28 13:38:22 +08002168 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2169 {
2170 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002171 return -1;
2172 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002173
you.chen35020192022-05-06 11:30:57 +08002174 CHECK_IDX(idx, CTRL_AP);
2175
2176 CHECK_WPA_CTRL(CTRL_AP);
2177
2178 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2179
2180 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2181 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002182 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2183 return 0;
you.chen35020192022-05-06 11:30:57 +08002184
qs.xiong7a105ce2022-03-02 09:43:11 -05002185}
2186
you.chen35020192022-05-06 11:30:57 +08002187int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002188{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002189 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002190 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002191 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002192}
2193
qs.xiongc9c79f72022-10-17 15:27:18 +08002194/*****
2195 *frequency <------>channel
2196 *
2197 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2198 *
2199 *
2200 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2201 *
2202 *
2203 * */
2204static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002205 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};
2206 int i;
2207 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2208
qs.xiong69a332b2022-12-02 09:58:57 +08002209 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002210 {
2211 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002212 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002213 }
qs.xiongc00b6032022-11-29 16:28:03 +08002214
2215 if(i == arr_len)
2216 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002217 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002218 return -1;
2219 }
qs.xiongc00b6032022-11-29 16:28:03 +08002220
qs.xiongc9c79f72022-10-17 15:27:18 +08002221 return 0;
2222}
qs.xiong13673462023-02-21 19:12:54 +08002223
2224static int lynq_check_frequencyby_country_code(int input_frequency)
2225{
2226 char str_cnc[]="CN";
2227 char str_dest[20]="";
2228
2229 if( lynq_get_country_code(1,str_dest) != 0 )
2230 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002231 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002232 return -1;
2233 }
2234 if( strncmp(str_dest,str_cnc,2) != 0 )
2235 {
2236 return 0;
2237 }else if( 2473 < input_frequency && input_frequency < 5744)
2238 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002239 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002240 return -1;
2241 }
2242 return 0;
2243}
qs.xiongf1b525b2022-03-31 00:58:23 -04002244int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002245{
qs.xiongc00b6032022-11-29 16:28:03 +08002246 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002247 char lynq_wifi_frequency_cmd[128]={0};
2248 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002249 char lynq_cmd_slect[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002250 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002251 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002252 check = lynq_check_set_frequency(lynq_wifi_frequency);
2253 if(check != 0)
2254 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002255 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002256 return -1;
you.chen35020192022-05-06 11:30:57 +08002257 }
qs.xiong13673462023-02-21 19:12:54 +08002258 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2259 if(check != 0)
2260 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002261 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002262 return -1;
2263 }
2264
qs.xiongc00b6032022-11-29 16:28:03 +08002265 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2266 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002267 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002268 return -1;
2269 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002270
you.chen35020192022-05-06 11:30:57 +08002271 CHECK_IDX(idx, CTRL_AP);
2272
2273 CHECK_WPA_CTRL(CTRL_AP);
2274
2275 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2276 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2277 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2278
you.chen6c2dd9c2022-05-16 17:55:28 +08002279 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002280 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2281 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2282 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong171c9ec2023-11-01 18:58:09 +08002283//@ tmp do down/up to fix 5G AP turn to 2.4G cannot be conenct
qs.xiongec8bbeb2023-11-20 15:51:45 +08002284
qs.xiong171c9ec2023-11-01 18:58:09 +08002285 system("wl down");
2286 system("wl up");
2287 RLOGD("[%s] -- run cmd down/up --%d",__func__,__LINE__);
qs.xiong7a105ce2022-03-02 09:43:11 -05002288
qs.xiong9fbf74e2023-03-28 13:38:22 +08002289 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002290}
2291
qs.xiongf1b525b2022-03-31 00:58:23 -04002292int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002293{
you.chen35020192022-05-06 11:30:57 +08002294 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002295 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002296 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002297
qs.xiong9fbf74e2023-03-28 13:38:22 +08002298 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2299 {
2300 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002301 return -1;
2302 }
2303 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002304
qs.xiong9fbf74e2023-03-28 13:38:22 +08002305 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002306}
2307
qs.xiongf1b525b2022-03-31 00:58:23 -04002308int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2309{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002310 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002311 CHECK_IDX(idx, CTRL_AP);
2312 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002313 case LYNQ_WIFI_BANDWIDTH_HT10:
2314 {
2315 RLOGE("bandwith [%d] not support now\n", bandwidth);
2316 return -1;
2317 }
2318 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002319 {
2320 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2321 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 if (system(lynq_cmd_bandwith) != 0 )
2323 {
2324 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002325 return -1;
2326 }
2327 system("wl up");
2328 break;
2329 }
2330 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002331 {
qs.xiong10379192023-02-21 13:19:42 +08002332 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002333 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2334 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 if (system(lynq_cmd_bandwith) != 0 )
2336 {
2337 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002338 return -1;
2339 }
2340 system("wl up");
2341 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002342 }
you.chen35020192022-05-06 11:30:57 +08002343 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002344 {
qs.xiong10379192023-02-21 13:19:42 +08002345 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002346 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002347 if (system(lynq_cmd_bandwith) != 0 )
2348 {
2349 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002350 return -1;
2351 }
2352 system("wl up");
2353 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002354 }
2355 default:
you.chen35020192022-05-06 11:30:57 +08002356 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 RLOGE("auth type [%d] not support now\n", bandwidth);
2358 return -1;
you.chen35020192022-05-06 11:30:57 +08002359 }
2360 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002361
2362
you.chen35020192022-05-06 11:30:57 +08002363 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002364}
you.chen35020192022-05-06 11:30:57 +08002365
qs.xiongf1b525b2022-03-31 00:58:23 -04002366int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2367{
you.chen35020192022-05-06 11:30:57 +08002368 int count = 0;
2369 int index = 0;
2370 char *split_words[128] = {0};
2371 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002373 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002374
you.chen35020192022-05-06 11:30:57 +08002375 CHECK_WPA_CTRL(CTRL_AP);
2376
2377 DO_REQUEST(lynq_chanspec_cmd);
2378
2379 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2380 for(;index < count; index++) {
2381 if (strncmp(split_words[index], "bw", 2) != 0) {
2382 continue;
2383 }
2384
2385 index++;
2386 if (index >= count) {
2387 return -1;
2388 }
2389
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002391 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2392 return 0;
2393 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002394 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002395 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002396}
qs.xiong0fb469a2022-04-14 03:50:45 -04002397
qs.xiongf1b525b2022-03-31 00:58:23 -04002398int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002399{
you.chen35020192022-05-06 11:30:57 +08002400 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002401 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002402 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002403
you.chen35020192022-05-06 11:30:57 +08002404 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002405
qs.xiong9fbf74e2023-03-28 13:38:22 +08002406 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2407 {
you.chen35020192022-05-06 11:30:57 +08002408 return -1;
2409 }
2410
2411 system("wl down");
2412 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002413 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002414 return -1;
2415 }
2416 system("wl up");
2417 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002418}
qs.xiong0fb469a2022-04-14 03:50:45 -04002419
qs.xiongf1b525b2022-03-31 00:58:23 -04002420int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002421{
you.chen35020192022-05-06 11:30:57 +08002422 int count = 0;
2423 int index = 0;
2424 char *split_words[128] = {0};
2425 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002426 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002427 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002428
you.chen35020192022-05-06 11:30:57 +08002429 CHECK_WPA_CTRL(CTRL_AP);
2430
2431 DO_REQUEST(lynq_chanspec_cmd);
2432
2433 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002434 for(;index < count; index++)
2435 {
2436 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002437 if (strncmp(split_words[index], "channel", 2) != 0) {
2438 continue;
2439 }
2440
2441 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002442 if (index >= count)
2443 {
you.chen35020192022-05-06 11:30:57 +08002444 return -1;
2445 }
2446
2447 *channel = atoi(split_words[index]);
2448 return 0;
2449 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002450 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002451 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002452}
2453
2454
you.chen35020192022-05-06 11:30:57 +08002455int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002456{
you.chen6c2dd9c2022-05-16 17:55:28 +08002457 char ssid[MAX_CMD] = {0};
2458 int freq = 0;
2459 char lynq_auth_cmd[64]={0};
2460 char lynq_auth_alg_cmd[64]={0};
2461 char lynq_psk_cmd[64]={0};
2462 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002463 char lynq_ieee80211_cmd[64]={0};
2464 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002465 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002466 CHECK_IDX(idx, CTRL_AP);
2467
you.chen6c2dd9c2022-05-16 17:55:28 +08002468 CHECK_WPA_CTRL(CTRL_AP);
2469
qs.xiong9fbf74e2023-03-28 13:38:22 +08002470 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2471 {
2472 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002473 return -1;
2474 }
2475
you.chen92fd5d32022-05-25 10:09:47 +08002476 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002477 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002478 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002479 return 0;
2480 }
2481 else {
2482 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2483 ssid[0] = '\0';
2484 }
2485 lynq_wifi_ap_frequency_get(idx, &freq);
2486
2487 DO_OK_FAIL_REQUEST(cmd_disconnect);
2488 DO_OK_FAIL_REQUEST(cmd_remove_all);
2489 if (ssid[0] != '\0') {
2490 lynq_wifi_ap_ssid_set(idx, ssid);
2491 }
2492 if (freq != 0) {
2493 lynq_wifi_ap_frequency_set(idx, freq);
2494 }
2495 }
2496 }
you.chen35020192022-05-06 11:30:57 +08002497
qs.xiong9fbf74e2023-03-28 13:38:22 +08002498 switch(auth){
2499 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002500 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002501 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002502 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002503 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002504 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002505 break;
2506 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002507 case LYNQ_WIFI_AUTH_WEP:
2508 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002509 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002510 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002511 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002512 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2513
2514 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2515 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2516 break;
2517 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002518 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002519 {
2520 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2521 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2522 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2523
2524 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2525 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2526 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2527 break;
2528
2529 }
you.chen35020192022-05-06 11:30:57 +08002530 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002531 {
2532 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2533 {
you.chen35020192022-05-06 11:30:57 +08002534 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2535 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2536 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002537 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2538 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002539 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002540 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002541 }
2542// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2543// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2544 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002545
you.chen35020192022-05-06 11:30:57 +08002546 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2547 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2548 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002549 break;
2550 }
2551 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2552 {
2553 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2554 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2555 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2556 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2557
2558 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2559 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2560 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2561 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2562 break;
2563 }
2564 case LYNQ_WIFI_AUTH_WPA3_PSK:
2565 {
2566 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2567 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002568 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002569 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2570
2571 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2572 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2573 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2574 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2575 break;
2576 }
2577 default:
you.chen35020192022-05-06 11:30:57 +08002578 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002579 RLOGE("auth type [%d] not support now\n", auth);
2580 return -1;
you.chen35020192022-05-06 11:30:57 +08002581 }
2582 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002583 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002584
qs.xiong9fbf74e2023-03-28 13:38:22 +08002585 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002586}
2587
you.chen35020192022-05-06 11:30:57 +08002588int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002589{
you.chen35020192022-05-06 11:30:57 +08002590 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002591 char lynq_auth_alg_str[MAX_RET] = {0};
2592 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002593 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002594 CHECK_IDX(idx, CTRL_AP);
2595
qs.xiong9fbf74e2023-03-28 13:38:22 +08002596 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2597 {
2598 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002599 return -1;
2600 }
2601
qs.xiong9fbf74e2023-03-28 13:38:22 +08002602 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2603 {
2604 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2605 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002606 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002607 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002608 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002609 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002610 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2611 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002612 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002613 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002614 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002615 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002616 else
2617 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002618 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002619 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002620 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002621 }
you.chen35020192022-05-06 11:30:57 +08002622 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002623 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2624 {
2625 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2626 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002627 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002628 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002629 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002630 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2631 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002632 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002633 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002634 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002635 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002636 else
2637 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002638 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002639 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002640 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002641 }
you.chen35020192022-05-06 11:30:57 +08002642 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002643
2644 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2645 {
2646 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2647 return -1;
2648 }
2649
2650 if (memcmp(lynq_auth_str,"1",1) == 0 )
2651 {
2652 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2653 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002654 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002655 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2656 {
2657 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2658 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002659 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002660 }
2661 else
2662 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002663 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002664 *auth = -1;
2665 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002666
you.chen6c2dd9c2022-05-16 17:55:28 +08002667 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002668}
qs.xiong1af5daf2022-03-14 09:12:12 -04002669
you.chenb95401e2023-05-12 19:39:06 +08002670static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2671{
2672 char status[64];
you.chencba13492023-05-19 13:53:43 +08002673 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002674 curr_status_info curr_state;
2675
2676 CHECK_WPA_CTRL(CTRL_AP);
2677
2678 memset(status, 0, sizeof (status));
2679
2680 curr_state.ap = NULL;
2681 curr_state.state = status;
2682
2683 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002684 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002685 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002686 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002687 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002688 {
2689 return 0;
2690 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002691 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002692 {
2693 DO_REQUEST("RECONNECT");
2694 return inner_check_ap_connected(idx, retry_count+1);
2695 }
you.chencba13492023-05-19 13:53:43 +08002696 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002697 {
2698 printf("retry 10 time\n");
2699 return -1;
2700 }
2701 else
2702 {
you.chen6d247052023-06-01 16:39:54 +08002703 if (strcmp(status, STATE_DISCONNECTED) == 0)
2704 {
2705 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2706 DO_REQUEST(LYNQ_WIFI_CMD);
2707 }
you.chenb95401e2023-05-12 19:39:06 +08002708 return inner_check_ap_connected(idx, retry_count+1);
2709 }
2710 }
2711 return -1;
2712}
qs.xiong1af5daf2022-03-14 09:12:12 -04002713
qs.xiongf1b525b2022-03-31 00:58:23 -04002714int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002715{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002716 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002717 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002718
qs.xiongec8bbeb2023-11-20 15:51:45 +08002719 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002720 CHECK_WPA_CTRL(CTRL_AP);
2721
you.chen0df3e7e2023-05-10 15:56:26 +08002722 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002723 {
you.chen0df3e7e2023-05-10 15:56:26 +08002724 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002725 return -1;
2726 }
you.chen35020192022-05-06 11:30:57 +08002727
qs.xiongb37f8c42023-09-13 21:21:58 +08002728
you.chen35020192022-05-06 11:30:57 +08002729 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2730 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2731
you.chenc9928582023-04-24 15:39:37 +08002732 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2733 if (ret != 0)
2734 {
2735 RLOGE("lynq_wifi_ap_start excute script fail");
2736 return -1;
2737 }
2738
you.chenb95401e2023-05-12 19:39:06 +08002739 if (inner_check_ap_connected(idx, 0) != 0)
2740 {
2741 return -1;
2742 }
2743
you.chen0df3e7e2023-05-10 15:56:26 +08002744 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002745 if (g_ap_tmp_watcher_pid == 0)
2746 {
2747 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2748 {
2749 g_ap_tmp_watcher_pid = 0;
2750 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2751 return -1;
2752 }
2753 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2754 }
qs.xiongb37f8c42023-09-13 21:21:58 +08002755
qs.xiongec8bbeb2023-11-20 15:51:45 +08002756 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002757 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002758}
2759
qs.xiongf1b525b2022-03-31 00:58:23 -04002760int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002761{
you.chen35020192022-05-06 11:30:57 +08002762 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002763}
2764
qs.xiongf1b525b2022-03-31 00:58:23 -04002765int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002766{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002767 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002768 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002769
you.chen35020192022-05-06 11:30:57 +08002770 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002771
you.chen35020192022-05-06 11:30:57 +08002772 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002773
you.chen35020192022-05-06 11:30:57 +08002774 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2775
2776 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2777
you.chenc9928582023-04-24 15:39:37 +08002778
2779 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2780 if (ret != 0)
2781 {
2782 RLOGE("lynq_wifi_ap_start excute script fail");
2783 return -1;
2784 }
qs.xiong44fac672023-08-29 16:15:55 +08002785 g_ap_tmp_watcher_stop_flag = 1;
2786 if (g_ap_tmp_watcher_pid != 0)
2787 pthread_join(g_ap_tmp_watcher_pid, NULL);
2788 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +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}
qs.xiong1af5daf2022-03-14 09:12:12 -04002793
qs.xiongf1b525b2022-03-31 00:58:23 -04002794int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002795{
you.chen35020192022-05-06 11:30:57 +08002796 char lynq_disable_cmd[128] = {0};
2797 char lynq_select_cmd[128] = {0};
2798 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002799 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002800 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002801
you.chen35020192022-05-06 11:30:57 +08002802 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002803 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2804 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2805
2806 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2807 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2808 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002809
qs.xiong9fbf74e2023-03-28 13:38:22 +08002810 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002811}
2812
qs.xiongf1b525b2022-03-31 00:58:23 -04002813int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002814{
you.chen35020192022-05-06 11:30:57 +08002815 char lynq_disable_cmd[128] = {0};
2816 char lynq_select_cmd[128] = {0};
2817 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002818 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002819 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002820
you.chen35020192022-05-06 11:30:57 +08002821 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002822
you.chen35020192022-05-06 11:30:57 +08002823 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2824 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2825
2826 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2827 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2828 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002829
qs.xiong9fbf74e2023-03-28 13:38:22 +08002830 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002831}
qs.xiongf1b525b2022-03-31 00:58:23 -04002832
you.chen35020192022-05-06 11:30:57 +08002833int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002834{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002835 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002836 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002837 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002838 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002839 RLOGD("enter lynq_ap_password_set");
2840 if( password == NULL )
2841 {
2842 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002843 return -1;
2844 }
2845 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002846 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002847 if(pass_len < 8 || pass_len >= 64)
2848 {
2849 RLOGE("[lynq_ap_password_set]input password len not in rage");
2850 return -1;
you.chen35020192022-05-06 11:30:57 +08002851 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002852
you.chen35020192022-05-06 11:30:57 +08002853 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002854
qs.xiong9fbf74e2023-03-28 13:38:22 +08002855 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2856 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002857 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002858 return -1;
2859 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002860 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2861 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002862 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2863 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002864 }
2865
you.chen35020192022-05-06 11:30:57 +08002866 CHECK_WPA_CTRL(CTRL_AP);
2867
qs.xiong9fbf74e2023-03-28 13:38:22 +08002868 if (auth == LYNQ_WIFI_AUTH_WEP)
2869 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002870 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002871 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2872 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2873 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2874 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2875 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002876 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2877 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002878 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 +08002879 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2880 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2881 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002882 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2883 {
2884
qs.xiongc8d92a62023-03-29 17:36:14 +08002885 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 +08002886 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002887 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2889 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2890
2891 }
2892 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002893 {
2894 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002895 return -1;
2896 }
you.chen35020192022-05-06 11:30:57 +08002897
you.chen35020192022-05-06 11:30:57 +08002898 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002899
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002901}
2902
you.chen35020192022-05-06 11:30:57 +08002903int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002904{
you.chen35020192022-05-06 11:30:57 +08002905 FILE * fp;
2906 int len, ret;
2907 int count, index;
2908 char *split_lines[128] = {0};
2909 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002910 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002911
you.chen35020192022-05-06 11:30:57 +08002912 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002913
you.chen35020192022-05-06 11:30:57 +08002914 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2915// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002916 if (NULL == fp)
2917 {
2918 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002919 return -1;
2920 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002921
you.chen35020192022-05-06 11:30:57 +08002922 buff = alloca(MAX_RET);
2923 fseek(fp, 0, SEEK_SET);
2924 len = fread(buff, 1, MAX_RET, fp);
2925 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002926
qs.xiong9fbf74e2023-03-28 13:38:22 +08002927 for(index=0; index < len; index ++)
2928 {
2929 if (memcmp(buff + index, "network={", 9) != 0)
2930 {
you.chen35020192022-05-06 11:30:57 +08002931 continue;
2932 }
2933 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002934 for (; index < len; index ++ )
2935 {
2936 if (buff[index] != '}')
2937 {
you.chen35020192022-05-06 11:30:57 +08002938 continue;
2939 }
2940 buff[index] = '\0';
2941 break;
2942 }
2943 len = buff + index - p;
2944 }
2945
2946 count = lynq_split(p, len, '\n', split_lines);
2947
2948 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002949 for(index=0; index < count; index++)
2950 {
you.chen35020192022-05-06 11:30:57 +08002951 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002952 if (p != NULL)
2953 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002954 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002955 if (*p == '\"')
2956 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002957 p++;
2958 }
you.chen35020192022-05-06 11:30:57 +08002959 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002960 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2961 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002962 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002963 if (*p == '\"')
2964 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002965 p++;
2966 }
2967 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002968 else
2969 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002970 continue;
you.chen35020192022-05-06 11:30:57 +08002971 }
2972
2973 strcpy(password, p);
2974
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 while(*password != '\0')
2976 {
2977 if (*password == '\"')
2978 {
you.chen35020192022-05-06 11:30:57 +08002979 *password = '\0';
2980 break;
2981 }
2982 password++;
2983 }
2984 ret = 0;
2985 break;
2986 } //end for(index=0; index < count; index++)
2987
2988 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002989}
2990
you.chen35020192022-05-06 11:30:57 +08002991static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2992 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002993 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002994
qs.xiong9fbf74e2023-03-28 13:38:22 +08002995 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2996 {
you.chen35020192022-05-06 11:30:57 +08002997 return -1;
2998 }
2999
3000 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003001
qs.xiong9fbf74e2023-03-28 13:38:22 +08003002 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3003 {
3004 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003005 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003006 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003007 {
you.chena6cd55a2022-05-08 12:20:18 +08003008 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003009 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003010 }
you.chen70f377f2023-04-14 18:17:09 +08003011 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3012 {
3013 return 0;
3014 }
you.chena6cd55a2022-05-08 12:20:18 +08003015 }
3016 }
you.chen70f377f2023-04-14 18:17:09 +08003017 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3018 {
3019 return 0;
3020 }
3021
qs.xiong9fbf74e2023-03-28 13:38:22 +08003022 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3023 {
you.chen70f377f2023-04-14 18:17:09 +08003024 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003025 return -1;
3026 }
3027 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3028 {
3029
you.chen70f377f2023-04-14 18:17:09 +08003030 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3031 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003032 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3033 {
3034
3035 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003036 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003037 }else
3038 {
you.chen70f377f2023-04-14 18:17:09 +08003039 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003040 *auth = -1;
3041 return -1;
3042 }
you.chen35020192022-05-06 11:30:57 +08003043 return 0;
3044}
3045
3046int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003047{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003048 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003049 int pass_len, net_no, count, index;
3050 char lynq_tmp_cmd[300]={0};
3051 int net_no_list[128];
3052 lynq_wifi_auth_s net_auth;
3053 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003054 if(pass_len < 8 || pass_len >= 64)
3055 {
3056 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003057 return -1;
3058 }
3059
3060 CHECK_IDX(idx, CTRL_STA);
3061
3062 net_no = -1;
3063 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3064
qs.xiong9fbf74e2023-03-28 13:38:22 +08003065 for (index=0; index < count; index++)
3066 {
you.chen35020192022-05-06 11:30:57 +08003067 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003068 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3069 {
you.chen35020192022-05-06 11:30:57 +08003070 net_no = net_no_list[index];
3071 break;
3072 }
3073 }
3074
qs.xiong9fbf74e2023-03-28 13:38:22 +08003075 if (net_no < 0)
3076 {
you.chen35020192022-05-06 11:30:57 +08003077 return -1;
3078 }
3079
3080 CHECK_WPA_CTRL(CTRL_STA);
3081
3082 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3083
3084 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3085 DO_OK_FAIL_REQUEST(cmd_save_config);
3086
3087 return 0;
3088}
3089
qs.xiongb5dab082023-10-13 14:43:41 +08003090/**
3091* buff data
3092* buff_len size of buff
3093* idx sta
3094* *ap ap info for find ssid && password
3095* password return password
3096*
3097*/
3098static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3099
3100 int ret, network_len, i, ssid_len,curr_auth;
3101 int count, index,org_index;
3102 char *split_lines[128] = {0};
3103 char *p, *ssid, *ssid_end_flag,*ptr;
3104 char tmp_ssid[128]={0};
3105 char tmp_auth[24]={0};
3106
3107 org_index = 0;
3108 network_len = 0;
3109 p = NULL;
3110
3111 CHECK_IDX(idx, CTRL_STA);
3112
3113 while(1){
3114 network_len = 0;
3115 p == NULL;
3116 for(; org_index < buff_len; org_index ++)
3117 {
3118 for(; org_index < buff_len; org_index ++)
3119 {
3120 if (memcmp(buff + org_index, "network={", 9) != 0)
3121 {
3122 continue;
3123 }
3124 p = buff + org_index + 9;
3125
3126 for (; org_index < buff_len; org_index ++ )
3127 {
3128 if (buff[org_index] != '}')
3129 {
3130 continue;
3131 }
3132 buff[org_index] = '\0';
3133 break;
3134 }
3135 network_len = buff + org_index - p;
3136 break;
3137 }
3138
3139 if (p == NULL)
3140 {
3141 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3142 return -1;
3143 }
3144
3145 ssid = strstr(p, "ssid=");
3146 if (ssid != NULL) {
3147 ssid += strlen("ssid=");
3148 if (ssid[0] == '\"')
3149 {
3150 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3151 {
qs.xiongb5dab082023-10-13 14:43:41 +08003152 break;
3153 }
qs.xiongb5dab082023-10-13 14:43:41 +08003154 }
3155 else
3156 {
3157 ssid_end_flag = strstr(ssid, "\n");
3158 if (ssid_end_flag != NULL)
3159 {
3160 ssid_len = (ssid_end_flag - ssid) / 2;
3161 for(i=0; i<ssid_len; i++)
3162 {
3163 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3164 }
3165 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3166 {
qs.xiongb5dab082023-10-13 14:43:41 +08003167 break;
3168 }
3169 }
3170 }
3171 }
3172
3173 }
3174
3175 if (org_index >= buff_len || NULL == p || network_len <= 0)
3176 {
3177
3178 if (buff != NULL)
3179 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3180 return -1;
3181 }
3182
3183 count = lynq_split(p, network_len, '\n', split_lines);
3184 ret = -1;
3185 for( index=0; index < count; index++ )
3186 {
3187 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003188 if(p != NULL)
3189 {
3190 p += 9;
3191 if(memcmp(p,"SAE",3) == 0)
3192 {
3193 curr_auth = 5;
3194 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3195 {
3196 curr_auth = 4;
3197 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3198 {
3199 curr_auth = 3;
3200 }else if(memcmp(p,"NONE",4) == 0 )
3201 {
3202 curr_auth = 0;
3203 }else{
3204 curr_auth = 1;
3205 }
qs.xiongb5dab082023-10-13 14:43:41 +08003206 if( curr_auth < 1 || curr_auth > 6)
3207 {
3208 ret = -1;
3209 }
3210 break;
3211 }
3212 }
3213 if( curr_auth == 0)
3214 {
3215 return 0;
3216 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3217 {
3218 for(index=0; index < count; index++)
3219 {
3220 /*get psw info*/
3221
3222 p = strstr(split_lines[index], "psk=");
3223 if (p != NULL)
3224 {
3225 p += 4;
3226 if (*p == '\"')
3227 {
3228 p++;
3229 }
3230 }
3231 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3232 {
3233 p += 9;
3234 if (*p == '\"')
3235 {
3236 p++;
3237 }
3238 }
3239 else
3240 {
3241 continue;
3242 }
3243
3244 if (*p == '\"')
3245 p++;
3246 strncpy(password, p, 64);
3247 p = password;
3248 while(password - p < 64 && *password != '\0')
3249 {
3250 if (*password == '\"')
3251 {
3252 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003253 ret = 0;
3254 break;
3255 }
3256 password++;
3257 }
3258 break;
3259 }
3260 break;
3261 }
3262 }
3263
3264 return ret;
3265}
3266
3267
3268
you.chen35020192022-05-06 11:30:57 +08003269int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3270
3271 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003272 int len, ret;
3273 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003274 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003275
qs.xiongb5dab082023-10-13 14:43:41 +08003276 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003277 CHECK_IDX(idx, CTRL_STA);
3278
qs.xiong9fbf74e2023-03-28 13:38:22 +08003279 if (NULL == password)
3280 {
3281 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003282 return -1;
3283 }
3284
you.chen35020192022-05-06 11:30:57 +08003285 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003286 if (NULL == fp)
3287 {
3288 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003289 return -1;
3290 }
3291
you.chen6d247052023-06-01 16:39:54 +08003292 fseek(fp, 0, SEEK_END);
3293 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003294 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003295
qs.xiongb5dab082023-10-13 14:43:41 +08003296 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003297 {
3298 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3299 return -1;
3300 }
3301
you.chen35020192022-05-06 11:30:57 +08003302 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003303 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003304 fclose(fp);
3305
qs.xiongb5dab082023-10-13 14:43:41 +08003306
3307 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3308
3309 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003310 {
qs.xiongb5dab082023-10-13 14:43:41 +08003311 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3312 free(info_buff);
3313 return 0;
you.chen35020192022-05-06 11:30:57 +08003314 }
qs.xiongb5dab082023-10-13 14:43:41 +08003315 else{
3316 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003317 return -1;
3318 }
3319
you.chen35020192022-05-06 11:30:57 +08003320}
3321
qs.xiongb5dab082023-10-13 14:43:41 +08003322
you.chen35020192022-05-06 11:30:57 +08003323static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3324{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003325 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003326
qs.xiong9fbf74e2023-03-28 13:38:22 +08003327 if (sta_ssid == NULL)
3328 {
3329 RLOGE("sta_ssid is null\n");
3330 return -1;
you.chen35020192022-05-06 11:30:57 +08003331 }
3332
qs.xiong9fbf74e2023-03-28 13:38:22 +08003333 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003334
3335 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3336
3337 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3338// DO_OK_FAIL_REQUEST(cmd_save_config);
3339
qs.xiong9fbf74e2023-03-28 13:38:22 +08003340 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003341
3342}
3343
you.chen35020192022-05-06 11:30:57 +08003344static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003345{
you.chen35020192022-05-06 11:30:57 +08003346 char lynq_disable_cmd[128]={0};
3347 char lynq_select_cmd[128]={0};
3348
3349 CHECK_WPA_CTRL(CTRL_STA);
3350
qs.xiong9fbf74e2023-03-28 13:38:22 +08003351 if (save != 0)
3352 {
you.chenc29444e2022-06-07 18:01:16 +08003353 if (start_flag != 0)
3354 {
3355 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3356 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3357 }
3358 else
3359 {
3360 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3361 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3362 }
you.chen35020192022-05-06 11:30:57 +08003363 DO_OK_FAIL_REQUEST(cmd_save_config);
3364 }
3365
qs.xiong9fbf74e2023-03-28 13:38:22 +08003366 if (start_flag == 0)
3367 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003368 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003369 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3370 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003371 else
3372 {
you.chen35020192022-05-06 11:30:57 +08003373 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3374 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3375 }
3376
3377 return 0;
3378}
3379
3380int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3381{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003382 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003383 CHECK_IDX(idx, CTRL_STA);
3384
you.chen6c2dd9c2022-05-16 17:55:28 +08003385 curr_status_info curr_state;
3386 ap_info_s ap_info;
3387 curr_state.ap = &ap_info;
3388 curr_state.state = NULL;
3389
qs.xiong9fbf74e2023-03-28 13:38:22 +08003390 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3391 {
you.chend2fef3f2023-02-13 10:50:35 +08003392 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003393 return 0;
3394 }
3395
3396 return -1;
you.chen35020192022-05-06 11:30:57 +08003397}
3398
3399int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3400{
qs.xiong5d716d22023-09-20 20:08:39 +08003401 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003402 scan_info_s *scan_list = NULL;
3403 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003404 int scan_len=0;
3405 int save_len=0;
3406 int best_index = -1;
3407 int best_scan_index = -1;
3408 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003409 int i, j, ret;
3410
3411 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003412
3413 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003414 if (info == NULL)
3415 {
you.chen35020192022-05-06 11:30:57 +08003416 return -1;
3417 }
3418
3419 curr_status_info curr_state;
3420 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003421 char status[64];
you.chen35020192022-05-06 11:30:57 +08003422
you.chen9ac66392022-08-06 17:01:16 +08003423 memset(&ap_info, 0, sizeof (ap_info));
3424 memset(status, 0, sizeof (status));
3425
3426 curr_state.ap = &ap_info;
3427 curr_state.state = status;
3428
qs.xiong9fbf74e2023-03-28 13:38:22 +08003429 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3430 {
you.chen35020192022-05-06 11:30:57 +08003431 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003432 if (strcmp(status, STATE_COMPLETED) == 0)
3433 {
3434 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003435 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003436 }
3437 else
3438 {
3439 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003440 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003441 }
you.chen593621d2023-04-27 17:52:44 +08003442 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003443 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003444 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003445 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003446 return 0;
3447 }
3448
you.chen9ac66392022-08-06 17:01:16 +08003449 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003450 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003451 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3452 {
you.chen9ac66392022-08-06 17:01:16 +08003453 if (NULL != scan_list)
3454 {
3455 free(scan_list);
3456 }
you.chen35020192022-05-06 11:30:57 +08003457 return -1;
3458 }
3459
qs.xiong9fbf74e2023-03-28 13:38:22 +08003460 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3461 {
you.chen9ac66392022-08-06 17:01:16 +08003462 if (NULL != scan_list)
3463 {
3464 free(scan_list);
3465 }
3466 if (NULL != save_list)
3467 {
3468 free(save_list);
3469 }
you.chen35020192022-05-06 11:30:57 +08003470 return -1;
3471 }
3472
qs.xiong9fbf74e2023-03-28 13:38:22 +08003473 for (i=0; i < save_len; i++)
3474 {
3475 for (j=0; j < scan_len; j++)
3476 {
you.chen35020192022-05-06 11:30:57 +08003477 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003478 && save_list[i].base_info.auth == scan_list[j].auth)
3479 {
3480 if (best_rssi == 0)
3481 {
you.chen9ac66392022-08-06 17:01:16 +08003482 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003483 best_rssi = scan_list[j].rssi;
3484 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003485 else if (best_rssi > scan_list[j].rssi)
3486 {
you.chen35020192022-05-06 11:30:57 +08003487 best_index = i;
3488 best_scan_index = j;
3489 best_rssi = scan_list[j].rssi;
3490 }
you.chend2fef3f2023-02-13 10:50:35 +08003491 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 +08003492 break;
3493 }
3494 }
3495 }
3496
qs.xiong9fbf74e2023-03-28 13:38:22 +08003497 if (best_index >= 0)
3498 {
you.chen35020192022-05-06 11:30:57 +08003499 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003500 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 +08003501 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003502 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003503 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003504 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003505 }
3506
you.chen9ac66392022-08-06 17:01:16 +08003507 if (NULL != scan_list)
3508 {
3509 free(scan_list);
3510 }
3511 if (NULL != save_list)
3512 {
3513 free(save_list);
3514 }
3515
qs.xiong5d716d22023-09-20 20:08:39 +08003516 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003517 return ret;
you.chen35020192022-05-06 11:30:57 +08003518}
3519
3520static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3521{
qs.xiongc8d92a62023-03-29 17:36:14 +08003522 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003523 char lynq_ket_mgmt_cmd[64]={0};
3524 char lynq_pairwise_cmd[64]={0};
3525 char lynq_psk_cmd[64]={0};
3526
3527 CHECK_WPA_CTRL(CTRL_STA);
3528
qs.xiong9fbf74e2023-03-28 13:38:22 +08003529 switch(auth)
3530 {
3531 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003532 {
3533 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003534
you.chen35020192022-05-06 11:30:57 +08003535 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003536// DO_OK_FAIL_REQUEST(cmd_save_config);
3537 break;
3538 }
3539 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003540 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003541 {
3542 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3543 {
you.chen35020192022-05-06 11:30:57 +08003544 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3545 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003546 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3547 {
you.chena6cd55a2022-05-08 12:20:18 +08003548 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003549 }
3550 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3551 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003552
you.chen35020192022-05-06 11:30:57 +08003553 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3554 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3555 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003556
qs.xiong9fbf74e2023-03-28 13:38:22 +08003557 if (password != NULL)
3558 {
you.chen35020192022-05-06 11:30:57 +08003559 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3560 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003561 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003562 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003563
you.chen35020192022-05-06 11:30:57 +08003564// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003565 break;
3566 }
3567 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3568 {
qs.xiong3e506812023-04-06 11:08:48 +08003569 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003570 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003571 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3572 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3573
qs.xiong3e506812023-04-06 11:08:48 +08003574 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003575 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3576 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3577 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3578
3579 break;
3580 }
3581 case LYNQ_WIFI_AUTH_WPA3_PSK:
3582 {
qs.xiong3e506812023-04-06 11:08:48 +08003583 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003584 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003585 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3586 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3587
qs.xiongb37f8c42023-09-13 21:21:58 +08003588 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003589 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3590 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3591 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3592
3593 break;
3594 }
3595 default:
3596 return -1;
you.chen35020192022-05-06 11:30:57 +08003597 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003598
qs.xiong9fbf74e2023-03-28 13:38:22 +08003599 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003600}
qs.xiong7a105ce2022-03-02 09:43:11 -05003601
you.chen35020192022-05-06 11:30:57 +08003602static int inner_get_curr_net_no(int interface) {
3603 curr_status_info curr_state;
3604 curr_state.ap = NULL;
3605 curr_state.state = NULL;
3606
qs.xiong9fbf74e2023-03-28 13:38:22 +08003607 if (0 != inner_get_status_info(interface, &curr_state))
3608 {
you.chen35020192022-05-06 11:30:57 +08003609 return -1;
3610 }
3611
3612 return curr_state.net_no;
3613}
3614
3615int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003616{
you.chen35020192022-05-06 11:30:57 +08003617 int net_no;
3618 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003619
you.chen35020192022-05-06 11:30:57 +08003620 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003621
qs.xiong9fbf74e2023-03-28 13:38:22 +08003622 if (net_no < 0)
3623 {
you.chen35020192022-05-06 11:30:57 +08003624 return -1;
3625 }
3626
3627 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003628}
3629
you.chenb95401e2023-05-12 19:39:06 +08003630int 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 -05003631{
you.chen35020192022-05-06 11:30:57 +08003632 int count, net_no, index;
3633 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003634 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003635 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003636 curr_status_info curr_state;
3637 ap_info_s ap_info;
3638 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003639
qs.xiong9fbf74e2023-03-28 13:38:22 +08003640 if (ssid == NULL || *ssid == '\0')
3641 {
3642 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003643 return -1;
3644 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003645
qs.xiong9fbf74e2023-03-28 13:38:22 +08003646 if (LYNQ_WIFI_AUTH_OPEN != auth)
3647 {
3648 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003649 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003650 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003651 return -1;
3652 }
3653 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003654
you.chen70f377f2023-04-14 18:17:09 +08003655
3656 pthread_mutex_lock(&s_global_check_mutex);
3657 if (s_sta_status != INNER_STA_STATUS_INIT)
3658 {
3659 s_sta_status = INNER_STA_STATUS_CANCEL;
3660 pthread_cond_signal(&s_global_check_cond);
3661 }
3662 pthread_mutex_unlock(&s_global_check_mutex);
3663
you.chen35020192022-05-06 11:30:57 +08003664 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003665 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003666
3667 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003668 memset(&ap_info, 0, sizeof (ap_info));
3669 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003670
you.chen70f377f2023-04-14 18:17:09 +08003671 curr_state.ap = &ap_info;
3672 curr_state.state = status;
3673
3674 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003675 {
you.chen70f377f2023-04-14 18:17:09 +08003676 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3677 {
3678 net_no = curr_state.net_no;
3679 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3680 && strcmp(ap_info.psw, psw) == 0)
3681 {
3682 RLOGD("already connected\n");
3683
3684 pthread_mutex_lock(&s_global_check_mutex);
3685 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003686 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003687 pthread_cond_signal(&s_global_check_cond);
3688 pthread_mutex_unlock(&s_global_check_mutex);
3689 return 0;
3690 }
you.chen35020192022-05-06 11:30:57 +08003691 }
3692 }
3693
you.chen70f377f2023-04-14 18:17:09 +08003694 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003695 {
you.chen70f377f2023-04-14 18:17:09 +08003696 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3697
3698 for (index=0; index < count; index++)
3699 {
3700 net_auth = -1;
3701 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3702 {
3703 net_no = net_no_list[index];
3704 break;
3705 }
you.chen35020192022-05-06 11:30:57 +08003706 }
3707
you.chen70f377f2023-04-14 18:17:09 +08003708 if (net_no < 0)
3709 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003710 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3711 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3712 {
3713 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3714 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3715 DO_OK_FAIL_REQUEST(rm_net_cmd);
3716 }
you.chen70f377f2023-04-14 18:17:09 +08003717 net_no = lynq_add_network(CTRL_STA);
3718 if (net_no == -1)
3719 {
3720 return -1;
3721 }
3722
3723 RLOGD("net no is %d\n", net_no);
3724 if (0 != inner_set_sta_ssid(net_no, ssid))
3725 {
3726 return -1;
3727 }
you.chen35020192022-05-06 11:30:57 +08003728 }
3729 }
3730
qs.xiong9fbf74e2023-03-28 13:38:22 +08003731 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3732 {
you.chen35020192022-05-06 11:30:57 +08003733 return -1;
3734 }
3735
you.chen70f377f2023-04-14 18:17:09 +08003736
3737 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003738 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003739 usleep(200*1000);
3740
qs.xiong09560402023-10-27 21:58:55 +08003741 pthread_mutex_lock(&s_global_check_mutex);
3742 lynq_sta_removeElement(net_no);
3743 pthread_mutex_unlock(&s_global_check_mutex);
3744
you.chen70f377f2023-04-14 18:17:09 +08003745 ret = inner_sta_start_stop(net_no, 1, 1);
3746
3747 pthread_mutex_lock(&s_global_check_mutex);
3748 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003749 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003750 strcpy(s_sta_current_connecting_ssid, ssid);
3751 struct timeval now;
3752 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003753 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003754 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3755 pthread_cond_signal(&s_global_check_cond);
3756 pthread_mutex_unlock(&s_global_check_mutex);
3757 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003758}
3759
you.chenb95401e2023-05-12 19:39:06 +08003760int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3761{
3762 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3763}
3764
you.chen35020192022-05-06 11:30:57 +08003765int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003766{
you.chen35020192022-05-06 11:30:57 +08003767 ap_info_s ap;
3768 curr_status_info curr_state;
3769 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003770
qs.xiong9fbf74e2023-03-28 13:38:22 +08003771 if (ssid == NULL || *ssid == '\0')
3772 {
3773 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003774 return -1;
3775 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003776
you.chen35020192022-05-06 11:30:57 +08003777 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003778
you.chen35020192022-05-06 11:30:57 +08003779 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003780 curr_state.state = NULL;
3781
qs.xiong9fbf74e2023-03-28 13:38:22 +08003782 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3783 {
you.chen35020192022-05-06 11:30:57 +08003784 return 0;
3785 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003786
qs.xiong9fbf74e2023-03-28 13:38:22 +08003787 if (strcmp(ap.ap_ssid, ssid) != 0)
3788 {
you.chen35020192022-05-06 11:30:57 +08003789 return 0;
3790 }
3791
you.chen70f377f2023-04-14 18:17:09 +08003792 pthread_mutex_lock(&s_global_check_mutex);
3793 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3794 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003795 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003796}
qs.xiong97fa59b2022-04-07 05:41:29 -04003797
qs.xiongc93bf2b2023-08-25 10:22:08 +08003798int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3799{
qs.xiong09560402023-10-27 21:58:55 +08003800 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003801 ap_info_s ap;
3802 curr_status_info curr_state;
3803 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003804 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003805
3806 if (ssid == NULL || *ssid == '\0')
3807 {
3808 RLOGE("input ssid is NULL\n");
3809 return -1;
3810 }
3811
3812 CHECK_IDX(idx, CTRL_STA);
3813
3814
3815 curr_state.ap = &ap;
3816 curr_state.state = NULL;
3817
3818 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3819 {
3820 return 0;
3821 }
3822
3823 if (strcmp(ap.ap_ssid, ssid) != 0)
3824 {
3825 return 0;
3826 }
3827
3828 pthread_mutex_lock(&s_global_check_mutex);
3829 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003830 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3831 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3832 {
3833 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);
3834 if( g_history_disconnect_net[i] == curr_state.net_no)
3835 {
3836 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3837 check_history_idx_flag = 1;
3838 break;
3839 }
3840 }
3841 if ( check_history_idx_flag == 0)
3842 {
3843 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__);
3844 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3845 g_history_disconnect_valid_num++;
3846 }
3847 RLOGD("%s %d",__func__,__LINE__);
3848 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003849 pthread_mutex_unlock(&s_global_check_mutex);
3850 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3851
3852}
3853
3854
you.chena6cd55a2022-05-08 12:20:18 +08003855int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3856{
qs.xiongb37f8c42023-09-13 21:21:58 +08003857
qs.xiongad2f89d2023-01-18 13:17:41 +08003858 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3859 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003860
you.chen35020192022-05-06 11:30:57 +08003861 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003862 CHECK_WPA_CTRL(CTRL_STA);
3863
you.chenc9928582023-04-24 15:39:37 +08003864 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3865 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003866 {
you.chenc9928582023-04-24 15:39:37 +08003867 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003868 return -1;
3869 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003870
qs.xiongad2f89d2023-01-18 13:17:41 +08003871 system(lynq_enable_sta_cmd);
3872 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003873 pthread_mutex_lock(&s_global_check_mutex);
3874 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003875 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003876 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003877 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003878}
3879
you.chen6d247052023-06-01 16:39:54 +08003880static int inner_get_status_info_state (int interface, char *state) {
3881 curr_status_info curr_state;
3882 curr_state.ap = NULL;
3883 curr_state.state = state;
3884 return inner_get_status_info(interface, &curr_state);
3885}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003886
3887int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3888{
3889
qs.xiongb37f8c42023-09-13 21:21:58 +08003890 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3891 int tmp_open_idx[128];
3892 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003893
qs.xiongb37f8c42023-09-13 21:21:58 +08003894 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003895 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003896 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3897 pthread_mutex_unlock(&s_global_check_mutex);
3898 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003899 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003900 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003901 }
3902
qs.xiongb37f8c42023-09-13 21:21:58 +08003903 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003904 return 0;
3905}
3906
3907
you.chen35020192022-05-06 11:30:57 +08003908int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003909{
qs.xiongec8bbeb2023-11-20 15:51:45 +08003910 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003911 int i=0;
3912 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003913
you.chena6cd55a2022-05-08 12:20:18 +08003914 CHECK_IDX(idx, CTRL_STA);
3915 CHECK_WPA_CTRL(CTRL_STA);
3916
you.chen6d247052023-06-01 16:39:54 +08003917 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003918 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003919
3920 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3921 if (ret != 0)
3922 {
3923 RLOGE("lynq_wifi_ap_start excute script fail");
3924 return -1;
3925 }
3926
you.chen6d247052023-06-01 16:39:54 +08003927 for (i=0; i < 30; i++) // to check if sta is realy stoped
3928 {
3929 if (inner_get_status_info_state(idx, state) != 0)
3930 {
3931 break;
3932 }
3933
3934 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3935 {
3936 break;
3937 }
3938 RLOGD("lynq_wifi_ap_start curr state %s", state);
3939 usleep(SLEEP_TIME_ON_IDLE);
3940 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003941 pthread_mutex_lock(&s_global_check_mutex);
3942 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3943 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08003944 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003945 return 0;
3946// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003947}
qs.xiongfcc914b2023-07-06 21:16:20 +08003948int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3949{
3950 char LYNQ_DISABLE_CMD[128]={0};
3951 CHECK_IDX(idx, CTRL_STA);
3952 CHECK_WPA_CTRL(CTRL_STA);
3953 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3954 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3955 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3956 return 0;
3957}
qs.xiong7a105ce2022-03-02 09:43:11 -05003958
you.chen35020192022-05-06 11:30:57 +08003959//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3960// int i, count;
3961// char *p;
3962// const char * FLAG_SSID = "ssid=";
3963// const char * FLAG_SBSID = "bssid=";
3964// const char * FLAG_KEY_MGMT = "key_mgmt=";
3965// const char * FLAG_FREQ = "freq=";
3966// char lynq_sta_cmd[MAX_CMD];
3967// char *split_lines[128] = {0};
3968
3969// CHECK_WPA_CTRL(CTRL_AP);
3970
3971// sprintf(lynq_sta_cmd, "STA %s", bssid);
3972
3973// DO_REQUEST(lynq_sta_cmd);
3974
3975// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3976
3977// for(i=0; i < count; i++) {
3978// p = strstr(split_lines[i], FLAG_SSID);
3979// if (p != NULL) {
3980// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3981// continue;
3982// }
3983// }
3984
3985// lynq_get_interface_ip(idx, ap->ap_ip);
3986// lynq_ap_password_set(idx, ap->psw);
3987
3988// return 0;
3989//}
3990
3991static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3992 curr_status_info curr_state;
3993 curr_state.ap = ap;
3994 curr_state.state = NULL;
3995 return inner_get_status_info(interface, &curr_state);
3996}
3997
3998int 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 -04003999{
qs.xiong5071c802023-09-06 14:04:15 +08004000 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004001 int index, line_count;
4002 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004003 const char *lynq_first_sta_cmd = "STA-FIRST";
4004 char lynq_next_sta_cmd[MAX_CMD];
4005 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004006 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004007
you.chen35020192022-05-06 11:30:57 +08004008 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004009
you.chen35020192022-05-06 11:30:57 +08004010 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004011
you.chenb95401e2023-05-12 19:39:06 +08004012 // ap_info_s * tmp_ap;
4013 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004014 if (ap == NULL || list == NULL || len == NULL)
4015 {
4016 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004017 return -1;
4018 }
4019
you.chenb95401e2023-05-12 19:39:06 +08004020 // ap = &tmp_ap;
4021 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004022 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004023 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004024
you.chenb95401e2023-05-12 19:39:06 +08004025 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004026 {
you.chenb95401e2023-05-12 19:39:06 +08004027 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004028 return -1;
4029 }
4030
4031 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4032 lynq_ap_password_get(idx, (*ap)->psw);
4033
you.chen35020192022-05-06 11:30:57 +08004034 DO_REQUEST(lynq_first_sta_cmd);
4035
4036 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004037 while (reply_len > 0)
4038 {
4039 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004040 {
you.chen35020192022-05-06 11:30:57 +08004041 break;
4042 }
4043 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4044 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4045 strcpy(bssid[index], split_lines[0]);
4046 index++;
4047 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4048 reply_len = MAX_RET;
4049 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004050 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 +08004051 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004052 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004053 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004054 break;
4055 }
4056 }
4057
4058 *len = index;
4059
4060 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 for (index=0; index < *len; index++)
4062 {
you.chend2fef3f2023-02-13 10:50:35 +08004063 dev_info = &(*list)[index];
4064 memset(dev_info, 0, sizeof(device_info_s));
4065 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4066 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4067 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4068 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004069 free(bssid[index]);
4070 }
qs.xiong5071c802023-09-06 14:04:15 +08004071 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004072 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004073}
4074
you.chen35020192022-05-06 11:30:57 +08004075int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004076{
you.chen35020192022-05-06 11:30:57 +08004077 int i, count, index, count_words;
4078 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4079 char *split_lines[128] = {0};
4080 char *split_words[128] = {0};
4081 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004082
qs.xiong9fbf74e2023-03-28 13:38:22 +08004083 if (list == NULL || len == NULL)
4084 {
you.chen35020192022-05-06 11:30:57 +08004085 return -1;
4086 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004087
you.chen9ac66392022-08-06 17:01:16 +08004088 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4089 {
4090 usleep(100 * 1000);
4091 }
4092
you.chen35020192022-05-06 11:30:57 +08004093 CHECK_IDX(idx, CTRL_STA);
4094
4095 CHECK_WPA_CTRL(CTRL_STA);
4096
4097 DO_REQUEST(lynq_scan_result_cmd);
4098
4099 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4100 *len = count - 1;
4101 *list = malloc(sizeof (scan_info_s) * *len);
4102
4103 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004104 for (index=0; index <count_words; index++)
4105 {
4106 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004107 }
4108
qs.xiong9fbf74e2023-03-28 13:38:22 +08004109 for(index = 1;index < count; index++)
4110 {
4111 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004112 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004113 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4114 if (count_words < 4)
4115 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004116 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004117 //bssid / frequency / signal level / flags / ssid
4118 p = (*list) + index - 1;
4119 strcpy(p->mac, split_words[0]);
4120 p->band = convert_band_from_freq(atoi(split_words[1]));
4121 p->rssi = -1 * atoi( split_words[2]);
4122 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004123 if (count_words == 4) // ssid hided
4124 {
4125 p->ssid[0] = '\0';
4126 }
4127 else
4128 {
4129 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4130 }
you.chen35020192022-05-06 11:30:57 +08004131 }
4132
4133 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004134}
qs.xiong97fa59b2022-04-07 05:41:29 -04004135
you.chen35020192022-05-06 11:30:57 +08004136int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4137{
4138 int count, net_no, index;
4139 int net_no_list[128];
4140 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004141
you.chen35020192022-05-06 11:30:57 +08004142 char lynq_remove_cmd[MAX_CMD];
4143
qs.xiong9fbf74e2023-03-28 13:38:22 +08004144 if (ssid == NULL || *ssid == '\0')
4145 {
4146 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004147 return -1;
4148 }
4149
4150 CHECK_IDX(idx, CTRL_STA);
4151
4152 CHECK_WPA_CTRL(CTRL_STA);
4153
4154 net_no = -1;
4155 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4156
qs.xiong9fbf74e2023-03-28 13:38:22 +08004157 for (index=0; index < count; index++)
4158 {
you.chen35020192022-05-06 11:30:57 +08004159 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004160 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4161 {
you.chen35020192022-05-06 11:30:57 +08004162 net_no = net_no_list[index];
4163 break;
4164 }
4165 }
4166
qs.xiong9fbf74e2023-03-28 13:38:22 +08004167 if (net_no < 0)
4168 {
you.chen35020192022-05-06 11:30:57 +08004169 return 0;
4170 }
4171
4172 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4173
4174 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004175
4176 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4177
4178 pthread_mutex_lock(&s_global_check_mutex);
4179 lynq_sta_removeElement(net_no);
4180 pthread_mutex_unlock(&s_global_check_mutex);
4181
4182 RLOGD("%s %d",__func__,__LINE__);
4183 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004184 DO_OK_FAIL_REQUEST(cmd_save_config);
4185
4186 return 0;
4187}
4188
4189int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004190{
you.chend2fef3f2023-02-13 10:50:35 +08004191 int count, index;
you.chen35020192022-05-06 11:30:57 +08004192 int net_no_list[128];
4193 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004194 RLOGD("enter lynq_get_sta_saved_ap api\n");
4195 if (list == NULL || len == NULL)
4196 {
4197 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004198 return -1;
4199 }
4200
4201 CHECK_IDX(idx, CTRL_STA);
4202
4203// CHECK_WPA_CTRL(CTRL_STA);
4204
4205 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004206 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004207
you.chen057aac42023-04-13 14:06:58 +08004208 if (count < 0)
4209 {
4210 RLOGE("list network fail");
4211 return count;
4212 }
4213 else if (count == 0)
4214 {
4215 *list = NULL;
4216 *len = 0;
4217 return 0;
4218 }
4219
you.chen35020192022-05-06 11:30:57 +08004220 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004221 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004222 *len = count;
4223
qs.xiong9fbf74e2023-03-28 13:38:22 +08004224 for (index=0; index < count; index++)
4225 {
you.chen35020192022-05-06 11:30:57 +08004226 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004227 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004228 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004229 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004230 {
you.chen35020192022-05-06 11:30:57 +08004231 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4232 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004233 else
you.chen057aac42023-04-13 14:06:58 +08004234 {
you.chen35020192022-05-06 11:30:57 +08004235 (*list)[index].base_info.band = -1;
4236 }
you.chen057aac42023-04-13 14:06:58 +08004237 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004238 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004239 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004240 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004241 return 0;
4242}
4243
4244int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4245{
you.chen0c9bee22023-10-25 13:03:14 +08004246 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004247 {
you.chen0c9bee22023-10-25 13:03:14 +08004248 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4249 g_sta_fake_scan_finish_flag = 1;
4250 return 0;
4251 }
4252 else if (g_sta_conncet_status_flag != 0)
4253 {
4254 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004255 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004256 }
you.chen0c9bee22023-10-25 13:03:14 +08004257
qs.xiongc8d92a62023-03-29 17:36:14 +08004258 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004259 const char *lynq_scan_cmd = "SCAN";
4260
4261 CHECK_IDX(idx, CTRL_STA);
4262
4263 CHECK_WPA_CTRL(CTRL_STA);
4264
you.chen0df3e7e2023-05-10 15:56:26 +08004265 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4266 {
4267 RLOGD("tmp clear scanlist");
4268 system(clean_last_re);
4269 }
you.chen9ac66392022-08-06 17:01:16 +08004270 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004271 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004272 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4273 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004274 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004275 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4276 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004277 g_sta_scan_finish_flag = 1;
4278 return -1;
4279 }
you.chen35020192022-05-06 11:30:57 +08004280
4281 return 0;
4282}
4283
4284int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004285 if (cb == NULL)
4286 {
4287 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004288 return -1;
4289 }
4290
you.chen6d247052023-06-01 16:39:54 +08004291 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004292 g_ap_callback_priv = priv;
4293 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004294 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004295
you.chen6d247052023-06-01 16:39:54 +08004296 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004297 if (g_ap_watcher_pid == 0 )
4298 {
4299 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4300 {
4301 g_ap_watcher_pid = 0;
4302 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4303 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4304 return -1;
4305 }
4306 }
4307
4308 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4309 RLOGD("creat APWatcherTheradProc susccs");
4310
you.chen35020192022-05-06 11:30:57 +08004311 return 0;
4312}
4313
4314int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004315 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004316 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004317 if (g_ap_callback_priv == priv)
4318 {
you.chen35020192022-05-06 11:30:57 +08004319 g_ap_callback_func = NULL;
4320 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004321 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004322 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004323 return 0;
4324 }
you.chen6d247052023-06-01 16:39:54 +08004325 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004326 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004327 return -1;
4328}
4329
4330int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004331 if (cb == NULL)
4332 {
4333 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004334 return -1;
4335 }
4336
you.chen6d247052023-06-01 16:39:54 +08004337 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004338 g_sta_callback_priv = priv;
4339 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004340 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004341
you.chen6d247052023-06-01 16:39:54 +08004342 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004343 if (g_sta_watcher_pid == 0 ) {
4344 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4345 {
4346 g_sta_watcher_pid = 0;
4347 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4348 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4349 return -1;
4350 }
4351 }
4352
4353 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4354 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004355 return 0;
4356}
4357
4358int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004359 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004360 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004361 if (g_sta_callback_priv == priv)
4362 {
you.chen35020192022-05-06 11:30:57 +08004363 g_sta_callback_func = NULL;
4364 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004365 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004366 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004367 return 0;
4368 }
you.chen6d247052023-06-01 16:39:54 +08004369 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004370 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004371 return -1;
4372}
4373
qs.xiongfcc914b2023-07-06 21:16:20 +08004374int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4375 if (cb == NULL)
4376 {
4377 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4378 return -1;
4379 }
4380 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4381 g_sta_auto_callback_priv = priv;
4382 g_sta_auto_callback_func = cb;
4383 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4384 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4385 if (g_sta_auto_watcher_pid == 0 ) {
4386 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4387 {
4388 g_sta_auto_watcher_pid = 0;
4389 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4390 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4391 return -1;
4392 }
4393 }
4394 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4395 RLOGD("creat STAWatcherTheradProc susccs");
4396 return 0;
4397}
4398int lynq_unreg_sta_auto_event_callback(void * priv) {
4399 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4400 if (g_sta_auto_callback_priv == priv)
4401 {
4402 g_sta_auto_watcher_stop_flag = 1;
4403 if (g_sta_auto_watcher_pid != 0)
4404 {
4405 pthread_join(g_sta_auto_watcher_pid, NULL);
4406 }
4407 g_sta_auto_watcher_pid = 0;
4408 g_sta_auto_callback_func = NULL;
4409 g_sta_auto_callback_priv = NULL;
4410 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4411 return 0;
4412 }
4413 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4414 return -1;
4415}
you.chen35020192022-05-06 11:30:57 +08004416int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4417{
4418 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004419 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004420 CHECK_IDX(idx, CTRL_AP);
4421
qs.xiong9fbf74e2023-03-28 13:38:22 +08004422 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4423 {
you.chen35020192022-05-06 11:30:57 +08004424 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4425 return 0;
4426 }
4427
qs.xiong9fbf74e2023-03-28 13:38:22 +08004428 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4429 {
you.chen35020192022-05-06 11:30:57 +08004430 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4431 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004432 else
4433 {
you.chen35020192022-05-06 11:30:57 +08004434 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4435 }
4436
4437 return 0;
4438}
4439
4440int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4441 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004442 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004443 CHECK_IDX(idx, CTRL_STA);
4444
qs.xiong9fbf74e2023-03-28 13:38:22 +08004445 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4446 {
you.chen35020192022-05-06 11:30:57 +08004447 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4448 return 0;
4449 }
4450
qs.xiong9fbf74e2023-03-28 13:38:22 +08004451 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4452 {
you.chen35020192022-05-06 11:30:57 +08004453 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4454 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004455 else
4456 {
you.chen35020192022-05-06 11:30:57 +08004457 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4458 }
4459
4460 return 0;
4461}
4462
4463int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4464// CHECK_IDX(idx, CTRL_AP);
4465// int ret = 0;
4466// size_t reply_len = MAX_RET;
4467// char cmd_reply[MAX_RET]={0};
4468// const char * cmd_str = "GET country";
4469// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4470// do{
4471// if (NULL == s_lynq_wpa_ctrl) {
4472// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4473// if (NULL == s_lynq_wpa_ctrl ) {
4474// printf("wpa_ctrl_open fail\n");
4475// return -1;
4476// }
4477// }
4478// }while(0);
4479
4480// do {
4481// reply_len = MAX_RET;
4482// cmd_reply[0] = '\0';
4483// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004484// 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 +08004485// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004486// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004487// return ret;
4488// }
4489// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004490// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004491// }while(0);
4492
4493 FILE *fp;
4494 size_t i = 0;
4495 char lynq_cmd_ret[MAX_RET]={0};
4496
4497// CHECK_IDX(idx, CTRL_AP);
4498
4499 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004500 {
4501 perror("popen error!");
4502 return -1;
4503 }
you.chen35020192022-05-06 11:30:57 +08004504 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4505 {
4506 perror("fread fail!");
4507 return -1;
4508 }
4509
qs.xiong9fbf74e2023-03-28 13:38:22 +08004510 for(i=0; i < strlen(lynq_cmd_ret); i++)
4511 {
4512 if (lynq_cmd_ret[i] == ' ')
4513 {
you.chen35020192022-05-06 11:30:57 +08004514 lynq_cmd_ret[i] = '\0';
4515 break;
4516 }
4517 }
4518
4519 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004520 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004521
4522 int ret=pclose(fp);
4523 if(ret==-1)
4524 {
4525 perror("close file faild");
4526 }
4527
4528 return 0;
4529}
4530
qs.xiong44fac672023-08-29 16:15:55 +08004531
you.chen705a7ef2023-06-01 22:06:45 +08004532static int check_and_init_uci_config(char * country_code)
4533{
4534 FILE * fp;
4535 int is_different = 0;
4536 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4537 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4538 const char * commit_uci_cmd ="uci commit";
4539 char set_country_cmd[MAX_CMD];
4540 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004541
you.chen705a7ef2023-06-01 22:06:45 +08004542 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004543
you.chen705a7ef2023-06-01 22:06:45 +08004544 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004545 {
you.chen705a7ef2023-06-01 22:06:45 +08004546 if (0 != system(create_uci_cmd))
4547 {
4548 RLOGE("creat_uci_cmd fail");
4549 return -1;
4550 }
4551 is_different = 1;
4552 }
4553
4554 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4555 {
4556 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004557 return -1;
4558 }
4559
you.chen705a7ef2023-06-01 22:06:45 +08004560 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004561 {
you.chen705a7ef2023-06-01 22:06:45 +08004562 RLOGE("fread fail!");
4563 fclose(fp);
4564 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004565 }
4566
you.chen705a7ef2023-06-01 22:06:45 +08004567 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4568 {
qs.xiong44fac672023-08-29 16:15:55 +08004569 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 +08004570 is_different = 1;
4571 }
4572
4573 fclose(fp);
4574
4575 if (is_different)
4576 {
4577 if ( 0 != system(set_country_cmd))
4578 {
4579 RLOGE("set_country_cmd fail");
4580 return -1;
4581 }
4582 if (0 != system(commit_uci_cmd))
4583 {
4584 RLOGE("commmit fail");
4585 }
4586 }
4587
4588 return is_different;
4589}
4590
4591int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4592 char check_current_code[10];
4593 const char * support_country[] = {"CN", "EU"};
4594
4595 int ret,is_different, i, cc_count;
4596
4597 if (country_code == NULL || country_code[0] == '\0')
4598 {
4599 RLOGE("bad country code\n");
4600 return -1;
4601 }
4602
4603 cc_count = sizeof (support_country) / sizeof (char*);
4604 for(i=0; i < cc_count; i++)
4605 {
4606 if (strcmp(support_country[i], country_code) == 0)
4607 {
4608 break;
4609 }
4610 }
4611
4612 if (i >= cc_count)
4613 {
4614 RLOGE("unspported country code %s\n", country_code);
4615 return -1;
4616 }
4617
4618 is_different = check_and_init_uci_config(country_code);
4619 if( is_different < 0 )
4620 {
4621 RLOGE("init set uci fail\n");
4622 return -1;
4623 }
4624
4625 ret = lynq_get_country_code(idx,check_current_code);
4626 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4627 {
4628 ret = lynq_wifi_disable();
4629 if(ret != 0 )
4630 {
4631 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4632 return -1;
4633 }
4634 }
4635
4636 return 0;
you.chen35020192022-05-06 11:30:57 +08004637}
4638
4639int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4640{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004641 RLOGD("enter lynq_get_connect_ap_mac\n");
4642 if (mac == NULL)
4643 {
4644 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004645 return -1;
4646 }
4647
4648 CHECK_IDX(idx, CTRL_STA);
4649 ap_info_s ap;
4650 ap.ap_mac[0] = '\0';
4651
qs.xiong9fbf74e2023-03-28 13:38:22 +08004652 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4653 {
you.chen35020192022-05-06 11:30:57 +08004654 return -1;
4655 }
4656 strcpy(mac, ap.ap_mac);
4657
4658 return 0;
4659}
4660
4661int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4662{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004663 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004664 struct ifaddrs *ifaddr_header, *ifaddr;
4665 struct in_addr * ifa;
4666 const char * ifaName = "wlan0";
4667 if (ip == NULL)
4668 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004669 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004670 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004671 }
you.chenf58b3c92022-06-21 16:53:48 +08004672
qs.xiong9fbf74e2023-03-28 13:38:22 +08004673 if (idx == 1)
4674 {
you.chen0df3e7e2023-05-10 15:56:26 +08004675 ifaName = inner_get_ap_interface_name();
4676 if (ifaName == NULL)
4677 {
4678 RLOGE("[lynq_get_interface_ip] ap name get fail");
4679 return -1;
4680 }
you.chen9ac66392022-08-06 17:01:16 +08004681 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004682 else if (idx != 0)
4683 {
you.chen35020192022-05-06 11:30:57 +08004684 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004685 }
you.chen35020192022-05-06 11:30:57 +08004686
you.chen9ac66392022-08-06 17:01:16 +08004687 if (getifaddrs(&ifaddr_header) == -1)
4688 {
you.chen35020192022-05-06 11:30:57 +08004689 perror("getifaddrs");
4690 return -1;
4691 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004692 }
you.chen35020192022-05-06 11:30:57 +08004693
4694
you.chen9ac66392022-08-06 17:01:16 +08004695 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4696 {
4697 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004698 continue;
you.chen9ac66392022-08-06 17:01:16 +08004699 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4700 {
4701 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4702 {
4703 // is a valid IP4 Address
4704 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4705 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004706 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004707 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004708 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004709 return 0;
4710 }
4711 }
4712 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004713 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004714 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004715 return -1;
you.chen35020192022-05-06 11:30:57 +08004716}
4717
4718int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4719{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004720 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004721 int count;
4722 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004723 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004724 char *split_words[128] = {0};
4725 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4726
4727 CHECK_WPA_CTRL(idx);
4728
4729 DO_REQUEST(lynq_get_mac_cmd);
4730
qs.xiong9fbf74e2023-03-28 13:38:22 +08004731 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4732 {
4733 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004734 return -1;
4735 }
4736
4737 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4738
qs.xiong9fbf74e2023-03-28 13:38:22 +08004739 if (count < 2)
4740 {
you.chen35020192022-05-06 11:30:57 +08004741 return -1;
4742 }
4743
qs.xiong9fbf74e2023-03-28 13:38:22 +08004744 for (i=0; i < strlen(split_words[1]); i++ )
4745 {
4746 if (split_words[1][i] != ' ')
4747 {
you.chen35020192022-05-06 11:30:57 +08004748 break;
4749 }
4750 }
4751
qs.xiongdd6e44c2023-08-08 15:02:53 +08004752 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004753
4754 return 0;
4755}
4756
4757int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4758{
4759// int count;
4760// char *split_words[128] = {0};
4761// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4762
4763// if (rssi == NULL) {
4764// return -1;
4765// }
4766
4767// CHECK_IDX(idx, CTRL_STA);
4768
4769// CHECK_WPA_CTRL(CTRL_STA);
4770
4771// DO_REQUEST(lynq_get_rssi_cmd);
4772
4773// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4774// return -1;
4775// }
4776
4777// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4778
4779// if (count < 2) {
4780// return -1;
4781// }
4782
4783// *rssi = atoi(split_words[1]) * -1;
4784
you.chen35020192022-05-06 11:30:57 +08004785 char lynq_cmd_ret[MAX_RET]={0};
4786
qs.xiongff0ae0f2022-10-11 15:47:14 +08004787/*******change other cmd to get rssi*******
4788 *
4789 *wl rssi ---> wl -i wlan0 rssi
4790 *
4791 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004792 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004793 {
you.chen23c4a5f2023-04-12 16:46:00 +08004794 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004795 return -1;
4796 }
you.chen9f17e4d2022-06-06 17:18:18 +08004797 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004798/****** if got rssi is 0,means sta didn't connected any device****/
4799 if(*rssi == 0)
4800 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004801 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004802 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004803 }
you.chen35020192022-05-06 11:30:57 +08004804
4805 return 0;
4806}
4807
4808int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4809{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004810 RLOGD("enter lynq_get_connect_ap_band\n");
4811 if (band == NULL)
4812 {
you.chen35020192022-05-06 11:30:57 +08004813 return -1;
4814 }
4815
4816 CHECK_IDX(idx, CTRL_STA);
4817 ap_info_s ap;
4818 ap.band = -1;
4819
qs.xiong9fbf74e2023-03-28 13:38:22 +08004820 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4821 {
you.chen35020192022-05-06 11:30:57 +08004822 return -1;
4823 }
4824 *band = ap.band;
4825
4826 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004827}
you.chenf58b3c92022-06-21 16:53:48 +08004828
4829int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4830{
you.chenb95401e2023-05-12 19:39:06 +08004831 int ret;
4832 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004833 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004834
4835 if (ip == NULL)
4836 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004837 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004838 return -1;
4839 }
4840
4841 CHECK_IDX(idx, CTRL_STA);
4842
qs.xionge4cbf1c2023-02-28 18:22:49 +08004843 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004844 {
4845 return -1;
4846 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004847
you.chenb95401e2023-05-12 19:39:06 +08004848 ip[0] = '\0';
4849 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4850 if (ret != 0)
4851 {
4852 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4853 return -1;
4854 }
4855
4856 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4857 {
4858 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004859 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004860 if (ret != 0)
4861 {
4862 ip[0] = '\0';
4863 return 0;
4864 }
4865 else
4866 {
4867 p = strchr(ip, '\n');
4868 if (p != NULL)
4869 {
4870 *p = '\0';
4871 }
4872 }
4873 }
4874 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004875}
4876
qs.xionge02a5252023-09-20 14:00:21 +08004877int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4878{
4879 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4880 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4881}
4882
qs.xiong026c5c72022-10-17 11:15:45 +08004883int lynq_ap_connect_num(int sta_number)
4884{
4885 char lynq_limit_cmd[32]={0};
4886 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004887 if((sta_number < 1 ) && (sta_number > 15))
4888 {
4889 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004890 return -1;
4891 }
4892 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4893 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004894 if(ret != 0)
4895 {
4896 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004897 }
4898 return 0;
4899}
you.chenf58b3c92022-06-21 16:53:48 +08004900
qs.xiong77905552022-10-17 11:19:57 +08004901int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4902{
4903
4904 char lynq_wifi_acs_cmd[128]={0};
4905 char lynq_cmd_mode[128]={0};
4906 char lynq_cmd_slect[128]={0};
4907
qs.xiong9fbf74e2023-03-28 13:38:22 +08004908 if((acs_mode != 2) && (acs_mode != 5))
4909 {
qs.xiong77905552022-10-17 11:19:57 +08004910 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4911 }
4912
qs.xiong9fbf74e2023-03-28 13:38:22 +08004913 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4914 {
qs.xiong77905552022-10-17 11:19:57 +08004915 return -1;
4916 }
4917
4918 CHECK_IDX(idx, CTRL_AP);
4919
4920 CHECK_WPA_CTRL(CTRL_AP);
4921
4922 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4923 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4924 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4925
4926 DO_OK_FAIL_REQUEST(cmd_disconnect);
4927 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4928 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4929 DO_OK_FAIL_REQUEST(cmd_save_config);
4930 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4931
4932 return 0;
4933}
you.chen0f5c6432022-11-07 18:31:14 +08004934//you.chen add for tv-box start
4935static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4936 FILE *fp;
4937 //printf("to exec cmd:%s\n", str_cmd);
4938 if((fp=popen(str_cmd,"r"))==NULL)
4939 {
4940 perror("popen error!");
4941 return -1;
4942 }
4943 if((fread(str_cmd_ret,max_len,1,fp))<0)
4944 {
4945 perror("fread fail!");
4946 fclose(fp);
4947 return -1;
4948 }
4949 fclose(fp);
4950 return 0;
4951}
4952
4953static int get_netmask_length(const char* mask)
4954{
4955 int masklen=0, i=0;
4956 int netmask=0;
4957
4958 if(mask == NULL)
4959 {
4960 return 0;
4961 }
4962
4963 struct in_addr ip_addr;
4964 if( inet_aton(mask, &ip_addr) )
4965 {
4966 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004967 }else
4968 {
you.chen0f5c6432022-11-07 18:31:14 +08004969 netmask = 0;
4970 return 0;
4971 }
4972
4973 while(0 == (netmask & 0x01) && i<32)
4974 {
4975 i++;
4976 netmask = netmask>>1;
4977 }
4978 masklen = 32-i;
4979 return masklen;
4980}
4981
4982static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4983 int mask_len;
4984 char *p;
4985 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004986 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4987 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004988 return -1;
4989 p = strstr(str_cmd_ret, "Mask:");
4990 if (p == NULL)
4991 return -1;
4992 mask_len = get_netmask_length(p + 5);
4993 if (mask_len == 0)
4994 return -1;
4995 p = strstr(str_cmd_ret, "inet addr:");
4996 if (p == NULL)
4997 return -1;
4998 strcpy(tmp, p + 10);
4999 p = strstr(tmp, " ");
5000 if (p != NULL)
5001 *p = '\0';
5002 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5003 return 0;
5004}
5005
5006static void GBWWatchThreadProc() {
5007 int i,n, nloop, nmax, ncheckcount, nidlecount;
5008 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5009 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5010 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5011 char *results[16] = {0};
5012 char str_cmd[256] = {0};
5013 char str_cmd_ret[128] = {0};
5014 char dest_ip[32] = {0};
5015 lastAP1Bytes = lastAP2Bytes = 0;
5016 lastAP1Drop = lastAP2Drop = 0;
5017 lastAP1Speed = lastAP2Speed = 0;
5018 setAP1Speed = 50;
5019 setAP2Speed = 80;
5020 nloop = 0;
5021 nmax = 6;
5022 ncheckcount = nidlecount = 0;
5023
you.chen0df3e7e2023-05-10 15:56:26 +08005024 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005025 {
5026 RLOGE("------gbw thread run\n");
5027 return;
5028 }
5029
qs.xiong9fbf74e2023-03-28 13:38:22 +08005030 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005031 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5032 while (dest_ip[0] == '\0') {
5033 sleep(1);
5034 str_cmd_ret[0] = '\0';
5035 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5036 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5037 if (str_cmd_ret[n] == '\n'){
5038 str_cmd_ret[n] = '\0';
5039 break;
5040 }
5041 }
5042 if (str_cmd_ret[0] != '\0')
5043 {
5044 strcpy(dest_ip, str_cmd_ret);
5045 }
5046 }
5047
you.chenc9928582023-04-24 15:39:37 +08005048 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5049 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5050 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 +08005051 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5052 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005053 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005054 return;
5055 }
you.chenc9928582023-04-24 15:39:37 +08005056 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);
5057 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);
5058 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 +08005059
5060 while (1) {
5061 sleep(1);
5062 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005063 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5064 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5065 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005066 continue;
5067 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005068 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005069 if (n > 9) {
5070 if (strcmp(results[1], "Sent") == 0) {
5071 currAP1Bytes = atoll(results[2]);
5072 }
5073 if (strcmp(results[6], "(dropped") == 0) {
5074 currAP1Drop = atoi(results[7]);
5075 }
5076 }
5077
5078 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005079 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5080 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5081 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005082 continue;
5083 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005084 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005085 if (n > 9) {
5086 if (strcmp(results[1], "Sent") == 0) {
5087 currAP2Bytes = atoll(results[2]);
5088 }
5089 if (strcmp(results[6], "(dropped") == 0) {
5090 currAP2Drop = atoi(results[7]);
5091 }
5092 }
5093
5094 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5095 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5096 lastAP1Bytes = currAP1Bytes;
5097 lastAP2Bytes = currAP2Bytes;
5098 continue;
5099 }
5100
5101 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5102 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5103 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5104 lastAP1Speed = currAP1Speed;
5105 lastAP2Speed = currAP2Speed;
5106 lastAP1Bytes = currAP1Bytes;
5107 lastAP2Bytes = currAP2Bytes;
5108
5109 currSetAP1Speed = setAP1Speed;
5110 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5111 ncheckcount++;
5112 if (ncheckcount > 3) {
5113 ncheckcount = 0;
5114 currSetAP1Speed = 5;
5115 }
5116 }
5117 else {
5118 ncheckcount = 0;
5119 if (currAP1Speed < 5)
5120 nidlecount++;
5121 else
5122 nidlecount = 0;
5123
5124 }
5125
5126 if (nidlecount > 60 ){
5127 currSetAP1Speed = 50;
5128 }
5129
5130 if (currSetAP1Speed != setAP1Speed) {
5131 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005132 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5133 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005134 }
5135 }
5136}
5137
5138int enableGBW(const char* mac) {
5139 int i,len;
5140 char get_ipaddr_cmd[128]={0};
5141 ap_info_s *ap;
5142 device_info_s * list;
5143
5144 if (mac == NULL || g_gbw_enabled == 1)
5145 return -1;
5146 len = strlen(mac);
5147 g_gbw_mac = malloc(len + 1);
5148 for(i=0;i<len;i++) {
5149 if (mac[i] >= 'A' && mac[i] <= 'Z')
5150 {
5151 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5152 }
5153 else
5154 g_gbw_mac[i] = mac[i];
5155 }
5156 g_gbw_mac[i] = '\0';
5157 g_gbw_enabled = 1;
5158
5159 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5160 if (system(get_ipaddr_cmd) == 0) {
5161 //startGBW();
5162 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5163 for (i=0;i<len;i++) {
5164 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5165 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5166 startGBW();
5167 }
5168 free(ap);
5169 free(list);
5170 }
5171 }
5172 return 0;
5173}
5174
5175int disableGBW() {
5176 stopGBW();
5177 free(g_gbw_mac);
5178 g_gbw_mac = NULL;
5179 g_gbw_enabled = 1;
5180 return 0;
5181}
5182
5183static int startGBW() {
5184 if (g_gbw_watcher_pid != 0) {
5185 stopGBW();
5186 }
5187 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5188}
5189
5190static int stopGBW() {
5191 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005192 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005193 pthread_cancel(g_gbw_watcher_pid);
5194 pthread_join(g_gbw_watcher_pid, &retval);
5195 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005196 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5197 if (s_ap_iterface_name[0] != '\0')
5198 {
5199 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5200 system(cmd);
5201 }
you.chen0f5c6432022-11-07 18:31:14 +08005202}
5203//you.chen add for tv-box end