blob: 70d66cbf5c0f319a75a8251a05df2d8723dc7305 [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.chen35020192022-05-06 11:30:57 +080057
qs.xiongfcc914b2023-07-06 21:16:20 +080058pthread_t g_sta_auto_watcher_pid = 0;
59volatile int g_sta_auto_watcher_stop_flag = 0;
60volatile int g_sta_auto_scan_finish_flag = 1;
61volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080062void * g_ap_callback_priv = NULL;
63AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
64void * g_sta_callback_priv = NULL;
65STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080066void * g_sta_auto_callback_priv = NULL;
67STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080068
69//const char * CTRL_PATH="/var/run/wpa_supplicant";
70const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
71//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
72const char * cmd_list_networks = "LIST_NETWORKS";
73const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080074const char * cmd_disconnect = "DISCONNECT";
75const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080076const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080077const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080078const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080079const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080080
you.chenf711c8a2023-04-13 13:49:45 +080081const char * cmd_ping = "PING";
82const char * rsp_pong = "PONG";
83const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
84const int MAX_IDLE_COUNT = 600; // 60s
85
you.chenc9928582023-04-24 15:39:37 +080086const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
87const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
88const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
89const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
90const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
91
92static char s_ap_iterface_name[64] = {0};
93
you.chend2fef3f2023-02-13 10:50:35 +080094struct local_wpa_ctrl{
95 struct wpa_ctrl *ctrl;
96 pthread_mutex_t mutex;
97};
98
qs.xiong09560402023-10-27 21:58:55 +080099volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800100int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800101
you.chend2fef3f2023-02-13 10:50:35 +0800102static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800103static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
104static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800105// add for auto connect
106static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800107
108static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800109
you.chen0f5c6432022-11-07 18:31:14 +0800110//you.chen add for tv-box start
111volatile int g_gbw_enabled = 0;
112char * g_gbw_mac = NULL;
113pthread_t g_gbw_watcher_pid = 0;
114static int startGBW();
115static int stopGBW();
116//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800117
118typedef struct __curr_status_info {
119 ap_info_s *ap;
120 char * state;
121 int net_no;
122}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400123
you.chen70f377f2023-04-14 18:17:09 +0800124typedef enum {
125 INNER_STA_STATUS_INIT = 0,
126 INNER_STA_STATUS_CONNECTING,
127 INNER_STA_STATUS_ASSOCIATING,
128 INNER_STA_STATUS_ASSOCIATED,
129 INNER_STA_STATUS_CONNECTED,
130 INNER_STA_STATUS_DISCONNECTING,
131 INNER_STA_STATUS_DISCONNECTED,
132 INNER_STA_STATUS_CANCEL,
133}inner_sta_status_s;
134
135static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
136static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800137volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800138static error_number_s s_sta_error_number = -1;
139static char s_sta_current_connecting_ssid[64] = {0};
140static struct timespec s_sta_connect_timeout;
141const int MAX_CONNNECT_TIME = 15; // second
142pthread_t g_global_watcher_pid = 0;
143static int s_service_invoke_timeout_cnt=0;
144const int FAKE_MAX_INT_VALUE = 99999;
145
qs.xiong09560402023-10-27 21:58:55 +0800146static void print_disconnect_list()
147{
148 int i;
149 for( i = 0; i < g_history_disconnect_valid_num; i++ )
150 {
151 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__);
152 }
153
154 return;
155}
156
157// idex ----> history_disconnect_list[x] index
158static int removeElement(int idex)
159{
160 RLOGD("into removeElement");
161 if( index < 0 )
162 {
163 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
164 return -1;
165 }
166 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]);
167 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
168 g_history_disconnect_valid_num --;
169 RLOGD("end removeElement");
170 return 0;
171}
172static int check_history_disconenct_ap_list(int val)
173{
174 print_disconnect_list();
175 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__);
176 int i;
177 for( i = 0; i < g_history_disconnect_valid_num; i++)
178 {
179 if( val == g_history_disconnect_net[i] )
180 {
181 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
182 RLOGD("end check_history_disconenct_ap_list && return network index");
183 return i;
184 }
185 }
186 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__);
187 return -1;
188}
189
190
191static void lynq_sta_removeElement(int net_no)
192{
193 int ret;
194
195 ret = check_history_disconenct_ap_list(net_no);
196 if( ret == -1 )
197 {
198 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
199 return;
200 }else
201 {
202 ret = removeElement(ret);
203 if( ret == 0 )
204 {
205 RLOGD("removeElement pass %s %d",__func__,__LINE__);
206 return;
207 }
208 }
209
210 return;
211}
212
you.chen70f377f2023-04-14 18:17:09 +0800213static void notify_service_invoke_fail(int error)
214{
215 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
216 pthread_mutex_lock(&s_global_check_mutex);
217 if (error == -2) //timeout
218 {
219 s_service_invoke_timeout_cnt++;
220 if (s_service_invoke_timeout_cnt > 10)
221 {
222 pthread_cond_signal(&s_global_check_cond);
223 }
224 }
225 else if (error == -1)
226 {
227 // check if can connect wpa service
228 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
229 if (lynq_wpa_ctrl == NULL)
230 {
231 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
232 pthread_cond_signal(&s_global_check_cond);
233 }
234 wpa_ctrl_close(lynq_wpa_ctrl);
235 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
236 if (lynq_wpa_ctrl == NULL)
237 {
238 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
239 pthread_cond_signal(&s_global_check_cond);
240 }
241 wpa_ctrl_close(lynq_wpa_ctrl);
242 }
243
244 pthread_mutex_unlock(&s_global_check_mutex);
245}
246
you.chenc9928582023-04-24 15:39:37 +0800247static int system_call_v(const char * fmt, ...)
248{
249 char str_cmd[256] = {0};
250 va_list args;
251 va_start(args, fmt);
252 vsprintf(str_cmd, fmt, args);
253 va_end(args);
254 printf("system call----------%s\n", str_cmd);
255 return system(str_cmd);
256}
257
you.chen0df3e7e2023-05-10 15:56:26 +0800258static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
259
260static const char * inner_get_ap_interface_name()
261{
262 char * p;
263 char cmd[128]={0};
264
265 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
266 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
267 {
268 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
269 return NULL;
270 }
271 p = strchr(s_ap_iterface_name, ' ');
272 if (NULL != p)
273 {
274 *p = '\0';
275 }
276 p = strchr(s_ap_iterface_name, '\n');
277 if (NULL != p)
278 {
279 *p = '\0';
280 }
281 if (s_ap_iterface_name[0] == '\0')
282 {
283 return NULL;
284 }
285
286 return s_ap_iterface_name;
287}
288
you.chen70f377f2023-04-14 18:17:09 +0800289static void check_tether_and_notify()
290{
291 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800292 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 +0800293 {
294 return;
295 }
296 pthread_mutex_lock(&s_global_check_mutex);
297 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
298 pthread_cond_signal(&s_global_check_cond);
299 pthread_mutex_unlock(&s_global_check_mutex);
300}
301
you.chend2fef3f2023-02-13 10:50:35 +0800302static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
303 char *reply, size_t *reply_len,
304 void (*msg_cb)(char *msg, size_t len))
305{
306 int ret;
307 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800308 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800309 return -1;
310 }
311 pthread_mutex_lock(&ctrl->mutex);
312 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
313 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800314 if (ret != 0)
315 {
316 notify_service_invoke_fail(ret);
317 }
you.chend2fef3f2023-02-13 10:50:35 +0800318 return ret;
319}
320
321static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
322 int repeat_cnt;
323 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
324 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800325 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800326 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
327 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
328// printf("wait enable finish\n");
329 usleep(500 * 1000);
330 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
331 }
332 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800333 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800334 goto out_addr;
335 }
336 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
337 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
338 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800339 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800340 goto out_addr;
341 }
342 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
343 }
344 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
345out_addr:
346 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
347 return lynq_wpa_ctrl;
348}
349
qs.xiong97fa59b2022-04-07 05:41:29 -0400350#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400351{\
you.chen35020192022-05-06 11:30:57 +0800352 perror((str));\
353 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400354}
355
you.chen35020192022-05-06 11:30:57 +0800356#define CHECK_IDX(idx, type) do { \
357 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
358 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800359 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800360 return -1; \
361 } \
362 }while (0)
363
364#define CHECK_WPA_CTRL(index) int ret = 0;\
365 size_t reply_len = MAX_RET; \
366 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800367 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800368 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800369 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
370 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800371 }while(0)
372
373#define DO_REQUEST(cmd_str) do { \
374 reply_len = MAX_RET;\
375 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800376 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800377 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 +0800378 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800379 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800380 return ret; \
381 } \
382 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800383 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800384 }while(0)
385
386#define DO_OK_FAIL_REQUEST(cmd_str) do { \
387 DO_REQUEST(cmd_str); \
388 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800389 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800390 return -1; \
391 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800392 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800393 return -1; \
394 } \
395 }while (0)
396
397
you.chenf711c8a2023-04-13 13:49:45 +0800398static int check_connection(struct wpa_ctrl * wpa_ctrl)
399{
400 size_t reply_len = MAX_RET;
401 char cmd_reply[MAX_RET]={0};
402 int ret;
403
404 RLOGD("check_connection [%p]", wpa_ctrl);
405 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
406
407 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
408 {
409 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 +0800410 if (ret != 0)
411 {
412 notify_service_invoke_fail(ret);
413 }
you.chenf711c8a2023-04-13 13:49:45 +0800414 return -1;
415 }
416
417 return 0;
418}
419
420/**
421 * @brief check_pending_msg
422 * @param lynq_wpa_ctrl
423 * @return 1 has msg, 0 no msg, -1 error
424 */
425static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
426{
427 int ret;
428
429 if (*pp_lynq_wpa_ctrl == NULL) // need connect
430 {
431 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
432 if (*pp_lynq_wpa_ctrl == NULL)
433 {
434 usleep(SLEEP_TIME_ON_IDLE);
435 return -1;
436 }
437
438 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
439 if (ret == 0) // attach success
440 {
441 *started_flag = 1;
442 }
443 else
444 {
you.chen70f377f2023-04-14 18:17:09 +0800445 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800446 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
447 *pp_lynq_wpa_ctrl = NULL;
448 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800449 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800450 usleep(SLEEP_TIME_ON_IDLE);
451 return -1;
452 }
453 }
454
455 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
456 if ( ret == 0) // no pending messages
457 {
458 usleep(SLEEP_TIME_ON_IDLE);
459 *idle_count += 1;
460 if (*idle_count > MAX_IDLE_COUNT)
461 {
462 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
463 {
464 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
465 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
466 *pp_lynq_wpa_ctrl = NULL;
467 *idle_count = 0;
468 return -1;
469 }
470 *idle_count = 0;
471 }
472 return 0;
473 }
474 else if ( ret == -1) // on error
475 {
476 RLOGE("[wifi error]sta wpa_ctrl_pending");
477 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
478 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
479 *pp_lynq_wpa_ctrl = NULL;
480 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800481 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800482 return -1;
483 }
484
485 *idle_count = 0;
486 return 1;
487}
488
you.chen6d247052023-06-01 16:39:54 +0800489static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
490{
491 pthread_mutex_lock(&s_ap_callback_mutex);
492 if (g_ap_callback_func != NULL)
493 g_ap_callback_func(g_ap_callback_priv, status);
494 pthread_mutex_unlock(&s_ap_callback_mutex);
495}
496
you.chen35020192022-05-06 11:30:57 +0800497static void APWatcherThreadProc() {
498 size_t len = MAX_RET;
499 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800500 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800501
you.chen6c2dd9c2022-05-16 17:55:28 +0800502 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800503 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800504
qs.xiong9fbf74e2023-03-28 13:38:22 +0800505 while (g_ap_watcher_stop_flag == 0)
506 {
you.chenf711c8a2023-04-13 13:49:45 +0800507 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
508 {
you.chen70f377f2023-04-14 18:17:09 +0800509 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
510 {
511 check_tether_and_notify();
512 }
513
you.chen35020192022-05-06 11:30:57 +0800514 continue;
515 }
you.chenf711c8a2023-04-13 13:49:45 +0800516
you.chen6c2dd9c2022-05-16 17:55:28 +0800517 memset(msg_notify, 0, MAX_RET);
518 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800519 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800520 {
you.chen35020192022-05-06 11:30:57 +0800521 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800522 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800523 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800524 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800525 {
you.chen6d247052023-06-01 16:39:54 +0800526 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800527 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800528 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800529 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
530 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800531 {
you.chen0f5c6432022-11-07 18:31:14 +0800532 stopGBW();
533 }
534 }
you.chen35020192022-05-06 11:30:57 +0800535 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800536 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800537 {
you.chen6d247052023-06-01 16:39:54 +0800538 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800539 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800540 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800541 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
542 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800543 {
you.chen0f5c6432022-11-07 18:31:14 +0800544 startGBW();
545 }
546 }
you.chen35020192022-05-06 11:30:57 +0800547 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800548 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800549 {
550 RLOGD("APWatcherThreadProc ap------> service error");
551 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
552 }
553 else
554 {
555 RLOGD("APWatcherThreadProc ap------> going on check next msg");
556 }
you.chenf711c8a2023-04-13 13:49:45 +0800557 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800558 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
559 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800560 if (lynq_wpa_ctrl != NULL)
561 {
you.chen92fd5d32022-05-25 10:09:47 +0800562 wpa_ctrl_detach(lynq_wpa_ctrl);
563 wpa_ctrl_close(lynq_wpa_ctrl);
564 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400565}
566
you.chen70f377f2023-04-14 18:17:09 +0800567static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
568{
569 char * p;
570 const char * try_associat_flag = "Trying to associate";
571 const char * associated_flag = "Associated with ";
572
573 pthread_mutex_lock(&s_global_check_mutex);
574 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
575 {
576 pthread_mutex_unlock(&s_global_check_mutex);
577 return;
578 }
579
you.chen6e724162023-10-19 19:10:01 +0800580 // youchen@2023-10-17 add for "not notify connect fail directly" begin
581 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
582 {
583 s_sta_error_number = error_num;
584 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
585 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
586 pthread_cond_signal(&s_global_check_cond);
587 pthread_mutex_unlock(&s_global_check_mutex);
588 return;
589 }
590 // youchen@2023-10-17 add for "not notify connect fail directly" end
591
you.chen70f377f2023-04-14 18:17:09 +0800592 if (state == LYNQ_WIFI_STATUS_EGNORE)
593 {
594 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
595 {
596 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
597 }
598 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
599 {
600 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
601 }
602 }
603 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
604 {
605 s_sta_error_number = error_num;
606 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
607 {
608 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800609 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 +0800610 pthread_cond_signal(&s_global_check_cond);
611 }
612 }
613 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
614 {
615 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800616 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 +0800617 pthread_cond_signal(&s_global_check_cond);
618 }
619 pthread_mutex_unlock(&s_global_check_mutex);
620}
621
qs.xiongb37f8c42023-09-13 21:21:58 +0800622static int lynq_split(char * str, int len, char delimiter, char * results[]);
623static inline char inner_convert_char(char in);
624static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
625static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
626
627
628
qs.xiong44fac672023-08-29 16:15:55 +0800629/*
630just tmp add for fix sta connect ap fail check ap connect info
631return 0 --->Current no sta device connect this AP
632*/
633static int lynq_connected_ap_sta_status() {
634
635 FILE *fp;
636 size_t i = 0;
637 int ret;
638 char lynq_cmd_ret[MAX_RET]={0};
639
640 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
641 {
642 perror("popen error!");
643 return -1;
644 }
645 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
646 {
647 perror("fread fail!");
648 ret=pclose(fp);
649 if(ret == -1)
650 perror("close file faild");
651 return -1;
652 }
653 if( strlen(lynq_cmd_ret) < MAC_LEN)
654 {
655 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
656 ret=pclose(fp);
657 if(ret==-1)
658 {
659 perror("close file faild");
660 }
661 return 0;
662 }else{
663 ret=pclose(fp);
664 if(ret==-1)
665 {
666 perror("close file faild");
667 }
668 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
669 return 1;
670 }
671}
672
673/*
674 just tmp add for fix sta connect ap fail; check fw status
675 return 1 ----> fw status error; need wl down/up
676*/
677static int check_current_fw_status() {
678
679 FILE *fp;
680 FILE *fp1;
681 size_t i = 0;
682 int ret;
683 char lynq_cmd_ret_2g[MAX_RET]={0};
684 char lynq_cmd_ret_5g[MAX_RET]={0};
685
686 const char * fw_status = "0x0096"; //0x0096 is normal fw status
687
688 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
689 {
690 perror("popen error!");
691 return -1;
692 }
693 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
694 {
695 perror("fread fail!");
696 if(pclose(fp) == -1)
697 perror("close fp file faild");
698 return -1;
699 }
700
701 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
702 {
703 perror("popen error!");
704 if(pclose(fp) == -1)
705 perror("clsoe fp file faild");
706 return -1;
707 }
708 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
709 {
710 perror("fread fail!");
711 if(pclose(fp1) == -1)
712 perror("clsoe fp1 file faild");
713 if(pclose(fp) == -1)
714 perror("clsoe fp file faild");
715 return -1;
716 }
717
718 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
719 {
720 ret=pclose(fp);
721 if(ret==-1)
722 {
723 perror("close fp file faild");
724 }
725 ret=pclose(fp1);
726 if(ret==-1)
727 {
728 perror("close fp1 file faild");
729 }
730 return 0;
731 }else
732 {
733 ret=pclose(fp);
734 if(ret==-1)
735 {
736 perror("close file faild");
737 }
738 if(pclose(fp1) == -1)
739 {
740 perror("clsoe file fp1 faild");
741 }
742 RLOGD("current fw status --error--");
743 return 1;
744 }
745}
746
qs.xiong1d4263a2023-09-06 10:46:23 +0800747/*
748eg: wl counters info
749sh-3.2# wl counters
750counters_version 30
751datalen 1648
752Slice_index: 0
753reinitreason_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)
754reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
755
756check reinit status
757return 0 ===> fw did wl reinit cmd
758*/
759static int check_current_reinit_info()
760{
761 FILE *fp;
762 int ret;
763 char lynq_cmd_ret[MAX_RET]={0};
764 char * dest;
765 char destid[3]={0};
766 if((fp=popen("wl counters","r"))==NULL)
767 {
768 perror("popen error!");
769 return -1;
770 }
771 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
772 {
773 perror("fread fail!");
774 if(pclose(fp) == -1)
775 {
776 perror("close fp file faild");
777 }
778 return -1;
779 }
780 dest = strstr(lynq_cmd_ret,"reinit ");
781 if(dest != NULL)
782 {
783 dest +=strlen("reinit ");
784 RLOGD("current get dest str is %s",dest);
785 memcpy(destid,dest,2);
786 ret = atoi(destid);
787 RLOGD("get current wl counters cmd return counts is %d",ret);
788 if( ret != 0 )
789 {
790 RLOGD("current fw did run cmd wl reinit");
791 if( pclose(fp) == -1 )
792 {
793 perror("close fp file faild");
794 }
795 return 0;
796 }
797 }
798 if( pclose(fp) == -1 )
799 {
800 perror("close fp file faild");
801 }
802 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
803 return -1;
804}
805
qs.xiong44fac672023-08-29 16:15:55 +0800806static void APTmpWatcherThreadProc() {
807
808 int i = 0;
809 int delytime = 300;
810 g_ap_tmp_watcher_stop_flag = 0;
811
812 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
813 while(1)
814 {
815 sleep(1);
816 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800817 if ( (i % 30) == 0 )
818 {
819 if ( check_current_reinit_info() == 0 )
820 {
821 system("wl reset_cnts");
822 system("wl down");
823 system("wl up");
824 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
825 }
826 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800827 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800828 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800829 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800830 {
831 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
832 {
833 system("wl down");
834 system("wl up");
835 }
qs.xiong44fac672023-08-29 16:15:55 +0800836 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800837 }
838 if ( i == delytime )
839 {
qs.xiong44fac672023-08-29 16:15:55 +0800840 i = 0;
841 }
842 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
843 {
844 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
845 return;
846 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800847
qs.xiong44fac672023-08-29 16:15:55 +0800848 }
849
850}
851
qs.xiongf0128b12023-06-29 17:29:39 +0800852static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
853{
854 char LYNQ_DISABLE_CMD[128]={0};
855
856 CHECK_IDX(idx, CTRL_STA);
857 CHECK_WPA_CTRL(CTRL_STA);
858
859 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800860 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800861 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800862
qs.xiongf0128b12023-06-29 17:29:39 +0800863 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800864
qs.xiongf0128b12023-06-29 17:29:39 +0800865}
866
qs.xiongb37f8c42023-09-13 21:21:58 +0800867static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
868{
869 char LYNQ_ENABLE_CMD[128]={0};
870
871 CHECK_IDX(idx, CTRL_STA);
872 CHECK_WPA_CTRL(CTRL_STA);
873
874
875 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800876 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800877 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
878
879 return 0;
880
881}
882
883static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
884{
885
886 int index,networkid;
887
888 for ( index = 0; index < len ;index++)
889 {
890 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800891 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800892 {
893 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
894 }
895 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
896 }
897 return 0;
898
899}
900
901
902/*
903 dis_net_list user disconnect list
904*/
905static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
906{
qs.xiong09560402023-10-27 21:58:55 +0800907 RLOGD("enter %s %d",__func__,__LINE__);
908 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800909 int count,ncount,index;
910 int flag = 0;
911 int merge_index = 0;
912 int net_no_list[128];
913
qs.xiong09560402023-10-27 21:58:55 +0800914 for(ncount = 0;ncount < valid_num; ncount++ )
915 {
916 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
917 }
918
qs.xiongb37f8c42023-09-13 21:21:58 +0800919 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
920 for( count = 0; count < index; count++)
921 {
922 for(ncount = 0; ncount < valid_num; ncount++)
923 {
qs.xiong09560402023-10-27 21:58:55 +0800924 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800925 if(net_no_list[count] == dis_net_list[ncount])
926 {
qs.xiong09560402023-10-27 21:58:55 +0800927 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800928 flag = 1;
929 break;
930 }
931 }
932 if( flag != 1 )
933 {
934 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800935 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 +0800936 merge_index ++;
937 }
938 flag = 0;
939 }
940 * outlen =merge_index;
941 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
942 return;
943}
qs.xiongf0128b12023-06-29 17:29:39 +0800944
qs.xiong455c30b2023-04-12 11:40:02 +0800945void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
946{
947 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800948 char *wpanetid;
949 char destid[3] = {0};
950 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800951 *error = LYNQ_WAIT_CONNECT_ACTIVE;
952 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
953 {
954 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
955 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
956 return;
957 }
958
qs.xiong20202422023-09-06 18:01:18 +0800959 if (strstr(modify, "Trying to associate") != NULL)
960 {
961 RLOGD("Current sta is Trying to associate");
962 *state = LYNQ_WIFI_STATUS_EGNORE;
963 g_sta_conncet_status_flag = 1;
964 return;
965 }
966
qs.xiong455c30b2023-04-12 11:40:02 +0800967 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
968 {
969 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800970 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800971 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800972 return;
973 }
974
qs.xiong1e81dfa2023-09-27 15:52:37 +0800975 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
976 {
977 *error = LYNQ_PSW_ERROR;
978 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
979 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
980 g_sta_conncet_status_flag = 0;
981 return;
982 }
qs.xiong455c30b2023-04-12 11:40:02 +0800983 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
984 {
qs.xiongf0128b12023-06-29 17:29:39 +0800985 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
986 wpanetid = strstr(modify,"id=");
987 if ( wpanetid != NULL )
988 {
989 wpanetid +=strlen("id=");
990 memcpy(destid,wpanetid,2);
991 tmpdisid = atoi(destid);
992
993 }
qs.xiong455c30b2023-04-12 11:40:02 +0800994 pReason = strstr(modify, "reason=");
995 if (pReason != NULL)
996 {
997 pReason += strlen("reason=");
998 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
999 {
1000 *error = LYNQ_TIME_OUT;
1001 }
1002 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1003 {
1004 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001005 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001006 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001007 // you.chen@2023-10-17 only disable network during autoconnect
1008 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 +08001009 {
1010 RLOGE("stop wlan0 network %d fail",tmpdisid);
1011 }
qs.xiong455c30b2023-04-12 11:40:02 +08001012 }
1013 else
1014 {
1015 *error = LYNQ_UNSPECIFIED_REASON;
1016 }
qs.xiong455c30b2023-04-12 11:40:02 +08001017 }
1018 else
1019 {
1020 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001021 }
qs.xiongf0128b12023-06-29 17:29:39 +08001022 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001023 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001024 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001025
1026 }
1027
1028 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1029 {
1030 *error = LYNQ_NOT_FIND_AP;
1031 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1032 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001033 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001034 return;
1035 }
1036
1037
1038 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1039 {
1040 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1041 pReason = strstr(modify, "status_code=");
1042 if (pReason != NULL)
1043 {
1044 pReason += strlen("status_code=");
1045 if (memcmp(pReason, "17", 2) == 0)
1046 {
1047 *error = LYNQ_AP_UNABLE_HANDLE;
1048 }
1049 else if (memcmp(pReason, "1",1) == 0)
1050 {
1051 *error = LYNQ_UNSPECIFIED_REASON;
1052 }
1053 else
1054 {
1055 *error = LYNQ_UNSPECIFIED_REASON;
1056 }
1057
1058 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1059 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 +08001060 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001061 return;
1062 }
1063 else
1064 {
1065 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1066 *error = LYNQ_UNSPECIFIED_REASON;
1067 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1068 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1069 return;
1070 }
1071 }
1072
1073 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1074 {
1075 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1076 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1077 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1078 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1079 return;
1080 }
1081
1082 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1083 {
1084 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1085 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1086 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1087 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1088 return;
1089 }
1090
qs.xiongdf637b22023-10-26 19:30:07 +08001091 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1092 if (strstr(modify, "Association request to the driver failed") != NULL)
1093 {
1094 RLOGD("Association request to the driver failed --- recover");
1095 system("wl down");
1096 system("wl up");
1097 *error = LYNQ_UNSPECIFIED_REASON;
1098 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1099 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1100 return;
1101 }
1102 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1103
1104
you.chen32cb31e2023-04-13 14:05:45 +08001105 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001106 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001107 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001108 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1109 return;
1110
1111}
1112
qs.xiongfcc914b2023-07-06 21:16:20 +08001113void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1114{
1115 char *pReason;
1116 char *wpanetid;
1117 char destid[3];
1118 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1119 *networkid = -1;
1120 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1121 {
1122 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1123 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1124 return;
1125 }
1126 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1127 {
1128 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1129 wpanetid = strstr(modify,"id=");
1130 if ( wpanetid != NULL )
1131 {
1132 wpanetid +=strlen("id=");
1133 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1134 if (memcpy(destid,wpanetid,2) != NULL)
1135 {
1136 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1137 *networkid = atoi(destid);
1138 RLOGD("get networkid is %d\n",*networkid);
1139 }
1140 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1141 }
1142 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1143 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1144 return;
1145 }
1146 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1147 {
1148 wpanetid = strstr(modify,"id=");
1149 if ( wpanetid != NULL )
1150 {
1151 wpanetid +=strlen("id=");
1152 if (memcpy(destid,wpanetid,2) != NULL)
1153 {
1154 *networkid = atoi(destid);
1155 RLOGD("get networkid is %d\n",*networkid);
1156 }
1157 }
1158 pReason = strstr(modify, "reason=");
1159 if (pReason != NULL)
1160 {
1161 pReason += strlen("reason=");
1162 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1163 {
1164 *error = LYNQ_TIME_OUT;
1165 }
1166 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1167 {
1168 *error = LYNQ_PSW_ERROR;
1169 }
1170 else
1171 {
1172 *error = LYNQ_UNSPECIFIED_REASON;
1173 }
1174 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1175 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1176 return;
1177 }
1178 else
1179 {
1180 *error = LYNQ_UNSPECIFIED_REASON;
1181 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1182 return;
1183 }
1184 }
1185 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1186 {
1187 wpanetid = strstr(modify,"id=");
1188 if ( wpanetid != NULL )
1189 {
1190 wpanetid +=strlen("id=");
1191 if (memcpy(destid,wpanetid,2) != NULL)
1192 {
1193 *networkid = atoi(destid);
1194 RLOGD("get networkid is %d\n",*networkid);
1195 }
1196 }
1197 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1198 pReason = strstr(modify, "status_code=");
1199 if (pReason != NULL)
1200 {
1201 pReason += strlen("status_code=");
1202 if (memcmp(pReason, "17", 2) == 0)
1203 {
1204 *error = LYNQ_AP_UNABLE_HANDLE;
1205 }
1206 else if (memcmp(pReason, "1",1) == 0)
1207 {
1208 *error = LYNQ_UNSPECIFIED_REASON;
1209 }
1210 else
1211 {
1212 *error = LYNQ_UNSPECIFIED_REASON;
1213 }
1214 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1215 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1216 return;
1217 }
1218 else
1219 {
1220 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1221 *error = LYNQ_UNSPECIFIED_REASON;
1222 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001223 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001224 return;
1225 }
1226 }
1227 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1228 {
1229 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1230 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1231 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1232 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1233 return;
1234 }
1235 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1236 {
1237 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1238 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1239 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1240 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1241 return;
1242 }
1243 RLOGD("EVENT : %s\n", modify);
1244 *error = LYNQ_UNSPECIFIED_REASON;
1245 *state = LYNQ_WIFI_STATUS_EGNORE;
1246 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1247 return;
1248}
you.chen70f377f2023-04-14 18:17:09 +08001249static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1250{
you.chen6d247052023-06-01 16:39:54 +08001251 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001252 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1253 {
1254 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1255 g_sta_callback_func(g_sta_callback_priv, state, error);
1256 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1257 }
you.chen6d247052023-06-01 16:39:54 +08001258 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001259}
qs.xiongfcc914b2023-07-06 21:16:20 +08001260static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1261{
1262 pthread_mutex_lock(&s_sta_callback_mutex);
1263 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1264 {
1265 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1266 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1267 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1268 }
1269 pthread_mutex_unlock(&s_sta_callback_mutex);
1270}
you.chen70f377f2023-04-14 18:17:09 +08001271
you.chen35020192022-05-06 11:30:57 +08001272static void STAWatcherThreadProc() {
1273 size_t len = MAX_RET;
1274 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001275 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001276 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001277 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001278
you.chen6c2dd9c2022-05-16 17:55:28 +08001279 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001280 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001281
you.chen70f377f2023-04-14 18:17:09 +08001282 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001283 while (g_sta_watcher_stop_flag == 0)
1284 {
you.chenf711c8a2023-04-13 13:49:45 +08001285 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001286 {
you.chen35020192022-05-06 11:30:57 +08001287 continue;
1288 }
you.chenf711c8a2023-04-13 13:49:45 +08001289
you.chen6c2dd9c2022-05-16 17:55:28 +08001290 memset(msg_notify, 0, MAX_RET);
1291 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001292 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001293 {
you.chen35020192022-05-06 11:30:57 +08001294 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001295 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1296 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001297 {
you.chen35020192022-05-06 11:30:57 +08001298 g_sta_scan_finish_flag = 1;
1299 }
1300
qs.xiong9fbf74e2023-03-28 13:38:22 +08001301 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001302 {
you.chen35020192022-05-06 11:30:57 +08001303 continue;
1304 }
qs.xiong455c30b2023-04-12 11:40:02 +08001305 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001306 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1307 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1308 {
1309 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1310 }
1311 else
1312 {
1313 notify_connect_status(state, error);
1314 }
1315 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001316
1317 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001318 {
you.chen70f377f2023-04-14 18:17:09 +08001319 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001320 if (last_state != state)
1321 {
1322 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1323 {
1324 system_call_v("%s %s", sta_status_change_script, "connect");
1325 }
1326 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1327 {
1328 system_call_v("%s %s", sta_status_change_script, "disconnect");
1329 }
1330 }
qs.xiong09560402023-10-27 21:58:55 +08001331
you.chenc9928582023-04-24 15:39:37 +08001332 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001333 }
you.chen35020192022-05-06 11:30:57 +08001334 }
1335 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001336 if (lynq_wpa_ctrl != NULL)
1337 {
you.chen92fd5d32022-05-25 10:09:47 +08001338 wpa_ctrl_detach(lynq_wpa_ctrl);
1339 wpa_ctrl_close(lynq_wpa_ctrl);
1340 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001341}
qs.xiongfcc914b2023-07-06 21:16:20 +08001342static void STAAutoWatcherThreadProc() {
1343 size_t len = MAX_RET;
1344 char msg_notify[MAX_RET];
1345 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001346 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001347 int idle_count = 0;
1348 int networkid;
1349 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1350 g_sta_auto_watcher_stop_flag = 0;
1351 RLOGD("STAAutoWatcherThreadProc thread started ------");
1352 while (g_sta_auto_watcher_stop_flag == 0)
1353 {
1354 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1355 {
1356 continue;
1357 }
1358 memset(msg_notify, 0, MAX_RET);
1359 len = MAX_RET;
1360 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1361 {
1362 msg_notify[len+1] = '\0';
1363 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1364 if (strstr(msg_notify, state_scan_result) != NULL)
1365 {
1366 g_sta_auto_scan_finish_flag = 1;
1367 }
1368 if (g_sta_auto_callback_func == NULL)
1369 {
1370 continue;
1371 }
1372 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1373 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001374 }
1375 }
1376 if (lynq_wpa_ctrl != NULL)
1377 {
1378 wpa_ctrl_detach(lynq_wpa_ctrl);
1379 wpa_ctrl_close(lynq_wpa_ctrl);
1380 }
1381}
qs.xiongf1b525b2022-03-31 00:58:23 -04001382
you.chen70f377f2023-04-14 18:17:09 +08001383// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1384static void GlobalWatcherThreadProc()
1385{
1386 int ret, connect_timeout, service_abnormal;
1387 error_number_s error_num = -1;
1388 inner_sta_status_s sta_status;
1389 scan_info_s *scan_list = NULL;
1390 int i, scan_len=0;
1391 char connecting_ssid[64];
1392 struct timeval now;
1393
1394 RLOGD("GlobalWatcherThreadProc start to run");
1395
1396 while (1)
1397 {
1398 pthread_mutex_lock(&s_global_check_mutex);
1399 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1400 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1401 {
1402 pthread_mutex_unlock(&s_global_check_mutex);
1403 usleep(50*1000);
1404 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1405 continue;
1406 }
1407
1408 connect_timeout = 0;
1409 service_abnormal = 0;
1410 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1411 {
1412 while (1)
1413 {
1414 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1415 if (ret == ETIME)
1416 {
1417 connect_timeout = 1;
1418 }
1419 else if (ret != 0)
1420 {
1421 gettimeofday(&now,NULL);
1422 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1423 {
1424 usleep(SLEEP_TIME_ON_IDLE);
1425 continue;
1426 }
1427 connect_timeout = 1;
1428 }
1429 sta_status = s_sta_status;
1430 error_num = s_sta_error_number;
1431 s_sta_status = INNER_STA_STATUS_INIT;
1432 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1433 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1434 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1435 break;
1436 }
1437 }
1438 if (s_service_invoke_timeout_cnt > 10)
1439 {
1440 service_abnormal = 1;
1441 s_service_invoke_timeout_cnt = 0;
1442 }
1443 pthread_mutex_unlock(&s_global_check_mutex);
1444
1445 if (service_abnormal == 1)
1446 {
1447 sleep(1);
1448 RLOGE("wpa service is abnormal info app to exit");
1449 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001450
1451 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1452
you.chen70f377f2023-04-14 18:17:09 +08001453 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1454 }
1455
1456 if (sta_status == INNER_STA_STATUS_CANCEL)
1457 {
1458 continue;
1459 }
1460 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1461 {
1462 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1463 }
1464 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1465 {
1466 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1467 {
1468 for(i=0; i < scan_len;i++)
1469 {
1470 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1471 {
1472 error_num = LYNQ_AUTH_ERROR;
1473 break;
1474 }
1475 }
1476 free(scan_list);
1477 }
1478 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1479 }
1480 else if (connect_timeout == 0)
1481 {
1482 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1483 }
1484 else // wait timeout
1485 {
1486 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1487 {
1488 ; // wpa service abnormal
1489 }
1490 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1491 {
1492 RLOGD("GlobalWatcherThreadProc notify connected");
1493 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1494 }
1495 else
1496 {
1497 RLOGD("GlobalWatcherThreadProc notify timeout");
1498 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1499 }
1500 }
1501 } // while (1)
1502}
1503
qs.xiong1af5daf2022-03-14 09:12:12 -04001504int lynq_wifi_enable(void)
1505{
you.chen35020192022-05-06 11:30:57 +08001506 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001507 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001508 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001509 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1510
qs.xiong9fbf74e2023-03-28 13:38:22 +08001511 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1512 {
you.chend2fef3f2023-02-13 10:50:35 +08001513 goto out_enable;
1514 }
1515
you.chenc9928582023-04-24 15:39:37 +08001516 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001517 if (ret != 0)
1518 {
1519 //printf("service state %d\n", ret);
1520 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001521 ret = -1;
1522 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001523 }
lhfe8da902022-10-11 18:55:36 +08001524
you.chen70f377f2023-04-14 18:17:09 +08001525 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1526 {
1527 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1528 if(ret<0)
1529 {
1530 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1531 ret = -1;
1532 goto out_enable;
1533 }
1534 }
1535
you.chend2fef3f2023-02-13 10:50:35 +08001536 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1537 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1538 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1539 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1540out_enable:
1541 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001542 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001543}
1544
qs.xiong1af5daf2022-03-14 09:12:12 -04001545int lynq_wifi_disable(void)
1546{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001547 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001548 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001549 g_ap_watcher_stop_flag = 1;
1550 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001551 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001552 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001553 if (g_ap_watcher_pid != 0)
1554 pthread_join(g_ap_watcher_pid, NULL);
1555 if (g_sta_watcher_pid != 0)
1556 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001557 if (g_sta_auto_watcher_pid != 0)
1558 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001559 if (g_lynq_wpa_ctrl[0] != NULL)
1560 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1561 if (g_lynq_wpa_ctrl[1] != NULL)
1562 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001563 if (g_ap_tmp_watcher_pid != 0)
1564 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001565 g_ap_watcher_pid = 0;
1566 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001567 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001568 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001569 g_lynq_wpa_ctrl[0] = NULL;
1570 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001571 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001572 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001573 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1574 return 0;
1575}
1576
1577static inline char inner_convert_char(char in)
1578{
1579 if (in >= '0' && in <= '9')
1580 {
1581 return in - '0';
1582 }
1583 else if (in >= 'a' && in <= 'f')
1584 {
1585 return in - 'a' + 10;
1586 }
1587 else if (in >= 'A' && in <= 'F')
1588 {
1589 return in - 'A' + 10;
1590 }
1591 else
1592 {
1593 return '\xff';
1594 }
1595}
1596
1597static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1598{
1599 char *p;
1600 size_t pos = 0;
1601 if (NULL == out_ssid)
1602 return;
1603 //printf("input ssid=[%s]\n", ssid);
1604 memset(out_ssid, 0, out_ssid_len);
1605 if (NULL == ssid)
1606 return;
1607 p = strchr(ssid, '\\');
1608 if (NULL == p)
1609 {
1610 strncpy(out_ssid, ssid, out_ssid_len);
1611 //printf(" first %s\n", out_ssid);
1612 }
1613 else
1614 {
1615 pos = p - ssid;
1616 memcpy(out_ssid, ssid, pos);
1617 //printf("pos %lu -- %s\n", pos, out_ssid);
1618 for(; pos < out_ssid_len; pos ++)
1619 {
1620 if (p[0] == '\0')
1621 {
1622 //printf(" out %s\n", out_ssid);
1623 return;
1624 }
1625 else if (p[0] != '\\')
1626 {
1627 out_ssid[pos] = p[0];
1628 p += 1;
1629 }
1630 else if (p[1] == 'x' || p[1] == 'X')
1631 {
1632 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1633 p += 4;
1634 }
1635 else if (p[1] == '\\')
1636 {
1637 out_ssid[pos] = '\\';
1638 p += 2;
1639 }
1640 else if (p[1] == 't')
1641 {
1642 out_ssid[pos] = '\t';
1643 p += 2;
1644 }
1645 else if (p[1] == 'r')
1646 {
1647 out_ssid[pos] = '\r';
1648 p += 2;
1649 }
1650 else if (p[1] == 'n')
1651 {
1652 out_ssid[pos] = '\n';
1653 p += 2;
1654 }//todo find a better way to convert?
1655 }
1656 }
1657 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001658}
qs.xiong1af5daf2022-03-14 09:12:12 -04001659
you.chen35020192022-05-06 11:30:57 +08001660static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001661 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001662 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001663 RLOGD("enter inner_get_param");
1664 if (out_put == NULL)
1665 {
1666 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001667 return -1;
1668 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001669 if (param_name == NULL)
1670 {
1671 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001672 return -1;
1673 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001674 if (param_name[0] == '\0')
1675 {
1676 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001677 return -1;
1678 }
1679
1680 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1681
1682 CHECK_WPA_CTRL(interface);
1683
1684 DO_REQUEST(lynq_cmd_get);
1685
qs.xiong9fbf74e2023-03-28 13:38:22 +08001686 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1687 {
1688 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001689 return -1;
1690 }
1691
you.chena6fa5b22022-05-18 10:28:19 +08001692// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001693 if (strcmp(param_name, "ssid") == 0)
1694 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001695 if (cmd_reply[0] == '\"')
1696 {
you.chend2fef3f2023-02-13 10:50:35 +08001697 ssid_len = reply_len - 1;
1698 memcpy(out_put, cmd_reply + 1, ssid_len);
1699 if (out_put[ssid_len-1] == '\"')
1700 {
1701 out_put[ssid_len-1] = '\0';
1702 }
1703 else
1704 {
1705 out_put[ssid_len] = '\0';
1706 }
1707 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001708 else
1709 {
you.chend2fef3f2023-02-13 10:50:35 +08001710 ssid_len = reply_len / 2;
1711 for(i=0; i<ssid_len; i++)
1712 {
1713 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1714 }
1715 out_put[ssid_len] = '\0';
1716 }
1717 }
1718 else
1719 {
1720 memcpy(out_put, cmd_reply, reply_len + 1);
1721 }
you.chen35020192022-05-06 11:30:57 +08001722 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001723}
qs.xiong1af5daf2022-03-14 09:12:12 -04001724
you.chen35020192022-05-06 11:30:57 +08001725static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1726 int ret = 0;
1727 char * end = str + len - 1;
1728 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001729 while(str < end)
1730 {
1731 if (*str == delimiter)
1732 {
you.chen35020192022-05-06 11:30:57 +08001733 *str++ = '\0';
1734 results[ret++] = str;
1735 continue;
1736 }
1737 str++;
1738 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001739 if (*str == delimiter)
1740 {
you.chen35020192022-05-06 11:30:57 +08001741 *str = '\0';
1742 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001743
you.chen6ed36a62023-04-27 17:51:56 +08001744 results[ret] = NULL;
1745
you.chen35020192022-05-06 11:30:57 +08001746 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001747}
1748
you.chend2fef3f2023-02-13 10:50:35 +08001749static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1750{
1751 char * p;
1752 int ret = 0;
1753 char cmd[256]={0};
1754 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001755 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001756 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001757 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001758 ret = exec_cmd(cmd, ip, ip_len);
1759 p = strchr(ip, '\n');
1760 if (NULL != p)
1761 {
1762 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001763 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001764 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001765 return ret;
1766}
1767
you.chend2fef3f2023-02-13 10:50:35 +08001768static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001769 struct in_addr addr ={0};
1770 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001771 char cmd[64] = {0};
1772 char * p;
1773 int ret;
you.chen35020192022-05-06 11:30:57 +08001774
qs.xiong9fbf74e2023-03-28 13:38:22 +08001775 if (ip == NULL || *ip == '\0' || hostname == NULL)
1776 {
1777 RLOGE("ip == NULL or hostname == NULL");
1778 return -1;
you.chen35020192022-05-06 11:30:57 +08001779 }
1780
you.chend2fef3f2023-02-13 10:50:35 +08001781 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001782 if (inet_aton(ip, &addr) == 0)
1783 {
you.chen35020192022-05-06 11:30:57 +08001784 printf("---inet_aton fail\n");
1785 return -1;
1786 }
1787
1788 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1789
qs.xiong9fbf74e2023-03-28 13:38:22 +08001790 if (ht == NULL)
1791 {
you.chen186d3c32023-05-18 14:19:46 +08001792 hostname[0] = '\0';
1793 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1794 ret = exec_cmd(cmd, hostname, 32);
1795 if (ret == 0)
1796 {
1797 p = strchr(hostname, '\n');
1798 if (p != NULL)
1799 {
1800 *p = '\0';
1801 }
1802 return 0;
1803 }
1804 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001805 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001806 herror(NULL);
1807 return -1;
1808 }
1809
1810 strcpy(hostname, ht->h_name);
1811
1812 return 0;
1813}
1814
1815static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1816{
1817 int count, index, words_count;
1818 char * split_lines[128]= {0};
1819 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001820 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001821 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001822 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001823
1824 CHECK_WPA_CTRL(ap_sta);
1825
1826 DO_REQUEST(lynq_wifi_list_networks);
1827
1828 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1829
1830 //@todo check ssid field to compatible
1831
1832 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001833 for(index=1; index < count; index++)
1834 {
you.chen35020192022-05-06 11:30:57 +08001835 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001836 if (words_count > 2)
1837 {
you.chend2fef3f2023-02-13 10:50:35 +08001838 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1840 {
you.chen35020192022-05-06 11:30:57 +08001841 net_no_list[ret++] = atoi(split_words[0]);
1842 }
1843 }
1844 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001845 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001846 return ret;
1847}
1848
1849static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001850 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001851 CHECK_WPA_CTRL(ap_sta);
1852 const char *lynq_wifi_add_network = "ADD_NETWORK";
1853
qs.xiong9fbf74e2023-03-28 13:38:22 +08001854 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001855 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1857 {
1858 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001859 return -1;
1860 }
1861
qs.xiong9fbf74e2023-03-28 13:38:22 +08001862 for(i=0;i<reply_len;i++)
1863 {
1864 if(cmd_reply[i] == '\n')
1865 {
you.chen35020192022-05-06 11:30:57 +08001866 cmd_reply[i] = '\0';
1867 break;
1868 }
1869 }
1870 return atoi(cmd_reply);
1871}
you.chena6cd55a2022-05-08 12:20:18 +08001872
you.chen35020192022-05-06 11:30:57 +08001873static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1874{
1875 int count, index;
1876 int net_no_list[128];
1877
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001879 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001880 for (index=0; index < count; index++)
1881 {
1882 if (net_no_list[index] == net_no)
1883 {
you.chen35020192022-05-06 11:30:57 +08001884 return 0;
1885 }
1886 }
1887
1888 if (count >= 1)
1889 index = net_no_list[count - 1];
1890 else
1891 index = -1;
1892
qs.xiong9fbf74e2023-03-28 13:38:22 +08001893 while (index < net_no )
1894 {
you.chen35020192022-05-06 11:30:57 +08001895 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001896 if (index >= net_no)
1897 { // required network no created
1898 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001899 return 0;
1900 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001901 else if( index < 0)
1902 {
1903 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001904 return -1;
1905 }
you.chen35020192022-05-06 11:30:57 +08001906 }
1907
1908 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001909 {
1910 RLOGE("[lynq_check_network_number] network index < 0");
1911 return -1;
1912 }
1913 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001914 return 0;
1915}
1916
1917static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001918 if (freq > 5000 && freq < 6000)
1919 {
you.chen35020192022-05-06 11:30:57 +08001920 return LYNQ_WIFI_5G_band;
1921 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 else if (freq > 2000 && freq < 3000)
1923 {
you.chen35020192022-05-06 11:30:57 +08001924 return LYNQ_WIFI_2G_band;
1925 }
1926 return LYNQ_WIFI_2_and_5G_band;
1927}
1928
1929static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001930 if (key_mgmt != NULL)
1931 {
1932 if (memcmp( key_mgmt, "NONE", 4) == 0)
1933 {
you.chen35020192022-05-06 11:30:57 +08001934 return LYNQ_WIFI_AUTH_OPEN;
1935 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001936 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1937 {
you.chen35020192022-05-06 11:30:57 +08001938 return LYNQ_WIFI_AUTH_WEP;
1939 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001940 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1941 {
you.chen35020192022-05-06 11:30:57 +08001942 return LYNQ_WIFI_AUTH_WPA_PSK;
1943 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001944 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1945 {
you.chen35020192022-05-06 11:30:57 +08001946 return LYNQ_WIFI_AUTH_WPA2_PSK;
1947 }
1948 }
1949
1950 return -1;
1951}
1952
1953static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001954 if (flag != NULL)
1955 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001956 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 +08001957 {
1958 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001959 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001960 {
1961 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1962 }else if (strstr( flag, "WPA2-PSK") != NULL)
1963 {
you.chen35020192022-05-06 11:30:57 +08001964 return LYNQ_WIFI_AUTH_WPA2_PSK;
1965 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001966 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001967 {
you.chen35020192022-05-06 11:30:57 +08001968 return LYNQ_WIFI_AUTH_WPA_PSK;
1969 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001970 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001971 {
you.chen35020192022-05-06 11:30:57 +08001972 return LYNQ_WIFI_AUTH_WEP;
1973 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001974 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001975 {
you.chen35020192022-05-06 11:30:57 +08001976 return LYNQ_WIFI_AUTH_OPEN;
1977 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001978 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001979 {
you.chend2fef3f2023-02-13 10:50:35 +08001980 return LYNQ_WIFI_AUTH_OPEN;
1981 }
qs.xiong46f41562023-07-11 21:06:47 +08001982 else
1983 {
1984 RLOGD("convert_max_auth_from_flag not-found auth mode");
1985 }
you.chen35020192022-05-06 11:30:57 +08001986 }
1987
1988 return -1;
1989}
1990
1991static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1992 switch (bw) {
1993 case 10:
1994 return LYNQ_WIFI_BANDWIDTH_HT10;
1995 break;
1996 case 20:
1997 return LYNQ_WIFI_BANDWIDTH_HT20;
1998 break;
1999 case 40:
2000 return LYNQ_WIFI_BANDWIDTH_HT40;
2001 break;
2002 case 80:
2003 return LYNQ_WIFI_BANDWIDTH_HT80;
2004 break;
2005 default:
2006 break;
2007 }
2008
2009 return -1;
2010}
2011
you.chen70f377f2023-04-14 18:17:09 +08002012static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002013static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2014 int i, count;
2015 char *p;
2016 const char *lynq_status_cmd = "STATUS";
2017 const char * FLAG_SSID = "ssid=";
2018 const char * FLAG_SBSID = "bssid=";
2019 const char * FLAG_KEY_MGMT = "key_mgmt=";
2020 const char * FLAG_FREQ = "freq=";
2021 const char * FLAG_STATE = "wpa_state=";
2022 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002023 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002024 char *split_lines[128] = {0};
2025
2026 CHECK_WPA_CTRL(interface);
2027
qs.xiong9fbf74e2023-03-28 13:38:22 +08002028 if (curr_state == NULL)
2029 {
2030 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002031 return -1;
2032 }
2033
2034 DO_REQUEST(lynq_status_cmd);
2035
2036 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2037
2038 curr_state->net_no = -1;
2039 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002040 for(i=0; i < count; i++)
2041 {
2042 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002043 {
you.chen35020192022-05-06 11:30:57 +08002044 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002045 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002046 {
you.chend2fef3f2023-02-13 10:50:35 +08002047 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002048 ret = 0;
2049 continue;
2050 }
you.chenf58b3c92022-06-21 16:53:48 +08002051 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002052 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002053 {
you.chend2fef3f2023-02-13 10:50:35 +08002054 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 +08002055 ret = 0;
2056 continue;
2057 }
you.chen35020192022-05-06 11:30:57 +08002058 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002059 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002060 {
you.chen450d0172022-07-15 17:56:48 +08002061 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002062 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002063 ret = 0;
2064 continue;
2065 }
2066 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002067 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002068 {
you.chen35020192022-05-06 11:30:57 +08002069 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2070 ret = 0;
2071 continue;
2072 }
you.chend2fef3f2023-02-13 10:50:35 +08002073 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002074 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002075 {
you.chend2fef3f2023-02-13 10:50:35 +08002076 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2077 ret = 0;
2078 continue;
2079 }
you.chen35020192022-05-06 11:30:57 +08002080 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002081 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002082 {
you.chen35020192022-05-06 11:30:57 +08002083 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002084 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002085 {
you.chen35020192022-05-06 11:30:57 +08002086 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2087 ret = 0;
2088 continue;
2089 }
2090
2091 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002092 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002093 {
you.chen35020192022-05-06 11:30:57 +08002094 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002095 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002096 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002097 }
2098 }
2099
you.chen70f377f2023-04-14 18:17:09 +08002100 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2101 {
2102 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2103 }
2104
you.chen35020192022-05-06 11:30:57 +08002105 return ret;
2106}
2107
qs.xiongf1b525b2022-03-31 00:58:23 -04002108int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002109{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002111 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002112
qs.xiong9fbf74e2023-03-28 13:38:22 +08002113 if (ap_ssid == NULL)
2114 {
2115 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002116 return -1;
2117 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 else
2119 {
2120 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002121 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002122
qs.xiong9fbf74e2023-03-28 13:38:22 +08002123 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2124 {
2125 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002126 return -1;
2127 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002128
you.chen35020192022-05-06 11:30:57 +08002129 CHECK_IDX(idx, CTRL_AP);
2130
2131 CHECK_WPA_CTRL(CTRL_AP);
2132
2133 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2134
2135 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2136 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002137 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2138 return 0;
you.chen35020192022-05-06 11:30:57 +08002139
qs.xiong7a105ce2022-03-02 09:43:11 -05002140}
2141
you.chen35020192022-05-06 11:30:57 +08002142int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002143{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002145 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002146 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002147}
2148
qs.xiongc9c79f72022-10-17 15:27:18 +08002149/*****
2150 *frequency <------>channel
2151 *
2152 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2153 *
2154 *
2155 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2156 *
2157 *
2158 * */
2159static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002160 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};
2161 int i;
2162 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2163
qs.xiong69a332b2022-12-02 09:58:57 +08002164 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002165 {
2166 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002167 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002168 }
qs.xiongc00b6032022-11-29 16:28:03 +08002169
2170 if(i == arr_len)
2171 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002173 return -1;
2174 }
qs.xiongc00b6032022-11-29 16:28:03 +08002175
qs.xiongc9c79f72022-10-17 15:27:18 +08002176 return 0;
2177}
qs.xiong13673462023-02-21 19:12:54 +08002178
2179static int lynq_check_frequencyby_country_code(int input_frequency)
2180{
2181 char str_cnc[]="CN";
2182 char str_dest[20]="";
2183
2184 if( lynq_get_country_code(1,str_dest) != 0 )
2185 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002186 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002187 return -1;
2188 }
2189 if( strncmp(str_dest,str_cnc,2) != 0 )
2190 {
2191 return 0;
2192 }else if( 2473 < input_frequency && input_frequency < 5744)
2193 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002194 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002195 return -1;
2196 }
2197 return 0;
2198}
qs.xiongf1b525b2022-03-31 00:58:23 -04002199int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002200{
qs.xiongc00b6032022-11-29 16:28:03 +08002201 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002202 char lynq_wifi_frequency_cmd[128]={0};
2203 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002204 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002205 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002206 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002207 check = lynq_check_set_frequency(lynq_wifi_frequency);
2208 if(check != 0)
2209 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002210 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002211 return -1;
you.chen35020192022-05-06 11:30:57 +08002212 }
qs.xiong13673462023-02-21 19:12:54 +08002213 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2214 if(check != 0)
2215 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002216 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002217 return -1;
2218 }
2219
qs.xiongc00b6032022-11-29 16:28:03 +08002220 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2221 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002222 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002223 return -1;
2224 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002225
you.chen35020192022-05-06 11:30:57 +08002226 CHECK_IDX(idx, CTRL_AP);
2227
2228 CHECK_WPA_CTRL(CTRL_AP);
2229
2230 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2231 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2232 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2233
you.chen6c2dd9c2022-05-16 17:55:28 +08002234 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002235 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2236 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2237 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002238
qs.xiong9fbf74e2023-03-28 13:38:22 +08002239 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002240}
2241
qs.xiongf1b525b2022-03-31 00:58:23 -04002242int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002243{
you.chen35020192022-05-06 11:30:57 +08002244 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002245 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002246 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002247
qs.xiong9fbf74e2023-03-28 13:38:22 +08002248 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2249 {
2250 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002251 return -1;
2252 }
2253 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002254
qs.xiong9fbf74e2023-03-28 13:38:22 +08002255 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002256}
2257
qs.xiongf1b525b2022-03-31 00:58:23 -04002258int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2259{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002260 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002261 CHECK_IDX(idx, CTRL_AP);
2262 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002263 case LYNQ_WIFI_BANDWIDTH_HT10:
2264 {
2265 RLOGE("bandwith [%d] not support now\n", bandwidth);
2266 return -1;
2267 }
2268 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002269 {
2270 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2271 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002272 if (system(lynq_cmd_bandwith) != 0 )
2273 {
2274 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002275 return -1;
2276 }
2277 system("wl up");
2278 break;
2279 }
2280 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002281 {
qs.xiong10379192023-02-21 13:19:42 +08002282 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002283 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2284 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 if (system(lynq_cmd_bandwith) != 0 )
2286 {
2287 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002288 return -1;
2289 }
2290 system("wl up");
2291 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002292 }
you.chen35020192022-05-06 11:30:57 +08002293 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002294 {
qs.xiong10379192023-02-21 13:19:42 +08002295 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002296 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002297 if (system(lynq_cmd_bandwith) != 0 )
2298 {
2299 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002300 return -1;
2301 }
2302 system("wl up");
2303 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002304 }
2305 default:
you.chen35020192022-05-06 11:30:57 +08002306 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002307 RLOGE("auth type [%d] not support now\n", bandwidth);
2308 return -1;
you.chen35020192022-05-06 11:30:57 +08002309 }
2310 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002311
2312
you.chen35020192022-05-06 11:30:57 +08002313 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002314}
you.chen35020192022-05-06 11:30:57 +08002315
qs.xiongf1b525b2022-03-31 00:58:23 -04002316int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2317{
you.chen35020192022-05-06 11:30:57 +08002318 int count = 0;
2319 int index = 0;
2320 char *split_words[128] = {0};
2321 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002323 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002324
you.chen35020192022-05-06 11:30:57 +08002325 CHECK_WPA_CTRL(CTRL_AP);
2326
2327 DO_REQUEST(lynq_chanspec_cmd);
2328
2329 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2330 for(;index < count; index++) {
2331 if (strncmp(split_words[index], "bw", 2) != 0) {
2332 continue;
2333 }
2334
2335 index++;
2336 if (index >= count) {
2337 return -1;
2338 }
2339
qs.xiong9fbf74e2023-03-28 13:38:22 +08002340 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002341 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2342 return 0;
2343 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002344 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002345 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002346}
qs.xiong0fb469a2022-04-14 03:50:45 -04002347
qs.xiongf1b525b2022-03-31 00:58:23 -04002348int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002349{
you.chen35020192022-05-06 11:30:57 +08002350 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002352 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002353
you.chen35020192022-05-06 11:30:57 +08002354 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002355
qs.xiong9fbf74e2023-03-28 13:38:22 +08002356 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2357 {
you.chen35020192022-05-06 11:30:57 +08002358 return -1;
2359 }
2360
2361 system("wl down");
2362 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002363 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002364 return -1;
2365 }
2366 system("wl up");
2367 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002368}
qs.xiong0fb469a2022-04-14 03:50:45 -04002369
qs.xiongf1b525b2022-03-31 00:58:23 -04002370int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002371{
you.chen35020192022-05-06 11:30:57 +08002372 int count = 0;
2373 int index = 0;
2374 char *split_words[128] = {0};
2375 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002376 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002377 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002378
you.chen35020192022-05-06 11:30:57 +08002379 CHECK_WPA_CTRL(CTRL_AP);
2380
2381 DO_REQUEST(lynq_chanspec_cmd);
2382
2383 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002384 for(;index < count; index++)
2385 {
2386 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002387 if (strncmp(split_words[index], "channel", 2) != 0) {
2388 continue;
2389 }
2390
2391 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002392 if (index >= count)
2393 {
you.chen35020192022-05-06 11:30:57 +08002394 return -1;
2395 }
2396
2397 *channel = atoi(split_words[index]);
2398 return 0;
2399 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002400 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002401 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002402}
2403
2404
you.chen35020192022-05-06 11:30:57 +08002405int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002406{
you.chen6c2dd9c2022-05-16 17:55:28 +08002407 char ssid[MAX_CMD] = {0};
2408 int freq = 0;
2409 char lynq_auth_cmd[64]={0};
2410 char lynq_auth_alg_cmd[64]={0};
2411 char lynq_psk_cmd[64]={0};
2412 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002413 char lynq_ieee80211_cmd[64]={0};
2414 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002415 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002416 CHECK_IDX(idx, CTRL_AP);
2417
you.chen6c2dd9c2022-05-16 17:55:28 +08002418 CHECK_WPA_CTRL(CTRL_AP);
2419
qs.xiong9fbf74e2023-03-28 13:38:22 +08002420 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2421 {
2422 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002423 return -1;
2424 }
2425
you.chen92fd5d32022-05-25 10:09:47 +08002426 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002427 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002428 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002429 return 0;
2430 }
2431 else {
2432 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2433 ssid[0] = '\0';
2434 }
2435 lynq_wifi_ap_frequency_get(idx, &freq);
2436
2437 DO_OK_FAIL_REQUEST(cmd_disconnect);
2438 DO_OK_FAIL_REQUEST(cmd_remove_all);
2439 if (ssid[0] != '\0') {
2440 lynq_wifi_ap_ssid_set(idx, ssid);
2441 }
2442 if (freq != 0) {
2443 lynq_wifi_ap_frequency_set(idx, freq);
2444 }
2445 }
2446 }
you.chen35020192022-05-06 11:30:57 +08002447
qs.xiong9fbf74e2023-03-28 13:38:22 +08002448 switch(auth){
2449 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002450 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002451 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002452 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002453 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002454 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002455 break;
2456 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002457 case LYNQ_WIFI_AUTH_WEP:
2458 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002459 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002460 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002461 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002462 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2463
2464 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2465 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2466 break;
2467 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002468 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002469 {
2470 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2471 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2472 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2473
2474 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2475 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2476 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2477 break;
2478
2479 }
you.chen35020192022-05-06 11:30:57 +08002480 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002481 {
2482 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2483 {
you.chen35020192022-05-06 11:30:57 +08002484 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2485 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2486 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002487 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2488 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002489 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002490 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002491 }
2492// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2493// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2494 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002495
you.chen35020192022-05-06 11:30:57 +08002496 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2497 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2498 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002499 break;
2500 }
2501 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2502 {
2503 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2504 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2505 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2506 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2507
2508 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2509 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2510 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2511 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2512 break;
2513 }
2514 case LYNQ_WIFI_AUTH_WPA3_PSK:
2515 {
2516 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2517 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002518 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002519 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2520
2521 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2522 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2523 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2524 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2525 break;
2526 }
2527 default:
you.chen35020192022-05-06 11:30:57 +08002528 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002529 RLOGE("auth type [%d] not support now\n", auth);
2530 return -1;
you.chen35020192022-05-06 11:30:57 +08002531 }
2532 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002533 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002534
qs.xiong9fbf74e2023-03-28 13:38:22 +08002535 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002536}
2537
you.chen35020192022-05-06 11:30:57 +08002538int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002539{
you.chen35020192022-05-06 11:30:57 +08002540 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002541 char lynq_auth_alg_str[MAX_RET] = {0};
2542 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002543 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002544 CHECK_IDX(idx, CTRL_AP);
2545
qs.xiong9fbf74e2023-03-28 13:38:22 +08002546 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2547 {
2548 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002549 return -1;
2550 }
2551
qs.xiong9fbf74e2023-03-28 13:38:22 +08002552 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2553 {
2554 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2555 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002556 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002557 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002558 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002559 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002560 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2561 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002562 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002563 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002564 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002565 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002566 else
2567 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002568 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002569 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002570 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002571 }
you.chen35020192022-05-06 11:30:57 +08002572 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002573 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2574 {
2575 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2576 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002577 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002578 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002579 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002580 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2581 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002582 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002583 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002584 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002585 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002586 else
2587 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002588 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002589 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002590 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002591 }
you.chen35020192022-05-06 11:30:57 +08002592 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002593
2594 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2595 {
2596 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2597 return -1;
2598 }
2599
2600 if (memcmp(lynq_auth_str,"1",1) == 0 )
2601 {
2602 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2603 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002604 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002605 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2606 {
2607 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2608 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002609 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002610 }
2611 else
2612 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002613 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002614 *auth = -1;
2615 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002616
you.chen6c2dd9c2022-05-16 17:55:28 +08002617 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002618}
qs.xiong1af5daf2022-03-14 09:12:12 -04002619
you.chenb95401e2023-05-12 19:39:06 +08002620static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2621{
2622 char status[64];
you.chencba13492023-05-19 13:53:43 +08002623 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002624 curr_status_info curr_state;
2625
2626 CHECK_WPA_CTRL(CTRL_AP);
2627
2628 memset(status, 0, sizeof (status));
2629
2630 curr_state.ap = NULL;
2631 curr_state.state = status;
2632
2633 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002634 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002635 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002636 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002637 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002638 {
2639 return 0;
2640 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002641 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002642 {
2643 DO_REQUEST("RECONNECT");
2644 return inner_check_ap_connected(idx, retry_count+1);
2645 }
you.chencba13492023-05-19 13:53:43 +08002646 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002647 {
2648 printf("retry 10 time\n");
2649 return -1;
2650 }
2651 else
2652 {
you.chen6d247052023-06-01 16:39:54 +08002653 if (strcmp(status, STATE_DISCONNECTED) == 0)
2654 {
2655 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2656 DO_REQUEST(LYNQ_WIFI_CMD);
2657 }
you.chenb95401e2023-05-12 19:39:06 +08002658 return inner_check_ap_connected(idx, retry_count+1);
2659 }
2660 }
2661 return -1;
2662}
qs.xiong1af5daf2022-03-14 09:12:12 -04002663
qs.xiongf1b525b2022-03-31 00:58:23 -04002664int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002665{
qs.xiong5a2ba932023-09-13 16:30:21 +08002666 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002667 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002668 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2669 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2670 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002671 CHECK_IDX(idx, CTRL_AP);
2672
2673 CHECK_WPA_CTRL(CTRL_AP);
2674
you.chen0df3e7e2023-05-10 15:56:26 +08002675 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002676 {
you.chen0df3e7e2023-05-10 15:56:26 +08002677 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002678 return -1;
2679 }
you.chen35020192022-05-06 11:30:57 +08002680
qs.xiongb37f8c42023-09-13 21:21:58 +08002681 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2682 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2683
you.chen35020192022-05-06 11:30:57 +08002684 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2685 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2686
you.chenc9928582023-04-24 15:39:37 +08002687 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2688 if (ret != 0)
2689 {
2690 RLOGE("lynq_wifi_ap_start excute script fail");
2691 return -1;
2692 }
2693
you.chenb95401e2023-05-12 19:39:06 +08002694 if (inner_check_ap_connected(idx, 0) != 0)
2695 {
2696 return -1;
2697 }
2698
you.chen0df3e7e2023-05-10 15:56:26 +08002699 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002700 if (g_ap_tmp_watcher_pid == 0)
2701 {
2702 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2703 {
2704 g_ap_tmp_watcher_pid = 0;
2705 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2706 return -1;
2707 }
2708 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2709 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002710 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002711
qs.xiong9fbf74e2023-03-28 13:38:22 +08002712 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002713}
2714
qs.xiongf1b525b2022-03-31 00:58:23 -04002715int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002716{
you.chen35020192022-05-06 11:30:57 +08002717 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002718}
2719
qs.xiongf1b525b2022-03-31 00:58:23 -04002720int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002721{
you.chen35020192022-05-06 11:30:57 +08002722 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002723
you.chen35020192022-05-06 11:30:57 +08002724 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002725
you.chen35020192022-05-06 11:30:57 +08002726 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002727
you.chen35020192022-05-06 11:30:57 +08002728 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2729
2730 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2731
you.chenb4b121c2022-05-06 17:50:16 +08002732// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002733
2734 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2735 if (ret != 0)
2736 {
2737 RLOGE("lynq_wifi_ap_start excute script fail");
2738 return -1;
2739 }
qs.xiong44fac672023-08-29 16:15:55 +08002740 g_ap_tmp_watcher_stop_flag = 1;
2741 if (g_ap_tmp_watcher_pid != 0)
2742 pthread_join(g_ap_tmp_watcher_pid, NULL);
2743 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002744
qs.xiong9fbf74e2023-03-28 13:38:22 +08002745 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002746}
qs.xiong1af5daf2022-03-14 09:12:12 -04002747
qs.xiongf1b525b2022-03-31 00:58:23 -04002748int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002749{
you.chen35020192022-05-06 11:30:57 +08002750 char lynq_disable_cmd[128] = {0};
2751 char lynq_select_cmd[128] = {0};
2752 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002753 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002754 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002755
you.chen35020192022-05-06 11:30:57 +08002756 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002757 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2758 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2759
2760 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2761 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2762 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002763
qs.xiong9fbf74e2023-03-28 13:38:22 +08002764 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002765}
2766
qs.xiongf1b525b2022-03-31 00:58:23 -04002767int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002768{
you.chen35020192022-05-06 11:30:57 +08002769 char lynq_disable_cmd[128] = {0};
2770 char lynq_select_cmd[128] = {0};
2771 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002772 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002773 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002774
you.chen35020192022-05-06 11:30:57 +08002775 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002776
you.chen35020192022-05-06 11:30:57 +08002777 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2778 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2779
2780 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2781 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2782 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002783
qs.xiong9fbf74e2023-03-28 13:38:22 +08002784 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002785}
qs.xiongf1b525b2022-03-31 00:58:23 -04002786
you.chen35020192022-05-06 11:30:57 +08002787int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002788{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002789 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002790 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002791 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002792 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002793 RLOGD("enter lynq_ap_password_set");
2794 if( password == NULL )
2795 {
2796 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002797 return -1;
2798 }
2799 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002800 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002801 if(pass_len < 8 || pass_len >= 64)
2802 {
2803 RLOGE("[lynq_ap_password_set]input password len not in rage");
2804 return -1;
you.chen35020192022-05-06 11:30:57 +08002805 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002806
you.chen35020192022-05-06 11:30:57 +08002807 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002808
qs.xiong9fbf74e2023-03-28 13:38:22 +08002809 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2810 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002811 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002812 return -1;
2813 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002814 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2815 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002816 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2817 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002818 }
2819
you.chen35020192022-05-06 11:30:57 +08002820 CHECK_WPA_CTRL(CTRL_AP);
2821
qs.xiong9fbf74e2023-03-28 13:38:22 +08002822 if (auth == LYNQ_WIFI_AUTH_WEP)
2823 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002824 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002825 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2826 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2827 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2828 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2829 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002830 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2831 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002832 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 +08002833 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2834 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2835 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002836 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2837 {
2838
qs.xiongc8d92a62023-03-29 17:36:14 +08002839 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 +08002840 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002841 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002842 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2843 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2844
2845 }
2846 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002847 {
2848 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002849 return -1;
2850 }
you.chen35020192022-05-06 11:30:57 +08002851
you.chen35020192022-05-06 11:30:57 +08002852 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002853
qs.xiong9fbf74e2023-03-28 13:38:22 +08002854 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002855}
2856
you.chen35020192022-05-06 11:30:57 +08002857int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002858{
you.chen35020192022-05-06 11:30:57 +08002859 FILE * fp;
2860 int len, ret;
2861 int count, index;
2862 char *split_lines[128] = {0};
2863 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002864 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002865
you.chen35020192022-05-06 11:30:57 +08002866 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002867
you.chen35020192022-05-06 11:30:57 +08002868 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2869// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002870 if (NULL == fp)
2871 {
2872 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002873 return -1;
2874 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002875
you.chen35020192022-05-06 11:30:57 +08002876 buff = alloca(MAX_RET);
2877 fseek(fp, 0, SEEK_SET);
2878 len = fread(buff, 1, MAX_RET, fp);
2879 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002880
qs.xiong9fbf74e2023-03-28 13:38:22 +08002881 for(index=0; index < len; index ++)
2882 {
2883 if (memcmp(buff + index, "network={", 9) != 0)
2884 {
you.chen35020192022-05-06 11:30:57 +08002885 continue;
2886 }
2887 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 for (; index < len; index ++ )
2889 {
2890 if (buff[index] != '}')
2891 {
you.chen35020192022-05-06 11:30:57 +08002892 continue;
2893 }
2894 buff[index] = '\0';
2895 break;
2896 }
2897 len = buff + index - p;
2898 }
2899
2900 count = lynq_split(p, len, '\n', split_lines);
2901
2902 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 for(index=0; index < count; index++)
2904 {
you.chen35020192022-05-06 11:30:57 +08002905 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002906 if (p != NULL)
2907 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002908 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002909 if (*p == '\"')
2910 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002911 p++;
2912 }
you.chen35020192022-05-06 11:30:57 +08002913 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002914 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2915 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002916 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 if (*p == '\"')
2918 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002919 p++;
2920 }
2921 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002922 else
2923 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002924 continue;
you.chen35020192022-05-06 11:30:57 +08002925 }
2926
2927 strcpy(password, p);
2928
qs.xiong9fbf74e2023-03-28 13:38:22 +08002929 while(*password != '\0')
2930 {
2931 if (*password == '\"')
2932 {
you.chen35020192022-05-06 11:30:57 +08002933 *password = '\0';
2934 break;
2935 }
2936 password++;
2937 }
2938 ret = 0;
2939 break;
2940 } //end for(index=0; index < count; index++)
2941
2942 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002943}
2944
you.chen35020192022-05-06 11:30:57 +08002945static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2946 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002947 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002948
qs.xiong9fbf74e2023-03-28 13:38:22 +08002949 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2950 {
you.chen35020192022-05-06 11:30:57 +08002951 return -1;
2952 }
2953
2954 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002955
qs.xiong9fbf74e2023-03-28 13:38:22 +08002956 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2957 {
2958 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002959 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002960 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002961 {
you.chena6cd55a2022-05-08 12:20:18 +08002962 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002963 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002964 }
you.chen70f377f2023-04-14 18:17:09 +08002965 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2966 {
2967 return 0;
2968 }
you.chena6cd55a2022-05-08 12:20:18 +08002969 }
2970 }
you.chen70f377f2023-04-14 18:17:09 +08002971 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2972 {
2973 return 0;
2974 }
2975
qs.xiong9fbf74e2023-03-28 13:38:22 +08002976 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2977 {
you.chen70f377f2023-04-14 18:17:09 +08002978 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002979 return -1;
2980 }
2981 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2982 {
2983
you.chen70f377f2023-04-14 18:17:09 +08002984 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2985 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002986 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2987 {
2988
2989 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002990 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002991 }else
2992 {
you.chen70f377f2023-04-14 18:17:09 +08002993 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002994 *auth = -1;
2995 return -1;
2996 }
you.chen35020192022-05-06 11:30:57 +08002997 return 0;
2998}
2999
3000int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003001{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003002 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003003 int pass_len, net_no, count, index;
3004 char lynq_tmp_cmd[300]={0};
3005 int net_no_list[128];
3006 lynq_wifi_auth_s net_auth;
3007 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003008 if(pass_len < 8 || pass_len >= 64)
3009 {
3010 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003011 return -1;
3012 }
3013
3014 CHECK_IDX(idx, CTRL_STA);
3015
3016 net_no = -1;
3017 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3018
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 for (index=0; index < count; index++)
3020 {
you.chen35020192022-05-06 11:30:57 +08003021 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003022 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3023 {
you.chen35020192022-05-06 11:30:57 +08003024 net_no = net_no_list[index];
3025 break;
3026 }
3027 }
3028
qs.xiong9fbf74e2023-03-28 13:38:22 +08003029 if (net_no < 0)
3030 {
you.chen35020192022-05-06 11:30:57 +08003031 return -1;
3032 }
3033
3034 CHECK_WPA_CTRL(CTRL_STA);
3035
3036 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3037
3038 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3039 DO_OK_FAIL_REQUEST(cmd_save_config);
3040
3041 return 0;
3042}
3043
qs.xiongb5dab082023-10-13 14:43:41 +08003044/**
3045* buff data
3046* buff_len size of buff
3047* idx sta
3048* *ap ap info for find ssid && password
3049* password return password
3050*
3051*/
3052static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3053
3054 int ret, network_len, i, ssid_len,curr_auth;
3055 int count, index,org_index;
3056 char *split_lines[128] = {0};
3057 char *p, *ssid, *ssid_end_flag,*ptr;
3058 char tmp_ssid[128]={0};
3059 char tmp_auth[24]={0};
3060
3061 org_index = 0;
3062 network_len = 0;
3063 p = NULL;
3064
3065 CHECK_IDX(idx, CTRL_STA);
3066
3067 while(1){
3068 network_len = 0;
3069 p == NULL;
3070 for(; org_index < buff_len; org_index ++)
3071 {
3072 for(; org_index < buff_len; org_index ++)
3073 {
3074 if (memcmp(buff + org_index, "network={", 9) != 0)
3075 {
3076 continue;
3077 }
3078 p = buff + org_index + 9;
3079
3080 for (; org_index < buff_len; org_index ++ )
3081 {
3082 if (buff[org_index] != '}')
3083 {
3084 continue;
3085 }
3086 buff[org_index] = '\0';
3087 break;
3088 }
3089 network_len = buff + org_index - p;
3090 break;
3091 }
3092
3093 if (p == NULL)
3094 {
3095 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3096 return -1;
3097 }
3098
3099 ssid = strstr(p, "ssid=");
3100 if (ssid != NULL) {
3101 ssid += strlen("ssid=");
3102 if (ssid[0] == '\"')
3103 {
3104 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3105 {
3106 RLOGD("-----curr_get ssid form config is %s",ssid);
3107 break;
3108 }
3109 RLOGD("-----countine to find dest ssid %s ---curr_get ssid from config is %s",ap->ap_ssid,ssid);
3110 }
3111 else
3112 {
3113 ssid_end_flag = strstr(ssid, "\n");
3114 if (ssid_end_flag != NULL)
3115 {
3116 ssid_len = (ssid_end_flag - ssid) / 2;
3117 for(i=0; i<ssid_len; i++)
3118 {
3119 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3120 }
3121 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3122 {
3123 RLOGD("curr_ssid is(from config) ---- %s ap_info ssid --->",tmp_ssid,ap->ap_ssid);
3124 break;
3125 }
3126 }
3127 }
3128 }
3129
3130 }
3131
3132 if (org_index >= buff_len || NULL == p || network_len <= 0)
3133 {
3134
3135 if (buff != NULL)
3136 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3137 return -1;
3138 }
3139
3140 count = lynq_split(p, network_len, '\n', split_lines);
3141 ret = -1;
3142 for( index=0; index < count; index++ )
3143 {
3144 p = strstr(split_lines[index], "key_mgmt=");
3145 RLOGD("current p str ------- %s",p);
3146 if(p != NULL)
3147 {
3148 p += 9;
3149 if(memcmp(p,"SAE",3) == 0)
3150 {
3151 curr_auth = 5;
3152 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3153 {
3154 curr_auth = 4;
3155 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3156 {
3157 curr_auth = 3;
3158 }else if(memcmp(p,"NONE",4) == 0 )
3159 {
3160 curr_auth = 0;
3161 }else{
3162 curr_auth = 1;
3163 }
3164 RLOGD("************curret_get_auth is %d ssid is %s",curr_auth,ap->ap_ssid);
3165 if( curr_auth < 1 || curr_auth > 6)
3166 {
3167 ret = -1;
3168 }
3169 break;
3170 }
3171 }
3172 if( curr_auth == 0)
3173 {
3174 return 0;
3175 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3176 {
3177 for(index=0; index < count; index++)
3178 {
3179 /*get psw info*/
3180
3181 p = strstr(split_lines[index], "psk=");
3182 if (p != NULL)
3183 {
3184 p += 4;
3185 if (*p == '\"')
3186 {
3187 p++;
3188 }
3189 }
3190 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3191 {
3192 p += 9;
3193 if (*p == '\"')
3194 {
3195 p++;
3196 }
3197 }
3198 else
3199 {
3200 continue;
3201 }
3202
3203 if (*p == '\"')
3204 p++;
3205 strncpy(password, p, 64);
3206 p = password;
3207 while(password - p < 64 && *password != '\0')
3208 {
3209 if (*password == '\"')
3210 {
3211 *password = '\0';
3212 RLOGD("---------password------- p:: %s",p);
3213 ret = 0;
3214 break;
3215 }
3216 password++;
3217 }
3218 break;
3219 }
3220 break;
3221 }
3222 }
3223
3224 return ret;
3225}
3226
3227
3228
you.chen35020192022-05-06 11:30:57 +08003229int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3230
3231 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003232 int len, ret;
3233 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003234 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003235
qs.xiongb5dab082023-10-13 14:43:41 +08003236 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003237 CHECK_IDX(idx, CTRL_STA);
3238
qs.xiong9fbf74e2023-03-28 13:38:22 +08003239 if (NULL == password)
3240 {
3241 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003242 return -1;
3243 }
3244
you.chen35020192022-05-06 11:30:57 +08003245 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003246 if (NULL == fp)
3247 {
3248 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003249 return -1;
3250 }
3251
you.chen6d247052023-06-01 16:39:54 +08003252 fseek(fp, 0, SEEK_END);
3253 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003254 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003255
qs.xiongb5dab082023-10-13 14:43:41 +08003256 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003257 {
3258 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3259 return -1;
3260 }
3261
you.chen35020192022-05-06 11:30:57 +08003262 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003263 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003264 fclose(fp);
3265
qs.xiongb5dab082023-10-13 14:43:41 +08003266
3267 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3268
3269 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003270 {
qs.xiongb5dab082023-10-13 14:43:41 +08003271 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3272 free(info_buff);
3273 return 0;
you.chen35020192022-05-06 11:30:57 +08003274 }
qs.xiongb5dab082023-10-13 14:43:41 +08003275 else{
3276 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003277 return -1;
3278 }
3279
you.chen35020192022-05-06 11:30:57 +08003280}
3281
qs.xiongb5dab082023-10-13 14:43:41 +08003282
you.chen35020192022-05-06 11:30:57 +08003283static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3284{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003285 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003286
qs.xiong9fbf74e2023-03-28 13:38:22 +08003287 if (sta_ssid == NULL)
3288 {
3289 RLOGE("sta_ssid is null\n");
3290 return -1;
you.chen35020192022-05-06 11:30:57 +08003291 }
3292
qs.xiong9fbf74e2023-03-28 13:38:22 +08003293 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003294
3295 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3296
3297 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3298// DO_OK_FAIL_REQUEST(cmd_save_config);
3299
qs.xiong9fbf74e2023-03-28 13:38:22 +08003300 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003301
3302}
3303
you.chen35020192022-05-06 11:30:57 +08003304static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003305{
you.chen35020192022-05-06 11:30:57 +08003306 char lynq_disable_cmd[128]={0};
3307 char lynq_select_cmd[128]={0};
3308
3309 CHECK_WPA_CTRL(CTRL_STA);
3310
qs.xiong9fbf74e2023-03-28 13:38:22 +08003311 if (save != 0)
3312 {
you.chenc29444e2022-06-07 18:01:16 +08003313 if (start_flag != 0)
3314 {
3315 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3316 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3317 }
3318 else
3319 {
3320 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3321 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3322 }
you.chen35020192022-05-06 11:30:57 +08003323 DO_OK_FAIL_REQUEST(cmd_save_config);
3324 }
3325
qs.xiong9fbf74e2023-03-28 13:38:22 +08003326 if (start_flag == 0)
3327 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003328 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003329 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3330 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003331 else
3332 {
you.chen35020192022-05-06 11:30:57 +08003333 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3334 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3335 }
3336
3337 return 0;
3338}
3339
3340int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3341{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003343 CHECK_IDX(idx, CTRL_STA);
3344
you.chen6c2dd9c2022-05-16 17:55:28 +08003345 curr_status_info curr_state;
3346 ap_info_s ap_info;
3347 curr_state.ap = &ap_info;
3348 curr_state.state = NULL;
3349
qs.xiong9fbf74e2023-03-28 13:38:22 +08003350 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3351 {
you.chend2fef3f2023-02-13 10:50:35 +08003352 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003353 return 0;
3354 }
3355
3356 return -1;
you.chen35020192022-05-06 11:30:57 +08003357}
3358
3359int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3360{
qs.xiong5d716d22023-09-20 20:08:39 +08003361 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003362 scan_info_s *scan_list = NULL;
3363 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003364 int scan_len=0;
3365 int save_len=0;
3366 int best_index = -1;
3367 int best_scan_index = -1;
3368 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003369 int i, j, ret;
3370
3371 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003372
3373 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003374 if (info == NULL)
3375 {
you.chen35020192022-05-06 11:30:57 +08003376 return -1;
3377 }
3378
3379 curr_status_info curr_state;
3380 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003381 char status[64];
you.chen35020192022-05-06 11:30:57 +08003382
you.chen9ac66392022-08-06 17:01:16 +08003383 memset(&ap_info, 0, sizeof (ap_info));
3384 memset(status, 0, sizeof (status));
3385
3386 curr_state.ap = &ap_info;
3387 curr_state.state = status;
3388
qs.xiong9fbf74e2023-03-28 13:38:22 +08003389 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3390 {
you.chen35020192022-05-06 11:30:57 +08003391 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003392 if (strcmp(status, STATE_COMPLETED) == 0)
3393 {
3394 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003395 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003396 }
3397 else
3398 {
3399 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003400 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003401 }
you.chen593621d2023-04-27 17:52:44 +08003402 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003403 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003404 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003405 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003406 return 0;
3407 }
3408
you.chen9ac66392022-08-06 17:01:16 +08003409 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003410 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003411 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3412 {
you.chen9ac66392022-08-06 17:01:16 +08003413 if (NULL != scan_list)
3414 {
3415 free(scan_list);
3416 }
you.chen35020192022-05-06 11:30:57 +08003417 return -1;
3418 }
3419
qs.xiong9fbf74e2023-03-28 13:38:22 +08003420 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3421 {
you.chen9ac66392022-08-06 17:01:16 +08003422 if (NULL != scan_list)
3423 {
3424 free(scan_list);
3425 }
3426 if (NULL != save_list)
3427 {
3428 free(save_list);
3429 }
you.chen35020192022-05-06 11:30:57 +08003430 return -1;
3431 }
3432
qs.xiong9fbf74e2023-03-28 13:38:22 +08003433 for (i=0; i < save_len; i++)
3434 {
3435 for (j=0; j < scan_len; j++)
3436 {
you.chen35020192022-05-06 11:30:57 +08003437 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003438 && save_list[i].base_info.auth == scan_list[j].auth)
3439 {
3440 if (best_rssi == 0)
3441 {
you.chen9ac66392022-08-06 17:01:16 +08003442 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003443 best_rssi = scan_list[j].rssi;
3444 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003445 else if (best_rssi > scan_list[j].rssi)
3446 {
you.chen35020192022-05-06 11:30:57 +08003447 best_index = i;
3448 best_scan_index = j;
3449 best_rssi = scan_list[j].rssi;
3450 }
you.chend2fef3f2023-02-13 10:50:35 +08003451 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 +08003452 break;
3453 }
3454 }
3455 }
3456
qs.xiong9fbf74e2023-03-28 13:38:22 +08003457 if (best_index >= 0)
3458 {
you.chen35020192022-05-06 11:30:57 +08003459 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003460 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 +08003461 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003462 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003463 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003464 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003465 }
3466
you.chen9ac66392022-08-06 17:01:16 +08003467 if (NULL != scan_list)
3468 {
3469 free(scan_list);
3470 }
3471 if (NULL != save_list)
3472 {
3473 free(save_list);
3474 }
3475
qs.xiong5d716d22023-09-20 20:08:39 +08003476 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003477 return ret;
you.chen35020192022-05-06 11:30:57 +08003478}
3479
3480static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3481{
qs.xiongc8d92a62023-03-29 17:36:14 +08003482 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003483 char lynq_ket_mgmt_cmd[64]={0};
3484 char lynq_pairwise_cmd[64]={0};
3485 char lynq_psk_cmd[64]={0};
3486
3487 CHECK_WPA_CTRL(CTRL_STA);
3488
qs.xiong9fbf74e2023-03-28 13:38:22 +08003489 switch(auth)
3490 {
3491 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003492 {
3493 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003494
you.chen35020192022-05-06 11:30:57 +08003495 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003496// DO_OK_FAIL_REQUEST(cmd_save_config);
3497 break;
3498 }
3499 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003500 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003501 {
3502 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3503 {
you.chen35020192022-05-06 11:30:57 +08003504 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3505 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003506 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3507 {
you.chena6cd55a2022-05-08 12:20:18 +08003508 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003509 }
3510 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3511 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003512
you.chen35020192022-05-06 11:30:57 +08003513 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3514 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3515 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003516
qs.xiong9fbf74e2023-03-28 13:38:22 +08003517 if (password != NULL)
3518 {
you.chen35020192022-05-06 11:30:57 +08003519 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3520 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003521 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003522 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003523
you.chen35020192022-05-06 11:30:57 +08003524// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003525 break;
3526 }
3527 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3528 {
qs.xiong3e506812023-04-06 11:08:48 +08003529 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003530 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003531 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3532 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3533
qs.xiong3e506812023-04-06 11:08:48 +08003534 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003535 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3536 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3537 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3538
3539 break;
3540 }
3541 case LYNQ_WIFI_AUTH_WPA3_PSK:
3542 {
qs.xiong3e506812023-04-06 11:08:48 +08003543 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003544 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003545 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3546 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3547
qs.xiongb37f8c42023-09-13 21:21:58 +08003548 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003549 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3550 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3551 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3552
3553 break;
3554 }
3555 default:
3556 return -1;
you.chen35020192022-05-06 11:30:57 +08003557 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003558
qs.xiong9fbf74e2023-03-28 13:38:22 +08003559 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003560}
qs.xiong7a105ce2022-03-02 09:43:11 -05003561
you.chen35020192022-05-06 11:30:57 +08003562static int inner_get_curr_net_no(int interface) {
3563 curr_status_info curr_state;
3564 curr_state.ap = NULL;
3565 curr_state.state = NULL;
3566
qs.xiong9fbf74e2023-03-28 13:38:22 +08003567 if (0 != inner_get_status_info(interface, &curr_state))
3568 {
you.chen35020192022-05-06 11:30:57 +08003569 return -1;
3570 }
3571
3572 return curr_state.net_no;
3573}
3574
3575int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003576{
you.chen35020192022-05-06 11:30:57 +08003577 int net_no;
3578 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003579
you.chen35020192022-05-06 11:30:57 +08003580 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003581
qs.xiong9fbf74e2023-03-28 13:38:22 +08003582 if (net_no < 0)
3583 {
you.chen35020192022-05-06 11:30:57 +08003584 return -1;
3585 }
3586
3587 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003588}
3589
you.chenb95401e2023-05-12 19:39:06 +08003590int 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 -05003591{
you.chen35020192022-05-06 11:30:57 +08003592 int count, net_no, index;
3593 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003594 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003595 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003596 curr_status_info curr_state;
3597 ap_info_s ap_info;
3598 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003599
qs.xiong9fbf74e2023-03-28 13:38:22 +08003600 if (ssid == NULL || *ssid == '\0')
3601 {
3602 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003603 return -1;
3604 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003605
qs.xiong9fbf74e2023-03-28 13:38:22 +08003606 if (LYNQ_WIFI_AUTH_OPEN != auth)
3607 {
3608 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003609 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003610 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003611 return -1;
3612 }
3613 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003614
you.chen70f377f2023-04-14 18:17:09 +08003615
3616 pthread_mutex_lock(&s_global_check_mutex);
3617 if (s_sta_status != INNER_STA_STATUS_INIT)
3618 {
3619 s_sta_status = INNER_STA_STATUS_CANCEL;
3620 pthread_cond_signal(&s_global_check_cond);
3621 }
3622 pthread_mutex_unlock(&s_global_check_mutex);
3623
you.chen35020192022-05-06 11:30:57 +08003624 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003625 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003626
3627 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003628 memset(&ap_info, 0, sizeof (ap_info));
3629 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003630
you.chen70f377f2023-04-14 18:17:09 +08003631 curr_state.ap = &ap_info;
3632 curr_state.state = status;
3633
3634 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003635 {
you.chen70f377f2023-04-14 18:17:09 +08003636 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3637 {
3638 net_no = curr_state.net_no;
3639 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3640 && strcmp(ap_info.psw, psw) == 0)
3641 {
3642 RLOGD("already connected\n");
3643
3644 pthread_mutex_lock(&s_global_check_mutex);
3645 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003646 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003647 pthread_cond_signal(&s_global_check_cond);
3648 pthread_mutex_unlock(&s_global_check_mutex);
3649 return 0;
3650 }
you.chen35020192022-05-06 11:30:57 +08003651 }
3652 }
3653
you.chen70f377f2023-04-14 18:17:09 +08003654 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003655 {
you.chen70f377f2023-04-14 18:17:09 +08003656 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3657
3658 for (index=0; index < count; index++)
3659 {
3660 net_auth = -1;
3661 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3662 {
3663 net_no = net_no_list[index];
3664 break;
3665 }
you.chen35020192022-05-06 11:30:57 +08003666 }
3667
you.chen70f377f2023-04-14 18:17:09 +08003668 if (net_no < 0)
3669 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003670 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3671 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3672 {
3673 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3674 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3675 DO_OK_FAIL_REQUEST(rm_net_cmd);
3676 }
you.chen70f377f2023-04-14 18:17:09 +08003677 net_no = lynq_add_network(CTRL_STA);
3678 if (net_no == -1)
3679 {
3680 return -1;
3681 }
3682
3683 RLOGD("net no is %d\n", net_no);
3684 if (0 != inner_set_sta_ssid(net_no, ssid))
3685 {
3686 return -1;
3687 }
you.chen35020192022-05-06 11:30:57 +08003688 }
3689 }
3690
qs.xiong9fbf74e2023-03-28 13:38:22 +08003691 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3692 {
you.chen35020192022-05-06 11:30:57 +08003693 return -1;
3694 }
3695
you.chen70f377f2023-04-14 18:17:09 +08003696
3697 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003698 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003699 usleep(200*1000);
3700
qs.xiong09560402023-10-27 21:58:55 +08003701 pthread_mutex_lock(&s_global_check_mutex);
3702 lynq_sta_removeElement(net_no);
3703 pthread_mutex_unlock(&s_global_check_mutex);
3704
you.chen70f377f2023-04-14 18:17:09 +08003705 ret = inner_sta_start_stop(net_no, 1, 1);
3706
3707 pthread_mutex_lock(&s_global_check_mutex);
3708 s_sta_status = INNER_STA_STATUS_CONNECTING;
3709 strcpy(s_sta_current_connecting_ssid, ssid);
3710 struct timeval now;
3711 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003712 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003713 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3714 pthread_cond_signal(&s_global_check_cond);
3715 pthread_mutex_unlock(&s_global_check_mutex);
3716 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003717}
3718
you.chenb95401e2023-05-12 19:39:06 +08003719int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3720{
3721 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3722}
3723
you.chen35020192022-05-06 11:30:57 +08003724int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003725{
you.chen35020192022-05-06 11:30:57 +08003726 ap_info_s ap;
3727 curr_status_info curr_state;
3728 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003729
qs.xiong9fbf74e2023-03-28 13:38:22 +08003730 if (ssid == NULL || *ssid == '\0')
3731 {
3732 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003733 return -1;
3734 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003735
you.chen35020192022-05-06 11:30:57 +08003736 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003737
you.chen35020192022-05-06 11:30:57 +08003738 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003739 curr_state.state = NULL;
3740
qs.xiong9fbf74e2023-03-28 13:38:22 +08003741 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3742 {
you.chen35020192022-05-06 11:30:57 +08003743 return 0;
3744 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003745
qs.xiong9fbf74e2023-03-28 13:38:22 +08003746 if (strcmp(ap.ap_ssid, ssid) != 0)
3747 {
you.chen35020192022-05-06 11:30:57 +08003748 return 0;
3749 }
3750
you.chen70f377f2023-04-14 18:17:09 +08003751 pthread_mutex_lock(&s_global_check_mutex);
3752 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3753 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003754 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003755}
qs.xiong97fa59b2022-04-07 05:41:29 -04003756
qs.xiongc93bf2b2023-08-25 10:22:08 +08003757int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3758{
qs.xiong09560402023-10-27 21:58:55 +08003759 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003760 ap_info_s ap;
3761 curr_status_info curr_state;
3762 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003763 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003764
3765 if (ssid == NULL || *ssid == '\0')
3766 {
3767 RLOGE("input ssid is NULL\n");
3768 return -1;
3769 }
3770
3771 CHECK_IDX(idx, CTRL_STA);
3772
3773
3774 curr_state.ap = &ap;
3775 curr_state.state = NULL;
3776
3777 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3778 {
3779 return 0;
3780 }
3781
3782 if (strcmp(ap.ap_ssid, ssid) != 0)
3783 {
3784 return 0;
3785 }
3786
3787 pthread_mutex_lock(&s_global_check_mutex);
3788 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003789 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3790 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3791 {
3792 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);
3793 if( g_history_disconnect_net[i] == curr_state.net_no)
3794 {
3795 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3796 check_history_idx_flag = 1;
3797 break;
3798 }
3799 }
3800 if ( check_history_idx_flag == 0)
3801 {
3802 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__);
3803 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3804 g_history_disconnect_valid_num++;
3805 }
3806 RLOGD("%s %d",__func__,__LINE__);
3807 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003808 pthread_mutex_unlock(&s_global_check_mutex);
3809 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3810
3811}
3812
3813
you.chena6cd55a2022-05-08 12:20:18 +08003814int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3815{
qs.xiongb37f8c42023-09-13 21:21:58 +08003816
qs.xiongad2f89d2023-01-18 13:17:41 +08003817 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3818 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003819
you.chen35020192022-05-06 11:30:57 +08003820 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003821 CHECK_WPA_CTRL(CTRL_STA);
3822
you.chenc9928582023-04-24 15:39:37 +08003823 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3824 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003825 {
you.chenc9928582023-04-24 15:39:37 +08003826 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003827 return -1;
3828 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003829
qs.xiongad2f89d2023-01-18 13:17:41 +08003830 system(lynq_enable_sta_cmd);
3831 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003832 pthread_mutex_lock(&s_global_check_mutex);
3833 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003834 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003835 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003836 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003837}
3838
you.chen6d247052023-06-01 16:39:54 +08003839static int inner_get_status_info_state (int interface, char *state) {
3840 curr_status_info curr_state;
3841 curr_state.ap = NULL;
3842 curr_state.state = state;
3843 return inner_get_status_info(interface, &curr_state);
3844}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003845
3846int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3847{
3848
qs.xiongb37f8c42023-09-13 21:21:58 +08003849 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3850 int tmp_open_idx[128];
3851 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003852
qs.xiongb37f8c42023-09-13 21:21:58 +08003853 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003854 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003855 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3856 pthread_mutex_unlock(&s_global_check_mutex);
3857 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003858 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003859 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003860 }
3861
qs.xiongb37f8c42023-09-13 21:21:58 +08003862 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003863 return 0;
3864}
3865
3866
you.chen35020192022-05-06 11:30:57 +08003867int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003868{
qs.xiongad2f89d2023-01-18 13:17:41 +08003869// char lynq_disable_network_cmd[MAX_CMD];
3870// curr_status_info curr_state;
3871// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003872 int i=0;
3873 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003874
you.chen6d247052023-06-01 16:39:54 +08003875// const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08003876 CHECK_IDX(idx, CTRL_STA);
3877 CHECK_WPA_CTRL(CTRL_STA);
3878
you.chen6d247052023-06-01 16:39:54 +08003879// system(lynq_disable_sta_cmd);
3880 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003881 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003882
3883 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3884 if (ret != 0)
3885 {
3886 RLOGE("lynq_wifi_ap_start excute script fail");
3887 return -1;
3888 }
3889
you.chen6d247052023-06-01 16:39:54 +08003890 for (i=0; i < 30; i++) // to check if sta is realy stoped
3891 {
3892 if (inner_get_status_info_state(idx, state) != 0)
3893 {
3894 break;
3895 }
3896
3897 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3898 {
3899 break;
3900 }
3901 RLOGD("lynq_wifi_ap_start curr state %s", state);
3902 usleep(SLEEP_TIME_ON_IDLE);
3903 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003904 pthread_mutex_lock(&s_global_check_mutex);
3905 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3906 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003907 return 0;
3908// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003909}
qs.xiongfcc914b2023-07-06 21:16:20 +08003910int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3911{
3912 char LYNQ_DISABLE_CMD[128]={0};
3913 CHECK_IDX(idx, CTRL_STA);
3914 CHECK_WPA_CTRL(CTRL_STA);
3915 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3916 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3917 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3918 return 0;
3919}
qs.xiong7a105ce2022-03-02 09:43:11 -05003920
you.chen35020192022-05-06 11:30:57 +08003921//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3922// int i, count;
3923// char *p;
3924// const char * FLAG_SSID = "ssid=";
3925// const char * FLAG_SBSID = "bssid=";
3926// const char * FLAG_KEY_MGMT = "key_mgmt=";
3927// const char * FLAG_FREQ = "freq=";
3928// char lynq_sta_cmd[MAX_CMD];
3929// char *split_lines[128] = {0};
3930
3931// CHECK_WPA_CTRL(CTRL_AP);
3932
3933// sprintf(lynq_sta_cmd, "STA %s", bssid);
3934
3935// DO_REQUEST(lynq_sta_cmd);
3936
3937// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3938
3939// for(i=0; i < count; i++) {
3940// p = strstr(split_lines[i], FLAG_SSID);
3941// if (p != NULL) {
3942// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3943// continue;
3944// }
3945// }
3946
3947// lynq_get_interface_ip(idx, ap->ap_ip);
3948// lynq_ap_password_set(idx, ap->psw);
3949
3950// return 0;
3951//}
3952
3953static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3954 curr_status_info curr_state;
3955 curr_state.ap = ap;
3956 curr_state.state = NULL;
3957 return inner_get_status_info(interface, &curr_state);
3958}
3959
3960int 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 -04003961{
qs.xiong5071c802023-09-06 14:04:15 +08003962 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003963 int index, line_count;
3964 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003965 const char *lynq_first_sta_cmd = "STA-FIRST";
3966 char lynq_next_sta_cmd[MAX_CMD];
3967 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003968 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003969
you.chen35020192022-05-06 11:30:57 +08003970 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003971
you.chen35020192022-05-06 11:30:57 +08003972 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003973
you.chenb95401e2023-05-12 19:39:06 +08003974 // ap_info_s * tmp_ap;
3975 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003976 if (ap == NULL || list == NULL || len == NULL)
3977 {
3978 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003979 return -1;
3980 }
3981
you.chenb95401e2023-05-12 19:39:06 +08003982 // ap = &tmp_ap;
3983 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003984 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003985 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003986
you.chenb95401e2023-05-12 19:39:06 +08003987 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003988 {
you.chenb95401e2023-05-12 19:39:06 +08003989 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003990 return -1;
3991 }
3992
3993 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3994 lynq_ap_password_get(idx, (*ap)->psw);
3995
you.chen35020192022-05-06 11:30:57 +08003996 DO_REQUEST(lynq_first_sta_cmd);
3997
3998 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003999 while (reply_len > 0)
4000 {
4001 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004002 {
you.chen35020192022-05-06 11:30:57 +08004003 break;
4004 }
4005 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4006 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4007 strcpy(bssid[index], split_lines[0]);
4008 index++;
4009 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4010 reply_len = MAX_RET;
4011 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004012 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 +08004013 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004014 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004015 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004016 break;
4017 }
4018 }
4019
4020 *len = index;
4021
4022 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004023 for (index=0; index < *len; index++)
4024 {
you.chend2fef3f2023-02-13 10:50:35 +08004025 dev_info = &(*list)[index];
4026 memset(dev_info, 0, sizeof(device_info_s));
4027 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4028 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4029 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4030 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004031 free(bssid[index]);
4032 }
qs.xiong5071c802023-09-06 14:04:15 +08004033 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004034 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004035}
4036
you.chen35020192022-05-06 11:30:57 +08004037int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004038{
you.chen35020192022-05-06 11:30:57 +08004039 int i, count, index, count_words;
4040 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4041 char *split_lines[128] = {0};
4042 char *split_words[128] = {0};
4043 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004044
qs.xiong9fbf74e2023-03-28 13:38:22 +08004045 if (list == NULL || len == NULL)
4046 {
you.chen35020192022-05-06 11:30:57 +08004047 return -1;
4048 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004049
you.chen9ac66392022-08-06 17:01:16 +08004050 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4051 {
4052 usleep(100 * 1000);
4053 }
4054
you.chen35020192022-05-06 11:30:57 +08004055 CHECK_IDX(idx, CTRL_STA);
4056
4057 CHECK_WPA_CTRL(CTRL_STA);
4058
4059 DO_REQUEST(lynq_scan_result_cmd);
4060
4061 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4062 *len = count - 1;
4063 *list = malloc(sizeof (scan_info_s) * *len);
4064
4065 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004066 for (index=0; index <count_words; index++)
4067 {
4068 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004069 }
4070
qs.xiong9fbf74e2023-03-28 13:38:22 +08004071 for(index = 1;index < count; index++)
4072 {
4073 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004074 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004075 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4076 if (count_words < 4)
4077 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004078 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004079 //bssid / frequency / signal level / flags / ssid
4080 p = (*list) + index - 1;
4081 strcpy(p->mac, split_words[0]);
4082 p->band = convert_band_from_freq(atoi(split_words[1]));
4083 p->rssi = -1 * atoi( split_words[2]);
4084 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004085 if (count_words == 4) // ssid hided
4086 {
4087 p->ssid[0] = '\0';
4088 }
4089 else
4090 {
4091 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4092 }
you.chen35020192022-05-06 11:30:57 +08004093 }
4094
4095 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004096}
qs.xiong97fa59b2022-04-07 05:41:29 -04004097
you.chen35020192022-05-06 11:30:57 +08004098int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4099{
4100 int count, net_no, index;
4101 int net_no_list[128];
4102 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004103
you.chen35020192022-05-06 11:30:57 +08004104 char lynq_remove_cmd[MAX_CMD];
4105
qs.xiong9fbf74e2023-03-28 13:38:22 +08004106 if (ssid == NULL || *ssid == '\0')
4107 {
4108 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004109 return -1;
4110 }
4111
4112 CHECK_IDX(idx, CTRL_STA);
4113
4114 CHECK_WPA_CTRL(CTRL_STA);
4115
4116 net_no = -1;
4117 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4118
qs.xiong9fbf74e2023-03-28 13:38:22 +08004119 for (index=0; index < count; index++)
4120 {
you.chen35020192022-05-06 11:30:57 +08004121 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004122 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4123 {
you.chen35020192022-05-06 11:30:57 +08004124 net_no = net_no_list[index];
4125 break;
4126 }
4127 }
4128
qs.xiong9fbf74e2023-03-28 13:38:22 +08004129 if (net_no < 0)
4130 {
you.chen35020192022-05-06 11:30:57 +08004131 return 0;
4132 }
4133
4134 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4135
4136 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004137
4138 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4139
4140 pthread_mutex_lock(&s_global_check_mutex);
4141 lynq_sta_removeElement(net_no);
4142 pthread_mutex_unlock(&s_global_check_mutex);
4143
4144 RLOGD("%s %d",__func__,__LINE__);
4145 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004146 DO_OK_FAIL_REQUEST(cmd_save_config);
4147
4148 return 0;
4149}
4150
4151int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004152{
you.chend2fef3f2023-02-13 10:50:35 +08004153 int count, index;
you.chen35020192022-05-06 11:30:57 +08004154 int net_no_list[128];
4155 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004156 RLOGD("enter lynq_get_sta_saved_ap api\n");
4157 if (list == NULL || len == NULL)
4158 {
4159 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004160 return -1;
4161 }
4162
4163 CHECK_IDX(idx, CTRL_STA);
4164
4165// CHECK_WPA_CTRL(CTRL_STA);
4166
4167 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004168 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004169
you.chen057aac42023-04-13 14:06:58 +08004170 if (count < 0)
4171 {
4172 RLOGE("list network fail");
4173 return count;
4174 }
4175 else if (count == 0)
4176 {
4177 *list = NULL;
4178 *len = 0;
4179 return 0;
4180 }
4181
you.chen35020192022-05-06 11:30:57 +08004182 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004183 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004184 *len = count;
4185
qs.xiong9fbf74e2023-03-28 13:38:22 +08004186 for (index=0; index < count; index++)
4187 {
you.chen35020192022-05-06 11:30:57 +08004188 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004189 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004190 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004191 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004192 {
you.chen35020192022-05-06 11:30:57 +08004193 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4194 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004195 else
you.chen057aac42023-04-13 14:06:58 +08004196 {
you.chen35020192022-05-06 11:30:57 +08004197 (*list)[index].base_info.band = -1;
4198 }
you.chen057aac42023-04-13 14:06:58 +08004199 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004200 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004201 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004202 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004203 return 0;
4204}
4205
4206int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4207{
qs.xiong20202422023-09-06 18:01:18 +08004208 if ( g_sta_conncet_status_flag != 0 )
4209 {
4210 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08004211 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004212 }
qs.xiongc8d92a62023-03-29 17:36:14 +08004213 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004214 const char *lynq_scan_cmd = "SCAN";
4215
4216 CHECK_IDX(idx, CTRL_STA);
4217
4218 CHECK_WPA_CTRL(CTRL_STA);
4219
you.chen0df3e7e2023-05-10 15:56:26 +08004220 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4221 {
4222 RLOGD("tmp clear scanlist");
4223 system(clean_last_re);
4224 }
you.chen9ac66392022-08-06 17:01:16 +08004225 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004226 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004227 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4228 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004229 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004230 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4231 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004232 g_sta_scan_finish_flag = 1;
4233 return -1;
4234 }
you.chen35020192022-05-06 11:30:57 +08004235
4236 return 0;
4237}
4238
4239int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004240 if (cb == NULL)
4241 {
4242 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004243 return -1;
4244 }
4245
you.chen6d247052023-06-01 16:39:54 +08004246 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004247 g_ap_callback_priv = priv;
4248 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004249 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004250
you.chen6d247052023-06-01 16:39:54 +08004251 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004252 if (g_ap_watcher_pid == 0 )
4253 {
4254 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4255 {
4256 g_ap_watcher_pid = 0;
4257 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4258 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4259 return -1;
4260 }
4261 }
4262
4263 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4264 RLOGD("creat APWatcherTheradProc susccs");
4265
you.chen35020192022-05-06 11:30:57 +08004266 return 0;
4267}
4268
4269int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004270 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004271 if (g_ap_callback_priv == priv)
4272 {
you.chen35020192022-05-06 11:30:57 +08004273 g_ap_callback_func = NULL;
4274 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004275 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004276 return 0;
4277 }
you.chen6d247052023-06-01 16:39:54 +08004278 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004279 return -1;
4280}
4281
4282int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004283 if (cb == NULL)
4284 {
4285 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004286 return -1;
4287 }
4288
you.chen6d247052023-06-01 16:39:54 +08004289 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004290 g_sta_callback_priv = priv;
4291 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004292 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004293
you.chen6d247052023-06-01 16:39:54 +08004294 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004295 if (g_sta_watcher_pid == 0 ) {
4296 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4297 {
4298 g_sta_watcher_pid = 0;
4299 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4300 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4301 return -1;
4302 }
4303 }
4304
4305 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4306 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004307 return 0;
4308}
4309
4310int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004311 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004312 if (g_sta_callback_priv == priv)
4313 {
you.chen35020192022-05-06 11:30:57 +08004314 g_sta_callback_func = NULL;
4315 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004316 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004317 return 0;
4318 }
you.chen6d247052023-06-01 16:39:54 +08004319 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004320 return -1;
4321}
4322
qs.xiongfcc914b2023-07-06 21:16:20 +08004323int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4324 if (cb == NULL)
4325 {
4326 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4327 return -1;
4328 }
4329 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4330 g_sta_auto_callback_priv = priv;
4331 g_sta_auto_callback_func = cb;
4332 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4333 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4334 if (g_sta_auto_watcher_pid == 0 ) {
4335 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4336 {
4337 g_sta_auto_watcher_pid = 0;
4338 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4339 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4340 return -1;
4341 }
4342 }
4343 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4344 RLOGD("creat STAWatcherTheradProc susccs");
4345 return 0;
4346}
4347int lynq_unreg_sta_auto_event_callback(void * priv) {
4348 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4349 if (g_sta_auto_callback_priv == priv)
4350 {
4351 g_sta_auto_watcher_stop_flag = 1;
4352 if (g_sta_auto_watcher_pid != 0)
4353 {
4354 pthread_join(g_sta_auto_watcher_pid, NULL);
4355 }
4356 g_sta_auto_watcher_pid = 0;
4357 g_sta_auto_callback_func = NULL;
4358 g_sta_auto_callback_priv = NULL;
4359 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4360 return 0;
4361 }
4362 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4363 return -1;
4364}
you.chen35020192022-05-06 11:30:57 +08004365int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4366{
4367 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004368 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004369 CHECK_IDX(idx, CTRL_AP);
4370
qs.xiong9fbf74e2023-03-28 13:38:22 +08004371 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4372 {
you.chen35020192022-05-06 11:30:57 +08004373 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4374 return 0;
4375 }
4376
qs.xiong9fbf74e2023-03-28 13:38:22 +08004377 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4378 {
you.chen35020192022-05-06 11:30:57 +08004379 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4380 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004381 else
4382 {
you.chen35020192022-05-06 11:30:57 +08004383 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4384 }
4385
4386 return 0;
4387}
4388
4389int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4390 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004391 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004392 CHECK_IDX(idx, CTRL_STA);
4393
qs.xiong9fbf74e2023-03-28 13:38:22 +08004394 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4395 {
you.chen35020192022-05-06 11:30:57 +08004396 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4397 return 0;
4398 }
4399
qs.xiong9fbf74e2023-03-28 13:38:22 +08004400 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4401 {
you.chen35020192022-05-06 11:30:57 +08004402 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4403 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004404 else
4405 {
you.chen35020192022-05-06 11:30:57 +08004406 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4407 }
4408
4409 return 0;
4410}
4411
4412int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4413// CHECK_IDX(idx, CTRL_AP);
4414// int ret = 0;
4415// size_t reply_len = MAX_RET;
4416// char cmd_reply[MAX_RET]={0};
4417// const char * cmd_str = "GET country";
4418// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4419// do{
4420// if (NULL == s_lynq_wpa_ctrl) {
4421// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4422// if (NULL == s_lynq_wpa_ctrl ) {
4423// printf("wpa_ctrl_open fail\n");
4424// return -1;
4425// }
4426// }
4427// }while(0);
4428
4429// do {
4430// reply_len = MAX_RET;
4431// cmd_reply[0] = '\0';
4432// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004433// 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 +08004434// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004435// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004436// return ret;
4437// }
4438// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004439// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004440// }while(0);
4441
4442 FILE *fp;
4443 size_t i = 0;
4444 char lynq_cmd_ret[MAX_RET]={0};
4445
4446// CHECK_IDX(idx, CTRL_AP);
4447
4448 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004449 {
4450 perror("popen error!");
4451 return -1;
4452 }
you.chen35020192022-05-06 11:30:57 +08004453 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4454 {
4455 perror("fread fail!");
4456 return -1;
4457 }
4458
qs.xiong9fbf74e2023-03-28 13:38:22 +08004459 for(i=0; i < strlen(lynq_cmd_ret); i++)
4460 {
4461 if (lynq_cmd_ret[i] == ' ')
4462 {
you.chen35020192022-05-06 11:30:57 +08004463 lynq_cmd_ret[i] = '\0';
4464 break;
4465 }
4466 }
4467
4468 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004469 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004470
4471 int ret=pclose(fp);
4472 if(ret==-1)
4473 {
4474 perror("close file faild");
4475 }
4476
4477 return 0;
4478}
4479
qs.xiong44fac672023-08-29 16:15:55 +08004480
you.chen705a7ef2023-06-01 22:06:45 +08004481static int check_and_init_uci_config(char * country_code)
4482{
4483 FILE * fp;
4484 int is_different = 0;
4485 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4486 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4487 const char * commit_uci_cmd ="uci commit";
4488 char set_country_cmd[MAX_CMD];
4489 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004490
you.chen705a7ef2023-06-01 22:06:45 +08004491 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004492
you.chen705a7ef2023-06-01 22:06:45 +08004493 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004494 {
you.chen705a7ef2023-06-01 22:06:45 +08004495 if (0 != system(create_uci_cmd))
4496 {
4497 RLOGE("creat_uci_cmd fail");
4498 return -1;
4499 }
4500 is_different = 1;
4501 }
4502
4503 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4504 {
4505 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004506 return -1;
4507 }
4508
you.chen705a7ef2023-06-01 22:06:45 +08004509 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004510 {
you.chen705a7ef2023-06-01 22:06:45 +08004511 RLOGE("fread fail!");
4512 fclose(fp);
4513 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004514 }
4515
you.chen705a7ef2023-06-01 22:06:45 +08004516 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4517 {
qs.xiong44fac672023-08-29 16:15:55 +08004518 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 +08004519 is_different = 1;
4520 }
4521
4522 fclose(fp);
4523
4524 if (is_different)
4525 {
4526 if ( 0 != system(set_country_cmd))
4527 {
4528 RLOGE("set_country_cmd fail");
4529 return -1;
4530 }
4531 if (0 != system(commit_uci_cmd))
4532 {
4533 RLOGE("commmit fail");
4534 }
4535 }
4536
4537 return is_different;
4538}
4539
4540int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4541 char check_current_code[10];
4542 const char * support_country[] = {"CN", "EU"};
4543
4544 int ret,is_different, i, cc_count;
4545
4546 if (country_code == NULL || country_code[0] == '\0')
4547 {
4548 RLOGE("bad country code\n");
4549 return -1;
4550 }
4551
4552 cc_count = sizeof (support_country) / sizeof (char*);
4553 for(i=0; i < cc_count; i++)
4554 {
4555 if (strcmp(support_country[i], country_code) == 0)
4556 {
4557 break;
4558 }
4559 }
4560
4561 if (i >= cc_count)
4562 {
4563 RLOGE("unspported country code %s\n", country_code);
4564 return -1;
4565 }
4566
4567 is_different = check_and_init_uci_config(country_code);
4568 if( is_different < 0 )
4569 {
4570 RLOGE("init set uci fail\n");
4571 return -1;
4572 }
4573
4574 ret = lynq_get_country_code(idx,check_current_code);
4575 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4576 {
4577 ret = lynq_wifi_disable();
4578 if(ret != 0 )
4579 {
4580 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4581 return -1;
4582 }
4583 }
4584
4585 return 0;
you.chen35020192022-05-06 11:30:57 +08004586}
4587
4588int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4589{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004590 RLOGD("enter lynq_get_connect_ap_mac\n");
4591 if (mac == NULL)
4592 {
4593 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004594 return -1;
4595 }
4596
4597 CHECK_IDX(idx, CTRL_STA);
4598 ap_info_s ap;
4599 ap.ap_mac[0] = '\0';
4600
qs.xiong9fbf74e2023-03-28 13:38:22 +08004601 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4602 {
you.chen35020192022-05-06 11:30:57 +08004603 return -1;
4604 }
4605 strcpy(mac, ap.ap_mac);
4606
4607 return 0;
4608}
4609
4610int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4611{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004612 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004613 struct ifaddrs *ifaddr_header, *ifaddr;
4614 struct in_addr * ifa;
4615 const char * ifaName = "wlan0";
4616 if (ip == NULL)
4617 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004618 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004619 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004620 }
you.chenf58b3c92022-06-21 16:53:48 +08004621
qs.xiong9fbf74e2023-03-28 13:38:22 +08004622 if (idx == 1)
4623 {
you.chen0df3e7e2023-05-10 15:56:26 +08004624 ifaName = inner_get_ap_interface_name();
4625 if (ifaName == NULL)
4626 {
4627 RLOGE("[lynq_get_interface_ip] ap name get fail");
4628 return -1;
4629 }
you.chen9ac66392022-08-06 17:01:16 +08004630 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004631 else if (idx != 0)
4632 {
you.chen35020192022-05-06 11:30:57 +08004633 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004634 }
you.chen35020192022-05-06 11:30:57 +08004635
you.chen9ac66392022-08-06 17:01:16 +08004636 if (getifaddrs(&ifaddr_header) == -1)
4637 {
you.chen35020192022-05-06 11:30:57 +08004638 perror("getifaddrs");
4639 return -1;
4640 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004641 }
you.chen35020192022-05-06 11:30:57 +08004642
4643
you.chen9ac66392022-08-06 17:01:16 +08004644 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4645 {
4646 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004647 continue;
you.chen9ac66392022-08-06 17:01:16 +08004648 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4649 {
4650 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4651 {
4652 // is a valid IP4 Address
4653 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4654 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004655 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004656 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004657 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004658 return 0;
4659 }
4660 }
4661 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004662 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004663 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004664 return -1;
you.chen35020192022-05-06 11:30:57 +08004665}
4666
4667int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4668{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004669 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004670 int count;
4671 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004672 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004673 char *split_words[128] = {0};
4674 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4675
4676 CHECK_WPA_CTRL(idx);
4677
4678 DO_REQUEST(lynq_get_mac_cmd);
4679
qs.xiong9fbf74e2023-03-28 13:38:22 +08004680 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4681 {
4682 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004683 return -1;
4684 }
4685
4686 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4687
qs.xiong9fbf74e2023-03-28 13:38:22 +08004688 if (count < 2)
4689 {
you.chen35020192022-05-06 11:30:57 +08004690 return -1;
4691 }
4692
qs.xiong9fbf74e2023-03-28 13:38:22 +08004693 for (i=0; i < strlen(split_words[1]); i++ )
4694 {
4695 if (split_words[1][i] != ' ')
4696 {
you.chen35020192022-05-06 11:30:57 +08004697 break;
4698 }
4699 }
4700
qs.xiongdd6e44c2023-08-08 15:02:53 +08004701 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004702
4703 return 0;
4704}
4705
4706int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4707{
4708// int count;
4709// char *split_words[128] = {0};
4710// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4711
4712// if (rssi == NULL) {
4713// return -1;
4714// }
4715
4716// CHECK_IDX(idx, CTRL_STA);
4717
4718// CHECK_WPA_CTRL(CTRL_STA);
4719
4720// DO_REQUEST(lynq_get_rssi_cmd);
4721
4722// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4723// return -1;
4724// }
4725
4726// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4727
4728// if (count < 2) {
4729// return -1;
4730// }
4731
4732// *rssi = atoi(split_words[1]) * -1;
4733
you.chen35020192022-05-06 11:30:57 +08004734 char lynq_cmd_ret[MAX_RET]={0};
4735
qs.xiongff0ae0f2022-10-11 15:47:14 +08004736/*******change other cmd to get rssi*******
4737 *
4738 *wl rssi ---> wl -i wlan0 rssi
4739 *
4740 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004741 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004742 {
you.chen23c4a5f2023-04-12 16:46:00 +08004743 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004744 return -1;
4745 }
you.chen9f17e4d2022-06-06 17:18:18 +08004746 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004747/****** if got rssi is 0,means sta didn't connected any device****/
4748 if(*rssi == 0)
4749 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004750 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004751 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004752 }
you.chen35020192022-05-06 11:30:57 +08004753
4754 return 0;
4755}
4756
4757int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4758{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004759 RLOGD("enter lynq_get_connect_ap_band\n");
4760 if (band == NULL)
4761 {
you.chen35020192022-05-06 11:30:57 +08004762 return -1;
4763 }
4764
4765 CHECK_IDX(idx, CTRL_STA);
4766 ap_info_s ap;
4767 ap.band = -1;
4768
qs.xiong9fbf74e2023-03-28 13:38:22 +08004769 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4770 {
you.chen35020192022-05-06 11:30:57 +08004771 return -1;
4772 }
4773 *band = ap.band;
4774
4775 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004776}
you.chenf58b3c92022-06-21 16:53:48 +08004777
4778int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4779{
you.chenb95401e2023-05-12 19:39:06 +08004780 int ret;
4781 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004782 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004783
4784 if (ip == NULL)
4785 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004786 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004787 return -1;
4788 }
4789
4790 CHECK_IDX(idx, CTRL_STA);
4791
qs.xionge4cbf1c2023-02-28 18:22:49 +08004792 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004793 {
4794 return -1;
4795 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004796
you.chenb95401e2023-05-12 19:39:06 +08004797 ip[0] = '\0';
4798 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4799 if (ret != 0)
4800 {
4801 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4802 return -1;
4803 }
4804
4805 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4806 {
4807 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004808 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004809 if (ret != 0)
4810 {
4811 ip[0] = '\0';
4812 return 0;
4813 }
4814 else
4815 {
4816 p = strchr(ip, '\n');
4817 if (p != NULL)
4818 {
4819 *p = '\0';
4820 }
4821 }
4822 }
4823 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004824}
4825
qs.xionge02a5252023-09-20 14:00:21 +08004826int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4827{
4828 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4829 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4830}
4831
qs.xiong026c5c72022-10-17 11:15:45 +08004832int lynq_ap_connect_num(int sta_number)
4833{
4834 char lynq_limit_cmd[32]={0};
4835 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004836 if((sta_number < 1 ) && (sta_number > 15))
4837 {
4838 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004839 return -1;
4840 }
4841 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4842 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004843 if(ret != 0)
4844 {
4845 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004846 }
4847 return 0;
4848}
you.chenf58b3c92022-06-21 16:53:48 +08004849
qs.xiong77905552022-10-17 11:19:57 +08004850int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4851{
4852
4853 char lynq_wifi_acs_cmd[128]={0};
4854 char lynq_cmd_mode[128]={0};
4855 char lynq_cmd_slect[128]={0};
4856
qs.xiong9fbf74e2023-03-28 13:38:22 +08004857 if((acs_mode != 2) && (acs_mode != 5))
4858 {
qs.xiong77905552022-10-17 11:19:57 +08004859 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4860 }
4861
qs.xiong9fbf74e2023-03-28 13:38:22 +08004862 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4863 {
qs.xiong77905552022-10-17 11:19:57 +08004864 return -1;
4865 }
4866
4867 CHECK_IDX(idx, CTRL_AP);
4868
4869 CHECK_WPA_CTRL(CTRL_AP);
4870
4871 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4872 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4873 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4874
4875 DO_OK_FAIL_REQUEST(cmd_disconnect);
4876 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4877 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4878 DO_OK_FAIL_REQUEST(cmd_save_config);
4879 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4880
4881 return 0;
4882}
you.chen0f5c6432022-11-07 18:31:14 +08004883//you.chen add for tv-box start
4884static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4885 FILE *fp;
4886 //printf("to exec cmd:%s\n", str_cmd);
4887 if((fp=popen(str_cmd,"r"))==NULL)
4888 {
4889 perror("popen error!");
4890 return -1;
4891 }
4892 if((fread(str_cmd_ret,max_len,1,fp))<0)
4893 {
4894 perror("fread fail!");
4895 fclose(fp);
4896 return -1;
4897 }
4898 fclose(fp);
4899 return 0;
4900}
4901
4902static int get_netmask_length(const char* mask)
4903{
4904 int masklen=0, i=0;
4905 int netmask=0;
4906
4907 if(mask == NULL)
4908 {
4909 return 0;
4910 }
4911
4912 struct in_addr ip_addr;
4913 if( inet_aton(mask, &ip_addr) )
4914 {
4915 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004916 }else
4917 {
you.chen0f5c6432022-11-07 18:31:14 +08004918 netmask = 0;
4919 return 0;
4920 }
4921
4922 while(0 == (netmask & 0x01) && i<32)
4923 {
4924 i++;
4925 netmask = netmask>>1;
4926 }
4927 masklen = 32-i;
4928 return masklen;
4929}
4930
4931static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4932 int mask_len;
4933 char *p;
4934 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004935 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4936 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004937 return -1;
4938 p = strstr(str_cmd_ret, "Mask:");
4939 if (p == NULL)
4940 return -1;
4941 mask_len = get_netmask_length(p + 5);
4942 if (mask_len == 0)
4943 return -1;
4944 p = strstr(str_cmd_ret, "inet addr:");
4945 if (p == NULL)
4946 return -1;
4947 strcpy(tmp, p + 10);
4948 p = strstr(tmp, " ");
4949 if (p != NULL)
4950 *p = '\0';
4951 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4952 return 0;
4953}
4954
4955static void GBWWatchThreadProc() {
4956 int i,n, nloop, nmax, ncheckcount, nidlecount;
4957 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4958 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4959 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4960 char *results[16] = {0};
4961 char str_cmd[256] = {0};
4962 char str_cmd_ret[128] = {0};
4963 char dest_ip[32] = {0};
4964 lastAP1Bytes = lastAP2Bytes = 0;
4965 lastAP1Drop = lastAP2Drop = 0;
4966 lastAP1Speed = lastAP2Speed = 0;
4967 setAP1Speed = 50;
4968 setAP2Speed = 80;
4969 nloop = 0;
4970 nmax = 6;
4971 ncheckcount = nidlecount = 0;
4972
you.chen0df3e7e2023-05-10 15:56:26 +08004973 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004974 {
4975 RLOGE("------gbw thread run\n");
4976 return;
4977 }
4978
qs.xiong9fbf74e2023-03-28 13:38:22 +08004979 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004980 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4981 while (dest_ip[0] == '\0') {
4982 sleep(1);
4983 str_cmd_ret[0] = '\0';
4984 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4985 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4986 if (str_cmd_ret[n] == '\n'){
4987 str_cmd_ret[n] = '\0';
4988 break;
4989 }
4990 }
4991 if (str_cmd_ret[0] != '\0')
4992 {
4993 strcpy(dest_ip, str_cmd_ret);
4994 }
4995 }
4996
you.chenc9928582023-04-24 15:39:37 +08004997 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4998 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4999 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 +08005000 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5001 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005002 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005003 return;
5004 }
you.chenc9928582023-04-24 15:39:37 +08005005 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);
5006 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);
5007 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 +08005008
5009 while (1) {
5010 sleep(1);
5011 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005012 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5013 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5014 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005015 continue;
5016 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005017 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005018 if (n > 9) {
5019 if (strcmp(results[1], "Sent") == 0) {
5020 currAP1Bytes = atoll(results[2]);
5021 }
5022 if (strcmp(results[6], "(dropped") == 0) {
5023 currAP1Drop = atoi(results[7]);
5024 }
5025 }
5026
5027 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005028 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5029 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5030 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005031 continue;
5032 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005033 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005034 if (n > 9) {
5035 if (strcmp(results[1], "Sent") == 0) {
5036 currAP2Bytes = atoll(results[2]);
5037 }
5038 if (strcmp(results[6], "(dropped") == 0) {
5039 currAP2Drop = atoi(results[7]);
5040 }
5041 }
5042
5043 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5044 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5045 lastAP1Bytes = currAP1Bytes;
5046 lastAP2Bytes = currAP2Bytes;
5047 continue;
5048 }
5049
5050 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5051 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5052 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5053 lastAP1Speed = currAP1Speed;
5054 lastAP2Speed = currAP2Speed;
5055 lastAP1Bytes = currAP1Bytes;
5056 lastAP2Bytes = currAP2Bytes;
5057
5058 currSetAP1Speed = setAP1Speed;
5059 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5060 ncheckcount++;
5061 if (ncheckcount > 3) {
5062 ncheckcount = 0;
5063 currSetAP1Speed = 5;
5064 }
5065 }
5066 else {
5067 ncheckcount = 0;
5068 if (currAP1Speed < 5)
5069 nidlecount++;
5070 else
5071 nidlecount = 0;
5072
5073 }
5074
5075 if (nidlecount > 60 ){
5076 currSetAP1Speed = 50;
5077 }
5078
5079 if (currSetAP1Speed != setAP1Speed) {
5080 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005081 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5082 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005083 }
5084 }
5085}
5086
5087int enableGBW(const char* mac) {
5088 int i,len;
5089 char get_ipaddr_cmd[128]={0};
5090 ap_info_s *ap;
5091 device_info_s * list;
5092
5093 if (mac == NULL || g_gbw_enabled == 1)
5094 return -1;
5095 len = strlen(mac);
5096 g_gbw_mac = malloc(len + 1);
5097 for(i=0;i<len;i++) {
5098 if (mac[i] >= 'A' && mac[i] <= 'Z')
5099 {
5100 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5101 }
5102 else
5103 g_gbw_mac[i] = mac[i];
5104 }
5105 g_gbw_mac[i] = '\0';
5106 g_gbw_enabled = 1;
5107
5108 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5109 if (system(get_ipaddr_cmd) == 0) {
5110 //startGBW();
5111 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5112 for (i=0;i<len;i++) {
5113 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5114 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5115 startGBW();
5116 }
5117 free(ap);
5118 free(list);
5119 }
5120 }
5121 return 0;
5122}
5123
5124int disableGBW() {
5125 stopGBW();
5126 free(g_gbw_mac);
5127 g_gbw_mac = NULL;
5128 g_gbw_enabled = 1;
5129 return 0;
5130}
5131
5132static int startGBW() {
5133 if (g_gbw_watcher_pid != 0) {
5134 stopGBW();
5135 }
5136 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5137}
5138
5139static int stopGBW() {
5140 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005141 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005142 pthread_cancel(g_gbw_watcher_pid);
5143 pthread_join(g_gbw_watcher_pid, &retval);
5144 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005145 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5146 if (s_ap_iterface_name[0] != '\0')
5147 {
5148 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5149 system(cmd);
5150 }
you.chen0f5c6432022-11-07 18:31:14 +08005151}
5152//you.chen add for tv-box end