blob: 3c42b83463fe6b703730deb39ccd207e1cbcdfb6 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen0c9bee22023-10-25 13:03:14 +080057volatile int g_sta_fake_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
qs.xiong6ad0e822023-11-24 17:53:30 +080070
you.chen35020192022-05-06 11:30:57 +080071//const char * CTRL_PATH="/var/run/wpa_supplicant";
72const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
73//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
74const char * cmd_list_networks = "LIST_NETWORKS";
75const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080076const char * cmd_disconnect = "DISCONNECT";
77const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080078const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080079const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080080const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080081const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080082
you.chenf711c8a2023-04-13 13:49:45 +080083const char * cmd_ping = "PING";
84const char * rsp_pong = "PONG";
85const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
86const int MAX_IDLE_COUNT = 600; // 60s
87
you.chenc9928582023-04-24 15:39:37 +080088const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
89const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
90const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
91const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
92const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
93
94static char s_ap_iterface_name[64] = {0};
95
you.chend2fef3f2023-02-13 10:50:35 +080096struct local_wpa_ctrl{
97 struct wpa_ctrl *ctrl;
98 pthread_mutex_t mutex;
99};
100
qs.xiong09560402023-10-27 21:58:55 +0800101volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800102int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800103
you.chend2fef3f2023-02-13 10:50:35 +0800104static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800105static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
106static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800107// add for auto connect
108static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800109
110static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800111
you.chen0f5c6432022-11-07 18:31:14 +0800112//you.chen add for tv-box start
113volatile int g_gbw_enabled = 0;
114char * g_gbw_mac = NULL;
115pthread_t g_gbw_watcher_pid = 0;
116static int startGBW();
117static int stopGBW();
118//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800119
120typedef struct __curr_status_info {
121 ap_info_s *ap;
122 char * state;
123 int net_no;
124}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400125
you.chen70f377f2023-04-14 18:17:09 +0800126typedef enum {
127 INNER_STA_STATUS_INIT = 0,
128 INNER_STA_STATUS_CONNECTING,
129 INNER_STA_STATUS_ASSOCIATING,
130 INNER_STA_STATUS_ASSOCIATED,
131 INNER_STA_STATUS_CONNECTED,
132 INNER_STA_STATUS_DISCONNECTING,
133 INNER_STA_STATUS_DISCONNECTED,
134 INNER_STA_STATUS_CANCEL,
135}inner_sta_status_s;
136
137static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
138static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800139volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800140static error_number_s s_sta_error_number = -1;
141static char s_sta_current_connecting_ssid[64] = {0};
142static struct timespec s_sta_connect_timeout;
143const int MAX_CONNNECT_TIME = 15; // second
144pthread_t g_global_watcher_pid = 0;
145static int s_service_invoke_timeout_cnt=0;
146const int FAKE_MAX_INT_VALUE = 99999;
147
qs.xiong09560402023-10-27 21:58:55 +0800148static void print_disconnect_list()
149{
150 int i;
151 for( i = 0; i < g_history_disconnect_valid_num; i++ )
152 {
153 RLOGD(" list of g_history_disconnect_valid_num is %d histroy_list[%d]:%d --------- %d",g_history_disconnect_valid_num,i,g_history_disconnect_net[i],__LINE__);
154 }
155
156 return;
157}
158
159// idex ----> history_disconnect_list[x] index
160static int removeElement(int idex)
161{
162 RLOGD("into removeElement");
163 if( index < 0 )
164 {
165 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
166 return -1;
167 }
168 RLOGD("%s line: %d g_history_disconnect_net[%d]: %d end g_history_disconnect_net[%d]:%d",__func__,__LINE__,idex,g_history_disconnect_net[idex],g_history_disconnect_valid_num-1, g_history_disconnect_net[g_history_disconnect_valid_num-1]);
169 g_history_disconnect_net[idex] = g_history_disconnect_net[g_history_disconnect_valid_num-1]; //g_history_disconnect_vaild_num -1 for get last index
170 g_history_disconnect_valid_num --;
171 RLOGD("end removeElement");
172 return 0;
173}
174static int check_history_disconenct_ap_list(int val)
175{
176 print_disconnect_list();
177 RLOGD("WIFI[check_history_disconenct_ap_list]into check_history_disconenct_ap_list && input val is %d g_history_disconnect_valid_num is %d line",val,g_history_disconnect_valid_num,__LINE__);
178 int i;
179 for( i = 0; i < g_history_disconnect_valid_num; i++)
180 {
181 if( val == g_history_disconnect_net[i] )
182 {
183 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
184 RLOGD("end check_history_disconenct_ap_list && return network index");
185 return i;
186 }
187 }
188 RLOGD("end check_history_disconenct_ap_list && return fail,didn't need remove networkid %d from list g_history_disconnect_valid_num is %d line %d",val,g_history_disconnect_valid_num,__LINE__);
189 return -1;
190}
191
192
193static void lynq_sta_removeElement(int net_no)
194{
195 int ret;
196
197 ret = check_history_disconenct_ap_list(net_no);
198 if( ret == -1 )
199 {
200 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
201 return;
202 }else
203 {
204 ret = removeElement(ret);
205 if( ret == 0 )
206 {
207 RLOGD("removeElement pass %s %d",__func__,__LINE__);
208 return;
209 }
210 }
211
212 return;
213}
214
you.chen70f377f2023-04-14 18:17:09 +0800215static void notify_service_invoke_fail(int error)
216{
217 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
218 pthread_mutex_lock(&s_global_check_mutex);
219 if (error == -2) //timeout
220 {
221 s_service_invoke_timeout_cnt++;
222 if (s_service_invoke_timeout_cnt > 10)
223 {
224 pthread_cond_signal(&s_global_check_cond);
225 }
226 }
227 else if (error == -1)
228 {
229 // check if can connect wpa service
230 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
231 if (lynq_wpa_ctrl == NULL)
232 {
233 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
234 pthread_cond_signal(&s_global_check_cond);
235 }
236 wpa_ctrl_close(lynq_wpa_ctrl);
237 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
238 if (lynq_wpa_ctrl == NULL)
239 {
240 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
241 pthread_cond_signal(&s_global_check_cond);
242 }
243 wpa_ctrl_close(lynq_wpa_ctrl);
244 }
245
246 pthread_mutex_unlock(&s_global_check_mutex);
247}
248
you.chenc9928582023-04-24 15:39:37 +0800249static int system_call_v(const char * fmt, ...)
250{
251 char str_cmd[256] = {0};
252 va_list args;
253 va_start(args, fmt);
254 vsprintf(str_cmd, fmt, args);
255 va_end(args);
256 printf("system call----------%s\n", str_cmd);
257 return system(str_cmd);
258}
259
you.chen0df3e7e2023-05-10 15:56:26 +0800260static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
261
262static const char * inner_get_ap_interface_name()
263{
264 char * p;
265 char cmd[128]={0};
266
267 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
268 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
269 {
270 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
271 return NULL;
272 }
273 p = strchr(s_ap_iterface_name, ' ');
274 if (NULL != p)
275 {
276 *p = '\0';
277 }
278 p = strchr(s_ap_iterface_name, '\n');
279 if (NULL != p)
280 {
281 *p = '\0';
282 }
283 if (s_ap_iterface_name[0] == '\0')
284 {
285 return NULL;
286 }
287
288 return s_ap_iterface_name;
289}
290
you.chen70f377f2023-04-14 18:17:09 +0800291static void check_tether_and_notify()
292{
293 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800294 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 +0800295 {
296 return;
297 }
298 pthread_mutex_lock(&s_global_check_mutex);
299 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
300 pthread_cond_signal(&s_global_check_cond);
301 pthread_mutex_unlock(&s_global_check_mutex);
302}
303
you.chend2fef3f2023-02-13 10:50:35 +0800304static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
305 char *reply, size_t *reply_len,
306 void (*msg_cb)(char *msg, size_t len))
307{
308 int ret;
309 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800310 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800311 return -1;
312 }
313 pthread_mutex_lock(&ctrl->mutex);
314 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
315 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800316 if (ret != 0)
317 {
318 notify_service_invoke_fail(ret);
319 }
you.chend2fef3f2023-02-13 10:50:35 +0800320 return ret;
321}
322
323static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
324 int repeat_cnt;
325 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
326 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800327 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800328 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
329 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
330// printf("wait enable finish\n");
331 usleep(500 * 1000);
332 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
333 }
334 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800335 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800336 goto out_addr;
337 }
338 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
339 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
340 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800341 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800342 goto out_addr;
343 }
344 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
345 }
346 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
347out_addr:
348 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
349 return lynq_wpa_ctrl;
350}
351
qs.xiong97fa59b2022-04-07 05:41:29 -0400352#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400353{\
you.chen35020192022-05-06 11:30:57 +0800354 perror((str));\
355 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400356}
357
you.chen35020192022-05-06 11:30:57 +0800358#define CHECK_IDX(idx, type) do { \
359 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
360 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800361 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800362 return -1; \
363 } \
364 }while (0)
365
366#define CHECK_WPA_CTRL(index) int ret = 0;\
367 size_t reply_len = MAX_RET; \
368 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800369 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800370 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800371 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
372 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800373 }while(0)
374
375#define DO_REQUEST(cmd_str) do { \
376 reply_len = MAX_RET;\
377 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800378 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800379 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 +0800380 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800381 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800382 return ret; \
383 } \
384 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800385 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800386 }while(0)
387
388#define DO_OK_FAIL_REQUEST(cmd_str) do { \
389 DO_REQUEST(cmd_str); \
390 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800391 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800392 return -1; \
393 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800394 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800395 return -1; \
396 } \
397 }while (0)
398
399
you.chenf711c8a2023-04-13 13:49:45 +0800400static int check_connection(struct wpa_ctrl * wpa_ctrl)
401{
402 size_t reply_len = MAX_RET;
403 char cmd_reply[MAX_RET]={0};
404 int ret;
405
406 RLOGD("check_connection [%p]", wpa_ctrl);
407 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
408
409 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
410 {
411 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 +0800412 if (ret != 0)
413 {
414 notify_service_invoke_fail(ret);
415 }
you.chenf711c8a2023-04-13 13:49:45 +0800416 return -1;
417 }
418
419 return 0;
420}
421
422/**
423 * @brief check_pending_msg
424 * @param lynq_wpa_ctrl
425 * @return 1 has msg, 0 no msg, -1 error
426 */
427static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
428{
429 int ret;
430
431 if (*pp_lynq_wpa_ctrl == NULL) // need connect
432 {
433 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
434 if (*pp_lynq_wpa_ctrl == NULL)
435 {
436 usleep(SLEEP_TIME_ON_IDLE);
437 return -1;
438 }
439
440 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
441 if (ret == 0) // attach success
442 {
443 *started_flag = 1;
444 }
445 else
446 {
you.chen70f377f2023-04-14 18:17:09 +0800447 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800448 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
449 *pp_lynq_wpa_ctrl = NULL;
450 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800451 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800452 usleep(SLEEP_TIME_ON_IDLE);
453 return -1;
454 }
455 }
456
457 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
458 if ( ret == 0) // no pending messages
459 {
460 usleep(SLEEP_TIME_ON_IDLE);
461 *idle_count += 1;
462 if (*idle_count > MAX_IDLE_COUNT)
463 {
464 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
465 {
466 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
467 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
468 *pp_lynq_wpa_ctrl = NULL;
469 *idle_count = 0;
470 return -1;
471 }
472 *idle_count = 0;
473 }
474 return 0;
475 }
476 else if ( ret == -1) // on error
477 {
478 RLOGE("[wifi error]sta wpa_ctrl_pending");
479 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
480 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
481 *pp_lynq_wpa_ctrl = NULL;
482 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800483 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800484 return -1;
485 }
486
487 *idle_count = 0;
488 return 1;
489}
490
you.chen6d247052023-06-01 16:39:54 +0800491static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
492{
493 pthread_mutex_lock(&s_ap_callback_mutex);
494 if (g_ap_callback_func != NULL)
495 g_ap_callback_func(g_ap_callback_priv, status);
496 pthread_mutex_unlock(&s_ap_callback_mutex);
497}
498
you.chen35020192022-05-06 11:30:57 +0800499static void APWatcherThreadProc() {
500 size_t len = MAX_RET;
501 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800502 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800503 char *ptr = NULL;
504 char mac[32] = {0};
505 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800506
you.chen6c2dd9c2022-05-16 17:55:28 +0800507 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800508 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800509
qs.xiong9fbf74e2023-03-28 13:38:22 +0800510 while (g_ap_watcher_stop_flag == 0)
511 {
you.chenf711c8a2023-04-13 13:49:45 +0800512 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
513 {
you.chen70f377f2023-04-14 18:17:09 +0800514 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
515 {
516 check_tether_and_notify();
517 }
518
you.chen35020192022-05-06 11:30:57 +0800519 continue;
520 }
you.chenf711c8a2023-04-13 13:49:45 +0800521
you.chen6c2dd9c2022-05-16 17:55:28 +0800522 memset(msg_notify, 0, MAX_RET);
523 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800524 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800525 {
you.chen35020192022-05-06 11:30:57 +0800526 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800527 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800528 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800529 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800530 {
you.chen6d247052023-06-01 16:39:54 +0800531 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800532 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
533 if( ptr != NULL)
534 {
535 ptr += strlen("AP-STA-DISCONNECTED ");
536 memcpy(mac,ptr,17);
537 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
538 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
539 system(cmd);
540 }
541
qs.xiong9fbf74e2023-03-28 13:38:22 +0800542 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800543 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800544 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
545 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800546 {
you.chen0f5c6432022-11-07 18:31:14 +0800547 stopGBW();
548 }
549 }
you.chen35020192022-05-06 11:30:57 +0800550 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800551 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800552 {
you.chen6d247052023-06-01 16:39:54 +0800553 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800554 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800555 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800556 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
557 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800558 {
you.chen0f5c6432022-11-07 18:31:14 +0800559 startGBW();
560 }
561 }
you.chen35020192022-05-06 11:30:57 +0800562 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800563 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800564 {
565 RLOGD("APWatcherThreadProc ap------> service error");
566 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
567 }
568 else
569 {
570 RLOGD("APWatcherThreadProc ap------> going on check next msg");
571 }
you.chenf711c8a2023-04-13 13:49:45 +0800572 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800573 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
574 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800575 if (lynq_wpa_ctrl != NULL)
576 {
you.chen92fd5d32022-05-25 10:09:47 +0800577 wpa_ctrl_detach(lynq_wpa_ctrl);
578 wpa_ctrl_close(lynq_wpa_ctrl);
579 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400580}
581
you.chen70f377f2023-04-14 18:17:09 +0800582static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
583{
584 char * p;
585 const char * try_associat_flag = "Trying to associate";
586 const char * associated_flag = "Associated with ";
587
588 pthread_mutex_lock(&s_global_check_mutex);
589 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
590 {
591 pthread_mutex_unlock(&s_global_check_mutex);
592 return;
593 }
594
you.chen6e724162023-10-19 19:10:01 +0800595 // youchen@2023-10-17 add for "not notify connect fail directly" begin
596 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
597 {
598 s_sta_error_number = error_num;
599 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
600 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
601 pthread_cond_signal(&s_global_check_cond);
602 pthread_mutex_unlock(&s_global_check_mutex);
603 return;
604 }
605 // youchen@2023-10-17 add for "not notify connect fail directly" end
606
you.chen70f377f2023-04-14 18:17:09 +0800607 if (state == LYNQ_WIFI_STATUS_EGNORE)
608 {
609 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
610 {
611 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
612 }
613 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
614 {
615 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
616 }
617 }
618 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
619 {
620 s_sta_error_number = error_num;
621 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
622 {
623 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800624 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 +0800625 pthread_cond_signal(&s_global_check_cond);
626 }
627 }
628 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
629 {
630 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800631 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 +0800632 pthread_cond_signal(&s_global_check_cond);
633 }
634 pthread_mutex_unlock(&s_global_check_mutex);
635}
636
qs.xiongb37f8c42023-09-13 21:21:58 +0800637static int lynq_split(char * str, int len, char delimiter, char * results[]);
638static inline char inner_convert_char(char in);
639static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
640static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
641
642
643
qs.xiong44fac672023-08-29 16:15:55 +0800644/*
645just tmp add for fix sta connect ap fail check ap connect info
646return 0 --->Current no sta device connect this AP
647*/
648static int lynq_connected_ap_sta_status() {
649
650 FILE *fp;
651 size_t i = 0;
652 int ret;
653 char lynq_cmd_ret[MAX_RET]={0};
654
655 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
656 {
657 perror("popen error!");
658 return -1;
659 }
660 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
661 {
662 perror("fread fail!");
663 ret=pclose(fp);
664 if(ret == -1)
665 perror("close file faild");
666 return -1;
667 }
668 if( strlen(lynq_cmd_ret) < MAC_LEN)
669 {
670 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
671 ret=pclose(fp);
672 if(ret==-1)
673 {
674 perror("close file faild");
675 }
676 return 0;
677 }else{
678 ret=pclose(fp);
679 if(ret==-1)
680 {
681 perror("close file faild");
682 }
683 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
684 return 1;
685 }
686}
687
688/*
689 just tmp add for fix sta connect ap fail; check fw status
690 return 1 ----> fw status error; need wl down/up
691*/
692static int check_current_fw_status() {
693
694 FILE *fp;
695 FILE *fp1;
696 size_t i = 0;
697 int ret;
698 char lynq_cmd_ret_2g[MAX_RET]={0};
699 char lynq_cmd_ret_5g[MAX_RET]={0};
700
701 const char * fw_status = "0x0096"; //0x0096 is normal fw status
702
703 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
704 {
705 perror("popen error!");
706 return -1;
707 }
708 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
709 {
710 perror("fread fail!");
711 if(pclose(fp) == -1)
712 perror("close fp file faild");
713 return -1;
714 }
715
716 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
717 {
718 perror("popen error!");
719 if(pclose(fp) == -1)
720 perror("clsoe fp file faild");
721 return -1;
722 }
723 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
724 {
725 perror("fread fail!");
726 if(pclose(fp1) == -1)
727 perror("clsoe fp1 file faild");
728 if(pclose(fp) == -1)
729 perror("clsoe fp file faild");
730 return -1;
731 }
732
733 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
734 {
735 ret=pclose(fp);
736 if(ret==-1)
737 {
738 perror("close fp file faild");
739 }
740 ret=pclose(fp1);
741 if(ret==-1)
742 {
743 perror("close fp1 file faild");
744 }
745 return 0;
746 }else
747 {
748 ret=pclose(fp);
749 if(ret==-1)
750 {
751 perror("close file faild");
752 }
753 if(pclose(fp1) == -1)
754 {
755 perror("clsoe file fp1 faild");
756 }
757 RLOGD("current fw status --error--");
758 return 1;
759 }
760}
761
qs.xiong1d4263a2023-09-06 10:46:23 +0800762/*
763eg: wl counters info
764sh-3.2# wl counters
765counters_version 30
766datalen 1648
767Slice_index: 0
768reinitreason_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)
769reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
770
771check reinit status
772return 0 ===> fw did wl reinit cmd
773*/
774static int check_current_reinit_info()
775{
776 FILE *fp;
777 int ret;
778 char lynq_cmd_ret[MAX_RET]={0};
779 char * dest;
780 char destid[3]={0};
781 if((fp=popen("wl counters","r"))==NULL)
782 {
783 perror("popen error!");
784 return -1;
785 }
786 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
787 {
788 perror("fread fail!");
789 if(pclose(fp) == -1)
790 {
791 perror("close fp file faild");
792 }
793 return -1;
794 }
795 dest = strstr(lynq_cmd_ret,"reinit ");
796 if(dest != NULL)
797 {
798 dest +=strlen("reinit ");
799 RLOGD("current get dest str is %s",dest);
800 memcpy(destid,dest,2);
801 ret = atoi(destid);
802 RLOGD("get current wl counters cmd return counts is %d",ret);
803 if( ret != 0 )
804 {
805 RLOGD("current fw did run cmd wl reinit");
806 if( pclose(fp) == -1 )
807 {
808 perror("close fp file faild");
809 }
810 return 0;
811 }
812 }
813 if( pclose(fp) == -1 )
814 {
815 perror("close fp file faild");
816 }
817 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
818 return -1;
819}
820
qs.xiong44fac672023-08-29 16:15:55 +0800821static void APTmpWatcherThreadProc() {
822
823 int i = 0;
824 int delytime = 300;
825 g_ap_tmp_watcher_stop_flag = 0;
826
827 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
828 while(1)
829 {
830 sleep(1);
831 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800832 if ( (i % 30) == 0 )
833 {
834 if ( check_current_reinit_info() == 0 )
835 {
836 system("wl reset_cnts");
837 system("wl down");
838 system("wl up");
839 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
840 }
841 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800842 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800843 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800844 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800845 {
846 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
847 {
848 system("wl down");
849 system("wl up");
850 }
qs.xiong44fac672023-08-29 16:15:55 +0800851 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800852 }
853 if ( i == delytime )
854 {
qs.xiong44fac672023-08-29 16:15:55 +0800855 i = 0;
856 }
857 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
858 {
859 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
860 return;
861 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800862
qs.xiong44fac672023-08-29 16:15:55 +0800863 }
864
865}
866
qs.xiongf0128b12023-06-29 17:29:39 +0800867static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
868{
869 char LYNQ_DISABLE_CMD[128]={0};
870
871 CHECK_IDX(idx, CTRL_STA);
872 CHECK_WPA_CTRL(CTRL_STA);
873
874 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800875 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800876 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800877
qs.xiongf0128b12023-06-29 17:29:39 +0800878 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800879
qs.xiongf0128b12023-06-29 17:29:39 +0800880}
881
qs.xiongb37f8c42023-09-13 21:21:58 +0800882static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
883{
884 char LYNQ_ENABLE_CMD[128]={0};
885
886 CHECK_IDX(idx, CTRL_STA);
887 CHECK_WPA_CTRL(CTRL_STA);
888
889
890 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800891 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800892 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
893
894 return 0;
895
896}
897
898static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
899{
900
901 int index,networkid;
902
903 for ( index = 0; index < len ;index++)
904 {
905 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800906 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800907 {
908 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
909 }
910 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
911 }
912 return 0;
913
914}
915
916
917/*
918 dis_net_list user disconnect list
919*/
920static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
921{
qs.xiong09560402023-10-27 21:58:55 +0800922 RLOGD("enter %s %d",__func__,__LINE__);
923 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800924 int count,ncount,index;
925 int flag = 0;
926 int merge_index = 0;
927 int net_no_list[128];
928
qs.xiong09560402023-10-27 21:58:55 +0800929 for(ncount = 0;ncount < valid_num; ncount++ )
930 {
931 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
932 }
933
qs.xiongb37f8c42023-09-13 21:21:58 +0800934 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
935 for( count = 0; count < index; count++)
936 {
937 for(ncount = 0; ncount < valid_num; ncount++)
938 {
qs.xiong09560402023-10-27 21:58:55 +0800939 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800940 if(net_no_list[count] == dis_net_list[ncount])
941 {
qs.xiong09560402023-10-27 21:58:55 +0800942 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800943 flag = 1;
944 break;
945 }
946 }
947 if( flag != 1 )
948 {
949 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800950 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 +0800951 merge_index ++;
952 }
953 flag = 0;
954 }
955 * outlen =merge_index;
956 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
957 return;
958}
qs.xiongf0128b12023-06-29 17:29:39 +0800959
qs.xiong455c30b2023-04-12 11:40:02 +0800960void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
961{
962 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800963 char *wpanetid;
964 char destid[3] = {0};
965 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800966 *error = LYNQ_WAIT_CONNECT_ACTIVE;
967 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
968 {
969 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
970 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
971 return;
972 }
973
qs.xiong20202422023-09-06 18:01:18 +0800974 if (strstr(modify, "Trying to associate") != NULL)
975 {
976 RLOGD("Current sta is Trying to associate");
977 *state = LYNQ_WIFI_STATUS_EGNORE;
978 g_sta_conncet_status_flag = 1;
979 return;
980 }
981
qs.xiong455c30b2023-04-12 11:40:02 +0800982 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
983 {
984 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800985 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800986 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800987 return;
988 }
989
qs.xiong1e81dfa2023-09-27 15:52:37 +0800990 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
991 {
992 *error = LYNQ_PSW_ERROR;
993 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
994 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
995 g_sta_conncet_status_flag = 0;
996 return;
997 }
qs.xiong455c30b2023-04-12 11:40:02 +0800998 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
999 {
qs.xiongf0128b12023-06-29 17:29:39 +08001000 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1001 wpanetid = strstr(modify,"id=");
1002 if ( wpanetid != NULL )
1003 {
1004 wpanetid +=strlen("id=");
1005 memcpy(destid,wpanetid,2);
1006 tmpdisid = atoi(destid);
1007
1008 }
qs.xiong455c30b2023-04-12 11:40:02 +08001009 pReason = strstr(modify, "reason=");
1010 if (pReason != NULL)
1011 {
1012 pReason += strlen("reason=");
1013 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1014 {
1015 *error = LYNQ_TIME_OUT;
1016 }
1017 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1018 {
1019 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001020 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001021 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001022 // you.chen@2023-10-17 only disable network during autoconnect
1023 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 +08001024 {
1025 RLOGE("stop wlan0 network %d fail",tmpdisid);
1026 }
qs.xiong455c30b2023-04-12 11:40:02 +08001027 }
1028 else
1029 {
1030 *error = LYNQ_UNSPECIFIED_REASON;
1031 }
qs.xiong455c30b2023-04-12 11:40:02 +08001032 }
1033 else
1034 {
1035 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001036 }
qs.xiongf0128b12023-06-29 17:29:39 +08001037 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001038 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001039 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001040
1041 }
1042
1043 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1044 {
1045 *error = LYNQ_NOT_FIND_AP;
1046 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1047 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001048 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001049 return;
1050 }
1051
1052
1053 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1054 {
1055 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1056 pReason = strstr(modify, "status_code=");
1057 if (pReason != NULL)
1058 {
1059 pReason += strlen("status_code=");
1060 if (memcmp(pReason, "17", 2) == 0)
1061 {
1062 *error = LYNQ_AP_UNABLE_HANDLE;
1063 }
1064 else if (memcmp(pReason, "1",1) == 0)
1065 {
1066 *error = LYNQ_UNSPECIFIED_REASON;
1067 }
1068 else
1069 {
1070 *error = LYNQ_UNSPECIFIED_REASON;
1071 }
1072
1073 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1074 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 +08001075 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001076 return;
1077 }
1078 else
1079 {
1080 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1081 *error = LYNQ_UNSPECIFIED_REASON;
1082 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1083 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1084 return;
1085 }
1086 }
1087
1088 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1089 {
1090 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1091 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1092 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1093 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1094 return;
1095 }
1096
1097 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1098 {
1099 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1100 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1101 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1102 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1103 return;
1104 }
1105
qs.xiongdf637b22023-10-26 19:30:07 +08001106 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1107 if (strstr(modify, "Association request to the driver failed") != NULL)
1108 {
1109 RLOGD("Association request to the driver failed --- recover");
1110 system("wl down");
1111 system("wl up");
1112 *error = LYNQ_UNSPECIFIED_REASON;
1113 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1114 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1115 return;
1116 }
1117 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1118
1119
you.chen32cb31e2023-04-13 14:05:45 +08001120 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001121 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001122 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001123 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1124 return;
1125
1126}
1127
qs.xiongfcc914b2023-07-06 21:16:20 +08001128void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1129{
1130 char *pReason;
1131 char *wpanetid;
1132 char destid[3];
1133 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1134 *networkid = -1;
1135 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1136 {
1137 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1138 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1139 return;
1140 }
1141 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1142 {
1143 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1144 wpanetid = strstr(modify,"id=");
1145 if ( wpanetid != NULL )
1146 {
1147 wpanetid +=strlen("id=");
1148 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1149 if (memcpy(destid,wpanetid,2) != NULL)
1150 {
1151 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1152 *networkid = atoi(destid);
1153 RLOGD("get networkid is %d\n",*networkid);
1154 }
1155 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1156 }
1157 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1158 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1159 return;
1160 }
1161 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1162 {
1163 wpanetid = strstr(modify,"id=");
1164 if ( wpanetid != NULL )
1165 {
1166 wpanetid +=strlen("id=");
1167 if (memcpy(destid,wpanetid,2) != NULL)
1168 {
1169 *networkid = atoi(destid);
1170 RLOGD("get networkid is %d\n",*networkid);
1171 }
1172 }
1173 pReason = strstr(modify, "reason=");
1174 if (pReason != NULL)
1175 {
1176 pReason += strlen("reason=");
1177 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1178 {
1179 *error = LYNQ_TIME_OUT;
1180 }
1181 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1182 {
1183 *error = LYNQ_PSW_ERROR;
1184 }
1185 else
1186 {
1187 *error = LYNQ_UNSPECIFIED_REASON;
1188 }
1189 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1190 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1191 return;
1192 }
1193 else
1194 {
1195 *error = LYNQ_UNSPECIFIED_REASON;
1196 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1197 return;
1198 }
1199 }
1200 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1201 {
1202 wpanetid = strstr(modify,"id=");
1203 if ( wpanetid != NULL )
1204 {
1205 wpanetid +=strlen("id=");
1206 if (memcpy(destid,wpanetid,2) != NULL)
1207 {
1208 *networkid = atoi(destid);
1209 RLOGD("get networkid is %d\n",*networkid);
1210 }
1211 }
1212 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1213 pReason = strstr(modify, "status_code=");
1214 if (pReason != NULL)
1215 {
1216 pReason += strlen("status_code=");
1217 if (memcmp(pReason, "17", 2) == 0)
1218 {
1219 *error = LYNQ_AP_UNABLE_HANDLE;
1220 }
1221 else if (memcmp(pReason, "1",1) == 0)
1222 {
1223 *error = LYNQ_UNSPECIFIED_REASON;
1224 }
1225 else
1226 {
1227 *error = LYNQ_UNSPECIFIED_REASON;
1228 }
1229 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1230 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1231 return;
1232 }
1233 else
1234 {
1235 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1236 *error = LYNQ_UNSPECIFIED_REASON;
1237 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001238 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001239 return;
1240 }
1241 }
1242 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1243 {
1244 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1245 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1246 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1247 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1248 return;
1249 }
1250 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1251 {
1252 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1253 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1254 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1255 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1256 return;
1257 }
1258 RLOGD("EVENT : %s\n", modify);
1259 *error = LYNQ_UNSPECIFIED_REASON;
1260 *state = LYNQ_WIFI_STATUS_EGNORE;
1261 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1262 return;
1263}
you.chen70f377f2023-04-14 18:17:09 +08001264static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1265{
you.chen6d247052023-06-01 16:39:54 +08001266 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001267 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1268 {
1269 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1270 g_sta_callback_func(g_sta_callback_priv, state, error);
1271 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1272 }
you.chen6d247052023-06-01 16:39:54 +08001273 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001274}
qs.xiongfcc914b2023-07-06 21:16:20 +08001275static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1276{
1277 pthread_mutex_lock(&s_sta_callback_mutex);
1278 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1279 {
1280 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1281 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1282 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1283 }
1284 pthread_mutex_unlock(&s_sta_callback_mutex);
1285}
you.chen70f377f2023-04-14 18:17:09 +08001286
you.chen35020192022-05-06 11:30:57 +08001287static void STAWatcherThreadProc() {
1288 size_t len = MAX_RET;
1289 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001290 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001291 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001292 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001293
you.chen6c2dd9c2022-05-16 17:55:28 +08001294 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001295 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001296
you.chen70f377f2023-04-14 18:17:09 +08001297 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001298 while (g_sta_watcher_stop_flag == 0)
1299 {
you.chenf711c8a2023-04-13 13:49:45 +08001300 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001301 {
you.chen35020192022-05-06 11:30:57 +08001302 continue;
1303 }
you.chenf711c8a2023-04-13 13:49:45 +08001304
you.chen6c2dd9c2022-05-16 17:55:28 +08001305 memset(msg_notify, 0, MAX_RET);
1306 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001307 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001308 {
you.chen35020192022-05-06 11:30:57 +08001309 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001310 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1311 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001312 {
you.chen35020192022-05-06 11:30:57 +08001313 g_sta_scan_finish_flag = 1;
1314 }
1315
qs.xiong9fbf74e2023-03-28 13:38:22 +08001316 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001317 {
you.chen35020192022-05-06 11:30:57 +08001318 continue;
1319 }
qs.xiong455c30b2023-04-12 11:40:02 +08001320 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001321 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1322 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1323 {
1324 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1325 }
you.chen0c9bee22023-10-25 13:03:14 +08001326 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1327 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1328 {
1329 RLOGD("donot report scan result during in call connect manual");
1330 }
you.chen6e724162023-10-19 19:10:01 +08001331 else
1332 {
1333 notify_connect_status(state, error);
1334 }
1335 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001336
1337 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001338 {
you.chen70f377f2023-04-14 18:17:09 +08001339 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001340 if (last_state != state)
1341 {
1342 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1343 {
1344 system_call_v("%s %s", sta_status_change_script, "connect");
1345 }
1346 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1347 {
1348 system_call_v("%s %s", sta_status_change_script, "disconnect");
1349 }
1350 }
qs.xiong09560402023-10-27 21:58:55 +08001351
you.chenc9928582023-04-24 15:39:37 +08001352 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001353 if (g_sta_fake_scan_finish_flag == 1)
1354 {
1355 g_sta_fake_scan_finish_flag = 0;
1356 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1357 }
you.chen32cb31e2023-04-13 14:05:45 +08001358 }
you.chen35020192022-05-06 11:30:57 +08001359 }
1360 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001361 if (lynq_wpa_ctrl != NULL)
1362 {
you.chen92fd5d32022-05-25 10:09:47 +08001363 wpa_ctrl_detach(lynq_wpa_ctrl);
1364 wpa_ctrl_close(lynq_wpa_ctrl);
1365 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001366}
qs.xiongfcc914b2023-07-06 21:16:20 +08001367static void STAAutoWatcherThreadProc() {
1368 size_t len = MAX_RET;
1369 char msg_notify[MAX_RET];
1370 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001371 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001372 int idle_count = 0;
1373 int networkid;
1374 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1375 g_sta_auto_watcher_stop_flag = 0;
1376 RLOGD("STAAutoWatcherThreadProc thread started ------");
1377 while (g_sta_auto_watcher_stop_flag == 0)
1378 {
1379 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1380 {
1381 continue;
1382 }
1383 memset(msg_notify, 0, MAX_RET);
1384 len = MAX_RET;
1385 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1386 {
1387 msg_notify[len+1] = '\0';
1388 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1389 if (strstr(msg_notify, state_scan_result) != NULL)
1390 {
1391 g_sta_auto_scan_finish_flag = 1;
1392 }
1393 if (g_sta_auto_callback_func == NULL)
1394 {
1395 continue;
1396 }
1397 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1398 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001399 }
1400 }
1401 if (lynq_wpa_ctrl != NULL)
1402 {
1403 wpa_ctrl_detach(lynq_wpa_ctrl);
1404 wpa_ctrl_close(lynq_wpa_ctrl);
1405 }
1406}
qs.xiongf1b525b2022-03-31 00:58:23 -04001407
you.chen70f377f2023-04-14 18:17:09 +08001408// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1409static void GlobalWatcherThreadProc()
1410{
1411 int ret, connect_timeout, service_abnormal;
1412 error_number_s error_num = -1;
1413 inner_sta_status_s sta_status;
1414 scan_info_s *scan_list = NULL;
1415 int i, scan_len=0;
1416 char connecting_ssid[64];
1417 struct timeval now;
1418
1419 RLOGD("GlobalWatcherThreadProc start to run");
1420
1421 while (1)
1422 {
1423 pthread_mutex_lock(&s_global_check_mutex);
1424 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1425 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1426 {
1427 pthread_mutex_unlock(&s_global_check_mutex);
1428 usleep(50*1000);
1429 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1430 continue;
1431 }
1432
1433 connect_timeout = 0;
1434 service_abnormal = 0;
1435 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1436 {
1437 while (1)
1438 {
1439 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1440 if (ret == ETIME)
1441 {
1442 connect_timeout = 1;
1443 }
1444 else if (ret != 0)
1445 {
1446 gettimeofday(&now,NULL);
1447 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1448 {
1449 usleep(SLEEP_TIME_ON_IDLE);
1450 continue;
1451 }
1452 connect_timeout = 1;
1453 }
1454 sta_status = s_sta_status;
1455 error_num = s_sta_error_number;
1456 s_sta_status = INNER_STA_STATUS_INIT;
1457 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1458 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1459 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1460 break;
1461 }
1462 }
1463 if (s_service_invoke_timeout_cnt > 10)
1464 {
1465 service_abnormal = 1;
1466 s_service_invoke_timeout_cnt = 0;
1467 }
1468 pthread_mutex_unlock(&s_global_check_mutex);
1469
1470 if (service_abnormal == 1)
1471 {
1472 sleep(1);
1473 RLOGE("wpa service is abnormal info app to exit");
1474 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001475
1476 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1477
you.chen70f377f2023-04-14 18:17:09 +08001478 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1479 }
1480
1481 if (sta_status == INNER_STA_STATUS_CANCEL)
1482 {
1483 continue;
1484 }
1485 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1486 {
1487 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1488 }
1489 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1490 {
1491 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1492 {
1493 for(i=0; i < scan_len;i++)
1494 {
1495 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1496 {
1497 error_num = LYNQ_AUTH_ERROR;
1498 break;
1499 }
1500 }
1501 free(scan_list);
1502 }
1503 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1504 }
1505 else if (connect_timeout == 0)
1506 {
1507 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1508 }
1509 else // wait timeout
1510 {
1511 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1512 {
1513 ; // wpa service abnormal
1514 }
1515 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1516 {
1517 RLOGD("GlobalWatcherThreadProc notify connected");
1518 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1519 }
1520 else
1521 {
1522 RLOGD("GlobalWatcherThreadProc notify timeout");
1523 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1524 }
1525 }
1526 } // while (1)
1527}
1528
qs.xiong1af5daf2022-03-14 09:12:12 -04001529int lynq_wifi_enable(void)
1530{
you.chen35020192022-05-06 11:30:57 +08001531 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001532 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001533 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001534 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1535
qs.xiong9fbf74e2023-03-28 13:38:22 +08001536 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1537 {
you.chend2fef3f2023-02-13 10:50:35 +08001538 goto out_enable;
1539 }
1540
you.chenc9928582023-04-24 15:39:37 +08001541 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001542 if (ret != 0)
1543 {
1544 //printf("service state %d\n", ret);
1545 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001546 ret = -1;
1547 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001548 }
lhfe8da902022-10-11 18:55:36 +08001549
you.chen70f377f2023-04-14 18:17:09 +08001550 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1551 {
1552 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1553 if(ret<0)
1554 {
1555 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1556 ret = -1;
1557 goto out_enable;
1558 }
1559 }
1560
you.chend2fef3f2023-02-13 10:50:35 +08001561 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1562 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1563 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1564 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1565out_enable:
1566 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001567 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001568}
1569
qs.xiong1af5daf2022-03-14 09:12:12 -04001570int lynq_wifi_disable(void)
1571{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001572 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001573 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001574 g_ap_watcher_stop_flag = 1;
1575 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001576 g_sta_auto_watcher_stop_flag = 1;
qs.xiong9d8f3102023-12-07 20:11:37 +08001577/* g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001578 if (g_ap_watcher_pid != 0)
1579 pthread_join(g_ap_watcher_pid, NULL);
qs.xiong9d8f3102023-12-07 20:11:37 +08001580*/
you.chen35020192022-05-06 11:30:57 +08001581 if (g_sta_watcher_pid != 0)
1582 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001583 if (g_sta_auto_watcher_pid != 0)
1584 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001585 if (g_lynq_wpa_ctrl[0] != NULL)
1586 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1587 if (g_lynq_wpa_ctrl[1] != NULL)
1588 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001589 if (g_ap_tmp_watcher_pid != 0)
1590 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001591 g_ap_watcher_pid = 0;
1592 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001593 g_sta_auto_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08001594// g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001595 g_lynq_wpa_ctrl[0] = NULL;
1596 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001597 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001598 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001599 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1600 return 0;
1601}
1602
1603static inline char inner_convert_char(char in)
1604{
1605 if (in >= '0' && in <= '9')
1606 {
1607 return in - '0';
1608 }
1609 else if (in >= 'a' && in <= 'f')
1610 {
1611 return in - 'a' + 10;
1612 }
1613 else if (in >= 'A' && in <= 'F')
1614 {
1615 return in - 'A' + 10;
1616 }
1617 else
1618 {
1619 return '\xff';
1620 }
1621}
1622
1623static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1624{
1625 char *p;
1626 size_t pos = 0;
1627 if (NULL == out_ssid)
1628 return;
1629 //printf("input ssid=[%s]\n", ssid);
1630 memset(out_ssid, 0, out_ssid_len);
1631 if (NULL == ssid)
1632 return;
1633 p = strchr(ssid, '\\');
1634 if (NULL == p)
1635 {
1636 strncpy(out_ssid, ssid, out_ssid_len);
1637 //printf(" first %s\n", out_ssid);
1638 }
1639 else
1640 {
1641 pos = p - ssid;
1642 memcpy(out_ssid, ssid, pos);
1643 //printf("pos %lu -- %s\n", pos, out_ssid);
1644 for(; pos < out_ssid_len; pos ++)
1645 {
1646 if (p[0] == '\0')
1647 {
1648 //printf(" out %s\n", out_ssid);
1649 return;
1650 }
1651 else if (p[0] != '\\')
1652 {
1653 out_ssid[pos] = p[0];
1654 p += 1;
1655 }
1656 else if (p[1] == 'x' || p[1] == 'X')
1657 {
1658 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1659 p += 4;
1660 }
1661 else if (p[1] == '\\')
1662 {
1663 out_ssid[pos] = '\\';
1664 p += 2;
1665 }
1666 else if (p[1] == 't')
1667 {
1668 out_ssid[pos] = '\t';
1669 p += 2;
1670 }
1671 else if (p[1] == 'r')
1672 {
1673 out_ssid[pos] = '\r';
1674 p += 2;
1675 }
1676 else if (p[1] == 'n')
1677 {
1678 out_ssid[pos] = '\n';
1679 p += 2;
1680 }//todo find a better way to convert?
1681 }
1682 }
1683 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001684}
qs.xiong1af5daf2022-03-14 09:12:12 -04001685
you.chen35020192022-05-06 11:30:57 +08001686static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001687 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001688 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001689 RLOGD("enter inner_get_param");
1690 if (out_put == NULL)
1691 {
1692 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001693 return -1;
1694 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001695 if (param_name == NULL)
1696 {
1697 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001698 return -1;
1699 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001700 if (param_name[0] == '\0')
1701 {
1702 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001703 return -1;
1704 }
1705
1706 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1707
1708 CHECK_WPA_CTRL(interface);
1709
1710 DO_REQUEST(lynq_cmd_get);
1711
qs.xiong9fbf74e2023-03-28 13:38:22 +08001712 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1713 {
1714 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001715 return -1;
1716 }
1717
you.chena6fa5b22022-05-18 10:28:19 +08001718// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001719 if (strcmp(param_name, "ssid") == 0)
1720 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001721 if (cmd_reply[0] == '\"')
1722 {
you.chend2fef3f2023-02-13 10:50:35 +08001723 ssid_len = reply_len - 1;
1724 memcpy(out_put, cmd_reply + 1, ssid_len);
1725 if (out_put[ssid_len-1] == '\"')
1726 {
1727 out_put[ssid_len-1] = '\0';
1728 }
1729 else
1730 {
1731 out_put[ssid_len] = '\0';
1732 }
1733 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001734 else
1735 {
you.chend2fef3f2023-02-13 10:50:35 +08001736 ssid_len = reply_len / 2;
1737 for(i=0; i<ssid_len; i++)
1738 {
1739 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1740 }
1741 out_put[ssid_len] = '\0';
1742 }
1743 }
1744 else
1745 {
1746 memcpy(out_put, cmd_reply, reply_len + 1);
1747 }
you.chen35020192022-05-06 11:30:57 +08001748 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001749}
qs.xiong1af5daf2022-03-14 09:12:12 -04001750
you.chen35020192022-05-06 11:30:57 +08001751static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1752 int ret = 0;
1753 char * end = str + len - 1;
1754 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001755 while(str < end)
1756 {
1757 if (*str == delimiter)
1758 {
you.chen35020192022-05-06 11:30:57 +08001759 *str++ = '\0';
1760 results[ret++] = str;
1761 continue;
1762 }
1763 str++;
1764 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 if (*str == delimiter)
1766 {
you.chen35020192022-05-06 11:30:57 +08001767 *str = '\0';
1768 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001769
you.chen6ed36a62023-04-27 17:51:56 +08001770 results[ret] = NULL;
1771
you.chen35020192022-05-06 11:30:57 +08001772 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001773}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001774/*
1775 *add func to get conencted STA device ip from dnsmasq ap0.lease
1776 *return 0 means get ip success
1777 */
1778static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1779{
1780 char * p;
1781 int ret;
1782 char cmd[256]={0};
1783 if (NULL == mac || NULL == ip)
1784 return -1;
1785 memset(ip, 0, ip_len);
1786 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1787 ret = exec_cmd(cmd, ip, ip_len);
1788 if( ret == 0 )
1789 {
1790 p = strchr(ip, '\n');
1791 if (NULL != p)
1792 {
1793 *p = '\0';
1794 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1795 return ret;
1796 }else
1797 {
1798 ret = -1;
1799 }
1800 }
1801 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1802 return ret;
1803
1804}
qs.xiong7a105ce2022-03-02 09:43:11 -05001805
you.chend2fef3f2023-02-13 10:50:35 +08001806static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1807{
1808 char * p;
1809 int ret = 0;
1810 char cmd[256]={0};
1811 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001812 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001813 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001814 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001815 ret = exec_cmd(cmd, ip, ip_len);
1816 p = strchr(ip, '\n');
1817 if (NULL != p)
1818 {
1819 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001820 }else
1821 {
1822 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001823 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001824 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001825 return ret;
1826}
1827
you.chend2fef3f2023-02-13 10:50:35 +08001828static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001829 struct in_addr addr ={0};
1830 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001831 char cmd[64] = {0};
1832 char * p;
1833 int ret;
you.chen35020192022-05-06 11:30:57 +08001834
qs.xiong9fbf74e2023-03-28 13:38:22 +08001835 if (ip == NULL || *ip == '\0' || hostname == NULL)
1836 {
1837 RLOGE("ip == NULL or hostname == NULL");
1838 return -1;
you.chen35020192022-05-06 11:30:57 +08001839 }
1840
you.chend2fef3f2023-02-13 10:50:35 +08001841 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001842 if (inet_aton(ip, &addr) == 0)
1843 {
you.chen35020192022-05-06 11:30:57 +08001844 printf("---inet_aton fail\n");
1845 return -1;
1846 }
1847
1848 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1849
qs.xiong9fbf74e2023-03-28 13:38:22 +08001850 if (ht == NULL)
1851 {
you.chen186d3c32023-05-18 14:19:46 +08001852 hostname[0] = '\0';
1853 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1854 ret = exec_cmd(cmd, hostname, 32);
1855 if (ret == 0)
1856 {
1857 p = strchr(hostname, '\n');
1858 if (p != NULL)
1859 {
1860 *p = '\0';
1861 }
1862 return 0;
1863 }
1864 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001866 herror(NULL);
1867 return -1;
1868 }
1869
1870 strcpy(hostname, ht->h_name);
1871
1872 return 0;
1873}
1874
1875static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1876{
1877 int count, index, words_count;
1878 char * split_lines[128]= {0};
1879 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001880 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001881 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001882 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001883
1884 CHECK_WPA_CTRL(ap_sta);
1885
1886 DO_REQUEST(lynq_wifi_list_networks);
1887
1888 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1889
1890 //@todo check ssid field to compatible
1891
1892 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001893 for(index=1; index < count; index++)
1894 {
you.chen35020192022-05-06 11:30:57 +08001895 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001896 if (words_count > 2)
1897 {
you.chend2fef3f2023-02-13 10:50:35 +08001898 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001899 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1900 {
you.chen35020192022-05-06 11:30:57 +08001901 net_no_list[ret++] = atoi(split_words[0]);
1902 }
1903 }
1904 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001905 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001906 return ret;
1907}
1908
1909static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001910 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001911 CHECK_WPA_CTRL(ap_sta);
1912 const char *lynq_wifi_add_network = "ADD_NETWORK";
1913
qs.xiong9fbf74e2023-03-28 13:38:22 +08001914 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001915 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001916 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1917 {
1918 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001919 return -1;
1920 }
1921
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 for(i=0;i<reply_len;i++)
1923 {
1924 if(cmd_reply[i] == '\n')
1925 {
you.chen35020192022-05-06 11:30:57 +08001926 cmd_reply[i] = '\0';
1927 break;
1928 }
1929 }
1930 return atoi(cmd_reply);
1931}
you.chena6cd55a2022-05-08 12:20:18 +08001932
you.chen35020192022-05-06 11:30:57 +08001933static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1934{
1935 int count, index;
1936 int net_no_list[128];
1937
qs.xiong9fbf74e2023-03-28 13:38:22 +08001938 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001939 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001940 for (index=0; index < count; index++)
1941 {
1942 if (net_no_list[index] == net_no)
1943 {
you.chen35020192022-05-06 11:30:57 +08001944 return 0;
1945 }
1946 }
1947
1948 if (count >= 1)
1949 index = net_no_list[count - 1];
1950 else
1951 index = -1;
1952
qs.xiong9fbf74e2023-03-28 13:38:22 +08001953 while (index < net_no )
1954 {
you.chen35020192022-05-06 11:30:57 +08001955 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001956 if (index >= net_no)
1957 { // required network no created
1958 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001959 return 0;
1960 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001961 else if( index < 0)
1962 {
1963 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001964 return -1;
1965 }
you.chen35020192022-05-06 11:30:57 +08001966 }
1967
1968 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001969 {
1970 RLOGE("[lynq_check_network_number] network index < 0");
1971 return -1;
1972 }
1973 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001974 return 0;
1975}
1976
1977static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001978 if (freq > 5000 && freq < 6000)
1979 {
you.chen35020192022-05-06 11:30:57 +08001980 return LYNQ_WIFI_5G_band;
1981 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001982 else if (freq > 2000 && freq < 3000)
1983 {
you.chen35020192022-05-06 11:30:57 +08001984 return LYNQ_WIFI_2G_band;
1985 }
1986 return LYNQ_WIFI_2_and_5G_band;
1987}
1988
1989static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 if (key_mgmt != NULL)
1991 {
1992 if (memcmp( key_mgmt, "NONE", 4) == 0)
1993 {
you.chen35020192022-05-06 11:30:57 +08001994 return LYNQ_WIFI_AUTH_OPEN;
1995 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001996 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1997 {
you.chen35020192022-05-06 11:30:57 +08001998 return LYNQ_WIFI_AUTH_WEP;
1999 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002000 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2001 {
you.chen35020192022-05-06 11:30:57 +08002002 return LYNQ_WIFI_AUTH_WPA_PSK;
2003 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2005 {
you.chen35020192022-05-06 11:30:57 +08002006 return LYNQ_WIFI_AUTH_WPA2_PSK;
2007 }
2008 }
2009
2010 return -1;
2011}
2012
2013static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002014 if (flag != NULL)
2015 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002016 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 +08002017 {
2018 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002019 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002020 {
2021 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2022 }else if (strstr( flag, "WPA2-PSK") != NULL)
2023 {
you.chen35020192022-05-06 11:30:57 +08002024 return LYNQ_WIFI_AUTH_WPA2_PSK;
2025 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002026 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002027 {
you.chen35020192022-05-06 11:30:57 +08002028 return LYNQ_WIFI_AUTH_WPA_PSK;
2029 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002030 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002031 {
you.chen35020192022-05-06 11:30:57 +08002032 return LYNQ_WIFI_AUTH_WEP;
2033 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002034 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002035 {
you.chen35020192022-05-06 11:30:57 +08002036 return LYNQ_WIFI_AUTH_OPEN;
2037 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002038 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002039 {
you.chend2fef3f2023-02-13 10:50:35 +08002040 return LYNQ_WIFI_AUTH_OPEN;
2041 }
qs.xiong46f41562023-07-11 21:06:47 +08002042 else
2043 {
2044 RLOGD("convert_max_auth_from_flag not-found auth mode");
2045 }
you.chen35020192022-05-06 11:30:57 +08002046 }
2047
2048 return -1;
2049}
2050
2051static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2052 switch (bw) {
2053 case 10:
2054 return LYNQ_WIFI_BANDWIDTH_HT10;
2055 break;
2056 case 20:
2057 return LYNQ_WIFI_BANDWIDTH_HT20;
2058 break;
2059 case 40:
2060 return LYNQ_WIFI_BANDWIDTH_HT40;
2061 break;
2062 case 80:
2063 return LYNQ_WIFI_BANDWIDTH_HT80;
2064 break;
2065 default:
2066 break;
2067 }
2068
2069 return -1;
2070}
2071
you.chen70f377f2023-04-14 18:17:09 +08002072static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002073static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2074 int i, count;
2075 char *p;
2076 const char *lynq_status_cmd = "STATUS";
2077 const char * FLAG_SSID = "ssid=";
2078 const char * FLAG_SBSID = "bssid=";
2079 const char * FLAG_KEY_MGMT = "key_mgmt=";
2080 const char * FLAG_FREQ = "freq=";
2081 const char * FLAG_STATE = "wpa_state=";
2082 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002083 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002084 char *split_lines[128] = {0};
2085
2086 CHECK_WPA_CTRL(interface);
2087
qs.xiong9fbf74e2023-03-28 13:38:22 +08002088 if (curr_state == NULL)
2089 {
2090 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002091 return -1;
2092 }
2093
2094 DO_REQUEST(lynq_status_cmd);
2095
2096 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2097
2098 curr_state->net_no = -1;
2099 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002100 for(i=0; i < count; i++)
2101 {
2102 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002103 {
you.chen35020192022-05-06 11:30:57 +08002104 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002105 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002106 {
you.chend2fef3f2023-02-13 10:50:35 +08002107 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002108 ret = 0;
2109 continue;
2110 }
you.chenf58b3c92022-06-21 16:53:48 +08002111 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002112 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002113 {
you.chend2fef3f2023-02-13 10:50:35 +08002114 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 +08002115 ret = 0;
2116 continue;
2117 }
you.chen35020192022-05-06 11:30:57 +08002118 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002120 {
you.chen450d0172022-07-15 17:56:48 +08002121 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002122 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002123 ret = 0;
2124 continue;
2125 }
2126 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002127 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002128 {
you.chen35020192022-05-06 11:30:57 +08002129 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2130 ret = 0;
2131 continue;
2132 }
you.chend2fef3f2023-02-13 10:50:35 +08002133 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002134 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002135 {
you.chend2fef3f2023-02-13 10:50:35 +08002136 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2137 ret = 0;
2138 continue;
2139 }
you.chen35020192022-05-06 11:30:57 +08002140 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002142 {
you.chen35020192022-05-06 11:30:57 +08002143 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002145 {
you.chen35020192022-05-06 11:30:57 +08002146 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2147 ret = 0;
2148 continue;
2149 }
2150
2151 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002152 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002153 {
you.chen35020192022-05-06 11:30:57 +08002154 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002155 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002157 }
2158 }
2159
you.chen70f377f2023-04-14 18:17:09 +08002160 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2161 {
2162 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2163 }
2164
you.chen35020192022-05-06 11:30:57 +08002165 return ret;
2166}
2167
qs.xiongf1b525b2022-03-31 00:58:23 -04002168int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002169{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002170 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002171 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002172
qs.xiong9fbf74e2023-03-28 13:38:22 +08002173 if (ap_ssid == NULL)
2174 {
2175 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002176 return -1;
2177 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002178 else
2179 {
2180 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002181 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002182
qs.xiong9fbf74e2023-03-28 13:38:22 +08002183 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2184 {
2185 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002186 return -1;
2187 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002188
you.chen35020192022-05-06 11:30:57 +08002189 CHECK_IDX(idx, CTRL_AP);
2190
2191 CHECK_WPA_CTRL(CTRL_AP);
2192
2193 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2194
2195 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2196 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002197
qs.xiong1b5e3472023-11-27 17:42:20 +08002198 RLOGD("[lynq_wifi_ap_ssid_set] set ssid succeed %d",__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002199 return 0;
you.chen35020192022-05-06 11:30:57 +08002200
qs.xiong7a105ce2022-03-02 09:43:11 -05002201}
2202
you.chen35020192022-05-06 11:30:57 +08002203int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002204{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002205 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002206 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002207 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002208}
2209
qs.xiongc9c79f72022-10-17 15:27:18 +08002210/*****
2211 *frequency <------>channel
2212 *
2213 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2214 *
2215 *
2216 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2217 *
2218 *
2219 * */
2220static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002221 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};
2222 int i;
2223 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2224
qs.xiong69a332b2022-12-02 09:58:57 +08002225 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002226 {
2227 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002228 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002229 }
qs.xiongc00b6032022-11-29 16:28:03 +08002230
2231 if(i == arr_len)
2232 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002233 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002234 return -1;
2235 }
qs.xiongc00b6032022-11-29 16:28:03 +08002236
qs.xiongc9c79f72022-10-17 15:27:18 +08002237 return 0;
2238}
qs.xiong13673462023-02-21 19:12:54 +08002239
2240static int lynq_check_frequencyby_country_code(int input_frequency)
2241{
2242 char str_cnc[]="CN";
2243 char str_dest[20]="";
2244
2245 if( lynq_get_country_code(1,str_dest) != 0 )
2246 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002247 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002248 return -1;
2249 }
2250 if( strncmp(str_dest,str_cnc,2) != 0 )
2251 {
2252 return 0;
2253 }else if( 2473 < input_frequency && input_frequency < 5744)
2254 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002255 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002256 return -1;
2257 }
2258 return 0;
2259}
qs.xiongf1b525b2022-03-31 00:58:23 -04002260int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002261{
qs.xiongc00b6032022-11-29 16:28:03 +08002262 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002263 char lynq_wifi_frequency_cmd[128]={0};
2264 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002265 char lynq_cmd_slect[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002266 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002267 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002268 check = lynq_check_set_frequency(lynq_wifi_frequency);
2269 if(check != 0)
2270 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002271 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002272 return -1;
you.chen35020192022-05-06 11:30:57 +08002273 }
qs.xiong13673462023-02-21 19:12:54 +08002274 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2275 if(check != 0)
2276 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002277 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002278 return -1;
2279 }
2280
qs.xiongc00b6032022-11-29 16:28:03 +08002281 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2282 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002283 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002284 return -1;
2285 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002286
you.chen35020192022-05-06 11:30:57 +08002287 CHECK_IDX(idx, CTRL_AP);
2288
2289 CHECK_WPA_CTRL(CTRL_AP);
2290
2291 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2292 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2293 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2294
you.chen6c2dd9c2022-05-16 17:55:28 +08002295 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002296 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2297 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2298 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002299
qs.xiong9fbf74e2023-03-28 13:38:22 +08002300 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002301}
2302
qs.xiongf1b525b2022-03-31 00:58:23 -04002303int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002304{
you.chen35020192022-05-06 11:30:57 +08002305 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002306 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002307 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002308
qs.xiong9fbf74e2023-03-28 13:38:22 +08002309 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2310 {
2311 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002312 return -1;
2313 }
2314 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002315
qs.xiong9fbf74e2023-03-28 13:38:22 +08002316 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002317}
2318
qs.xiongf1b525b2022-03-31 00:58:23 -04002319int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2320{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002321 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002322 CHECK_IDX(idx, CTRL_AP);
2323 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002324 case LYNQ_WIFI_BANDWIDTH_HT10:
2325 {
2326 RLOGE("bandwith [%d] not support now\n", bandwidth);
2327 return -1;
2328 }
2329 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002330 {
2331 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2332 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002333 if (system(lynq_cmd_bandwith) != 0 )
2334 {
2335 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002336 return -1;
2337 }
2338 system("wl up");
2339 break;
2340 }
2341 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002342 {
qs.xiong10379192023-02-21 13:19:42 +08002343 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002344 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2345 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 if (system(lynq_cmd_bandwith) != 0 )
2347 {
2348 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002349 return -1;
2350 }
2351 system("wl up");
2352 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002353 }
you.chen35020192022-05-06 11:30:57 +08002354 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002355 {
qs.xiong10379192023-02-21 13:19:42 +08002356 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002357 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002358 if (system(lynq_cmd_bandwith) != 0 )
2359 {
2360 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002361 return -1;
2362 }
2363 system("wl up");
2364 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002365 }
2366 default:
you.chen35020192022-05-06 11:30:57 +08002367 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002368 RLOGE("auth type [%d] not support now\n", bandwidth);
2369 return -1;
you.chen35020192022-05-06 11:30:57 +08002370 }
2371 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002372
2373
you.chen35020192022-05-06 11:30:57 +08002374 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002375}
you.chen35020192022-05-06 11:30:57 +08002376
qs.xiongf1b525b2022-03-31 00:58:23 -04002377int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2378{
you.chen35020192022-05-06 11:30:57 +08002379 int count = 0;
2380 int index = 0;
2381 char *split_words[128] = {0};
2382 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002383 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002384 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002385
you.chen35020192022-05-06 11:30:57 +08002386 CHECK_WPA_CTRL(CTRL_AP);
2387
2388 DO_REQUEST(lynq_chanspec_cmd);
2389
2390 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2391 for(;index < count; index++) {
2392 if (strncmp(split_words[index], "bw", 2) != 0) {
2393 continue;
2394 }
2395
2396 index++;
2397 if (index >= count) {
2398 return -1;
2399 }
2400
qs.xiong9fbf74e2023-03-28 13:38:22 +08002401 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002402 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2403 return 0;
2404 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002406 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002407}
qs.xiong0fb469a2022-04-14 03:50:45 -04002408
qs.xiongf1b525b2022-03-31 00:58:23 -04002409int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002410{
you.chen35020192022-05-06 11:30:57 +08002411 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002412 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002413 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002414
you.chen35020192022-05-06 11:30:57 +08002415 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002416
qs.xiong9fbf74e2023-03-28 13:38:22 +08002417 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2418 {
you.chen35020192022-05-06 11:30:57 +08002419 return -1;
2420 }
2421
2422 system("wl down");
2423 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002424 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002425 return -1;
2426 }
2427 system("wl up");
2428 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002429}
qs.xiong0fb469a2022-04-14 03:50:45 -04002430
qs.xiongf1b525b2022-03-31 00:58:23 -04002431int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002432{
you.chen35020192022-05-06 11:30:57 +08002433 int count = 0;
2434 int index = 0;
2435 char *split_words[128] = {0};
2436 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002437 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002438 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002439
you.chen35020192022-05-06 11:30:57 +08002440 CHECK_WPA_CTRL(CTRL_AP);
2441
2442 DO_REQUEST(lynq_chanspec_cmd);
2443
2444 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002445 for(;index < count; index++)
2446 {
2447 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002448 if (strncmp(split_words[index], "channel", 2) != 0) {
2449 continue;
2450 }
2451
2452 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002453 if (index >= count)
2454 {
you.chen35020192022-05-06 11:30:57 +08002455 return -1;
2456 }
2457
2458 *channel = atoi(split_words[index]);
2459 return 0;
2460 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002461 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002462 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002463}
2464
2465
you.chen35020192022-05-06 11:30:57 +08002466int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002467{
you.chen6c2dd9c2022-05-16 17:55:28 +08002468 char ssid[MAX_CMD] = {0};
2469 int freq = 0;
2470 char lynq_auth_cmd[64]={0};
2471 char lynq_auth_alg_cmd[64]={0};
2472 char lynq_psk_cmd[64]={0};
2473 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002474 char lynq_ieee80211_cmd[64]={0};
2475 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002476 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002477 CHECK_IDX(idx, CTRL_AP);
2478
you.chen6c2dd9c2022-05-16 17:55:28 +08002479 CHECK_WPA_CTRL(CTRL_AP);
2480
qs.xiong9fbf74e2023-03-28 13:38:22 +08002481 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2482 {
2483 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002484 return -1;
2485 }
2486
you.chen92fd5d32022-05-25 10:09:47 +08002487 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002488 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002489 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002490 return 0;
2491 }
2492 else {
2493 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2494 ssid[0] = '\0';
2495 }
2496 lynq_wifi_ap_frequency_get(idx, &freq);
2497
2498 DO_OK_FAIL_REQUEST(cmd_disconnect);
2499 DO_OK_FAIL_REQUEST(cmd_remove_all);
2500 if (ssid[0] != '\0') {
2501 lynq_wifi_ap_ssid_set(idx, ssid);
2502 }
2503 if (freq != 0) {
2504 lynq_wifi_ap_frequency_set(idx, freq);
2505 }
2506 }
2507 }
you.chen35020192022-05-06 11:30:57 +08002508
qs.xiong9fbf74e2023-03-28 13:38:22 +08002509 switch(auth){
2510 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002511 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002512 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002513 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002514 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002515 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002516 break;
2517 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002518 case LYNQ_WIFI_AUTH_WEP:
2519 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002520 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002521 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002522 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002523 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2524
2525 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2526 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2527 break;
2528 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002529 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002530 {
2531 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2532 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2533 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2534
2535 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2536 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2537 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2538 break;
2539
2540 }
you.chen35020192022-05-06 11:30:57 +08002541 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002542 {
2543 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2544 {
you.chen35020192022-05-06 11:30:57 +08002545 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2546 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2547 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002548 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2549 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002550 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002551 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002552 }
2553// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2554// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2555 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002556
you.chen35020192022-05-06 11:30:57 +08002557 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2558 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2559 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002560 break;
2561 }
2562 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2563 {
2564 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2565 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2566 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2567 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2568
2569 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2570 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2571 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2572 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2573 break;
2574 }
2575 case LYNQ_WIFI_AUTH_WPA3_PSK:
2576 {
2577 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2578 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002579 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002580 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2581
2582 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2583 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2584 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2585 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2586 break;
2587 }
2588 default:
you.chen35020192022-05-06 11:30:57 +08002589 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002590 RLOGE("auth type [%d] not support now\n", auth);
2591 return -1;
you.chen35020192022-05-06 11:30:57 +08002592 }
2593 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002594 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002595
qs.xiong9fbf74e2023-03-28 13:38:22 +08002596 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002597}
2598
you.chen35020192022-05-06 11:30:57 +08002599int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002600{
you.chen35020192022-05-06 11:30:57 +08002601 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002602 char lynq_auth_alg_str[MAX_RET] = {0};
2603 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002604 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002605 CHECK_IDX(idx, CTRL_AP);
2606
qs.xiong9fbf74e2023-03-28 13:38:22 +08002607 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2608 {
2609 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002610 return -1;
2611 }
2612
qs.xiong9fbf74e2023-03-28 13:38:22 +08002613 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2614 {
2615 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2616 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002617 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002618 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002619 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002620 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002621 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2622 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002623 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002624 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002625 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002626 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002627 else
2628 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002629 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002630 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002631 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002632 }
you.chen35020192022-05-06 11:30:57 +08002633 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002634 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2635 {
2636 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2637 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002638 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002639 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002640 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002641 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2642 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002643 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002644 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002645 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002646 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 else
2648 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002649 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002650 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002651 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002652 }
you.chen35020192022-05-06 11:30:57 +08002653 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002654
2655 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2656 {
2657 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2658 return -1;
2659 }
2660
2661 if (memcmp(lynq_auth_str,"1",1) == 0 )
2662 {
2663 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2664 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002665 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002666 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2667 {
2668 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2669 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002670 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002671 }
2672 else
2673 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002674 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002675 *auth = -1;
2676 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002677
you.chen6c2dd9c2022-05-16 17:55:28 +08002678 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002679}
qs.xiong1af5daf2022-03-14 09:12:12 -04002680
you.chenb95401e2023-05-12 19:39:06 +08002681static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2682{
2683 char status[64];
you.chencba13492023-05-19 13:53:43 +08002684 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002685 curr_status_info curr_state;
2686
2687 CHECK_WPA_CTRL(CTRL_AP);
2688
2689 memset(status, 0, sizeof (status));
2690
2691 curr_state.ap = NULL;
2692 curr_state.state = status;
2693
2694 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002695 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002696 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002697 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002698 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002699 {
2700 return 0;
2701 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002702 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002703 {
2704 DO_REQUEST("RECONNECT");
2705 return inner_check_ap_connected(idx, retry_count+1);
2706 }
you.chencba13492023-05-19 13:53:43 +08002707 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002708 {
2709 printf("retry 10 time\n");
2710 return -1;
2711 }
2712 else
2713 {
you.chen6d247052023-06-01 16:39:54 +08002714 if (strcmp(status, STATE_DISCONNECTED) == 0)
2715 {
2716 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2717 DO_REQUEST(LYNQ_WIFI_CMD);
2718 }
you.chenb95401e2023-05-12 19:39:06 +08002719 return inner_check_ap_connected(idx, retry_count+1);
2720 }
2721 }
2722 return -1;
2723}
qs.xiong1af5daf2022-03-14 09:12:12 -04002724
qs.xiongf1b525b2022-03-31 00:58:23 -04002725int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002726{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002727 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002728 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002729
qs.xiongec8bbeb2023-11-20 15:51:45 +08002730 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002731 CHECK_WPA_CTRL(CTRL_AP);
2732
you.chen0df3e7e2023-05-10 15:56:26 +08002733 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002734 {
you.chen0df3e7e2023-05-10 15:56:26 +08002735 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002736 return -1;
2737 }
you.chen35020192022-05-06 11:30:57 +08002738
qs.xiongb37f8c42023-09-13 21:21:58 +08002739
you.chen35020192022-05-06 11:30:57 +08002740 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2741 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2742
you.chenc9928582023-04-24 15:39:37 +08002743 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2744 if (ret != 0)
2745 {
2746 RLOGE("lynq_wifi_ap_start excute script fail");
2747 return -1;
2748 }
2749
you.chenb95401e2023-05-12 19:39:06 +08002750 if (inner_check_ap_connected(idx, 0) != 0)
2751 {
2752 return -1;
2753 }
2754
you.chen0df3e7e2023-05-10 15:56:26 +08002755 check_tether_and_notify();
qs.xiong9d8f3102023-12-07 20:11:37 +08002756/*
qs.xiong44fac672023-08-29 16:15:55 +08002757 if (g_ap_tmp_watcher_pid == 0)
2758 {
2759 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2760 {
2761 g_ap_tmp_watcher_pid = 0;
2762 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2763 return -1;
2764 }
2765 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2766 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002767*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002768 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002769 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002770}
2771
qs.xiongf1b525b2022-03-31 00:58:23 -04002772int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002773{
you.chen35020192022-05-06 11:30:57 +08002774 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002775}
2776
qs.xiongf1b525b2022-03-31 00:58:23 -04002777int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002778{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002779 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002780 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002781
you.chen35020192022-05-06 11:30:57 +08002782 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002783
you.chen35020192022-05-06 11:30:57 +08002784 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002785
you.chen35020192022-05-06 11:30:57 +08002786 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2787
2788 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2789
you.chenc9928582023-04-24 15:39:37 +08002790
2791 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2792 if (ret != 0)
2793 {
2794 RLOGE("lynq_wifi_ap_start excute script fail");
2795 return -1;
2796 }
qs.xiong9d8f3102023-12-07 20:11:37 +08002797/*
qs.xiong44fac672023-08-29 16:15:55 +08002798 g_ap_tmp_watcher_stop_flag = 1;
2799 if (g_ap_tmp_watcher_pid != 0)
2800 pthread_join(g_ap_tmp_watcher_pid, NULL);
2801 g_ap_tmp_watcher_pid = 0;
qs.xiong9d8f3102023-12-07 20:11:37 +08002802*/
qs.xiongec8bbeb2023-11-20 15:51:45 +08002803 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002804 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002805}
qs.xiong1af5daf2022-03-14 09:12:12 -04002806
qs.xiongf1b525b2022-03-31 00:58:23 -04002807int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002808{
you.chen35020192022-05-06 11:30:57 +08002809 char lynq_disable_cmd[128] = {0};
2810 char lynq_select_cmd[128] = {0};
2811 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002812 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002813 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002814
you.chen35020192022-05-06 11:30:57 +08002815 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002816 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2817 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2818
2819 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2820 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2821 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002822
qs.xiong9fbf74e2023-03-28 13:38:22 +08002823 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002824}
2825
qs.xiongf1b525b2022-03-31 00:58:23 -04002826int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002827{
you.chen35020192022-05-06 11:30:57 +08002828 char lynq_disable_cmd[128] = {0};
2829 char lynq_select_cmd[128] = {0};
2830 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002831 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002832 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002833
you.chen35020192022-05-06 11:30:57 +08002834 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002835
you.chen35020192022-05-06 11:30:57 +08002836 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2837 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2838
2839 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2840 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2841 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002842
qs.xiong9fbf74e2023-03-28 13:38:22 +08002843 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002844}
qs.xiongf1b525b2022-03-31 00:58:23 -04002845
you.chen35020192022-05-06 11:30:57 +08002846int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002847{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002848 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002849 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002850 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002851 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002852 RLOGD("enter lynq_ap_password_set");
2853 if( password == NULL )
2854 {
2855 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002856 return -1;
2857 }
2858 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002859 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002860 if(pass_len < 8 || pass_len >= 64)
2861 {
2862 RLOGE("[lynq_ap_password_set]input password len not in rage");
2863 return -1;
you.chen35020192022-05-06 11:30:57 +08002864 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002865
you.chen35020192022-05-06 11:30:57 +08002866 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002867
qs.xiong9fbf74e2023-03-28 13:38:22 +08002868 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2869 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002870 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002871 return -1;
2872 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002873 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2874 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002875 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2876 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002877 }
2878
you.chen35020192022-05-06 11:30:57 +08002879 CHECK_WPA_CTRL(CTRL_AP);
2880
qs.xiong9fbf74e2023-03-28 13:38:22 +08002881 if (auth == LYNQ_WIFI_AUTH_WEP)
2882 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002883 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002884 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2885 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2886 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2887 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2888 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002889 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2890 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002891 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 +08002892 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2893 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2894 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2896 {
2897
qs.xiongc8d92a62023-03-29 17:36:14 +08002898 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 +08002899 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002900 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002901 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2902 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2903
2904 }
2905 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002906 {
2907 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002908 return -1;
2909 }
you.chen35020192022-05-06 11:30:57 +08002910
you.chen35020192022-05-06 11:30:57 +08002911 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002912
qs.xiong9fbf74e2023-03-28 13:38:22 +08002913 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002914}
2915
you.chen35020192022-05-06 11:30:57 +08002916int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002917{
you.chen35020192022-05-06 11:30:57 +08002918 FILE * fp;
2919 int len, ret;
2920 int count, index;
2921 char *split_lines[128] = {0};
2922 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002923 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002924
you.chen35020192022-05-06 11:30:57 +08002925 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002926
you.chen35020192022-05-06 11:30:57 +08002927 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2928// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002929 if (NULL == fp)
2930 {
2931 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002932 return -1;
2933 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002934
you.chen35020192022-05-06 11:30:57 +08002935 buff = alloca(MAX_RET);
2936 fseek(fp, 0, SEEK_SET);
2937 len = fread(buff, 1, MAX_RET, fp);
2938 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002939
qs.xiong9fbf74e2023-03-28 13:38:22 +08002940 for(index=0; index < len; index ++)
2941 {
2942 if (memcmp(buff + index, "network={", 9) != 0)
2943 {
you.chen35020192022-05-06 11:30:57 +08002944 continue;
2945 }
2946 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002947 for (; index < len; index ++ )
2948 {
2949 if (buff[index] != '}')
2950 {
you.chen35020192022-05-06 11:30:57 +08002951 continue;
2952 }
2953 buff[index] = '\0';
2954 break;
2955 }
2956 len = buff + index - p;
2957 }
2958
2959 count = lynq_split(p, len, '\n', split_lines);
2960
2961 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002962 for(index=0; index < count; index++)
2963 {
you.chen35020192022-05-06 11:30:57 +08002964 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002965 if (p != NULL)
2966 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002967 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002968 if (*p == '\"')
2969 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002970 p++;
2971 }
you.chen35020192022-05-06 11:30:57 +08002972 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002973 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2974 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002975 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002976 if (*p == '\"')
2977 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002978 p++;
2979 }
2980 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981 else
2982 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002983 continue;
you.chen35020192022-05-06 11:30:57 +08002984 }
2985
2986 strcpy(password, p);
2987
qs.xiong9fbf74e2023-03-28 13:38:22 +08002988 while(*password != '\0')
2989 {
2990 if (*password == '\"')
2991 {
you.chen35020192022-05-06 11:30:57 +08002992 *password = '\0';
2993 break;
2994 }
2995 password++;
2996 }
2997 ret = 0;
2998 break;
2999 } //end for(index=0; index < count; index++)
3000
3001 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003002}
3003
you.chen35020192022-05-06 11:30:57 +08003004static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3005 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003006 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003007
qs.xiong9fbf74e2023-03-28 13:38:22 +08003008 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3009 {
you.chen35020192022-05-06 11:30:57 +08003010 return -1;
3011 }
3012
3013 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003014
qs.xiong9fbf74e2023-03-28 13:38:22 +08003015 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3016 {
3017 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003018 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003020 {
you.chena6cd55a2022-05-08 12:20:18 +08003021 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003022 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003023 }
you.chen70f377f2023-04-14 18:17:09 +08003024 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3025 {
3026 return 0;
3027 }
you.chena6cd55a2022-05-08 12:20:18 +08003028 }
3029 }
you.chen70f377f2023-04-14 18:17:09 +08003030 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3031 {
3032 return 0;
3033 }
3034
qs.xiong9fbf74e2023-03-28 13:38:22 +08003035 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3036 {
you.chen70f377f2023-04-14 18:17:09 +08003037 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003038 return -1;
3039 }
3040 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3041 {
3042
you.chen70f377f2023-04-14 18:17:09 +08003043 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3044 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003045 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3046 {
3047
3048 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003049 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003050 }else
3051 {
you.chen70f377f2023-04-14 18:17:09 +08003052 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003053 *auth = -1;
3054 return -1;
3055 }
you.chen35020192022-05-06 11:30:57 +08003056 return 0;
3057}
3058
3059int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003060{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003061 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003062 int pass_len, net_no, count, index;
3063 char lynq_tmp_cmd[300]={0};
3064 int net_no_list[128];
3065 lynq_wifi_auth_s net_auth;
3066 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003067 if(pass_len < 8 || pass_len >= 64)
3068 {
3069 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003070 return -1;
3071 }
3072
3073 CHECK_IDX(idx, CTRL_STA);
3074
3075 net_no = -1;
3076 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3077
qs.xiong9fbf74e2023-03-28 13:38:22 +08003078 for (index=0; index < count; index++)
3079 {
you.chen35020192022-05-06 11:30:57 +08003080 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003081 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3082 {
you.chen35020192022-05-06 11:30:57 +08003083 net_no = net_no_list[index];
3084 break;
3085 }
3086 }
3087
qs.xiong9fbf74e2023-03-28 13:38:22 +08003088 if (net_no < 0)
3089 {
you.chen35020192022-05-06 11:30:57 +08003090 return -1;
3091 }
3092
3093 CHECK_WPA_CTRL(CTRL_STA);
3094
3095 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3096
3097 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3098 DO_OK_FAIL_REQUEST(cmd_save_config);
3099
3100 return 0;
3101}
3102
qs.xiongb5dab082023-10-13 14:43:41 +08003103/**
3104* buff data
3105* buff_len size of buff
3106* idx sta
3107* *ap ap info for find ssid && password
3108* password return password
3109*
3110*/
3111static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3112
3113 int ret, network_len, i, ssid_len,curr_auth;
3114 int count, index,org_index;
3115 char *split_lines[128] = {0};
3116 char *p, *ssid, *ssid_end_flag,*ptr;
3117 char tmp_ssid[128]={0};
3118 char tmp_auth[24]={0};
3119
3120 org_index = 0;
3121 network_len = 0;
3122 p = NULL;
3123
3124 CHECK_IDX(idx, CTRL_STA);
3125
3126 while(1){
3127 network_len = 0;
3128 p == NULL;
3129 for(; org_index < buff_len; org_index ++)
3130 {
3131 for(; org_index < buff_len; org_index ++)
3132 {
3133 if (memcmp(buff + org_index, "network={", 9) != 0)
3134 {
3135 continue;
3136 }
3137 p = buff + org_index + 9;
3138
3139 for (; org_index < buff_len; org_index ++ )
3140 {
3141 if (buff[org_index] != '}')
3142 {
3143 continue;
3144 }
3145 buff[org_index] = '\0';
3146 break;
3147 }
3148 network_len = buff + org_index - p;
3149 break;
3150 }
3151
3152 if (p == NULL)
3153 {
3154 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3155 return -1;
3156 }
3157
3158 ssid = strstr(p, "ssid=");
3159 if (ssid != NULL) {
3160 ssid += strlen("ssid=");
3161 if (ssid[0] == '\"')
3162 {
3163 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3164 {
qs.xiongb5dab082023-10-13 14:43:41 +08003165 break;
3166 }
qs.xiongb5dab082023-10-13 14:43:41 +08003167 }
3168 else
3169 {
3170 ssid_end_flag = strstr(ssid, "\n");
3171 if (ssid_end_flag != NULL)
3172 {
3173 ssid_len = (ssid_end_flag - ssid) / 2;
3174 for(i=0; i<ssid_len; i++)
3175 {
3176 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3177 }
3178 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3179 {
qs.xiongb5dab082023-10-13 14:43:41 +08003180 break;
3181 }
3182 }
3183 }
3184 }
3185
3186 }
3187
3188 if (org_index >= buff_len || NULL == p || network_len <= 0)
3189 {
3190
3191 if (buff != NULL)
3192 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3193 return -1;
3194 }
3195
3196 count = lynq_split(p, network_len, '\n', split_lines);
3197 ret = -1;
3198 for( index=0; index < count; index++ )
3199 {
3200 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003201 if(p != NULL)
3202 {
3203 p += 9;
3204 if(memcmp(p,"SAE",3) == 0)
3205 {
3206 curr_auth = 5;
3207 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3208 {
3209 curr_auth = 4;
3210 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3211 {
3212 curr_auth = 3;
3213 }else if(memcmp(p,"NONE",4) == 0 )
3214 {
3215 curr_auth = 0;
3216 }else{
3217 curr_auth = 1;
3218 }
qs.xiongb5dab082023-10-13 14:43:41 +08003219 if( curr_auth < 1 || curr_auth > 6)
3220 {
3221 ret = -1;
3222 }
3223 break;
3224 }
3225 }
3226 if( curr_auth == 0)
3227 {
3228 return 0;
3229 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3230 {
3231 for(index=0; index < count; index++)
3232 {
3233 /*get psw info*/
3234
3235 p = strstr(split_lines[index], "psk=");
3236 if (p != NULL)
3237 {
3238 p += 4;
3239 if (*p == '\"')
3240 {
3241 p++;
3242 }
3243 }
3244 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3245 {
3246 p += 9;
3247 if (*p == '\"')
3248 {
3249 p++;
3250 }
3251 }
3252 else
3253 {
3254 continue;
3255 }
3256
3257 if (*p == '\"')
3258 p++;
3259 strncpy(password, p, 64);
3260 p = password;
3261 while(password - p < 64 && *password != '\0')
3262 {
3263 if (*password == '\"')
3264 {
3265 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003266 ret = 0;
3267 break;
3268 }
3269 password++;
3270 }
3271 break;
3272 }
3273 break;
3274 }
3275 }
3276
3277 return ret;
3278}
3279
3280
3281
you.chen35020192022-05-06 11:30:57 +08003282int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3283
3284 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003285 int len, ret;
3286 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003287 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003288
qs.xiongb5dab082023-10-13 14:43:41 +08003289 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003290 CHECK_IDX(idx, CTRL_STA);
3291
qs.xiong9fbf74e2023-03-28 13:38:22 +08003292 if (NULL == password)
3293 {
3294 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003295 return -1;
3296 }
3297
you.chen35020192022-05-06 11:30:57 +08003298 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003299 if (NULL == fp)
3300 {
3301 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003302 return -1;
3303 }
3304
you.chen6d247052023-06-01 16:39:54 +08003305 fseek(fp, 0, SEEK_END);
3306 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003307 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003308
qs.xiongb5dab082023-10-13 14:43:41 +08003309 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003310 {
3311 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3312 return -1;
3313 }
3314
you.chen35020192022-05-06 11:30:57 +08003315 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003316 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003317 fclose(fp);
3318
qs.xiongb5dab082023-10-13 14:43:41 +08003319
3320 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3321
3322 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003323 {
qs.xiongb5dab082023-10-13 14:43:41 +08003324 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3325 free(info_buff);
3326 return 0;
you.chen35020192022-05-06 11:30:57 +08003327 }
qs.xiongb5dab082023-10-13 14:43:41 +08003328 else{
3329 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003330 return -1;
3331 }
3332
you.chen35020192022-05-06 11:30:57 +08003333}
3334
qs.xiongb5dab082023-10-13 14:43:41 +08003335
you.chen35020192022-05-06 11:30:57 +08003336static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3337{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003338 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003339
qs.xiong9fbf74e2023-03-28 13:38:22 +08003340 if (sta_ssid == NULL)
3341 {
3342 RLOGE("sta_ssid is null\n");
3343 return -1;
you.chen35020192022-05-06 11:30:57 +08003344 }
3345
qs.xiong9fbf74e2023-03-28 13:38:22 +08003346 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003347
3348 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3349
3350 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3351// DO_OK_FAIL_REQUEST(cmd_save_config);
3352
qs.xiong9fbf74e2023-03-28 13:38:22 +08003353 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003354
3355}
3356
you.chen35020192022-05-06 11:30:57 +08003357static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003358{
you.chen35020192022-05-06 11:30:57 +08003359 char lynq_disable_cmd[128]={0};
3360 char lynq_select_cmd[128]={0};
3361
3362 CHECK_WPA_CTRL(CTRL_STA);
3363
qs.xiong9fbf74e2023-03-28 13:38:22 +08003364 if (save != 0)
3365 {
you.chenc29444e2022-06-07 18:01:16 +08003366 if (start_flag != 0)
3367 {
3368 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3369 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3370 }
3371 else
3372 {
3373 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3374 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3375 }
you.chen35020192022-05-06 11:30:57 +08003376 DO_OK_FAIL_REQUEST(cmd_save_config);
3377 }
3378
qs.xiong9fbf74e2023-03-28 13:38:22 +08003379 if (start_flag == 0)
3380 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003381 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003382 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3383 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003384 else
3385 {
you.chen35020192022-05-06 11:30:57 +08003386 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3387 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3388 }
3389
3390 return 0;
3391}
3392
3393int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3394{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003395 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003396 CHECK_IDX(idx, CTRL_STA);
3397
you.chen6c2dd9c2022-05-16 17:55:28 +08003398 curr_status_info curr_state;
3399 ap_info_s ap_info;
3400 curr_state.ap = &ap_info;
3401 curr_state.state = NULL;
3402
qs.xiong9fbf74e2023-03-28 13:38:22 +08003403 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3404 {
you.chend2fef3f2023-02-13 10:50:35 +08003405 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003406 return 0;
3407 }
3408
3409 return -1;
you.chen35020192022-05-06 11:30:57 +08003410}
3411
3412int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3413{
qs.xiong5d716d22023-09-20 20:08:39 +08003414 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003415 scan_info_s *scan_list = NULL;
3416 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003417 int scan_len=0;
3418 int save_len=0;
3419 int best_index = -1;
3420 int best_scan_index = -1;
3421 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003422 int i, j, ret;
3423
3424 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003425
3426 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003427 if (info == NULL)
3428 {
you.chen35020192022-05-06 11:30:57 +08003429 return -1;
3430 }
3431
3432 curr_status_info curr_state;
3433 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003434 char status[64];
you.chen35020192022-05-06 11:30:57 +08003435
you.chen9ac66392022-08-06 17:01:16 +08003436 memset(&ap_info, 0, sizeof (ap_info));
3437 memset(status, 0, sizeof (status));
3438
3439 curr_state.ap = &ap_info;
3440 curr_state.state = status;
3441
qs.xiong9fbf74e2023-03-28 13:38:22 +08003442 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3443 {
you.chen35020192022-05-06 11:30:57 +08003444 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003445 if (strcmp(status, STATE_COMPLETED) == 0)
3446 {
3447 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003448 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003449 }
3450 else
3451 {
3452 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003453 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003454 }
you.chen593621d2023-04-27 17:52:44 +08003455 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003456 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003457 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003458 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003459 return 0;
3460 }
3461
you.chen9ac66392022-08-06 17:01:16 +08003462 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003463 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003464 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3465 {
you.chen9ac66392022-08-06 17:01:16 +08003466 if (NULL != scan_list)
3467 {
3468 free(scan_list);
3469 }
you.chen35020192022-05-06 11:30:57 +08003470 return -1;
3471 }
3472
qs.xiong9fbf74e2023-03-28 13:38:22 +08003473 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3474 {
you.chen9ac66392022-08-06 17:01:16 +08003475 if (NULL != scan_list)
3476 {
3477 free(scan_list);
3478 }
3479 if (NULL != save_list)
3480 {
3481 free(save_list);
3482 }
you.chen35020192022-05-06 11:30:57 +08003483 return -1;
3484 }
3485
qs.xiong9fbf74e2023-03-28 13:38:22 +08003486 for (i=0; i < save_len; i++)
3487 {
3488 for (j=0; j < scan_len; j++)
3489 {
you.chen35020192022-05-06 11:30:57 +08003490 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003491 && save_list[i].base_info.auth == scan_list[j].auth)
3492 {
3493 if (best_rssi == 0)
3494 {
you.chen9ac66392022-08-06 17:01:16 +08003495 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003496 best_rssi = scan_list[j].rssi;
3497 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003498 else if (best_rssi > scan_list[j].rssi)
3499 {
you.chen35020192022-05-06 11:30:57 +08003500 best_index = i;
3501 best_scan_index = j;
3502 best_rssi = scan_list[j].rssi;
3503 }
you.chend2fef3f2023-02-13 10:50:35 +08003504 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 +08003505 break;
3506 }
3507 }
3508 }
3509
qs.xiong9fbf74e2023-03-28 13:38:22 +08003510 if (best_index >= 0)
3511 {
you.chen35020192022-05-06 11:30:57 +08003512 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003513 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 +08003514 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003515 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003516 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003517 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003518 }
3519
you.chen9ac66392022-08-06 17:01:16 +08003520 if (NULL != scan_list)
3521 {
3522 free(scan_list);
3523 }
3524 if (NULL != save_list)
3525 {
3526 free(save_list);
3527 }
3528
qs.xiong5d716d22023-09-20 20:08:39 +08003529 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003530 return ret;
you.chen35020192022-05-06 11:30:57 +08003531}
3532
3533static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3534{
qs.xiongc8d92a62023-03-29 17:36:14 +08003535 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003536 char lynq_ket_mgmt_cmd[64]={0};
3537 char lynq_pairwise_cmd[64]={0};
3538 char lynq_psk_cmd[64]={0};
3539
3540 CHECK_WPA_CTRL(CTRL_STA);
3541
qs.xiong9fbf74e2023-03-28 13:38:22 +08003542 switch(auth)
3543 {
3544 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003545 {
3546 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003547
you.chen35020192022-05-06 11:30:57 +08003548 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003549// DO_OK_FAIL_REQUEST(cmd_save_config);
3550 break;
3551 }
3552 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003553 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003554 {
3555 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3556 {
you.chen35020192022-05-06 11:30:57 +08003557 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3558 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003559 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3560 {
you.chena6cd55a2022-05-08 12:20:18 +08003561 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003562 }
3563 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3564 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003565
you.chen35020192022-05-06 11:30:57 +08003566 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3567 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3568 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003569
qs.xiong9fbf74e2023-03-28 13:38:22 +08003570 if (password != NULL)
3571 {
you.chen35020192022-05-06 11:30:57 +08003572 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3573 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003574 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003575 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003576
you.chen35020192022-05-06 11:30:57 +08003577// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003578 break;
3579 }
3580 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3581 {
qs.xiong3e506812023-04-06 11:08:48 +08003582 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003583 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003584 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3585 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3586
qs.xiong3e506812023-04-06 11:08:48 +08003587 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003588 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3589 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3590 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3591
3592 break;
3593 }
3594 case LYNQ_WIFI_AUTH_WPA3_PSK:
3595 {
qs.xiong3e506812023-04-06 11:08:48 +08003596 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003597 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003598 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3599 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3600
qs.xiongb37f8c42023-09-13 21:21:58 +08003601 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003602 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3603 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3604 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3605
3606 break;
3607 }
3608 default:
3609 return -1;
you.chen35020192022-05-06 11:30:57 +08003610 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003611
qs.xiong9fbf74e2023-03-28 13:38:22 +08003612 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003613}
qs.xiong7a105ce2022-03-02 09:43:11 -05003614
you.chen35020192022-05-06 11:30:57 +08003615static int inner_get_curr_net_no(int interface) {
3616 curr_status_info curr_state;
3617 curr_state.ap = NULL;
3618 curr_state.state = NULL;
3619
qs.xiong9fbf74e2023-03-28 13:38:22 +08003620 if (0 != inner_get_status_info(interface, &curr_state))
3621 {
you.chen35020192022-05-06 11:30:57 +08003622 return -1;
3623 }
3624
3625 return curr_state.net_no;
3626}
3627
3628int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003629{
you.chen35020192022-05-06 11:30:57 +08003630 int net_no;
3631 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003632
you.chen35020192022-05-06 11:30:57 +08003633 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003634
qs.xiong9fbf74e2023-03-28 13:38:22 +08003635 if (net_no < 0)
3636 {
you.chen35020192022-05-06 11:30:57 +08003637 return -1;
3638 }
3639
3640 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003641}
3642
you.chenb95401e2023-05-12 19:39:06 +08003643int 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 -05003644{
you.chen35020192022-05-06 11:30:57 +08003645 int count, net_no, index;
3646 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003647 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003648 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003649 curr_status_info curr_state;
3650 ap_info_s ap_info;
3651 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003652
qs.xiong9fbf74e2023-03-28 13:38:22 +08003653 if (ssid == NULL || *ssid == '\0')
3654 {
3655 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003656 return -1;
3657 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003658
qs.xiong9fbf74e2023-03-28 13:38:22 +08003659 if (LYNQ_WIFI_AUTH_OPEN != auth)
3660 {
3661 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003662 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003663 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003664 return -1;
3665 }
3666 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003667
you.chen70f377f2023-04-14 18:17:09 +08003668
3669 pthread_mutex_lock(&s_global_check_mutex);
3670 if (s_sta_status != INNER_STA_STATUS_INIT)
3671 {
3672 s_sta_status = INNER_STA_STATUS_CANCEL;
3673 pthread_cond_signal(&s_global_check_cond);
3674 }
3675 pthread_mutex_unlock(&s_global_check_mutex);
3676
you.chen35020192022-05-06 11:30:57 +08003677 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003678 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003679
3680 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003681 memset(&ap_info, 0, sizeof (ap_info));
3682 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003683
you.chen70f377f2023-04-14 18:17:09 +08003684 curr_state.ap = &ap_info;
3685 curr_state.state = status;
3686
3687 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003688 {
you.chen70f377f2023-04-14 18:17:09 +08003689 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3690 {
3691 net_no = curr_state.net_no;
3692 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3693 && strcmp(ap_info.psw, psw) == 0)
3694 {
3695 RLOGD("already connected\n");
3696
3697 pthread_mutex_lock(&s_global_check_mutex);
3698 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003699 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003700 pthread_cond_signal(&s_global_check_cond);
3701 pthread_mutex_unlock(&s_global_check_mutex);
3702 return 0;
3703 }
you.chen35020192022-05-06 11:30:57 +08003704 }
3705 }
3706
you.chen70f377f2023-04-14 18:17:09 +08003707 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003708 {
you.chen70f377f2023-04-14 18:17:09 +08003709 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3710
3711 for (index=0; index < count; index++)
3712 {
3713 net_auth = -1;
3714 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3715 {
3716 net_no = net_no_list[index];
3717 break;
3718 }
you.chen35020192022-05-06 11:30:57 +08003719 }
3720
you.chen70f377f2023-04-14 18:17:09 +08003721 if (net_no < 0)
3722 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003723 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3724 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3725 {
3726 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3727 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3728 DO_OK_FAIL_REQUEST(rm_net_cmd);
3729 }
you.chen70f377f2023-04-14 18:17:09 +08003730 net_no = lynq_add_network(CTRL_STA);
3731 if (net_no == -1)
3732 {
3733 return -1;
3734 }
3735
3736 RLOGD("net no is %d\n", net_no);
3737 if (0 != inner_set_sta_ssid(net_no, ssid))
3738 {
3739 return -1;
3740 }
you.chen35020192022-05-06 11:30:57 +08003741 }
3742 }
3743
qs.xiong9fbf74e2023-03-28 13:38:22 +08003744 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3745 {
you.chen35020192022-05-06 11:30:57 +08003746 return -1;
3747 }
3748
you.chen70f377f2023-04-14 18:17:09 +08003749
3750 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003751 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003752 usleep(200*1000);
3753
qs.xiong09560402023-10-27 21:58:55 +08003754 pthread_mutex_lock(&s_global_check_mutex);
3755 lynq_sta_removeElement(net_no);
3756 pthread_mutex_unlock(&s_global_check_mutex);
3757
you.chen70f377f2023-04-14 18:17:09 +08003758 ret = inner_sta_start_stop(net_no, 1, 1);
3759
3760 pthread_mutex_lock(&s_global_check_mutex);
3761 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003762 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003763 strcpy(s_sta_current_connecting_ssid, ssid);
3764 struct timeval now;
3765 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003766 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003767 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3768 pthread_cond_signal(&s_global_check_cond);
3769 pthread_mutex_unlock(&s_global_check_mutex);
3770 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003771}
3772
you.chenb95401e2023-05-12 19:39:06 +08003773int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3774{
3775 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3776}
3777
you.chen35020192022-05-06 11:30:57 +08003778int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003779{
you.chen35020192022-05-06 11:30:57 +08003780 ap_info_s ap;
3781 curr_status_info curr_state;
3782 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003783
qs.xiong9fbf74e2023-03-28 13:38:22 +08003784 if (ssid == NULL || *ssid == '\0')
3785 {
3786 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003787 return -1;
3788 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003789
you.chen35020192022-05-06 11:30:57 +08003790 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003791
you.chen35020192022-05-06 11:30:57 +08003792 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003793 curr_state.state = NULL;
3794
qs.xiong9fbf74e2023-03-28 13:38:22 +08003795 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3796 {
you.chen35020192022-05-06 11:30:57 +08003797 return 0;
3798 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003799
qs.xiong9fbf74e2023-03-28 13:38:22 +08003800 if (strcmp(ap.ap_ssid, ssid) != 0)
3801 {
you.chen35020192022-05-06 11:30:57 +08003802 return 0;
3803 }
3804
you.chen70f377f2023-04-14 18:17:09 +08003805 pthread_mutex_lock(&s_global_check_mutex);
3806 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3807 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003808 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003809}
qs.xiong97fa59b2022-04-07 05:41:29 -04003810
qs.xiongc93bf2b2023-08-25 10:22:08 +08003811int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3812{
qs.xiong09560402023-10-27 21:58:55 +08003813 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003814 ap_info_s ap;
3815 curr_status_info curr_state;
3816 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003817 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003818
3819 if (ssid == NULL || *ssid == '\0')
3820 {
3821 RLOGE("input ssid is NULL\n");
3822 return -1;
3823 }
3824
3825 CHECK_IDX(idx, CTRL_STA);
3826
3827
3828 curr_state.ap = &ap;
3829 curr_state.state = NULL;
3830
3831 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3832 {
3833 return 0;
3834 }
3835
3836 if (strcmp(ap.ap_ssid, ssid) != 0)
3837 {
3838 return 0;
3839 }
3840
3841 pthread_mutex_lock(&s_global_check_mutex);
3842 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003843 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3844 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3845 {
3846 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);
3847 if( g_history_disconnect_net[i] == curr_state.net_no)
3848 {
3849 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3850 check_history_idx_flag = 1;
3851 break;
3852 }
3853 }
3854 if ( check_history_idx_flag == 0)
3855 {
3856 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__);
3857 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3858 g_history_disconnect_valid_num++;
3859 }
3860 RLOGD("%s %d",__func__,__LINE__);
3861 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003862 pthread_mutex_unlock(&s_global_check_mutex);
3863 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3864
3865}
3866
3867
you.chena6cd55a2022-05-08 12:20:18 +08003868int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3869{
qs.xiong1b5e3472023-11-27 17:42:20 +08003870 RLOGD("enter %s %d func",__func__,__LINE__);
qs.xiongad2f89d2023-01-18 13:17:41 +08003871 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3872 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003873
you.chen35020192022-05-06 11:30:57 +08003874 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003875 CHECK_WPA_CTRL(CTRL_STA);
3876
you.chenc9928582023-04-24 15:39:37 +08003877 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3878 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003879 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003880 RLOGE("lynq_wifi_sta_start excute script fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08003881 return -1;
3882 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003883
qs.xiongad2f89d2023-01-18 13:17:41 +08003884 system(lynq_enable_sta_cmd);
3885 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003886 pthread_mutex_lock(&s_global_check_mutex);
3887 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003888 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003889 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08003890 RLOGD("end %s %d func",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003891 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003892}
3893
you.chen6d247052023-06-01 16:39:54 +08003894static int inner_get_status_info_state (int interface, char *state) {
3895 curr_status_info curr_state;
3896 curr_state.ap = NULL;
3897 curr_state.state = state;
3898 return inner_get_status_info(interface, &curr_state);
3899}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003900
3901int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3902{
3903
qs.xiongb37f8c42023-09-13 21:21:58 +08003904 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3905 int tmp_open_idx[128];
3906 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003907
qs.xiongb37f8c42023-09-13 21:21:58 +08003908 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003909 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003910 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3911 pthread_mutex_unlock(&s_global_check_mutex);
3912 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003913 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003914 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003915 }
3916
qs.xiongb37f8c42023-09-13 21:21:58 +08003917 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003918 return 0;
3919}
3920
3921
you.chen35020192022-05-06 11:30:57 +08003922int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003923{
qs.xiongec8bbeb2023-11-20 15:51:45 +08003924 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003925 int i=0;
3926 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003927
you.chena6cd55a2022-05-08 12:20:18 +08003928 CHECK_IDX(idx, CTRL_STA);
3929 CHECK_WPA_CTRL(CTRL_STA);
3930
you.chen6d247052023-06-01 16:39:54 +08003931 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003932 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003933
3934 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3935 if (ret != 0)
3936 {
qs.xiong1b5e3472023-11-27 17:42:20 +08003937 RLOGE("lynq_wifi_sta_stop excute script fail %s %d",__func__,__LINE__);
you.chenc9928582023-04-24 15:39:37 +08003938 return -1;
3939 }
3940
you.chen6d247052023-06-01 16:39:54 +08003941 for (i=0; i < 30; i++) // to check if sta is realy stoped
3942 {
3943 if (inner_get_status_info_state(idx, state) != 0)
3944 {
3945 break;
3946 }
3947
3948 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3949 {
3950 break;
3951 }
qs.xiong1b5e3472023-11-27 17:42:20 +08003952 RLOGD("lynq_wifi_sta_stop curr state %s %s %d", state,__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003953 usleep(SLEEP_TIME_ON_IDLE);
3954 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003955 pthread_mutex_lock(&s_global_check_mutex);
3956 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3957 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08003958 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003959 return 0;
3960// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003961}
qs.xiongfcc914b2023-07-06 21:16:20 +08003962int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3963{
3964 char LYNQ_DISABLE_CMD[128]={0};
3965 CHECK_IDX(idx, CTRL_STA);
3966 CHECK_WPA_CTRL(CTRL_STA);
3967 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3968 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3969 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3970 return 0;
3971}
qs.xiong7a105ce2022-03-02 09:43:11 -05003972
you.chen35020192022-05-06 11:30:57 +08003973//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3974// int i, count;
3975// char *p;
3976// const char * FLAG_SSID = "ssid=";
3977// const char * FLAG_SBSID = "bssid=";
3978// const char * FLAG_KEY_MGMT = "key_mgmt=";
3979// const char * FLAG_FREQ = "freq=";
3980// char lynq_sta_cmd[MAX_CMD];
3981// char *split_lines[128] = {0};
3982
3983// CHECK_WPA_CTRL(CTRL_AP);
3984
3985// sprintf(lynq_sta_cmd, "STA %s", bssid);
3986
3987// DO_REQUEST(lynq_sta_cmd);
3988
3989// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3990
3991// for(i=0; i < count; i++) {
3992// p = strstr(split_lines[i], FLAG_SSID);
3993// if (p != NULL) {
3994// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3995// continue;
3996// }
3997// }
3998
3999// lynq_get_interface_ip(idx, ap->ap_ip);
4000// lynq_ap_password_set(idx, ap->psw);
4001
4002// return 0;
4003//}
4004
4005static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4006 curr_status_info curr_state;
4007 curr_state.ap = ap;
4008 curr_state.state = NULL;
4009 return inner_get_status_info(interface, &curr_state);
4010}
4011
4012int 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 -04004013{
qs.xiong5071c802023-09-06 14:04:15 +08004014 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004015 int index, line_count;
4016 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004017 const char *lynq_first_sta_cmd = "STA-FIRST";
4018 char lynq_next_sta_cmd[MAX_CMD];
4019 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004020 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004021
you.chen35020192022-05-06 11:30:57 +08004022 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004023
you.chen35020192022-05-06 11:30:57 +08004024 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004025
you.chenb95401e2023-05-12 19:39:06 +08004026 // ap_info_s * tmp_ap;
4027 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004028 if (ap == NULL || list == NULL || len == NULL)
4029 {
4030 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004031 return -1;
4032 }
4033
you.chenb95401e2023-05-12 19:39:06 +08004034 // ap = &tmp_ap;
4035 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004036 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004037 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004038
you.chenb95401e2023-05-12 19:39:06 +08004039 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004040 {
you.chenb95401e2023-05-12 19:39:06 +08004041 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004042 return -1;
4043 }
4044
4045 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4046 lynq_ap_password_get(idx, (*ap)->psw);
4047
you.chen35020192022-05-06 11:30:57 +08004048 DO_REQUEST(lynq_first_sta_cmd);
4049
4050 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004051 while (reply_len > 0)
4052 {
4053 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004054 {
you.chen35020192022-05-06 11:30:57 +08004055 break;
4056 }
4057 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4058 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4059 strcpy(bssid[index], split_lines[0]);
4060 index++;
4061 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4062 reply_len = MAX_RET;
4063 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004064 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 +08004065 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004066 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004067 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004068 break;
4069 }
4070 }
4071
4072 *len = index;
4073
4074 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004075 for (index=0; index < *len; index++)
4076 {
you.chend2fef3f2023-02-13 10:50:35 +08004077 dev_info = &(*list)[index];
4078 memset(dev_info, 0, sizeof(device_info_s));
4079 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4080 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4081 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4082 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004083 free(bssid[index]);
4084 }
qs.xiong5071c802023-09-06 14:04:15 +08004085 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004086 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004087}
4088
you.chen35020192022-05-06 11:30:57 +08004089int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004090{
you.chen35020192022-05-06 11:30:57 +08004091 int i, count, index, count_words;
4092 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4093 char *split_lines[128] = {0};
4094 char *split_words[128] = {0};
4095 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004096
qs.xiong9fbf74e2023-03-28 13:38:22 +08004097 if (list == NULL || len == NULL)
4098 {
you.chen35020192022-05-06 11:30:57 +08004099 return -1;
4100 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004101
you.chen9ac66392022-08-06 17:01:16 +08004102 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4103 {
4104 usleep(100 * 1000);
4105 }
4106
you.chen35020192022-05-06 11:30:57 +08004107 CHECK_IDX(idx, CTRL_STA);
4108
4109 CHECK_WPA_CTRL(CTRL_STA);
4110
4111 DO_REQUEST(lynq_scan_result_cmd);
4112
4113 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4114 *len = count - 1;
4115 *list = malloc(sizeof (scan_info_s) * *len);
4116
4117 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004118 for (index=0; index <count_words; index++)
4119 {
4120 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004121 }
4122
qs.xiong9fbf74e2023-03-28 13:38:22 +08004123 for(index = 1;index < count; index++)
4124 {
4125 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004126 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004127 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4128 if (count_words < 4)
4129 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004130 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004131 //bssid / frequency / signal level / flags / ssid
4132 p = (*list) + index - 1;
4133 strcpy(p->mac, split_words[0]);
4134 p->band = convert_band_from_freq(atoi(split_words[1]));
4135 p->rssi = -1 * atoi( split_words[2]);
4136 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004137 if (count_words == 4) // ssid hided
4138 {
4139 p->ssid[0] = '\0';
4140 }
4141 else
4142 {
4143 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4144 }
you.chen35020192022-05-06 11:30:57 +08004145 }
4146
4147 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004148}
qs.xiong97fa59b2022-04-07 05:41:29 -04004149
you.chen35020192022-05-06 11:30:57 +08004150int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4151{
4152 int count, net_no, index;
4153 int net_no_list[128];
4154 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004155
you.chen35020192022-05-06 11:30:57 +08004156 char lynq_remove_cmd[MAX_CMD];
4157
qs.xiong9fbf74e2023-03-28 13:38:22 +08004158 if (ssid == NULL || *ssid == '\0')
4159 {
4160 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004161 return -1;
4162 }
4163
4164 CHECK_IDX(idx, CTRL_STA);
4165
4166 CHECK_WPA_CTRL(CTRL_STA);
4167
4168 net_no = -1;
4169 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4170
qs.xiong9fbf74e2023-03-28 13:38:22 +08004171 for (index=0; index < count; index++)
4172 {
you.chen35020192022-05-06 11:30:57 +08004173 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004174 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4175 {
you.chen35020192022-05-06 11:30:57 +08004176 net_no = net_no_list[index];
4177 break;
4178 }
4179 }
4180
qs.xiong9fbf74e2023-03-28 13:38:22 +08004181 if (net_no < 0)
4182 {
you.chen35020192022-05-06 11:30:57 +08004183 return 0;
4184 }
4185
4186 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4187
4188 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004189
4190 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4191
4192 pthread_mutex_lock(&s_global_check_mutex);
4193 lynq_sta_removeElement(net_no);
4194 pthread_mutex_unlock(&s_global_check_mutex);
4195
4196 RLOGD("%s %d",__func__,__LINE__);
4197 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004198 DO_OK_FAIL_REQUEST(cmd_save_config);
4199
4200 return 0;
4201}
4202
4203int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004204{
you.chend2fef3f2023-02-13 10:50:35 +08004205 int count, index;
you.chen35020192022-05-06 11:30:57 +08004206 int net_no_list[128];
4207 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004208 RLOGD("enter lynq_get_sta_saved_ap api\n");
4209 if (list == NULL || len == NULL)
4210 {
4211 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004212 return -1;
4213 }
4214
4215 CHECK_IDX(idx, CTRL_STA);
4216
4217// CHECK_WPA_CTRL(CTRL_STA);
4218
4219 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004220 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004221
you.chen057aac42023-04-13 14:06:58 +08004222 if (count < 0)
4223 {
4224 RLOGE("list network fail");
4225 return count;
4226 }
4227 else if (count == 0)
4228 {
4229 *list = NULL;
4230 *len = 0;
4231 return 0;
4232 }
4233
you.chen35020192022-05-06 11:30:57 +08004234 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004235 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004236 *len = count;
4237
qs.xiong9fbf74e2023-03-28 13:38:22 +08004238 for (index=0; index < count; index++)
4239 {
you.chen35020192022-05-06 11:30:57 +08004240 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004241 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004242 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004243 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004244 {
you.chen35020192022-05-06 11:30:57 +08004245 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4246 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004247 else
you.chen057aac42023-04-13 14:06:58 +08004248 {
you.chen35020192022-05-06 11:30:57 +08004249 (*list)[index].base_info.band = -1;
4250 }
you.chen057aac42023-04-13 14:06:58 +08004251 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004252 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004253 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004254 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004255 return 0;
4256}
4257
4258int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4259{
you.chen0c9bee22023-10-25 13:03:14 +08004260 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004261 {
you.chen0c9bee22023-10-25 13:03:14 +08004262 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4263 g_sta_fake_scan_finish_flag = 1;
4264 return 0;
4265 }
4266 else if (g_sta_conncet_status_flag != 0)
4267 {
4268 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004269 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004270 }
you.chen0c9bee22023-10-25 13:03:14 +08004271
qs.xiongc8d92a62023-03-29 17:36:14 +08004272 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004273 const char *lynq_scan_cmd = "SCAN";
4274
4275 CHECK_IDX(idx, CTRL_STA);
4276
4277 CHECK_WPA_CTRL(CTRL_STA);
4278
you.chen0df3e7e2023-05-10 15:56:26 +08004279 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4280 {
4281 RLOGD("tmp clear scanlist");
4282 system(clean_last_re);
4283 }
you.chen9ac66392022-08-06 17:01:16 +08004284 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004285 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004286 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4287 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004288 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004289 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4290 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004291 g_sta_scan_finish_flag = 1;
4292 return -1;
4293 }
you.chen35020192022-05-06 11:30:57 +08004294
4295 return 0;
4296}
4297
4298int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004299 if (cb == NULL)
4300 {
4301 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004302 return -1;
4303 }
4304
you.chen6d247052023-06-01 16:39:54 +08004305 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004306 g_ap_callback_priv = priv;
4307 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004308 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004309
you.chen6d247052023-06-01 16:39:54 +08004310 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004311 if (g_ap_watcher_pid == 0 )
4312 {
4313 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4314 {
4315 g_ap_watcher_pid = 0;
4316 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4317 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4318 return -1;
4319 }
4320 }
4321
4322 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004323 RLOGD("creat APWatcherTheradProc succeed");
you.chene9d00032023-04-24 13:55:29 +08004324
you.chen35020192022-05-06 11:30:57 +08004325 return 0;
4326}
4327
4328int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004329 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004330 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004331 if (g_ap_callback_priv == priv)
4332 {
you.chen35020192022-05-06 11:30:57 +08004333 g_ap_callback_func = NULL;
4334 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004335 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004336 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004337 return 0;
4338 }
you.chen6d247052023-06-01 16:39:54 +08004339 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004340 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004341 return -1;
4342}
4343
4344int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004345 if (cb == NULL)
4346 {
4347 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004348 return -1;
4349 }
4350
you.chen6d247052023-06-01 16:39:54 +08004351 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004352 g_sta_callback_priv = priv;
4353 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004354 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004355
you.chen6d247052023-06-01 16:39:54 +08004356 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004357 if (g_sta_watcher_pid == 0 ) {
4358 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4359 {
4360 g_sta_watcher_pid = 0;
4361 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4362 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4363 return -1;
4364 }
4365 }
4366
4367 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004368 RLOGD("creat STAWatcherTheradProc succeed");
you.chen35020192022-05-06 11:30:57 +08004369 return 0;
4370}
4371
4372int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004373 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004374 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004375 if (g_sta_callback_priv == priv)
4376 {
you.chen35020192022-05-06 11:30:57 +08004377 g_sta_callback_func = NULL;
4378 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004379 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004380 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004381 return 0;
4382 }
you.chen6d247052023-06-01 16:39:54 +08004383 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004384 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004385 return -1;
4386}
4387
qs.xiongfcc914b2023-07-06 21:16:20 +08004388int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4389 if (cb == NULL)
4390 {
4391 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4392 return -1;
4393 }
4394 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4395 g_sta_auto_callback_priv = priv;
4396 g_sta_auto_callback_func = cb;
4397 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4398 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4399 if (g_sta_auto_watcher_pid == 0 ) {
4400 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4401 {
4402 g_sta_auto_watcher_pid = 0;
4403 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4404 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4405 return -1;
4406 }
4407 }
4408 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
qs.xiong1b5e3472023-11-27 17:42:20 +08004409 RLOGD("creat STAWatcherTheradProc succeed");
qs.xiongfcc914b2023-07-06 21:16:20 +08004410 return 0;
4411}
4412int lynq_unreg_sta_auto_event_callback(void * priv) {
4413 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4414 if (g_sta_auto_callback_priv == priv)
4415 {
4416 g_sta_auto_watcher_stop_flag = 1;
4417 if (g_sta_auto_watcher_pid != 0)
4418 {
4419 pthread_join(g_sta_auto_watcher_pid, NULL);
4420 }
4421 g_sta_auto_watcher_pid = 0;
4422 g_sta_auto_callback_func = NULL;
4423 g_sta_auto_callback_priv = NULL;
4424 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4425 return 0;
4426 }
4427 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4428 return -1;
4429}
you.chen35020192022-05-06 11:30:57 +08004430int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4431{
4432 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004433 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004434 CHECK_IDX(idx, CTRL_AP);
4435
qs.xiong9fbf74e2023-03-28 13:38:22 +08004436 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4437 {
you.chen35020192022-05-06 11:30:57 +08004438 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4439 return 0;
4440 }
4441
qs.xiong9fbf74e2023-03-28 13:38:22 +08004442 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4443 {
you.chen35020192022-05-06 11:30:57 +08004444 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4445 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004446 else
4447 {
you.chen35020192022-05-06 11:30:57 +08004448 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4449 }
4450
4451 return 0;
4452}
4453
4454int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4455 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004456 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004457 CHECK_IDX(idx, CTRL_STA);
4458
qs.xiong9fbf74e2023-03-28 13:38:22 +08004459 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4460 {
you.chen35020192022-05-06 11:30:57 +08004461 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4462 return 0;
4463 }
4464
qs.xiong9fbf74e2023-03-28 13:38:22 +08004465 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4466 {
you.chen35020192022-05-06 11:30:57 +08004467 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004469 else
4470 {
you.chen35020192022-05-06 11:30:57 +08004471 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4472 }
4473
4474 return 0;
4475}
4476
4477int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4478// CHECK_IDX(idx, CTRL_AP);
4479// int ret = 0;
4480// size_t reply_len = MAX_RET;
4481// char cmd_reply[MAX_RET]={0};
4482// const char * cmd_str = "GET country";
4483// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4484// do{
4485// if (NULL == s_lynq_wpa_ctrl) {
4486// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4487// if (NULL == s_lynq_wpa_ctrl ) {
4488// printf("wpa_ctrl_open fail\n");
4489// return -1;
4490// }
4491// }
4492// }while(0);
4493
4494// do {
4495// reply_len = MAX_RET;
4496// cmd_reply[0] = '\0';
4497// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004498// 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 +08004499// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004500// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004501// return ret;
4502// }
4503// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004504// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004505// }while(0);
4506
4507 FILE *fp;
4508 size_t i = 0;
4509 char lynq_cmd_ret[MAX_RET]={0};
4510
4511// CHECK_IDX(idx, CTRL_AP);
4512
4513 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004514 {
4515 perror("popen error!");
4516 return -1;
4517 }
you.chen35020192022-05-06 11:30:57 +08004518 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4519 {
4520 perror("fread fail!");
4521 return -1;
4522 }
4523
qs.xiong9fbf74e2023-03-28 13:38:22 +08004524 for(i=0; i < strlen(lynq_cmd_ret); i++)
4525 {
4526 if (lynq_cmd_ret[i] == ' ')
4527 {
you.chen35020192022-05-06 11:30:57 +08004528 lynq_cmd_ret[i] = '\0';
4529 break;
4530 }
4531 }
4532
4533 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004534 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004535
4536 int ret=pclose(fp);
4537 if(ret==-1)
4538 {
4539 perror("close file faild");
4540 }
4541
4542 return 0;
4543}
4544
qs.xiong44fac672023-08-29 16:15:55 +08004545
you.chen705a7ef2023-06-01 22:06:45 +08004546static int check_and_init_uci_config(char * country_code)
4547{
4548 FILE * fp;
4549 int is_different = 0;
4550 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4551 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4552 const char * commit_uci_cmd ="uci commit";
4553 char set_country_cmd[MAX_CMD];
4554 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004555
you.chen705a7ef2023-06-01 22:06:45 +08004556 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004557
you.chen705a7ef2023-06-01 22:06:45 +08004558 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004559 {
you.chen705a7ef2023-06-01 22:06:45 +08004560 if (0 != system(create_uci_cmd))
4561 {
4562 RLOGE("creat_uci_cmd fail");
4563 return -1;
4564 }
4565 is_different = 1;
4566 }
4567
4568 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4569 {
4570 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004571 return -1;
4572 }
4573
you.chen705a7ef2023-06-01 22:06:45 +08004574 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004575 {
you.chen705a7ef2023-06-01 22:06:45 +08004576 RLOGE("fread fail!");
4577 fclose(fp);
4578 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004579 }
4580
you.chen705a7ef2023-06-01 22:06:45 +08004581 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4582 {
qs.xiong44fac672023-08-29 16:15:55 +08004583 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 +08004584 is_different = 1;
4585 }
4586
4587 fclose(fp);
4588
4589 if (is_different)
4590 {
4591 if ( 0 != system(set_country_cmd))
4592 {
4593 RLOGE("set_country_cmd fail");
4594 return -1;
4595 }
4596 if (0 != system(commit_uci_cmd))
4597 {
4598 RLOGE("commmit fail");
4599 }
4600 }
4601
4602 return is_different;
4603}
4604
4605int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4606 char check_current_code[10];
4607 const char * support_country[] = {"CN", "EU"};
4608
4609 int ret,is_different, i, cc_count;
4610
4611 if (country_code == NULL || country_code[0] == '\0')
4612 {
4613 RLOGE("bad country code\n");
4614 return -1;
4615 }
4616
4617 cc_count = sizeof (support_country) / sizeof (char*);
4618 for(i=0; i < cc_count; i++)
4619 {
4620 if (strcmp(support_country[i], country_code) == 0)
4621 {
4622 break;
4623 }
4624 }
4625
4626 if (i >= cc_count)
4627 {
4628 RLOGE("unspported country code %s\n", country_code);
4629 return -1;
4630 }
4631
4632 is_different = check_and_init_uci_config(country_code);
4633 if( is_different < 0 )
4634 {
4635 RLOGE("init set uci fail\n");
4636 return -1;
4637 }
4638
4639 ret = lynq_get_country_code(idx,check_current_code);
4640 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4641 {
4642 ret = lynq_wifi_disable();
4643 if(ret != 0 )
4644 {
4645 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4646 return -1;
4647 }
4648 }
4649
4650 return 0;
you.chen35020192022-05-06 11:30:57 +08004651}
4652
4653int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4654{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004655 RLOGD("enter lynq_get_connect_ap_mac\n");
4656 if (mac == NULL)
4657 {
4658 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004659 return -1;
4660 }
4661
4662 CHECK_IDX(idx, CTRL_STA);
4663 ap_info_s ap;
4664 ap.ap_mac[0] = '\0';
4665
qs.xiong9fbf74e2023-03-28 13:38:22 +08004666 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4667 {
you.chen35020192022-05-06 11:30:57 +08004668 return -1;
4669 }
4670 strcpy(mac, ap.ap_mac);
4671
4672 return 0;
4673}
4674
4675int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4676{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004677 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004678 struct ifaddrs *ifaddr_header, *ifaddr;
4679 struct in_addr * ifa;
4680 const char * ifaName = "wlan0";
4681 if (ip == NULL)
4682 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004683 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004684 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004685 }
you.chenf58b3c92022-06-21 16:53:48 +08004686
qs.xiong9fbf74e2023-03-28 13:38:22 +08004687 if (idx == 1)
4688 {
you.chen0df3e7e2023-05-10 15:56:26 +08004689 ifaName = inner_get_ap_interface_name();
4690 if (ifaName == NULL)
4691 {
4692 RLOGE("[lynq_get_interface_ip] ap name get fail");
4693 return -1;
4694 }
you.chen9ac66392022-08-06 17:01:16 +08004695 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004696 else if (idx != 0)
4697 {
you.chen35020192022-05-06 11:30:57 +08004698 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004699 }
you.chen35020192022-05-06 11:30:57 +08004700
you.chen9ac66392022-08-06 17:01:16 +08004701 if (getifaddrs(&ifaddr_header) == -1)
4702 {
you.chen35020192022-05-06 11:30:57 +08004703 perror("getifaddrs");
4704 return -1;
4705 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004706 }
you.chen35020192022-05-06 11:30:57 +08004707
4708
you.chen9ac66392022-08-06 17:01:16 +08004709 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4710 {
4711 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004712 continue;
you.chen9ac66392022-08-06 17:01:16 +08004713 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4714 {
4715 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4716 {
4717 // is a valid IP4 Address
4718 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4719 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004720 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004721 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004722 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004723 return 0;
4724 }
4725 }
4726 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004727 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004728 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004729 return -1;
you.chen35020192022-05-06 11:30:57 +08004730}
4731
4732int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4733{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004734 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004735 int count;
4736 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004737 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004738 char *split_words[128] = {0};
4739 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4740
4741 CHECK_WPA_CTRL(idx);
4742
4743 DO_REQUEST(lynq_get_mac_cmd);
4744
qs.xiong9fbf74e2023-03-28 13:38:22 +08004745 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4746 {
4747 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004748 return -1;
4749 }
4750
4751 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4752
qs.xiong9fbf74e2023-03-28 13:38:22 +08004753 if (count < 2)
4754 {
you.chen35020192022-05-06 11:30:57 +08004755 return -1;
4756 }
4757
qs.xiong9fbf74e2023-03-28 13:38:22 +08004758 for (i=0; i < strlen(split_words[1]); i++ )
4759 {
4760 if (split_words[1][i] != ' ')
4761 {
you.chen35020192022-05-06 11:30:57 +08004762 break;
4763 }
4764 }
4765
qs.xiongdd6e44c2023-08-08 15:02:53 +08004766 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004767
4768 return 0;
4769}
4770
4771int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4772{
4773// int count;
4774// char *split_words[128] = {0};
4775// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4776
4777// if (rssi == NULL) {
4778// return -1;
4779// }
4780
4781// CHECK_IDX(idx, CTRL_STA);
4782
4783// CHECK_WPA_CTRL(CTRL_STA);
4784
4785// DO_REQUEST(lynq_get_rssi_cmd);
4786
4787// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4788// return -1;
4789// }
4790
4791// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4792
4793// if (count < 2) {
4794// return -1;
4795// }
4796
4797// *rssi = atoi(split_words[1]) * -1;
4798
you.chen35020192022-05-06 11:30:57 +08004799 char lynq_cmd_ret[MAX_RET]={0};
4800
qs.xiongff0ae0f2022-10-11 15:47:14 +08004801/*******change other cmd to get rssi*******
4802 *
4803 *wl rssi ---> wl -i wlan0 rssi
4804 *
4805 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004806 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004807 {
you.chen23c4a5f2023-04-12 16:46:00 +08004808 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004809 return -1;
4810 }
you.chen9f17e4d2022-06-06 17:18:18 +08004811 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004812/****** if got rssi is 0,means sta didn't connected any device****/
4813 if(*rssi == 0)
4814 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004815 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004816 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004817 }
you.chen35020192022-05-06 11:30:57 +08004818
4819 return 0;
4820}
4821
4822int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4823{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004824 RLOGD("enter lynq_get_connect_ap_band\n");
4825 if (band == NULL)
4826 {
you.chen35020192022-05-06 11:30:57 +08004827 return -1;
4828 }
4829
4830 CHECK_IDX(idx, CTRL_STA);
4831 ap_info_s ap;
4832 ap.band = -1;
4833
qs.xiong9fbf74e2023-03-28 13:38:22 +08004834 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4835 {
you.chen35020192022-05-06 11:30:57 +08004836 return -1;
4837 }
4838 *band = ap.band;
4839
4840 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004841}
you.chenf58b3c92022-06-21 16:53:48 +08004842
4843int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4844{
you.chenb95401e2023-05-12 19:39:06 +08004845 int ret;
4846 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004847 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004848
4849 if (ip == NULL)
4850 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004851 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004852 return -1;
4853 }
4854
4855 CHECK_IDX(idx, CTRL_STA);
4856
qs.xionge4cbf1c2023-02-28 18:22:49 +08004857 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004858 {
4859 return -1;
4860 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004861
you.chenb95401e2023-05-12 19:39:06 +08004862 ip[0] = '\0';
4863 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4864 if (ret != 0)
4865 {
4866 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
you.chenb95401e2023-05-12 19:39:06 +08004867 }
4868
4869 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4870 {
4871 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004872 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004873 if (ret != 0)
4874 {
4875 ip[0] = '\0';
4876 return 0;
4877 }
4878 else
4879 {
4880 p = strchr(ip, '\n');
4881 if (p != NULL)
4882 {
4883 *p = '\0';
4884 }
4885 }
4886 }
4887 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004888}
4889
qs.xionge02a5252023-09-20 14:00:21 +08004890int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4891{
4892 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4893 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4894}
4895
qs.xiong026c5c72022-10-17 11:15:45 +08004896int lynq_ap_connect_num(int sta_number)
4897{
4898 char lynq_limit_cmd[32]={0};
4899 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004900 if((sta_number < 1 ) && (sta_number > 15))
4901 {
4902 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004903 return -1;
4904 }
4905 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4906 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004907 if(ret != 0)
4908 {
4909 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004910 }
4911 return 0;
4912}
you.chenf58b3c92022-06-21 16:53:48 +08004913
qs.xiong77905552022-10-17 11:19:57 +08004914int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4915{
4916
4917 char lynq_wifi_acs_cmd[128]={0};
4918 char lynq_cmd_mode[128]={0};
4919 char lynq_cmd_slect[128]={0};
4920
qs.xiong9fbf74e2023-03-28 13:38:22 +08004921 if((acs_mode != 2) && (acs_mode != 5))
4922 {
qs.xiong77905552022-10-17 11:19:57 +08004923 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4924 }
4925
qs.xiong9fbf74e2023-03-28 13:38:22 +08004926 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4927 {
qs.xiong77905552022-10-17 11:19:57 +08004928 return -1;
4929 }
4930
4931 CHECK_IDX(idx, CTRL_AP);
4932
4933 CHECK_WPA_CTRL(CTRL_AP);
4934
4935 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4936 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4937 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4938
4939 DO_OK_FAIL_REQUEST(cmd_disconnect);
4940 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4941 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4942 DO_OK_FAIL_REQUEST(cmd_save_config);
4943 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4944
4945 return 0;
4946}
you.chen0f5c6432022-11-07 18:31:14 +08004947//you.chen add for tv-box start
4948static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4949 FILE *fp;
4950 //printf("to exec cmd:%s\n", str_cmd);
4951 if((fp=popen(str_cmd,"r"))==NULL)
4952 {
4953 perror("popen error!");
4954 return -1;
4955 }
4956 if((fread(str_cmd_ret,max_len,1,fp))<0)
4957 {
4958 perror("fread fail!");
4959 fclose(fp);
4960 return -1;
4961 }
4962 fclose(fp);
4963 return 0;
4964}
4965
4966static int get_netmask_length(const char* mask)
4967{
4968 int masklen=0, i=0;
4969 int netmask=0;
4970
4971 if(mask == NULL)
4972 {
4973 return 0;
4974 }
4975
4976 struct in_addr ip_addr;
4977 if( inet_aton(mask, &ip_addr) )
4978 {
4979 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004980 }else
4981 {
you.chen0f5c6432022-11-07 18:31:14 +08004982 netmask = 0;
4983 return 0;
4984 }
4985
4986 while(0 == (netmask & 0x01) && i<32)
4987 {
4988 i++;
4989 netmask = netmask>>1;
4990 }
4991 masklen = 32-i;
4992 return masklen;
4993}
4994
4995static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4996 int mask_len;
4997 char *p;
4998 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004999 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5000 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005001 return -1;
5002 p = strstr(str_cmd_ret, "Mask:");
5003 if (p == NULL)
5004 return -1;
5005 mask_len = get_netmask_length(p + 5);
5006 if (mask_len == 0)
5007 return -1;
5008 p = strstr(str_cmd_ret, "inet addr:");
5009 if (p == NULL)
5010 return -1;
5011 strcpy(tmp, p + 10);
5012 p = strstr(tmp, " ");
5013 if (p != NULL)
5014 *p = '\0';
5015 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5016 return 0;
5017}
5018
5019static void GBWWatchThreadProc() {
5020 int i,n, nloop, nmax, ncheckcount, nidlecount;
5021 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5022 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5023 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5024 char *results[16] = {0};
5025 char str_cmd[256] = {0};
5026 char str_cmd_ret[128] = {0};
5027 char dest_ip[32] = {0};
5028 lastAP1Bytes = lastAP2Bytes = 0;
5029 lastAP1Drop = lastAP2Drop = 0;
5030 lastAP1Speed = lastAP2Speed = 0;
5031 setAP1Speed = 50;
5032 setAP2Speed = 80;
5033 nloop = 0;
5034 nmax = 6;
5035 ncheckcount = nidlecount = 0;
5036
you.chen0df3e7e2023-05-10 15:56:26 +08005037 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005038 {
5039 RLOGE("------gbw thread run\n");
5040 return;
5041 }
5042
qs.xiong9fbf74e2023-03-28 13:38:22 +08005043 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005044 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5045 while (dest_ip[0] == '\0') {
5046 sleep(1);
5047 str_cmd_ret[0] = '\0';
5048 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5049 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5050 if (str_cmd_ret[n] == '\n'){
5051 str_cmd_ret[n] = '\0';
5052 break;
5053 }
5054 }
5055 if (str_cmd_ret[0] != '\0')
5056 {
5057 strcpy(dest_ip, str_cmd_ret);
5058 }
5059 }
5060
you.chenc9928582023-04-24 15:39:37 +08005061 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5062 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5063 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 +08005064 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5065 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005066 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005067 return;
5068 }
you.chenc9928582023-04-24 15:39:37 +08005069 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);
5070 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);
5071 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 +08005072
5073 while (1) {
5074 sleep(1);
5075 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005076 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5077 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5078 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005079 continue;
5080 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005081 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005082 if (n > 9) {
5083 if (strcmp(results[1], "Sent") == 0) {
5084 currAP1Bytes = atoll(results[2]);
5085 }
5086 if (strcmp(results[6], "(dropped") == 0) {
5087 currAP1Drop = atoi(results[7]);
5088 }
5089 }
5090
5091 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005092 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5093 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5094 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005095 continue;
5096 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005097 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005098 if (n > 9) {
5099 if (strcmp(results[1], "Sent") == 0) {
5100 currAP2Bytes = atoll(results[2]);
5101 }
5102 if (strcmp(results[6], "(dropped") == 0) {
5103 currAP2Drop = atoi(results[7]);
5104 }
5105 }
5106
5107 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5108 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5109 lastAP1Bytes = currAP1Bytes;
5110 lastAP2Bytes = currAP2Bytes;
5111 continue;
5112 }
5113
5114 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5115 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5116 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5117 lastAP1Speed = currAP1Speed;
5118 lastAP2Speed = currAP2Speed;
5119 lastAP1Bytes = currAP1Bytes;
5120 lastAP2Bytes = currAP2Bytes;
5121
5122 currSetAP1Speed = setAP1Speed;
5123 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5124 ncheckcount++;
5125 if (ncheckcount > 3) {
5126 ncheckcount = 0;
5127 currSetAP1Speed = 5;
5128 }
5129 }
5130 else {
5131 ncheckcount = 0;
5132 if (currAP1Speed < 5)
5133 nidlecount++;
5134 else
5135 nidlecount = 0;
5136
5137 }
5138
5139 if (nidlecount > 60 ){
5140 currSetAP1Speed = 50;
5141 }
5142
5143 if (currSetAP1Speed != setAP1Speed) {
5144 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005145 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5146 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005147 }
5148 }
5149}
5150
5151int enableGBW(const char* mac) {
5152 int i,len;
5153 char get_ipaddr_cmd[128]={0};
5154 ap_info_s *ap;
5155 device_info_s * list;
5156
5157 if (mac == NULL || g_gbw_enabled == 1)
5158 return -1;
5159 len = strlen(mac);
5160 g_gbw_mac = malloc(len + 1);
5161 for(i=0;i<len;i++) {
5162 if (mac[i] >= 'A' && mac[i] <= 'Z')
5163 {
5164 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5165 }
5166 else
5167 g_gbw_mac[i] = mac[i];
5168 }
5169 g_gbw_mac[i] = '\0';
5170 g_gbw_enabled = 1;
5171
5172 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5173 if (system(get_ipaddr_cmd) == 0) {
5174 //startGBW();
5175 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5176 for (i=0;i<len;i++) {
5177 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5178 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5179 startGBW();
5180 }
5181 free(ap);
5182 free(list);
5183 }
5184 }
5185 return 0;
5186}
5187
5188int disableGBW() {
5189 stopGBW();
5190 free(g_gbw_mac);
5191 g_gbw_mac = NULL;
5192 g_gbw_enabled = 1;
5193 return 0;
5194}
5195
5196static int startGBW() {
5197 if (g_gbw_watcher_pid != 0) {
5198 stopGBW();
5199 }
5200 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5201}
5202
5203static int stopGBW() {
5204 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005205 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005206 pthread_cancel(g_gbw_watcher_pid);
5207 pthread_join(g_gbw_watcher_pid, &retval);
5208 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005209 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5210 if (s_ap_iterface_name[0] != '\0')
5211 {
5212 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5213 system(cmd);
5214 }
you.chen0f5c6432022-11-07 18:31:14 +08005215}
5216//you.chen add for tv-box end