blob: 221170ca8c846f9c6f1c54de12adecc61befd649 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen0c9bee22023-10-25 13:03:14 +080057volatile int g_sta_fake_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
70//const char * CTRL_PATH="/var/run/wpa_supplicant";
71const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
72//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
73const char * cmd_list_networks = "LIST_NETWORKS";
74const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080075const char * cmd_disconnect = "DISCONNECT";
76const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080077const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080078const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080079const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080080const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080081
you.chenf711c8a2023-04-13 13:49:45 +080082const char * cmd_ping = "PING";
83const char * rsp_pong = "PONG";
84const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
85const int MAX_IDLE_COUNT = 600; // 60s
86
you.chenc9928582023-04-24 15:39:37 +080087const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
88const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
89const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
90const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
91const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
92
93static char s_ap_iterface_name[64] = {0};
94
you.chend2fef3f2023-02-13 10:50:35 +080095struct local_wpa_ctrl{
96 struct wpa_ctrl *ctrl;
97 pthread_mutex_t mutex;
98};
99
qs.xiong09560402023-10-27 21:58:55 +0800100volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800101int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800102
you.chend2fef3f2023-02-13 10:50:35 +0800103static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800104static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
105static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800106// add for auto connect
107static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800108
109static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800110
you.chen0f5c6432022-11-07 18:31:14 +0800111//you.chen add for tv-box start
112volatile int g_gbw_enabled = 0;
113char * g_gbw_mac = NULL;
114pthread_t g_gbw_watcher_pid = 0;
115static int startGBW();
116static int stopGBW();
117//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800118
119typedef struct __curr_status_info {
120 ap_info_s *ap;
121 char * state;
122 int net_no;
123}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400124
you.chen70f377f2023-04-14 18:17:09 +0800125typedef enum {
126 INNER_STA_STATUS_INIT = 0,
127 INNER_STA_STATUS_CONNECTING,
128 INNER_STA_STATUS_ASSOCIATING,
129 INNER_STA_STATUS_ASSOCIATED,
130 INNER_STA_STATUS_CONNECTED,
131 INNER_STA_STATUS_DISCONNECTING,
132 INNER_STA_STATUS_DISCONNECTED,
133 INNER_STA_STATUS_CANCEL,
134}inner_sta_status_s;
135
136static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
137static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800138volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800139static error_number_s s_sta_error_number = -1;
140static char s_sta_current_connecting_ssid[64] = {0};
141static struct timespec s_sta_connect_timeout;
142const int MAX_CONNNECT_TIME = 15; // second
143pthread_t g_global_watcher_pid = 0;
144static int s_service_invoke_timeout_cnt=0;
145const int FAKE_MAX_INT_VALUE = 99999;
146
qs.xiong09560402023-10-27 21:58:55 +0800147static void print_disconnect_list()
148{
149 int i;
150 for( i = 0; i < g_history_disconnect_valid_num; i++ )
151 {
152 RLOGD(" list of g_history_disconnect_valid_num is %d histroy_list[%d]:%d --------- %d",g_history_disconnect_valid_num,i,g_history_disconnect_net[i],__LINE__);
153 }
154
155 return;
156}
157
158// idex ----> history_disconnect_list[x] index
159static int removeElement(int idex)
160{
161 RLOGD("into removeElement");
162 if( index < 0 )
163 {
164 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
165 return -1;
166 }
167 RLOGD("%s line: %d g_history_disconnect_net[%d]: %d end g_history_disconnect_net[%d]:%d",__func__,__LINE__,idex,g_history_disconnect_net[idex],g_history_disconnect_valid_num-1, g_history_disconnect_net[g_history_disconnect_valid_num-1]);
168 g_history_disconnect_net[idex] = g_history_disconnect_net[g_history_disconnect_valid_num-1]; //g_history_disconnect_vaild_num -1 for get last index
169 g_history_disconnect_valid_num --;
170 RLOGD("end removeElement");
171 return 0;
172}
173static int check_history_disconenct_ap_list(int val)
174{
175 print_disconnect_list();
176 RLOGD("WIFI[check_history_disconenct_ap_list]into check_history_disconenct_ap_list && input val is %d g_history_disconnect_valid_num is %d line",val,g_history_disconnect_valid_num,__LINE__);
177 int i;
178 for( i = 0; i < g_history_disconnect_valid_num; i++)
179 {
180 if( val == g_history_disconnect_net[i] )
181 {
182 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
183 RLOGD("end check_history_disconenct_ap_list && return network index");
184 return i;
185 }
186 }
187 RLOGD("end check_history_disconenct_ap_list && return fail,didn't need remove networkid %d from list g_history_disconnect_valid_num is %d line %d",val,g_history_disconnect_valid_num,__LINE__);
188 return -1;
189}
190
191
192static void lynq_sta_removeElement(int net_no)
193{
194 int ret;
195
196 ret = check_history_disconenct_ap_list(net_no);
197 if( ret == -1 )
198 {
199 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
200 return;
201 }else
202 {
203 ret = removeElement(ret);
204 if( ret == 0 )
205 {
206 RLOGD("removeElement pass %s %d",__func__,__LINE__);
207 return;
208 }
209 }
210
211 return;
212}
213
you.chen70f377f2023-04-14 18:17:09 +0800214static void notify_service_invoke_fail(int error)
215{
216 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
217 pthread_mutex_lock(&s_global_check_mutex);
218 if (error == -2) //timeout
219 {
220 s_service_invoke_timeout_cnt++;
221 if (s_service_invoke_timeout_cnt > 10)
222 {
223 pthread_cond_signal(&s_global_check_cond);
224 }
225 }
226 else if (error == -1)
227 {
228 // check if can connect wpa service
229 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
230 if (lynq_wpa_ctrl == NULL)
231 {
232 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
233 pthread_cond_signal(&s_global_check_cond);
234 }
235 wpa_ctrl_close(lynq_wpa_ctrl);
236 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
237 if (lynq_wpa_ctrl == NULL)
238 {
239 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
240 pthread_cond_signal(&s_global_check_cond);
241 }
242 wpa_ctrl_close(lynq_wpa_ctrl);
243 }
244
245 pthread_mutex_unlock(&s_global_check_mutex);
246}
247
you.chenc9928582023-04-24 15:39:37 +0800248static int system_call_v(const char * fmt, ...)
249{
250 char str_cmd[256] = {0};
251 va_list args;
252 va_start(args, fmt);
253 vsprintf(str_cmd, fmt, args);
254 va_end(args);
255 printf("system call----------%s\n", str_cmd);
256 return system(str_cmd);
257}
258
you.chen0df3e7e2023-05-10 15:56:26 +0800259static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
260
261static const char * inner_get_ap_interface_name()
262{
263 char * p;
264 char cmd[128]={0};
265
266 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
267 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
268 {
269 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
270 return NULL;
271 }
272 p = strchr(s_ap_iterface_name, ' ');
273 if (NULL != p)
274 {
275 *p = '\0';
276 }
277 p = strchr(s_ap_iterface_name, '\n');
278 if (NULL != p)
279 {
280 *p = '\0';
281 }
282 if (s_ap_iterface_name[0] == '\0')
283 {
284 return NULL;
285 }
286
287 return s_ap_iterface_name;
288}
289
you.chen70f377f2023-04-14 18:17:09 +0800290static void check_tether_and_notify()
291{
292 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800293 if (inner_get_ap_interface_name() == NULL || 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chen70f377f2023-04-14 18:17:09 +0800294 {
295 return;
296 }
297 pthread_mutex_lock(&s_global_check_mutex);
298 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
299 pthread_cond_signal(&s_global_check_cond);
300 pthread_mutex_unlock(&s_global_check_mutex);
301}
302
you.chend2fef3f2023-02-13 10:50:35 +0800303static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
304 char *reply, size_t *reply_len,
305 void (*msg_cb)(char *msg, size_t len))
306{
307 int ret;
308 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800310 return -1;
311 }
312 pthread_mutex_lock(&ctrl->mutex);
313 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
314 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800315 if (ret != 0)
316 {
317 notify_service_invoke_fail(ret);
318 }
you.chend2fef3f2023-02-13 10:50:35 +0800319 return ret;
320}
321
322static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
323 int repeat_cnt;
324 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
325 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800326 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800327 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
328 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
329// printf("wait enable finish\n");
330 usleep(500 * 1000);
331 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
332 }
333 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800334 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800335 goto out_addr;
336 }
337 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
338 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
339 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800340 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800341 goto out_addr;
342 }
343 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
344 }
345 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
346out_addr:
347 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
348 return lynq_wpa_ctrl;
349}
350
qs.xiong97fa59b2022-04-07 05:41:29 -0400351#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400352{\
you.chen35020192022-05-06 11:30:57 +0800353 perror((str));\
354 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400355}
356
you.chen35020192022-05-06 11:30:57 +0800357#define CHECK_IDX(idx, type) do { \
358 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
359 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800360 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800361 return -1; \
362 } \
363 }while (0)
364
365#define CHECK_WPA_CTRL(index) int ret = 0;\
366 size_t reply_len = MAX_RET; \
367 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800368 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800369 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800370 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
371 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800372 }while(0)
373
374#define DO_REQUEST(cmd_str) do { \
375 reply_len = MAX_RET;\
376 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800377 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800378 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800379 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800380 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800381 return ret; \
382 } \
383 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800384 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800385 }while(0)
386
387#define DO_OK_FAIL_REQUEST(cmd_str) do { \
388 DO_REQUEST(cmd_str); \
389 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800390 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800391 return -1; \
392 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800393 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800394 return -1; \
395 } \
396 }while (0)
397
398
you.chenf711c8a2023-04-13 13:49:45 +0800399static int check_connection(struct wpa_ctrl * wpa_ctrl)
400{
401 size_t reply_len = MAX_RET;
402 char cmd_reply[MAX_RET]={0};
403 int ret;
404
405 RLOGD("check_connection [%p]", wpa_ctrl);
406 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
407
408 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
409 {
410 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chen70f377f2023-04-14 18:17:09 +0800411 if (ret != 0)
412 {
413 notify_service_invoke_fail(ret);
414 }
you.chenf711c8a2023-04-13 13:49:45 +0800415 return -1;
416 }
417
418 return 0;
419}
420
421/**
422 * @brief check_pending_msg
423 * @param lynq_wpa_ctrl
424 * @return 1 has msg, 0 no msg, -1 error
425 */
426static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
427{
428 int ret;
429
430 if (*pp_lynq_wpa_ctrl == NULL) // need connect
431 {
432 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
433 if (*pp_lynq_wpa_ctrl == NULL)
434 {
435 usleep(SLEEP_TIME_ON_IDLE);
436 return -1;
437 }
438
439 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
440 if (ret == 0) // attach success
441 {
442 *started_flag = 1;
443 }
444 else
445 {
you.chen70f377f2023-04-14 18:17:09 +0800446 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800447 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
448 *pp_lynq_wpa_ctrl = NULL;
449 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800450 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800451 usleep(SLEEP_TIME_ON_IDLE);
452 return -1;
453 }
454 }
455
456 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
457 if ( ret == 0) // no pending messages
458 {
459 usleep(SLEEP_TIME_ON_IDLE);
460 *idle_count += 1;
461 if (*idle_count > MAX_IDLE_COUNT)
462 {
463 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
464 {
465 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
466 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
467 *pp_lynq_wpa_ctrl = NULL;
468 *idle_count = 0;
469 return -1;
470 }
471 *idle_count = 0;
472 }
473 return 0;
474 }
475 else if ( ret == -1) // on error
476 {
477 RLOGE("[wifi error]sta wpa_ctrl_pending");
478 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
479 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
480 *pp_lynq_wpa_ctrl = NULL;
481 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800482 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800483 return -1;
484 }
485
486 *idle_count = 0;
487 return 1;
488}
489
you.chen6d247052023-06-01 16:39:54 +0800490static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
491{
492 pthread_mutex_lock(&s_ap_callback_mutex);
493 if (g_ap_callback_func != NULL)
494 g_ap_callback_func(g_ap_callback_priv, status);
495 pthread_mutex_unlock(&s_ap_callback_mutex);
496}
497
you.chen35020192022-05-06 11:30:57 +0800498static void APWatcherThreadProc() {
499 size_t len = MAX_RET;
500 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800501 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800502
you.chen6c2dd9c2022-05-16 17:55:28 +0800503 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800504 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800505
qs.xiong9fbf74e2023-03-28 13:38:22 +0800506 while (g_ap_watcher_stop_flag == 0)
507 {
you.chenf711c8a2023-04-13 13:49:45 +0800508 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
509 {
you.chen70f377f2023-04-14 18:17:09 +0800510 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
511 {
512 check_tether_and_notify();
513 }
514
you.chen35020192022-05-06 11:30:57 +0800515 continue;
516 }
you.chenf711c8a2023-04-13 13:49:45 +0800517
you.chen6c2dd9c2022-05-16 17:55:28 +0800518 memset(msg_notify, 0, MAX_RET);
519 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800520 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800521 {
you.chen35020192022-05-06 11:30:57 +0800522 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800523 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800524 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800525 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800526 {
you.chen6d247052023-06-01 16:39:54 +0800527 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800528 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800529 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800530 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
531 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800532 {
you.chen0f5c6432022-11-07 18:31:14 +0800533 stopGBW();
534 }
535 }
you.chen35020192022-05-06 11:30:57 +0800536 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800537 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800538 {
you.chen6d247052023-06-01 16:39:54 +0800539 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800540 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800541 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800542 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
543 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800544 {
you.chen0f5c6432022-11-07 18:31:14 +0800545 startGBW();
546 }
547 }
you.chen35020192022-05-06 11:30:57 +0800548 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800549 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800550 {
551 RLOGD("APWatcherThreadProc ap------> service error");
552 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
553 }
554 else
555 {
556 RLOGD("APWatcherThreadProc ap------> going on check next msg");
557 }
you.chenf711c8a2023-04-13 13:49:45 +0800558 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800559 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
560 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800561 if (lynq_wpa_ctrl != NULL)
562 {
you.chen92fd5d32022-05-25 10:09:47 +0800563 wpa_ctrl_detach(lynq_wpa_ctrl);
564 wpa_ctrl_close(lynq_wpa_ctrl);
565 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400566}
567
you.chen70f377f2023-04-14 18:17:09 +0800568static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
569{
570 char * p;
571 const char * try_associat_flag = "Trying to associate";
572 const char * associated_flag = "Associated with ";
573
574 pthread_mutex_lock(&s_global_check_mutex);
575 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
576 {
577 pthread_mutex_unlock(&s_global_check_mutex);
578 return;
579 }
580
you.chen6e724162023-10-19 19:10:01 +0800581 // youchen@2023-10-17 add for "not notify connect fail directly" begin
582 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
583 {
584 s_sta_error_number = error_num;
585 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
586 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
587 pthread_cond_signal(&s_global_check_cond);
588 pthread_mutex_unlock(&s_global_check_mutex);
589 return;
590 }
591 // youchen@2023-10-17 add for "not notify connect fail directly" end
592
you.chen70f377f2023-04-14 18:17:09 +0800593 if (state == LYNQ_WIFI_STATUS_EGNORE)
594 {
595 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
596 {
597 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
598 }
599 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
600 {
601 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
602 }
603 }
604 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
605 {
606 s_sta_error_number = error_num;
607 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
608 {
609 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800610 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800611 pthread_cond_signal(&s_global_check_cond);
612 }
613 }
614 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
615 {
616 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800617 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800618 pthread_cond_signal(&s_global_check_cond);
619 }
620 pthread_mutex_unlock(&s_global_check_mutex);
621}
622
qs.xiongb37f8c42023-09-13 21:21:58 +0800623static int lynq_split(char * str, int len, char delimiter, char * results[]);
624static inline char inner_convert_char(char in);
625static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
626static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
627
628
629
qs.xiong44fac672023-08-29 16:15:55 +0800630/*
631just tmp add for fix sta connect ap fail check ap connect info
632return 0 --->Current no sta device connect this AP
633*/
634static int lynq_connected_ap_sta_status() {
635
636 FILE *fp;
637 size_t i = 0;
638 int ret;
639 char lynq_cmd_ret[MAX_RET]={0};
640
641 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
642 {
643 perror("popen error!");
644 return -1;
645 }
646 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
647 {
648 perror("fread fail!");
649 ret=pclose(fp);
650 if(ret == -1)
651 perror("close file faild");
652 return -1;
653 }
654 if( strlen(lynq_cmd_ret) < MAC_LEN)
655 {
656 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
657 ret=pclose(fp);
658 if(ret==-1)
659 {
660 perror("close file faild");
661 }
662 return 0;
663 }else{
664 ret=pclose(fp);
665 if(ret==-1)
666 {
667 perror("close file faild");
668 }
669 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
670 return 1;
671 }
672}
673
674/*
675 just tmp add for fix sta connect ap fail; check fw status
676 return 1 ----> fw status error; need wl down/up
677*/
678static int check_current_fw_status() {
679
680 FILE *fp;
681 FILE *fp1;
682 size_t i = 0;
683 int ret;
684 char lynq_cmd_ret_2g[MAX_RET]={0};
685 char lynq_cmd_ret_5g[MAX_RET]={0};
686
687 const char * fw_status = "0x0096"; //0x0096 is normal fw status
688
689 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
690 {
691 perror("popen error!");
692 return -1;
693 }
694 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
695 {
696 perror("fread fail!");
697 if(pclose(fp) == -1)
698 perror("close fp file faild");
699 return -1;
700 }
701
702 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
703 {
704 perror("popen error!");
705 if(pclose(fp) == -1)
706 perror("clsoe fp file faild");
707 return -1;
708 }
709 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
710 {
711 perror("fread fail!");
712 if(pclose(fp1) == -1)
713 perror("clsoe fp1 file faild");
714 if(pclose(fp) == -1)
715 perror("clsoe fp file faild");
716 return -1;
717 }
718
719 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
720 {
721 ret=pclose(fp);
722 if(ret==-1)
723 {
724 perror("close fp file faild");
725 }
726 ret=pclose(fp1);
727 if(ret==-1)
728 {
729 perror("close fp1 file faild");
730 }
731 return 0;
732 }else
733 {
734 ret=pclose(fp);
735 if(ret==-1)
736 {
737 perror("close file faild");
738 }
739 if(pclose(fp1) == -1)
740 {
741 perror("clsoe file fp1 faild");
742 }
743 RLOGD("current fw status --error--");
744 return 1;
745 }
746}
747
qs.xiong1d4263a2023-09-06 10:46:23 +0800748/*
749eg: wl counters info
750sh-3.2# wl counters
751counters_version 30
752datalen 1648
753Slice_index: 0
754reinitreason_counts: 0(0) 1(0) 2(3) 3(0) 4(0) 5(0) 6(0) 7(0) 8(0) 9(0) 10(0) 11(0) 12(0) 13(0) 14(2) 15(0) 16(0) 17(0) 18(0) 19(0) 20(0) 21(0) 22(0) 23(0) 24(0) 25(0) 26(0) 27(0) 28(0) 29(0) 30(0) 31(0) 32(0) 33(0) 34(0) 35(0) 36(0) 37(0) 38(0) 39(0) 40(0) 41(0) 42(0) 43(0) 44(0) 45(0) 46(0) 47(0) 48(0) 49(0) 50(0) 51(0) 52(0) 53(0) 54(0)
755reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
756
757check reinit status
758return 0 ===> fw did wl reinit cmd
759*/
760static int check_current_reinit_info()
761{
762 FILE *fp;
763 int ret;
764 char lynq_cmd_ret[MAX_RET]={0};
765 char * dest;
766 char destid[3]={0};
767 if((fp=popen("wl counters","r"))==NULL)
768 {
769 perror("popen error!");
770 return -1;
771 }
772 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
773 {
774 perror("fread fail!");
775 if(pclose(fp) == -1)
776 {
777 perror("close fp file faild");
778 }
779 return -1;
780 }
781 dest = strstr(lynq_cmd_ret,"reinit ");
782 if(dest != NULL)
783 {
784 dest +=strlen("reinit ");
785 RLOGD("current get dest str is %s",dest);
786 memcpy(destid,dest,2);
787 ret = atoi(destid);
788 RLOGD("get current wl counters cmd return counts is %d",ret);
789 if( ret != 0 )
790 {
791 RLOGD("current fw did run cmd wl reinit");
792 if( pclose(fp) == -1 )
793 {
794 perror("close fp file faild");
795 }
796 return 0;
797 }
798 }
799 if( pclose(fp) == -1 )
800 {
801 perror("close fp file faild");
802 }
803 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
804 return -1;
805}
806
qs.xiong44fac672023-08-29 16:15:55 +0800807static void APTmpWatcherThreadProc() {
808
809 int i = 0;
810 int delytime = 300;
811 g_ap_tmp_watcher_stop_flag = 0;
812
813 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
814 while(1)
815 {
816 sleep(1);
817 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800818 if ( (i % 30) == 0 )
819 {
820 if ( check_current_reinit_info() == 0 )
821 {
822 system("wl reset_cnts");
823 system("wl down");
824 system("wl up");
825 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
826 }
827 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800828 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800829 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800830 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800831 {
832 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
833 {
834 system("wl down");
835 system("wl up");
836 }
qs.xiong44fac672023-08-29 16:15:55 +0800837 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800838 }
839 if ( i == delytime )
840 {
qs.xiong44fac672023-08-29 16:15:55 +0800841 i = 0;
842 }
843 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
844 {
845 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
846 return;
847 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800848
qs.xiong44fac672023-08-29 16:15:55 +0800849 }
850
851}
852
qs.xiongf0128b12023-06-29 17:29:39 +0800853static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
854{
855 char LYNQ_DISABLE_CMD[128]={0};
856
857 CHECK_IDX(idx, CTRL_STA);
858 CHECK_WPA_CTRL(CTRL_STA);
859
860 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800861 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800862 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800863
qs.xiongf0128b12023-06-29 17:29:39 +0800864 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800865
qs.xiongf0128b12023-06-29 17:29:39 +0800866}
867
qs.xiongb37f8c42023-09-13 21:21:58 +0800868static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
869{
870 char LYNQ_ENABLE_CMD[128]={0};
871
872 CHECK_IDX(idx, CTRL_STA);
873 CHECK_WPA_CTRL(CTRL_STA);
874
875
876 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800877 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800878 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
879
880 return 0;
881
882}
883
884static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
885{
886
887 int index,networkid;
888
889 for ( index = 0; index < len ;index++)
890 {
891 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800892 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800893 {
894 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
895 }
896 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
897 }
898 return 0;
899
900}
901
902
903/*
904 dis_net_list user disconnect list
905*/
906static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
907{
qs.xiong09560402023-10-27 21:58:55 +0800908 RLOGD("enter %s %d",__func__,__LINE__);
909 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800910 int count,ncount,index;
911 int flag = 0;
912 int merge_index = 0;
913 int net_no_list[128];
914
qs.xiong09560402023-10-27 21:58:55 +0800915 for(ncount = 0;ncount < valid_num; ncount++ )
916 {
917 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
918 }
919
qs.xiongb37f8c42023-09-13 21:21:58 +0800920 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
921 for( count = 0; count < index; count++)
922 {
923 for(ncount = 0; ncount < valid_num; ncount++)
924 {
qs.xiong09560402023-10-27 21:58:55 +0800925 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800926 if(net_no_list[count] == dis_net_list[ncount])
927 {
qs.xiong09560402023-10-27 21:58:55 +0800928 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800929 flag = 1;
930 break;
931 }
932 }
933 if( flag != 1 )
934 {
935 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800936 RLOGD("out[%d]: %d net_no_list[%d]: %d %d",merge_index,out[merge_index],count,net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800937 merge_index ++;
938 }
939 flag = 0;
940 }
941 * outlen =merge_index;
942 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
943 return;
944}
qs.xiongf0128b12023-06-29 17:29:39 +0800945
qs.xiong455c30b2023-04-12 11:40:02 +0800946void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
947{
948 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800949 char *wpanetid;
950 char destid[3] = {0};
951 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800952 *error = LYNQ_WAIT_CONNECT_ACTIVE;
953 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
954 {
955 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
956 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
957 return;
958 }
959
qs.xiong20202422023-09-06 18:01:18 +0800960 if (strstr(modify, "Trying to associate") != NULL)
961 {
962 RLOGD("Current sta is Trying to associate");
963 *state = LYNQ_WIFI_STATUS_EGNORE;
964 g_sta_conncet_status_flag = 1;
965 return;
966 }
967
qs.xiong455c30b2023-04-12 11:40:02 +0800968 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
969 {
970 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800971 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800972 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800973 return;
974 }
975
qs.xiong1e81dfa2023-09-27 15:52:37 +0800976 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
977 {
978 *error = LYNQ_PSW_ERROR;
979 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
980 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
981 g_sta_conncet_status_flag = 0;
982 return;
983 }
qs.xiong455c30b2023-04-12 11:40:02 +0800984 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
985 {
qs.xiongf0128b12023-06-29 17:29:39 +0800986 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
987 wpanetid = strstr(modify,"id=");
988 if ( wpanetid != NULL )
989 {
990 wpanetid +=strlen("id=");
991 memcpy(destid,wpanetid,2);
992 tmpdisid = atoi(destid);
993
994 }
qs.xiong455c30b2023-04-12 11:40:02 +0800995 pReason = strstr(modify, "reason=");
996 if (pReason != NULL)
997 {
998 pReason += strlen("reason=");
999 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1000 {
1001 *error = LYNQ_TIME_OUT;
1002 }
1003 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1004 {
1005 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001006 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001007 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001008 // you.chen@2023-10-17 only disable network during autoconnect
1009 if(tmpdisid != -1 && s_sta_status == INNER_STA_STATUS_INIT && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
qs.xiongf0128b12023-06-29 17:29:39 +08001010 {
1011 RLOGE("stop wlan0 network %d fail",tmpdisid);
1012 }
qs.xiong455c30b2023-04-12 11:40:02 +08001013 }
1014 else
1015 {
1016 *error = LYNQ_UNSPECIFIED_REASON;
1017 }
qs.xiong455c30b2023-04-12 11:40:02 +08001018 }
1019 else
1020 {
1021 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001022 }
qs.xiongf0128b12023-06-29 17:29:39 +08001023 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001024 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001025 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001026
1027 }
1028
1029 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1030 {
1031 *error = LYNQ_NOT_FIND_AP;
1032 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1033 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001034 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001035 return;
1036 }
1037
1038
1039 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1040 {
1041 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1042 pReason = strstr(modify, "status_code=");
1043 if (pReason != NULL)
1044 {
1045 pReason += strlen("status_code=");
1046 if (memcmp(pReason, "17", 2) == 0)
1047 {
1048 *error = LYNQ_AP_UNABLE_HANDLE;
1049 }
1050 else if (memcmp(pReason, "1",1) == 0)
1051 {
1052 *error = LYNQ_UNSPECIFIED_REASON;
1053 }
1054 else
1055 {
1056 *error = LYNQ_UNSPECIFIED_REASON;
1057 }
1058
1059 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1060 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001061 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001062 return;
1063 }
1064 else
1065 {
1066 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1067 *error = LYNQ_UNSPECIFIED_REASON;
1068 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1069 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1070 return;
1071 }
1072 }
1073
1074 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1075 {
1076 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1077 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1078 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1079 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1080 return;
1081 }
1082
1083 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1084 {
1085 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1086 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1087 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1088 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1089 return;
1090 }
1091
qs.xiongdf637b22023-10-26 19:30:07 +08001092 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1093 if (strstr(modify, "Association request to the driver failed") != NULL)
1094 {
1095 RLOGD("Association request to the driver failed --- recover");
1096 system("wl down");
1097 system("wl up");
1098 *error = LYNQ_UNSPECIFIED_REASON;
1099 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1100 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1101 return;
1102 }
1103 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1104
1105
you.chen32cb31e2023-04-13 14:05:45 +08001106 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001107 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001108 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001109 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1110 return;
1111
1112}
1113
qs.xiongfcc914b2023-07-06 21:16:20 +08001114void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1115{
1116 char *pReason;
1117 char *wpanetid;
1118 char destid[3];
1119 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1120 *networkid = -1;
1121 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1122 {
1123 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1124 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1125 return;
1126 }
1127 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1128 {
1129 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1130 wpanetid = strstr(modify,"id=");
1131 if ( wpanetid != NULL )
1132 {
1133 wpanetid +=strlen("id=");
1134 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1135 if (memcpy(destid,wpanetid,2) != NULL)
1136 {
1137 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1138 *networkid = atoi(destid);
1139 RLOGD("get networkid is %d\n",*networkid);
1140 }
1141 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1142 }
1143 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1144 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1145 return;
1146 }
1147 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1148 {
1149 wpanetid = strstr(modify,"id=");
1150 if ( wpanetid != NULL )
1151 {
1152 wpanetid +=strlen("id=");
1153 if (memcpy(destid,wpanetid,2) != NULL)
1154 {
1155 *networkid = atoi(destid);
1156 RLOGD("get networkid is %d\n",*networkid);
1157 }
1158 }
1159 pReason = strstr(modify, "reason=");
1160 if (pReason != NULL)
1161 {
1162 pReason += strlen("reason=");
1163 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1164 {
1165 *error = LYNQ_TIME_OUT;
1166 }
1167 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1168 {
1169 *error = LYNQ_PSW_ERROR;
1170 }
1171 else
1172 {
1173 *error = LYNQ_UNSPECIFIED_REASON;
1174 }
1175 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1176 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1177 return;
1178 }
1179 else
1180 {
1181 *error = LYNQ_UNSPECIFIED_REASON;
1182 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1183 return;
1184 }
1185 }
1186 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1187 {
1188 wpanetid = strstr(modify,"id=");
1189 if ( wpanetid != NULL )
1190 {
1191 wpanetid +=strlen("id=");
1192 if (memcpy(destid,wpanetid,2) != NULL)
1193 {
1194 *networkid = atoi(destid);
1195 RLOGD("get networkid is %d\n",*networkid);
1196 }
1197 }
1198 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1199 pReason = strstr(modify, "status_code=");
1200 if (pReason != NULL)
1201 {
1202 pReason += strlen("status_code=");
1203 if (memcmp(pReason, "17", 2) == 0)
1204 {
1205 *error = LYNQ_AP_UNABLE_HANDLE;
1206 }
1207 else if (memcmp(pReason, "1",1) == 0)
1208 {
1209 *error = LYNQ_UNSPECIFIED_REASON;
1210 }
1211 else
1212 {
1213 *error = LYNQ_UNSPECIFIED_REASON;
1214 }
1215 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1216 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1217 return;
1218 }
1219 else
1220 {
1221 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1222 *error = LYNQ_UNSPECIFIED_REASON;
1223 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001224 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001225 return;
1226 }
1227 }
1228 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1229 {
1230 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1231 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1232 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1233 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1234 return;
1235 }
1236 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1237 {
1238 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1239 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1240 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1241 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1242 return;
1243 }
1244 RLOGD("EVENT : %s\n", modify);
1245 *error = LYNQ_UNSPECIFIED_REASON;
1246 *state = LYNQ_WIFI_STATUS_EGNORE;
1247 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1248 return;
1249}
you.chen70f377f2023-04-14 18:17:09 +08001250static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1251{
you.chen6d247052023-06-01 16:39:54 +08001252 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001253 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1254 {
1255 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1256 g_sta_callback_func(g_sta_callback_priv, state, error);
1257 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1258 }
you.chen6d247052023-06-01 16:39:54 +08001259 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001260}
qs.xiongfcc914b2023-07-06 21:16:20 +08001261static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1262{
1263 pthread_mutex_lock(&s_sta_callback_mutex);
1264 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1265 {
1266 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1267 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1268 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1269 }
1270 pthread_mutex_unlock(&s_sta_callback_mutex);
1271}
you.chen70f377f2023-04-14 18:17:09 +08001272
you.chen35020192022-05-06 11:30:57 +08001273static void STAWatcherThreadProc() {
1274 size_t len = MAX_RET;
1275 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001276 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001277 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001278 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001279
you.chen6c2dd9c2022-05-16 17:55:28 +08001280 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001281 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001282
you.chen70f377f2023-04-14 18:17:09 +08001283 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001284 while (g_sta_watcher_stop_flag == 0)
1285 {
you.chenf711c8a2023-04-13 13:49:45 +08001286 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001287 {
you.chen35020192022-05-06 11:30:57 +08001288 continue;
1289 }
you.chenf711c8a2023-04-13 13:49:45 +08001290
you.chen6c2dd9c2022-05-16 17:55:28 +08001291 memset(msg_notify, 0, MAX_RET);
1292 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001293 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001294 {
you.chen35020192022-05-06 11:30:57 +08001295 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001296 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1297 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001298 {
you.chen35020192022-05-06 11:30:57 +08001299 g_sta_scan_finish_flag = 1;
1300 }
1301
qs.xiong9fbf74e2023-03-28 13:38:22 +08001302 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001303 {
you.chen35020192022-05-06 11:30:57 +08001304 continue;
1305 }
qs.xiong455c30b2023-04-12 11:40:02 +08001306 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001307 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1308 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1309 {
1310 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1311 }
you.chen0c9bee22023-10-25 13:03:14 +08001312 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1313 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1314 {
1315 RLOGD("donot report scan result during in call connect manual");
1316 }
you.chen6e724162023-10-19 19:10:01 +08001317 else
1318 {
1319 notify_connect_status(state, error);
1320 }
1321 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001322
1323 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001324 {
you.chen70f377f2023-04-14 18:17:09 +08001325 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001326 if (last_state != state)
1327 {
1328 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1329 {
1330 system_call_v("%s %s", sta_status_change_script, "connect");
1331 }
1332 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1333 {
1334 system_call_v("%s %s", sta_status_change_script, "disconnect");
1335 }
1336 }
qs.xiong09560402023-10-27 21:58:55 +08001337
you.chenc9928582023-04-24 15:39:37 +08001338 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001339 if (g_sta_fake_scan_finish_flag == 1)
1340 {
1341 g_sta_fake_scan_finish_flag = 0;
1342 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1343 }
you.chen32cb31e2023-04-13 14:05:45 +08001344 }
you.chen35020192022-05-06 11:30:57 +08001345 }
1346 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001347 if (lynq_wpa_ctrl != NULL)
1348 {
you.chen92fd5d32022-05-25 10:09:47 +08001349 wpa_ctrl_detach(lynq_wpa_ctrl);
1350 wpa_ctrl_close(lynq_wpa_ctrl);
1351 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001352}
qs.xiongfcc914b2023-07-06 21:16:20 +08001353static void STAAutoWatcherThreadProc() {
1354 size_t len = MAX_RET;
1355 char msg_notify[MAX_RET];
1356 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001357 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001358 int idle_count = 0;
1359 int networkid;
1360 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1361 g_sta_auto_watcher_stop_flag = 0;
1362 RLOGD("STAAutoWatcherThreadProc thread started ------");
1363 while (g_sta_auto_watcher_stop_flag == 0)
1364 {
1365 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1366 {
1367 continue;
1368 }
1369 memset(msg_notify, 0, MAX_RET);
1370 len = MAX_RET;
1371 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1372 {
1373 msg_notify[len+1] = '\0';
1374 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1375 if (strstr(msg_notify, state_scan_result) != NULL)
1376 {
1377 g_sta_auto_scan_finish_flag = 1;
1378 }
1379 if (g_sta_auto_callback_func == NULL)
1380 {
1381 continue;
1382 }
1383 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1384 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001385 }
1386 }
1387 if (lynq_wpa_ctrl != NULL)
1388 {
1389 wpa_ctrl_detach(lynq_wpa_ctrl);
1390 wpa_ctrl_close(lynq_wpa_ctrl);
1391 }
1392}
qs.xiongf1b525b2022-03-31 00:58:23 -04001393
you.chen70f377f2023-04-14 18:17:09 +08001394// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1395static void GlobalWatcherThreadProc()
1396{
1397 int ret, connect_timeout, service_abnormal;
1398 error_number_s error_num = -1;
1399 inner_sta_status_s sta_status;
1400 scan_info_s *scan_list = NULL;
1401 int i, scan_len=0;
1402 char connecting_ssid[64];
1403 struct timeval now;
1404
1405 RLOGD("GlobalWatcherThreadProc start to run");
1406
1407 while (1)
1408 {
1409 pthread_mutex_lock(&s_global_check_mutex);
1410 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1411 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1412 {
1413 pthread_mutex_unlock(&s_global_check_mutex);
1414 usleep(50*1000);
1415 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1416 continue;
1417 }
1418
1419 connect_timeout = 0;
1420 service_abnormal = 0;
1421 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1422 {
1423 while (1)
1424 {
1425 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1426 if (ret == ETIME)
1427 {
1428 connect_timeout = 1;
1429 }
1430 else if (ret != 0)
1431 {
1432 gettimeofday(&now,NULL);
1433 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1434 {
1435 usleep(SLEEP_TIME_ON_IDLE);
1436 continue;
1437 }
1438 connect_timeout = 1;
1439 }
1440 sta_status = s_sta_status;
1441 error_num = s_sta_error_number;
1442 s_sta_status = INNER_STA_STATUS_INIT;
1443 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1444 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1445 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1446 break;
1447 }
1448 }
1449 if (s_service_invoke_timeout_cnt > 10)
1450 {
1451 service_abnormal = 1;
1452 s_service_invoke_timeout_cnt = 0;
1453 }
1454 pthread_mutex_unlock(&s_global_check_mutex);
1455
1456 if (service_abnormal == 1)
1457 {
1458 sleep(1);
1459 RLOGE("wpa service is abnormal info app to exit");
1460 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001461
1462 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1463
you.chen70f377f2023-04-14 18:17:09 +08001464 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1465 }
1466
1467 if (sta_status == INNER_STA_STATUS_CANCEL)
1468 {
1469 continue;
1470 }
1471 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1472 {
1473 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1474 }
1475 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1476 {
1477 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1478 {
1479 for(i=0; i < scan_len;i++)
1480 {
1481 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1482 {
1483 error_num = LYNQ_AUTH_ERROR;
1484 break;
1485 }
1486 }
1487 free(scan_list);
1488 }
1489 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1490 }
1491 else if (connect_timeout == 0)
1492 {
1493 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1494 }
1495 else // wait timeout
1496 {
1497 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1498 {
1499 ; // wpa service abnormal
1500 }
1501 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1502 {
1503 RLOGD("GlobalWatcherThreadProc notify connected");
1504 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1505 }
1506 else
1507 {
1508 RLOGD("GlobalWatcherThreadProc notify timeout");
1509 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1510 }
1511 }
1512 } // while (1)
1513}
1514
qs.xiong1af5daf2022-03-14 09:12:12 -04001515int lynq_wifi_enable(void)
1516{
you.chen35020192022-05-06 11:30:57 +08001517 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001518 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001519 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001520 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1521
qs.xiong9fbf74e2023-03-28 13:38:22 +08001522 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1523 {
you.chend2fef3f2023-02-13 10:50:35 +08001524 goto out_enable;
1525 }
1526
you.chenc9928582023-04-24 15:39:37 +08001527 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001528 if (ret != 0)
1529 {
1530 //printf("service state %d\n", ret);
1531 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001532 ret = -1;
1533 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001534 }
lhfe8da902022-10-11 18:55:36 +08001535
you.chen70f377f2023-04-14 18:17:09 +08001536 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1537 {
1538 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1539 if(ret<0)
1540 {
1541 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1542 ret = -1;
1543 goto out_enable;
1544 }
1545 }
1546
you.chend2fef3f2023-02-13 10:50:35 +08001547 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1548 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1549 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1550 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1551out_enable:
1552 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001553 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001554}
1555
qs.xiong1af5daf2022-03-14 09:12:12 -04001556int lynq_wifi_disable(void)
1557{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001558 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001559 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001560 g_ap_watcher_stop_flag = 1;
1561 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001562 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001563 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001564 if (g_ap_watcher_pid != 0)
1565 pthread_join(g_ap_watcher_pid, NULL);
1566 if (g_sta_watcher_pid != 0)
1567 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001568 if (g_sta_auto_watcher_pid != 0)
1569 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001570 if (g_lynq_wpa_ctrl[0] != NULL)
1571 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1572 if (g_lynq_wpa_ctrl[1] != NULL)
1573 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001574 if (g_ap_tmp_watcher_pid != 0)
1575 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001576 g_ap_watcher_pid = 0;
1577 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001578 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001579 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001580 g_lynq_wpa_ctrl[0] = NULL;
1581 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001582 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001583 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001584 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1585 return 0;
1586}
1587
1588static inline char inner_convert_char(char in)
1589{
1590 if (in >= '0' && in <= '9')
1591 {
1592 return in - '0';
1593 }
1594 else if (in >= 'a' && in <= 'f')
1595 {
1596 return in - 'a' + 10;
1597 }
1598 else if (in >= 'A' && in <= 'F')
1599 {
1600 return in - 'A' + 10;
1601 }
1602 else
1603 {
1604 return '\xff';
1605 }
1606}
1607
1608static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1609{
1610 char *p;
1611 size_t pos = 0;
1612 if (NULL == out_ssid)
1613 return;
1614 //printf("input ssid=[%s]\n", ssid);
1615 memset(out_ssid, 0, out_ssid_len);
1616 if (NULL == ssid)
1617 return;
1618 p = strchr(ssid, '\\');
1619 if (NULL == p)
1620 {
1621 strncpy(out_ssid, ssid, out_ssid_len);
1622 //printf(" first %s\n", out_ssid);
1623 }
1624 else
1625 {
1626 pos = p - ssid;
1627 memcpy(out_ssid, ssid, pos);
1628 //printf("pos %lu -- %s\n", pos, out_ssid);
1629 for(; pos < out_ssid_len; pos ++)
1630 {
1631 if (p[0] == '\0')
1632 {
1633 //printf(" out %s\n", out_ssid);
1634 return;
1635 }
1636 else if (p[0] != '\\')
1637 {
1638 out_ssid[pos] = p[0];
1639 p += 1;
1640 }
1641 else if (p[1] == 'x' || p[1] == 'X')
1642 {
1643 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1644 p += 4;
1645 }
1646 else if (p[1] == '\\')
1647 {
1648 out_ssid[pos] = '\\';
1649 p += 2;
1650 }
1651 else if (p[1] == 't')
1652 {
1653 out_ssid[pos] = '\t';
1654 p += 2;
1655 }
1656 else if (p[1] == 'r')
1657 {
1658 out_ssid[pos] = '\r';
1659 p += 2;
1660 }
1661 else if (p[1] == 'n')
1662 {
1663 out_ssid[pos] = '\n';
1664 p += 2;
1665 }//todo find a better way to convert?
1666 }
1667 }
1668 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001669}
qs.xiong1af5daf2022-03-14 09:12:12 -04001670
you.chen35020192022-05-06 11:30:57 +08001671static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001672 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001673 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001674 RLOGD("enter inner_get_param");
1675 if (out_put == NULL)
1676 {
1677 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001678 return -1;
1679 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001680 if (param_name == NULL)
1681 {
1682 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001683 return -1;
1684 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001685 if (param_name[0] == '\0')
1686 {
1687 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001688 return -1;
1689 }
1690
1691 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1692
1693 CHECK_WPA_CTRL(interface);
1694
1695 DO_REQUEST(lynq_cmd_get);
1696
qs.xiong9fbf74e2023-03-28 13:38:22 +08001697 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1698 {
1699 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001700 return -1;
1701 }
1702
you.chena6fa5b22022-05-18 10:28:19 +08001703// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001704 if (strcmp(param_name, "ssid") == 0)
1705 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001706 if (cmd_reply[0] == '\"')
1707 {
you.chend2fef3f2023-02-13 10:50:35 +08001708 ssid_len = reply_len - 1;
1709 memcpy(out_put, cmd_reply + 1, ssid_len);
1710 if (out_put[ssid_len-1] == '\"')
1711 {
1712 out_put[ssid_len-1] = '\0';
1713 }
1714 else
1715 {
1716 out_put[ssid_len] = '\0';
1717 }
1718 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001719 else
1720 {
you.chend2fef3f2023-02-13 10:50:35 +08001721 ssid_len = reply_len / 2;
1722 for(i=0; i<ssid_len; i++)
1723 {
1724 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1725 }
1726 out_put[ssid_len] = '\0';
1727 }
1728 }
1729 else
1730 {
1731 memcpy(out_put, cmd_reply, reply_len + 1);
1732 }
you.chen35020192022-05-06 11:30:57 +08001733 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001734}
qs.xiong1af5daf2022-03-14 09:12:12 -04001735
you.chen35020192022-05-06 11:30:57 +08001736static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1737 int ret = 0;
1738 char * end = str + len - 1;
1739 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001740 while(str < end)
1741 {
1742 if (*str == delimiter)
1743 {
you.chen35020192022-05-06 11:30:57 +08001744 *str++ = '\0';
1745 results[ret++] = str;
1746 continue;
1747 }
1748 str++;
1749 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001750 if (*str == delimiter)
1751 {
you.chen35020192022-05-06 11:30:57 +08001752 *str = '\0';
1753 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001754
you.chen6ed36a62023-04-27 17:51:56 +08001755 results[ret] = NULL;
1756
you.chen35020192022-05-06 11:30:57 +08001757 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001758}
1759
you.chend2fef3f2023-02-13 10:50:35 +08001760static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1761{
1762 char * p;
1763 int ret = 0;
1764 char cmd[256]={0};
1765 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001766 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001767 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001768 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001769 ret = exec_cmd(cmd, ip, ip_len);
1770 p = strchr(ip, '\n');
1771 if (NULL != p)
1772 {
1773 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001774 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001775 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001776 return ret;
1777}
1778
you.chend2fef3f2023-02-13 10:50:35 +08001779static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001780 struct in_addr addr ={0};
1781 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001782 char cmd[64] = {0};
1783 char * p;
1784 int ret;
you.chen35020192022-05-06 11:30:57 +08001785
qs.xiong9fbf74e2023-03-28 13:38:22 +08001786 if (ip == NULL || *ip == '\0' || hostname == NULL)
1787 {
1788 RLOGE("ip == NULL or hostname == NULL");
1789 return -1;
you.chen35020192022-05-06 11:30:57 +08001790 }
1791
you.chend2fef3f2023-02-13 10:50:35 +08001792 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001793 if (inet_aton(ip, &addr) == 0)
1794 {
you.chen35020192022-05-06 11:30:57 +08001795 printf("---inet_aton fail\n");
1796 return -1;
1797 }
1798
1799 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1800
qs.xiong9fbf74e2023-03-28 13:38:22 +08001801 if (ht == NULL)
1802 {
you.chen186d3c32023-05-18 14:19:46 +08001803 hostname[0] = '\0';
1804 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1805 ret = exec_cmd(cmd, hostname, 32);
1806 if (ret == 0)
1807 {
1808 p = strchr(hostname, '\n');
1809 if (p != NULL)
1810 {
1811 *p = '\0';
1812 }
1813 return 0;
1814 }
1815 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001816 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001817 herror(NULL);
1818 return -1;
1819 }
1820
1821 strcpy(hostname, ht->h_name);
1822
1823 return 0;
1824}
1825
1826static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1827{
1828 int count, index, words_count;
1829 char * split_lines[128]= {0};
1830 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001831 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001832 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001833 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001834
1835 CHECK_WPA_CTRL(ap_sta);
1836
1837 DO_REQUEST(lynq_wifi_list_networks);
1838
1839 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1840
1841 //@todo check ssid field to compatible
1842
1843 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001844 for(index=1; index < count; index++)
1845 {
you.chen35020192022-05-06 11:30:57 +08001846 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001847 if (words_count > 2)
1848 {
you.chend2fef3f2023-02-13 10:50:35 +08001849 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001850 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1851 {
you.chen35020192022-05-06 11:30:57 +08001852 net_no_list[ret++] = atoi(split_words[0]);
1853 }
1854 }
1855 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001857 return ret;
1858}
1859
1860static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001861 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001862 CHECK_WPA_CTRL(ap_sta);
1863 const char *lynq_wifi_add_network = "ADD_NETWORK";
1864
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001866 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001867 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1868 {
1869 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001870 return -1;
1871 }
1872
qs.xiong9fbf74e2023-03-28 13:38:22 +08001873 for(i=0;i<reply_len;i++)
1874 {
1875 if(cmd_reply[i] == '\n')
1876 {
you.chen35020192022-05-06 11:30:57 +08001877 cmd_reply[i] = '\0';
1878 break;
1879 }
1880 }
1881 return atoi(cmd_reply);
1882}
you.chena6cd55a2022-05-08 12:20:18 +08001883
you.chen35020192022-05-06 11:30:57 +08001884static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1885{
1886 int count, index;
1887 int net_no_list[128];
1888
qs.xiong9fbf74e2023-03-28 13:38:22 +08001889 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001890 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001891 for (index=0; index < count; index++)
1892 {
1893 if (net_no_list[index] == net_no)
1894 {
you.chen35020192022-05-06 11:30:57 +08001895 return 0;
1896 }
1897 }
1898
1899 if (count >= 1)
1900 index = net_no_list[count - 1];
1901 else
1902 index = -1;
1903
qs.xiong9fbf74e2023-03-28 13:38:22 +08001904 while (index < net_no )
1905 {
you.chen35020192022-05-06 11:30:57 +08001906 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001907 if (index >= net_no)
1908 { // required network no created
1909 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001910 return 0;
1911 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001912 else if( index < 0)
1913 {
1914 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001915 return -1;
1916 }
you.chen35020192022-05-06 11:30:57 +08001917 }
1918
1919 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001920 {
1921 RLOGE("[lynq_check_network_number] network index < 0");
1922 return -1;
1923 }
1924 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001925 return 0;
1926}
1927
1928static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001929 if (freq > 5000 && freq < 6000)
1930 {
you.chen35020192022-05-06 11:30:57 +08001931 return LYNQ_WIFI_5G_band;
1932 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001933 else if (freq > 2000 && freq < 3000)
1934 {
you.chen35020192022-05-06 11:30:57 +08001935 return LYNQ_WIFI_2G_band;
1936 }
1937 return LYNQ_WIFI_2_and_5G_band;
1938}
1939
1940static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001941 if (key_mgmt != NULL)
1942 {
1943 if (memcmp( key_mgmt, "NONE", 4) == 0)
1944 {
you.chen35020192022-05-06 11:30:57 +08001945 return LYNQ_WIFI_AUTH_OPEN;
1946 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001947 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1948 {
you.chen35020192022-05-06 11:30:57 +08001949 return LYNQ_WIFI_AUTH_WEP;
1950 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001951 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1952 {
you.chen35020192022-05-06 11:30:57 +08001953 return LYNQ_WIFI_AUTH_WPA_PSK;
1954 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1956 {
you.chen35020192022-05-06 11:30:57 +08001957 return LYNQ_WIFI_AUTH_WPA2_PSK;
1958 }
1959 }
1960
1961 return -1;
1962}
1963
1964static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001965 if (flag != NULL)
1966 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001967 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 +08001968 {
1969 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001970 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001971 {
1972 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1973 }else if (strstr( flag, "WPA2-PSK") != NULL)
1974 {
you.chen35020192022-05-06 11:30:57 +08001975 return LYNQ_WIFI_AUTH_WPA2_PSK;
1976 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001977 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001978 {
you.chen35020192022-05-06 11:30:57 +08001979 return LYNQ_WIFI_AUTH_WPA_PSK;
1980 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001981 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001982 {
you.chen35020192022-05-06 11:30:57 +08001983 return LYNQ_WIFI_AUTH_WEP;
1984 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001985 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001986 {
you.chen35020192022-05-06 11:30:57 +08001987 return LYNQ_WIFI_AUTH_OPEN;
1988 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001989 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001990 {
you.chend2fef3f2023-02-13 10:50:35 +08001991 return LYNQ_WIFI_AUTH_OPEN;
1992 }
qs.xiong46f41562023-07-11 21:06:47 +08001993 else
1994 {
1995 RLOGD("convert_max_auth_from_flag not-found auth mode");
1996 }
you.chen35020192022-05-06 11:30:57 +08001997 }
1998
1999 return -1;
2000}
2001
2002static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2003 switch (bw) {
2004 case 10:
2005 return LYNQ_WIFI_BANDWIDTH_HT10;
2006 break;
2007 case 20:
2008 return LYNQ_WIFI_BANDWIDTH_HT20;
2009 break;
2010 case 40:
2011 return LYNQ_WIFI_BANDWIDTH_HT40;
2012 break;
2013 case 80:
2014 return LYNQ_WIFI_BANDWIDTH_HT80;
2015 break;
2016 default:
2017 break;
2018 }
2019
2020 return -1;
2021}
2022
you.chen70f377f2023-04-14 18:17:09 +08002023static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002024static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2025 int i, count;
2026 char *p;
2027 const char *lynq_status_cmd = "STATUS";
2028 const char * FLAG_SSID = "ssid=";
2029 const char * FLAG_SBSID = "bssid=";
2030 const char * FLAG_KEY_MGMT = "key_mgmt=";
2031 const char * FLAG_FREQ = "freq=";
2032 const char * FLAG_STATE = "wpa_state=";
2033 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002034 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002035 char *split_lines[128] = {0};
2036
2037 CHECK_WPA_CTRL(interface);
2038
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 if (curr_state == NULL)
2040 {
2041 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002042 return -1;
2043 }
2044
2045 DO_REQUEST(lynq_status_cmd);
2046
2047 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2048
2049 curr_state->net_no = -1;
2050 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002051 for(i=0; i < count; i++)
2052 {
2053 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002054 {
you.chen35020192022-05-06 11:30:57 +08002055 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002056 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002057 {
you.chend2fef3f2023-02-13 10:50:35 +08002058 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002059 ret = 0;
2060 continue;
2061 }
you.chenf58b3c92022-06-21 16:53:48 +08002062 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002063 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002064 {
you.chend2fef3f2023-02-13 10:50:35 +08002065 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 +08002066 ret = 0;
2067 continue;
2068 }
you.chen35020192022-05-06 11:30:57 +08002069 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002070 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002071 {
you.chen450d0172022-07-15 17:56:48 +08002072 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002073 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002074 ret = 0;
2075 continue;
2076 }
2077 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002078 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002079 {
you.chen35020192022-05-06 11:30:57 +08002080 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2081 ret = 0;
2082 continue;
2083 }
you.chend2fef3f2023-02-13 10:50:35 +08002084 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002085 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002086 {
you.chend2fef3f2023-02-13 10:50:35 +08002087 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2088 ret = 0;
2089 continue;
2090 }
you.chen35020192022-05-06 11:30:57 +08002091 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002092 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002093 {
you.chen35020192022-05-06 11:30:57 +08002094 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002095 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002096 {
you.chen35020192022-05-06 11:30:57 +08002097 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2098 ret = 0;
2099 continue;
2100 }
2101
2102 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002103 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002104 {
you.chen35020192022-05-06 11:30:57 +08002105 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002106 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002107 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002108 }
2109 }
2110
you.chen70f377f2023-04-14 18:17:09 +08002111 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2112 {
2113 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2114 }
2115
you.chen35020192022-05-06 11:30:57 +08002116 return ret;
2117}
2118
qs.xiongf1b525b2022-03-31 00:58:23 -04002119int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002120{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002121 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002122 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002123
qs.xiong9fbf74e2023-03-28 13:38:22 +08002124 if (ap_ssid == NULL)
2125 {
2126 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002127 return -1;
2128 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002129 else
2130 {
2131 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002132 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002133
qs.xiong9fbf74e2023-03-28 13:38:22 +08002134 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2135 {
2136 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002137 return -1;
2138 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002139
you.chen35020192022-05-06 11:30:57 +08002140 CHECK_IDX(idx, CTRL_AP);
2141
2142 CHECK_WPA_CTRL(CTRL_AP);
2143
2144 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2145
2146 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2147 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002148 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2149 return 0;
you.chen35020192022-05-06 11:30:57 +08002150
qs.xiong7a105ce2022-03-02 09:43:11 -05002151}
2152
you.chen35020192022-05-06 11:30:57 +08002153int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002154{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002156 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002157 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002158}
2159
qs.xiongc9c79f72022-10-17 15:27:18 +08002160/*****
2161 *frequency <------>channel
2162 *
2163 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2164 *
2165 *
2166 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2167 *
2168 *
2169 * */
2170static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002171 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};
2172 int i;
2173 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2174
qs.xiong69a332b2022-12-02 09:58:57 +08002175 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002176 {
2177 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002178 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002179 }
qs.xiongc00b6032022-11-29 16:28:03 +08002180
2181 if(i == arr_len)
2182 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002183 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002184 return -1;
2185 }
qs.xiongc00b6032022-11-29 16:28:03 +08002186
qs.xiongc9c79f72022-10-17 15:27:18 +08002187 return 0;
2188}
qs.xiong13673462023-02-21 19:12:54 +08002189
2190static int lynq_check_frequencyby_country_code(int input_frequency)
2191{
2192 char str_cnc[]="CN";
2193 char str_dest[20]="";
2194
2195 if( lynq_get_country_code(1,str_dest) != 0 )
2196 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002197 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002198 return -1;
2199 }
2200 if( strncmp(str_dest,str_cnc,2) != 0 )
2201 {
2202 return 0;
2203 }else if( 2473 < input_frequency && input_frequency < 5744)
2204 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002205 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002206 return -1;
2207 }
2208 return 0;
2209}
qs.xiongf1b525b2022-03-31 00:58:23 -04002210int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002211{
qs.xiongc00b6032022-11-29 16:28:03 +08002212 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002213 char lynq_wifi_frequency_cmd[128]={0};
2214 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002215 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002216 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002217 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002218 check = lynq_check_set_frequency(lynq_wifi_frequency);
2219 if(check != 0)
2220 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002221 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002222 return -1;
you.chen35020192022-05-06 11:30:57 +08002223 }
qs.xiong13673462023-02-21 19:12:54 +08002224 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2225 if(check != 0)
2226 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002227 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002228 return -1;
2229 }
2230
qs.xiongc00b6032022-11-29 16:28:03 +08002231 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2232 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002233 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002234 return -1;
2235 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002236
you.chen35020192022-05-06 11:30:57 +08002237 CHECK_IDX(idx, CTRL_AP);
2238
2239 CHECK_WPA_CTRL(CTRL_AP);
2240
2241 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2242 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2243 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2244
you.chen6c2dd9c2022-05-16 17:55:28 +08002245 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002246 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2247 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2248 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002249
qs.xiong9fbf74e2023-03-28 13:38:22 +08002250 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002251}
2252
qs.xiongf1b525b2022-03-31 00:58:23 -04002253int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002254{
you.chen35020192022-05-06 11:30:57 +08002255 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002256 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002257 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002258
qs.xiong9fbf74e2023-03-28 13:38:22 +08002259 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2260 {
2261 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002262 return -1;
2263 }
2264 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002265
qs.xiong9fbf74e2023-03-28 13:38:22 +08002266 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002267}
2268
qs.xiongf1b525b2022-03-31 00:58:23 -04002269int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2270{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002271 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002272 CHECK_IDX(idx, CTRL_AP);
2273 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002274 case LYNQ_WIFI_BANDWIDTH_HT10:
2275 {
2276 RLOGE("bandwith [%d] not support now\n", bandwidth);
2277 return -1;
2278 }
2279 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002280 {
2281 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2282 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002283 if (system(lynq_cmd_bandwith) != 0 )
2284 {
2285 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002286 return -1;
2287 }
2288 system("wl up");
2289 break;
2290 }
2291 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002292 {
qs.xiong10379192023-02-21 13:19:42 +08002293 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002294 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2295 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002296 if (system(lynq_cmd_bandwith) != 0 )
2297 {
2298 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002299 return -1;
2300 }
2301 system("wl up");
2302 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002303 }
you.chen35020192022-05-06 11:30:57 +08002304 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002305 {
qs.xiong10379192023-02-21 13:19:42 +08002306 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002307 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002308 if (system(lynq_cmd_bandwith) != 0 )
2309 {
2310 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002311 return -1;
2312 }
2313 system("wl up");
2314 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 }
2316 default:
you.chen35020192022-05-06 11:30:57 +08002317 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002318 RLOGE("auth type [%d] not support now\n", bandwidth);
2319 return -1;
you.chen35020192022-05-06 11:30:57 +08002320 }
2321 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002322
2323
you.chen35020192022-05-06 11:30:57 +08002324 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002325}
you.chen35020192022-05-06 11:30:57 +08002326
qs.xiongf1b525b2022-03-31 00:58:23 -04002327int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2328{
you.chen35020192022-05-06 11:30:57 +08002329 int count = 0;
2330 int index = 0;
2331 char *split_words[128] = {0};
2332 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002333 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002334 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002335
you.chen35020192022-05-06 11:30:57 +08002336 CHECK_WPA_CTRL(CTRL_AP);
2337
2338 DO_REQUEST(lynq_chanspec_cmd);
2339
2340 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2341 for(;index < count; index++) {
2342 if (strncmp(split_words[index], "bw", 2) != 0) {
2343 continue;
2344 }
2345
2346 index++;
2347 if (index >= count) {
2348 return -1;
2349 }
2350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002352 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2353 return 0;
2354 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002355 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002356 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002357}
qs.xiong0fb469a2022-04-14 03:50:45 -04002358
qs.xiongf1b525b2022-03-31 00:58:23 -04002359int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002360{
you.chen35020192022-05-06 11:30:57 +08002361 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002362 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002363 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002364
you.chen35020192022-05-06 11:30:57 +08002365 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002366
qs.xiong9fbf74e2023-03-28 13:38:22 +08002367 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2368 {
you.chen35020192022-05-06 11:30:57 +08002369 return -1;
2370 }
2371
2372 system("wl down");
2373 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002374 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002375 return -1;
2376 }
2377 system("wl up");
2378 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002379}
qs.xiong0fb469a2022-04-14 03:50:45 -04002380
qs.xiongf1b525b2022-03-31 00:58:23 -04002381int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002382{
you.chen35020192022-05-06 11:30:57 +08002383 int count = 0;
2384 int index = 0;
2385 char *split_words[128] = {0};
2386 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002387 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002388 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002389
you.chen35020192022-05-06 11:30:57 +08002390 CHECK_WPA_CTRL(CTRL_AP);
2391
2392 DO_REQUEST(lynq_chanspec_cmd);
2393
2394 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002395 for(;index < count; index++)
2396 {
2397 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002398 if (strncmp(split_words[index], "channel", 2) != 0) {
2399 continue;
2400 }
2401
2402 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002403 if (index >= count)
2404 {
you.chen35020192022-05-06 11:30:57 +08002405 return -1;
2406 }
2407
2408 *channel = atoi(split_words[index]);
2409 return 0;
2410 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002411 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002412 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002413}
2414
2415
you.chen35020192022-05-06 11:30:57 +08002416int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002417{
you.chen6c2dd9c2022-05-16 17:55:28 +08002418 char ssid[MAX_CMD] = {0};
2419 int freq = 0;
2420 char lynq_auth_cmd[64]={0};
2421 char lynq_auth_alg_cmd[64]={0};
2422 char lynq_psk_cmd[64]={0};
2423 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002424 char lynq_ieee80211_cmd[64]={0};
2425 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002426 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002427 CHECK_IDX(idx, CTRL_AP);
2428
you.chen6c2dd9c2022-05-16 17:55:28 +08002429 CHECK_WPA_CTRL(CTRL_AP);
2430
qs.xiong9fbf74e2023-03-28 13:38:22 +08002431 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2432 {
2433 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002434 return -1;
2435 }
2436
you.chen92fd5d32022-05-25 10:09:47 +08002437 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002438 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002439 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002440 return 0;
2441 }
2442 else {
2443 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2444 ssid[0] = '\0';
2445 }
2446 lynq_wifi_ap_frequency_get(idx, &freq);
2447
2448 DO_OK_FAIL_REQUEST(cmd_disconnect);
2449 DO_OK_FAIL_REQUEST(cmd_remove_all);
2450 if (ssid[0] != '\0') {
2451 lynq_wifi_ap_ssid_set(idx, ssid);
2452 }
2453 if (freq != 0) {
2454 lynq_wifi_ap_frequency_set(idx, freq);
2455 }
2456 }
2457 }
you.chen35020192022-05-06 11:30:57 +08002458
qs.xiong9fbf74e2023-03-28 13:38:22 +08002459 switch(auth){
2460 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002461 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002462 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002463 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002464 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002465 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002466 break;
2467 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002468 case LYNQ_WIFI_AUTH_WEP:
2469 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002470 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002471 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002472 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002473 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2474
2475 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2476 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2477 break;
2478 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002479 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002480 {
2481 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2482 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2483 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2484
2485 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2486 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2487 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2488 break;
2489
2490 }
you.chen35020192022-05-06 11:30:57 +08002491 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002492 {
2493 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2494 {
you.chen35020192022-05-06 11:30:57 +08002495 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2496 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2497 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002498 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2499 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002500 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002501 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002502 }
2503// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2504// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2505 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002506
you.chen35020192022-05-06 11:30:57 +08002507 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2508 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2509 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002510 break;
2511 }
2512 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2513 {
2514 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2515 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2516 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2517 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2518
2519 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2520 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2521 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2522 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2523 break;
2524 }
2525 case LYNQ_WIFI_AUTH_WPA3_PSK:
2526 {
2527 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2528 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002529 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002530 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2531
2532 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2533 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2534 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2535 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2536 break;
2537 }
2538 default:
you.chen35020192022-05-06 11:30:57 +08002539 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002540 RLOGE("auth type [%d] not support now\n", auth);
2541 return -1;
you.chen35020192022-05-06 11:30:57 +08002542 }
2543 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002544 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002545
qs.xiong9fbf74e2023-03-28 13:38:22 +08002546 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002547}
2548
you.chen35020192022-05-06 11:30:57 +08002549int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002550{
you.chen35020192022-05-06 11:30:57 +08002551 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002552 char lynq_auth_alg_str[MAX_RET] = {0};
2553 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002554 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002555 CHECK_IDX(idx, CTRL_AP);
2556
qs.xiong9fbf74e2023-03-28 13:38:22 +08002557 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2558 {
2559 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002560 return -1;
2561 }
2562
qs.xiong9fbf74e2023-03-28 13:38:22 +08002563 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2564 {
2565 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2566 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002567 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002568 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002569 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002570 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002571 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2572 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002573 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002574 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002575 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002576 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002577 else
2578 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002579 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002580 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002581 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002582 }
you.chen35020192022-05-06 11:30:57 +08002583 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002584 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2585 {
2586 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2587 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002588 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002589 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002590 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002591 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2592 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002593 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002594 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002595 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002596 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002597 else
2598 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002599 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002600 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002601 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002602 }
you.chen35020192022-05-06 11:30:57 +08002603 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002604
2605 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2606 {
2607 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2608 return -1;
2609 }
2610
2611 if (memcmp(lynq_auth_str,"1",1) == 0 )
2612 {
2613 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2614 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002615 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002616 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2617 {
2618 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2619 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002620 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002621 }
2622 else
2623 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002624 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002625 *auth = -1;
2626 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002627
you.chen6c2dd9c2022-05-16 17:55:28 +08002628 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002629}
qs.xiong1af5daf2022-03-14 09:12:12 -04002630
you.chenb95401e2023-05-12 19:39:06 +08002631static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2632{
2633 char status[64];
you.chencba13492023-05-19 13:53:43 +08002634 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002635 curr_status_info curr_state;
2636
2637 CHECK_WPA_CTRL(CTRL_AP);
2638
2639 memset(status, 0, sizeof (status));
2640
2641 curr_state.ap = NULL;
2642 curr_state.state = status;
2643
2644 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002645 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002646 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002647 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002648 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002649 {
2650 return 0;
2651 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002652 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002653 {
2654 DO_REQUEST("RECONNECT");
2655 return inner_check_ap_connected(idx, retry_count+1);
2656 }
you.chencba13492023-05-19 13:53:43 +08002657 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002658 {
2659 printf("retry 10 time\n");
2660 return -1;
2661 }
2662 else
2663 {
you.chen6d247052023-06-01 16:39:54 +08002664 if (strcmp(status, STATE_DISCONNECTED) == 0)
2665 {
2666 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2667 DO_REQUEST(LYNQ_WIFI_CMD);
2668 }
you.chenb95401e2023-05-12 19:39:06 +08002669 return inner_check_ap_connected(idx, retry_count+1);
2670 }
2671 }
2672 return -1;
2673}
qs.xiong1af5daf2022-03-14 09:12:12 -04002674
qs.xiongf1b525b2022-03-31 00:58:23 -04002675int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002676{
qs.xiong5a2ba932023-09-13 16:30:21 +08002677 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002678 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002679 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2680 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2681 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002682 CHECK_IDX(idx, CTRL_AP);
2683
2684 CHECK_WPA_CTRL(CTRL_AP);
2685
you.chen0df3e7e2023-05-10 15:56:26 +08002686 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002687 {
you.chen0df3e7e2023-05-10 15:56:26 +08002688 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002689 return -1;
2690 }
you.chen35020192022-05-06 11:30:57 +08002691
qs.xiongb37f8c42023-09-13 21:21:58 +08002692 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2693 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2694
you.chen35020192022-05-06 11:30:57 +08002695 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2696 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2697
you.chenc9928582023-04-24 15:39:37 +08002698 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2699 if (ret != 0)
2700 {
2701 RLOGE("lynq_wifi_ap_start excute script fail");
2702 return -1;
2703 }
2704
you.chenb95401e2023-05-12 19:39:06 +08002705 if (inner_check_ap_connected(idx, 0) != 0)
2706 {
2707 return -1;
2708 }
2709
you.chen0df3e7e2023-05-10 15:56:26 +08002710 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002711 if (g_ap_tmp_watcher_pid == 0)
2712 {
2713 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2714 {
2715 g_ap_tmp_watcher_pid = 0;
2716 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2717 return -1;
2718 }
2719 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2720 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002721 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002722
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002724}
2725
qs.xiongf1b525b2022-03-31 00:58:23 -04002726int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002727{
you.chen35020192022-05-06 11:30:57 +08002728 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002729}
2730
qs.xiongf1b525b2022-03-31 00:58:23 -04002731int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002732{
you.chen35020192022-05-06 11:30:57 +08002733 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002734
you.chen35020192022-05-06 11:30:57 +08002735 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002736
you.chen35020192022-05-06 11:30:57 +08002737 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002738
you.chen35020192022-05-06 11:30:57 +08002739 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2740
2741 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2742
you.chenb4b121c2022-05-06 17:50:16 +08002743// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002744
2745 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2746 if (ret != 0)
2747 {
2748 RLOGE("lynq_wifi_ap_start excute script fail");
2749 return -1;
2750 }
qs.xiong44fac672023-08-29 16:15:55 +08002751 g_ap_tmp_watcher_stop_flag = 1;
2752 if (g_ap_tmp_watcher_pid != 0)
2753 pthread_join(g_ap_tmp_watcher_pid, NULL);
2754 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002755
qs.xiong9fbf74e2023-03-28 13:38:22 +08002756 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002757}
qs.xiong1af5daf2022-03-14 09:12:12 -04002758
qs.xiongf1b525b2022-03-31 00:58:23 -04002759int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002760{
you.chen35020192022-05-06 11:30:57 +08002761 char lynq_disable_cmd[128] = {0};
2762 char lynq_select_cmd[128] = {0};
2763 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002764 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002765 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002766
you.chen35020192022-05-06 11:30:57 +08002767 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002768 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2769 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2770
2771 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2772 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2773 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002774
qs.xiong9fbf74e2023-03-28 13:38:22 +08002775 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002776}
2777
qs.xiongf1b525b2022-03-31 00:58:23 -04002778int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002779{
you.chen35020192022-05-06 11:30:57 +08002780 char lynq_disable_cmd[128] = {0};
2781 char lynq_select_cmd[128] = {0};
2782 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002783 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002784 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002785
you.chen35020192022-05-06 11:30:57 +08002786 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002787
you.chen35020192022-05-06 11:30:57 +08002788 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2789 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2790
2791 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2792 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2793 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002794
qs.xiong9fbf74e2023-03-28 13:38:22 +08002795 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002796}
qs.xiongf1b525b2022-03-31 00:58:23 -04002797
you.chen35020192022-05-06 11:30:57 +08002798int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002799{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002800 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002801 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002802 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002803 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002804 RLOGD("enter lynq_ap_password_set");
2805 if( password == NULL )
2806 {
2807 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002808 return -1;
2809 }
2810 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002811 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002812 if(pass_len < 8 || pass_len >= 64)
2813 {
2814 RLOGE("[lynq_ap_password_set]input password len not in rage");
2815 return -1;
you.chen35020192022-05-06 11:30:57 +08002816 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002817
you.chen35020192022-05-06 11:30:57 +08002818 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002819
qs.xiong9fbf74e2023-03-28 13:38:22 +08002820 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2821 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002822 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002823 return -1;
2824 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002825 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2826 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002827 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2828 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002829 }
2830
you.chen35020192022-05-06 11:30:57 +08002831 CHECK_WPA_CTRL(CTRL_AP);
2832
qs.xiong9fbf74e2023-03-28 13:38:22 +08002833 if (auth == LYNQ_WIFI_AUTH_WEP)
2834 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002835 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002836 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2837 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2838 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2839 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2840 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002841 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2842 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002843 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 +08002844 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2845 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2846 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002847 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2848 {
2849
qs.xiongc8d92a62023-03-29 17:36:14 +08002850 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 +08002851 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002852 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002853 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2854 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2855
2856 }
2857 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002858 {
2859 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002860 return -1;
2861 }
you.chen35020192022-05-06 11:30:57 +08002862
you.chen35020192022-05-06 11:30:57 +08002863 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002864
qs.xiong9fbf74e2023-03-28 13:38:22 +08002865 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002866}
2867
you.chen35020192022-05-06 11:30:57 +08002868int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002869{
you.chen35020192022-05-06 11:30:57 +08002870 FILE * fp;
2871 int len, ret;
2872 int count, index;
2873 char *split_lines[128] = {0};
2874 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002875 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002876
you.chen35020192022-05-06 11:30:57 +08002877 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002878
you.chen35020192022-05-06 11:30:57 +08002879 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2880// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002881 if (NULL == fp)
2882 {
2883 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002884 return -1;
2885 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002886
you.chen35020192022-05-06 11:30:57 +08002887 buff = alloca(MAX_RET);
2888 fseek(fp, 0, SEEK_SET);
2889 len = fread(buff, 1, MAX_RET, fp);
2890 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002891
qs.xiong9fbf74e2023-03-28 13:38:22 +08002892 for(index=0; index < len; index ++)
2893 {
2894 if (memcmp(buff + index, "network={", 9) != 0)
2895 {
you.chen35020192022-05-06 11:30:57 +08002896 continue;
2897 }
2898 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002899 for (; index < len; index ++ )
2900 {
2901 if (buff[index] != '}')
2902 {
you.chen35020192022-05-06 11:30:57 +08002903 continue;
2904 }
2905 buff[index] = '\0';
2906 break;
2907 }
2908 len = buff + index - p;
2909 }
2910
2911 count = lynq_split(p, len, '\n', split_lines);
2912
2913 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002914 for(index=0; index < count; index++)
2915 {
you.chen35020192022-05-06 11:30:57 +08002916 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 if (p != NULL)
2918 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002919 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002920 if (*p == '\"')
2921 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002922 p++;
2923 }
you.chen35020192022-05-06 11:30:57 +08002924 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2926 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002927 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002928 if (*p == '\"')
2929 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002930 p++;
2931 }
2932 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002933 else
2934 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002935 continue;
you.chen35020192022-05-06 11:30:57 +08002936 }
2937
2938 strcpy(password, p);
2939
qs.xiong9fbf74e2023-03-28 13:38:22 +08002940 while(*password != '\0')
2941 {
2942 if (*password == '\"')
2943 {
you.chen35020192022-05-06 11:30:57 +08002944 *password = '\0';
2945 break;
2946 }
2947 password++;
2948 }
2949 ret = 0;
2950 break;
2951 } //end for(index=0; index < count; index++)
2952
2953 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002954}
2955
you.chen35020192022-05-06 11:30:57 +08002956static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2957 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002958 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002959
qs.xiong9fbf74e2023-03-28 13:38:22 +08002960 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2961 {
you.chen35020192022-05-06 11:30:57 +08002962 return -1;
2963 }
2964
2965 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002966
qs.xiong9fbf74e2023-03-28 13:38:22 +08002967 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2968 {
2969 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002970 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002971 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002972 {
you.chena6cd55a2022-05-08 12:20:18 +08002973 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002974 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002975 }
you.chen70f377f2023-04-14 18:17:09 +08002976 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2977 {
2978 return 0;
2979 }
you.chena6cd55a2022-05-08 12:20:18 +08002980 }
2981 }
you.chen70f377f2023-04-14 18:17:09 +08002982 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2983 {
2984 return 0;
2985 }
2986
qs.xiong9fbf74e2023-03-28 13:38:22 +08002987 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2988 {
you.chen70f377f2023-04-14 18:17:09 +08002989 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 return -1;
2991 }
2992 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2993 {
2994
you.chen70f377f2023-04-14 18:17:09 +08002995 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2996 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002997 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2998 {
2999
3000 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003001 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003002 }else
3003 {
you.chen70f377f2023-04-14 18:17:09 +08003004 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003005 *auth = -1;
3006 return -1;
3007 }
you.chen35020192022-05-06 11:30:57 +08003008 return 0;
3009}
3010
3011int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003012{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003013 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003014 int pass_len, net_no, count, index;
3015 char lynq_tmp_cmd[300]={0};
3016 int net_no_list[128];
3017 lynq_wifi_auth_s net_auth;
3018 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 if(pass_len < 8 || pass_len >= 64)
3020 {
3021 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003022 return -1;
3023 }
3024
3025 CHECK_IDX(idx, CTRL_STA);
3026
3027 net_no = -1;
3028 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3029
qs.xiong9fbf74e2023-03-28 13:38:22 +08003030 for (index=0; index < count; index++)
3031 {
you.chen35020192022-05-06 11:30:57 +08003032 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003033 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3034 {
you.chen35020192022-05-06 11:30:57 +08003035 net_no = net_no_list[index];
3036 break;
3037 }
3038 }
3039
qs.xiong9fbf74e2023-03-28 13:38:22 +08003040 if (net_no < 0)
3041 {
you.chen35020192022-05-06 11:30:57 +08003042 return -1;
3043 }
3044
3045 CHECK_WPA_CTRL(CTRL_STA);
3046
3047 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3048
3049 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3050 DO_OK_FAIL_REQUEST(cmd_save_config);
3051
3052 return 0;
3053}
3054
qs.xiongb5dab082023-10-13 14:43:41 +08003055/**
3056* buff data
3057* buff_len size of buff
3058* idx sta
3059* *ap ap info for find ssid && password
3060* password return password
3061*
3062*/
3063static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3064
3065 int ret, network_len, i, ssid_len,curr_auth;
3066 int count, index,org_index;
3067 char *split_lines[128] = {0};
3068 char *p, *ssid, *ssid_end_flag,*ptr;
3069 char tmp_ssid[128]={0};
3070 char tmp_auth[24]={0};
3071
3072 org_index = 0;
3073 network_len = 0;
3074 p = NULL;
3075
3076 CHECK_IDX(idx, CTRL_STA);
3077
3078 while(1){
3079 network_len = 0;
3080 p == NULL;
3081 for(; org_index < buff_len; org_index ++)
3082 {
3083 for(; org_index < buff_len; org_index ++)
3084 {
3085 if (memcmp(buff + org_index, "network={", 9) != 0)
3086 {
3087 continue;
3088 }
3089 p = buff + org_index + 9;
3090
3091 for (; org_index < buff_len; org_index ++ )
3092 {
3093 if (buff[org_index] != '}')
3094 {
3095 continue;
3096 }
3097 buff[org_index] = '\0';
3098 break;
3099 }
3100 network_len = buff + org_index - p;
3101 break;
3102 }
3103
3104 if (p == NULL)
3105 {
3106 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3107 return -1;
3108 }
3109
3110 ssid = strstr(p, "ssid=");
3111 if (ssid != NULL) {
3112 ssid += strlen("ssid=");
3113 if (ssid[0] == '\"')
3114 {
3115 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3116 {
3117 RLOGD("-----curr_get ssid form config is %s",ssid);
3118 break;
3119 }
3120 RLOGD("-----countine to find dest ssid %s ---curr_get ssid from config is %s",ap->ap_ssid,ssid);
3121 }
3122 else
3123 {
3124 ssid_end_flag = strstr(ssid, "\n");
3125 if (ssid_end_flag != NULL)
3126 {
3127 ssid_len = (ssid_end_flag - ssid) / 2;
3128 for(i=0; i<ssid_len; i++)
3129 {
3130 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3131 }
3132 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3133 {
3134 RLOGD("curr_ssid is(from config) ---- %s ap_info ssid --->",tmp_ssid,ap->ap_ssid);
3135 break;
3136 }
3137 }
3138 }
3139 }
3140
3141 }
3142
3143 if (org_index >= buff_len || NULL == p || network_len <= 0)
3144 {
3145
3146 if (buff != NULL)
3147 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3148 return -1;
3149 }
3150
3151 count = lynq_split(p, network_len, '\n', split_lines);
3152 ret = -1;
3153 for( index=0; index < count; index++ )
3154 {
3155 p = strstr(split_lines[index], "key_mgmt=");
3156 RLOGD("current p str ------- %s",p);
3157 if(p != NULL)
3158 {
3159 p += 9;
3160 if(memcmp(p,"SAE",3) == 0)
3161 {
3162 curr_auth = 5;
3163 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3164 {
3165 curr_auth = 4;
3166 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3167 {
3168 curr_auth = 3;
3169 }else if(memcmp(p,"NONE",4) == 0 )
3170 {
3171 curr_auth = 0;
3172 }else{
3173 curr_auth = 1;
3174 }
3175 RLOGD("************curret_get_auth is %d ssid is %s",curr_auth,ap->ap_ssid);
3176 if( curr_auth < 1 || curr_auth > 6)
3177 {
3178 ret = -1;
3179 }
3180 break;
3181 }
3182 }
3183 if( curr_auth == 0)
3184 {
3185 return 0;
3186 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3187 {
3188 for(index=0; index < count; index++)
3189 {
3190 /*get psw info*/
3191
3192 p = strstr(split_lines[index], "psk=");
3193 if (p != NULL)
3194 {
3195 p += 4;
3196 if (*p == '\"')
3197 {
3198 p++;
3199 }
3200 }
3201 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3202 {
3203 p += 9;
3204 if (*p == '\"')
3205 {
3206 p++;
3207 }
3208 }
3209 else
3210 {
3211 continue;
3212 }
3213
3214 if (*p == '\"')
3215 p++;
3216 strncpy(password, p, 64);
3217 p = password;
3218 while(password - p < 64 && *password != '\0')
3219 {
3220 if (*password == '\"')
3221 {
3222 *password = '\0';
3223 RLOGD("---------password------- p:: %s",p);
3224 ret = 0;
3225 break;
3226 }
3227 password++;
3228 }
3229 break;
3230 }
3231 break;
3232 }
3233 }
3234
3235 return ret;
3236}
3237
3238
3239
you.chen35020192022-05-06 11:30:57 +08003240int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3241
3242 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003243 int len, ret;
3244 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003245 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003246
qs.xiongb5dab082023-10-13 14:43:41 +08003247 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003248 CHECK_IDX(idx, CTRL_STA);
3249
qs.xiong9fbf74e2023-03-28 13:38:22 +08003250 if (NULL == password)
3251 {
3252 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003253 return -1;
3254 }
3255
you.chen35020192022-05-06 11:30:57 +08003256 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003257 if (NULL == fp)
3258 {
3259 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003260 return -1;
3261 }
3262
you.chen6d247052023-06-01 16:39:54 +08003263 fseek(fp, 0, SEEK_END);
3264 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003265 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003266
qs.xiongb5dab082023-10-13 14:43:41 +08003267 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003268 {
3269 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3270 return -1;
3271 }
3272
you.chen35020192022-05-06 11:30:57 +08003273 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003274 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003275 fclose(fp);
3276
qs.xiongb5dab082023-10-13 14:43:41 +08003277
3278 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3279
3280 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003281 {
qs.xiongb5dab082023-10-13 14:43:41 +08003282 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3283 free(info_buff);
3284 return 0;
you.chen35020192022-05-06 11:30:57 +08003285 }
qs.xiongb5dab082023-10-13 14:43:41 +08003286 else{
3287 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003288 return -1;
3289 }
3290
you.chen35020192022-05-06 11:30:57 +08003291}
3292
qs.xiongb5dab082023-10-13 14:43:41 +08003293
you.chen35020192022-05-06 11:30:57 +08003294static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3295{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003296 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003297
qs.xiong9fbf74e2023-03-28 13:38:22 +08003298 if (sta_ssid == NULL)
3299 {
3300 RLOGE("sta_ssid is null\n");
3301 return -1;
you.chen35020192022-05-06 11:30:57 +08003302 }
3303
qs.xiong9fbf74e2023-03-28 13:38:22 +08003304 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003305
3306 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3307
3308 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3309// DO_OK_FAIL_REQUEST(cmd_save_config);
3310
qs.xiong9fbf74e2023-03-28 13:38:22 +08003311 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003312
3313}
3314
you.chen35020192022-05-06 11:30:57 +08003315static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003316{
you.chen35020192022-05-06 11:30:57 +08003317 char lynq_disable_cmd[128]={0};
3318 char lynq_select_cmd[128]={0};
3319
3320 CHECK_WPA_CTRL(CTRL_STA);
3321
qs.xiong9fbf74e2023-03-28 13:38:22 +08003322 if (save != 0)
3323 {
you.chenc29444e2022-06-07 18:01:16 +08003324 if (start_flag != 0)
3325 {
3326 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3327 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3328 }
3329 else
3330 {
3331 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3332 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3333 }
you.chen35020192022-05-06 11:30:57 +08003334 DO_OK_FAIL_REQUEST(cmd_save_config);
3335 }
3336
qs.xiong9fbf74e2023-03-28 13:38:22 +08003337 if (start_flag == 0)
3338 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003339 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003340 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3341 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 else
3343 {
you.chen35020192022-05-06 11:30:57 +08003344 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3345 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3346 }
3347
3348 return 0;
3349}
3350
3351int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3352{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003353 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003354 CHECK_IDX(idx, CTRL_STA);
3355
you.chen6c2dd9c2022-05-16 17:55:28 +08003356 curr_status_info curr_state;
3357 ap_info_s ap_info;
3358 curr_state.ap = &ap_info;
3359 curr_state.state = NULL;
3360
qs.xiong9fbf74e2023-03-28 13:38:22 +08003361 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3362 {
you.chend2fef3f2023-02-13 10:50:35 +08003363 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003364 return 0;
3365 }
3366
3367 return -1;
you.chen35020192022-05-06 11:30:57 +08003368}
3369
3370int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3371{
qs.xiong5d716d22023-09-20 20:08:39 +08003372 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003373 scan_info_s *scan_list = NULL;
3374 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003375 int scan_len=0;
3376 int save_len=0;
3377 int best_index = -1;
3378 int best_scan_index = -1;
3379 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003380 int i, j, ret;
3381
3382 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003383
3384 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003385 if (info == NULL)
3386 {
you.chen35020192022-05-06 11:30:57 +08003387 return -1;
3388 }
3389
3390 curr_status_info curr_state;
3391 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003392 char status[64];
you.chen35020192022-05-06 11:30:57 +08003393
you.chen9ac66392022-08-06 17:01:16 +08003394 memset(&ap_info, 0, sizeof (ap_info));
3395 memset(status, 0, sizeof (status));
3396
3397 curr_state.ap = &ap_info;
3398 curr_state.state = status;
3399
qs.xiong9fbf74e2023-03-28 13:38:22 +08003400 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3401 {
you.chen35020192022-05-06 11:30:57 +08003402 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003403 if (strcmp(status, STATE_COMPLETED) == 0)
3404 {
3405 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003406 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003407 }
3408 else
3409 {
3410 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003411 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003412 }
you.chen593621d2023-04-27 17:52:44 +08003413 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003414 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003415 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003416 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003417 return 0;
3418 }
3419
you.chen9ac66392022-08-06 17:01:16 +08003420 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003421 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003422 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3423 {
you.chen9ac66392022-08-06 17:01:16 +08003424 if (NULL != scan_list)
3425 {
3426 free(scan_list);
3427 }
you.chen35020192022-05-06 11:30:57 +08003428 return -1;
3429 }
3430
qs.xiong9fbf74e2023-03-28 13:38:22 +08003431 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3432 {
you.chen9ac66392022-08-06 17:01:16 +08003433 if (NULL != scan_list)
3434 {
3435 free(scan_list);
3436 }
3437 if (NULL != save_list)
3438 {
3439 free(save_list);
3440 }
you.chen35020192022-05-06 11:30:57 +08003441 return -1;
3442 }
3443
qs.xiong9fbf74e2023-03-28 13:38:22 +08003444 for (i=0; i < save_len; i++)
3445 {
3446 for (j=0; j < scan_len; j++)
3447 {
you.chen35020192022-05-06 11:30:57 +08003448 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003449 && save_list[i].base_info.auth == scan_list[j].auth)
3450 {
3451 if (best_rssi == 0)
3452 {
you.chen9ac66392022-08-06 17:01:16 +08003453 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003454 best_rssi = scan_list[j].rssi;
3455 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003456 else if (best_rssi > scan_list[j].rssi)
3457 {
you.chen35020192022-05-06 11:30:57 +08003458 best_index = i;
3459 best_scan_index = j;
3460 best_rssi = scan_list[j].rssi;
3461 }
you.chend2fef3f2023-02-13 10:50:35 +08003462 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 +08003463 break;
3464 }
3465 }
3466 }
3467
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 if (best_index >= 0)
3469 {
you.chen35020192022-05-06 11:30:57 +08003470 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003471 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 +08003472 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003473 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003474 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003475 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003476 }
3477
you.chen9ac66392022-08-06 17:01:16 +08003478 if (NULL != scan_list)
3479 {
3480 free(scan_list);
3481 }
3482 if (NULL != save_list)
3483 {
3484 free(save_list);
3485 }
3486
qs.xiong5d716d22023-09-20 20:08:39 +08003487 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003488 return ret;
you.chen35020192022-05-06 11:30:57 +08003489}
3490
3491static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3492{
qs.xiongc8d92a62023-03-29 17:36:14 +08003493 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003494 char lynq_ket_mgmt_cmd[64]={0};
3495 char lynq_pairwise_cmd[64]={0};
3496 char lynq_psk_cmd[64]={0};
3497
3498 CHECK_WPA_CTRL(CTRL_STA);
3499
qs.xiong9fbf74e2023-03-28 13:38:22 +08003500 switch(auth)
3501 {
3502 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003503 {
3504 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003505
you.chen35020192022-05-06 11:30:57 +08003506 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003507// DO_OK_FAIL_REQUEST(cmd_save_config);
3508 break;
3509 }
3510 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003511 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003512 {
3513 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3514 {
you.chen35020192022-05-06 11:30:57 +08003515 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3516 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003517 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3518 {
you.chena6cd55a2022-05-08 12:20:18 +08003519 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003520 }
3521 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3522 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003523
you.chen35020192022-05-06 11:30:57 +08003524 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3525 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3526 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003527
qs.xiong9fbf74e2023-03-28 13:38:22 +08003528 if (password != NULL)
3529 {
you.chen35020192022-05-06 11:30:57 +08003530 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3531 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003532 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003533 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003534
you.chen35020192022-05-06 11:30:57 +08003535// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003536 break;
3537 }
3538 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3539 {
qs.xiong3e506812023-04-06 11:08:48 +08003540 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003541 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003542 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3543 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3544
qs.xiong3e506812023-04-06 11:08:48 +08003545 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003546 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3547 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3548 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3549
3550 break;
3551 }
3552 case LYNQ_WIFI_AUTH_WPA3_PSK:
3553 {
qs.xiong3e506812023-04-06 11:08:48 +08003554 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003555 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003556 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3557 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3558
qs.xiongb37f8c42023-09-13 21:21:58 +08003559 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003560 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3561 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3562 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3563
3564 break;
3565 }
3566 default:
3567 return -1;
you.chen35020192022-05-06 11:30:57 +08003568 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003569
qs.xiong9fbf74e2023-03-28 13:38:22 +08003570 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003571}
qs.xiong7a105ce2022-03-02 09:43:11 -05003572
you.chen35020192022-05-06 11:30:57 +08003573static int inner_get_curr_net_no(int interface) {
3574 curr_status_info curr_state;
3575 curr_state.ap = NULL;
3576 curr_state.state = NULL;
3577
qs.xiong9fbf74e2023-03-28 13:38:22 +08003578 if (0 != inner_get_status_info(interface, &curr_state))
3579 {
you.chen35020192022-05-06 11:30:57 +08003580 return -1;
3581 }
3582
3583 return curr_state.net_no;
3584}
3585
3586int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003587{
you.chen35020192022-05-06 11:30:57 +08003588 int net_no;
3589 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003590
you.chen35020192022-05-06 11:30:57 +08003591 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003592
qs.xiong9fbf74e2023-03-28 13:38:22 +08003593 if (net_no < 0)
3594 {
you.chen35020192022-05-06 11:30:57 +08003595 return -1;
3596 }
3597
3598 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003599}
3600
you.chenb95401e2023-05-12 19:39:06 +08003601int 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 -05003602{
you.chen35020192022-05-06 11:30:57 +08003603 int count, net_no, index;
3604 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003605 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003606 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003607 curr_status_info curr_state;
3608 ap_info_s ap_info;
3609 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003610
qs.xiong9fbf74e2023-03-28 13:38:22 +08003611 if (ssid == NULL || *ssid == '\0')
3612 {
3613 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003614 return -1;
3615 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003616
qs.xiong9fbf74e2023-03-28 13:38:22 +08003617 if (LYNQ_WIFI_AUTH_OPEN != auth)
3618 {
3619 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003620 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003621 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003622 return -1;
3623 }
3624 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003625
you.chen70f377f2023-04-14 18:17:09 +08003626
3627 pthread_mutex_lock(&s_global_check_mutex);
3628 if (s_sta_status != INNER_STA_STATUS_INIT)
3629 {
3630 s_sta_status = INNER_STA_STATUS_CANCEL;
3631 pthread_cond_signal(&s_global_check_cond);
3632 }
3633 pthread_mutex_unlock(&s_global_check_mutex);
3634
you.chen35020192022-05-06 11:30:57 +08003635 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003636 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003637
3638 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003639 memset(&ap_info, 0, sizeof (ap_info));
3640 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003641
you.chen70f377f2023-04-14 18:17:09 +08003642 curr_state.ap = &ap_info;
3643 curr_state.state = status;
3644
3645 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003646 {
you.chen70f377f2023-04-14 18:17:09 +08003647 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3648 {
3649 net_no = curr_state.net_no;
3650 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3651 && strcmp(ap_info.psw, psw) == 0)
3652 {
3653 RLOGD("already connected\n");
3654
3655 pthread_mutex_lock(&s_global_check_mutex);
3656 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003657 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003658 pthread_cond_signal(&s_global_check_cond);
3659 pthread_mutex_unlock(&s_global_check_mutex);
3660 return 0;
3661 }
you.chen35020192022-05-06 11:30:57 +08003662 }
3663 }
3664
you.chen70f377f2023-04-14 18:17:09 +08003665 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003666 {
you.chen70f377f2023-04-14 18:17:09 +08003667 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3668
3669 for (index=0; index < count; index++)
3670 {
3671 net_auth = -1;
3672 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3673 {
3674 net_no = net_no_list[index];
3675 break;
3676 }
you.chen35020192022-05-06 11:30:57 +08003677 }
3678
you.chen70f377f2023-04-14 18:17:09 +08003679 if (net_no < 0)
3680 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003681 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3682 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3683 {
3684 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3685 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3686 DO_OK_FAIL_REQUEST(rm_net_cmd);
3687 }
you.chen70f377f2023-04-14 18:17:09 +08003688 net_no = lynq_add_network(CTRL_STA);
3689 if (net_no == -1)
3690 {
3691 return -1;
3692 }
3693
3694 RLOGD("net no is %d\n", net_no);
3695 if (0 != inner_set_sta_ssid(net_no, ssid))
3696 {
3697 return -1;
3698 }
you.chen35020192022-05-06 11:30:57 +08003699 }
3700 }
3701
qs.xiong9fbf74e2023-03-28 13:38:22 +08003702 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3703 {
you.chen35020192022-05-06 11:30:57 +08003704 return -1;
3705 }
3706
you.chen70f377f2023-04-14 18:17:09 +08003707
3708 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003709 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003710 usleep(200*1000);
3711
qs.xiong09560402023-10-27 21:58:55 +08003712 pthread_mutex_lock(&s_global_check_mutex);
3713 lynq_sta_removeElement(net_no);
3714 pthread_mutex_unlock(&s_global_check_mutex);
3715
you.chen70f377f2023-04-14 18:17:09 +08003716 ret = inner_sta_start_stop(net_no, 1, 1);
3717
3718 pthread_mutex_lock(&s_global_check_mutex);
3719 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003720 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003721 strcpy(s_sta_current_connecting_ssid, ssid);
3722 struct timeval now;
3723 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003724 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003725 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3726 pthread_cond_signal(&s_global_check_cond);
3727 pthread_mutex_unlock(&s_global_check_mutex);
3728 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003729}
3730
you.chenb95401e2023-05-12 19:39:06 +08003731int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3732{
3733 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3734}
3735
you.chen35020192022-05-06 11:30:57 +08003736int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003737{
you.chen35020192022-05-06 11:30:57 +08003738 ap_info_s ap;
3739 curr_status_info curr_state;
3740 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003741
qs.xiong9fbf74e2023-03-28 13:38:22 +08003742 if (ssid == NULL || *ssid == '\0')
3743 {
3744 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003745 return -1;
3746 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003747
you.chen35020192022-05-06 11:30:57 +08003748 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003749
you.chen35020192022-05-06 11:30:57 +08003750 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003751 curr_state.state = NULL;
3752
qs.xiong9fbf74e2023-03-28 13:38:22 +08003753 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3754 {
you.chen35020192022-05-06 11:30:57 +08003755 return 0;
3756 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003757
qs.xiong9fbf74e2023-03-28 13:38:22 +08003758 if (strcmp(ap.ap_ssid, ssid) != 0)
3759 {
you.chen35020192022-05-06 11:30:57 +08003760 return 0;
3761 }
3762
you.chen70f377f2023-04-14 18:17:09 +08003763 pthread_mutex_lock(&s_global_check_mutex);
3764 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3765 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003766 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003767}
qs.xiong97fa59b2022-04-07 05:41:29 -04003768
qs.xiongc93bf2b2023-08-25 10:22:08 +08003769int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3770{
qs.xiong09560402023-10-27 21:58:55 +08003771 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003772 ap_info_s ap;
3773 curr_status_info curr_state;
3774 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003775 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003776
3777 if (ssid == NULL || *ssid == '\0')
3778 {
3779 RLOGE("input ssid is NULL\n");
3780 return -1;
3781 }
3782
3783 CHECK_IDX(idx, CTRL_STA);
3784
3785
3786 curr_state.ap = &ap;
3787 curr_state.state = NULL;
3788
3789 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3790 {
3791 return 0;
3792 }
3793
3794 if (strcmp(ap.ap_ssid, ssid) != 0)
3795 {
3796 return 0;
3797 }
3798
3799 pthread_mutex_lock(&s_global_check_mutex);
3800 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003801 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3802 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3803 {
3804 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);
3805 if( g_history_disconnect_net[i] == curr_state.net_no)
3806 {
3807 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3808 check_history_idx_flag = 1;
3809 break;
3810 }
3811 }
3812 if ( check_history_idx_flag == 0)
3813 {
3814 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__);
3815 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3816 g_history_disconnect_valid_num++;
3817 }
3818 RLOGD("%s %d",__func__,__LINE__);
3819 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003820 pthread_mutex_unlock(&s_global_check_mutex);
3821 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3822
3823}
3824
3825
you.chena6cd55a2022-05-08 12:20:18 +08003826int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3827{
qs.xiongb37f8c42023-09-13 21:21:58 +08003828
qs.xiongad2f89d2023-01-18 13:17:41 +08003829 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3830 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003831
you.chen35020192022-05-06 11:30:57 +08003832 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003833 CHECK_WPA_CTRL(CTRL_STA);
3834
you.chenc9928582023-04-24 15:39:37 +08003835 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3836 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003837 {
you.chenc9928582023-04-24 15:39:37 +08003838 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003839 return -1;
3840 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003841
qs.xiongad2f89d2023-01-18 13:17:41 +08003842 system(lynq_enable_sta_cmd);
3843 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003844 pthread_mutex_lock(&s_global_check_mutex);
3845 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003846 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003847 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003848 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003849}
3850
you.chen6d247052023-06-01 16:39:54 +08003851static int inner_get_status_info_state (int interface, char *state) {
3852 curr_status_info curr_state;
3853 curr_state.ap = NULL;
3854 curr_state.state = state;
3855 return inner_get_status_info(interface, &curr_state);
3856}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003857
3858int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3859{
3860
qs.xiongb37f8c42023-09-13 21:21:58 +08003861 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3862 int tmp_open_idx[128];
3863 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003864
qs.xiongb37f8c42023-09-13 21:21:58 +08003865 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003866 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003867 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3868 pthread_mutex_unlock(&s_global_check_mutex);
3869 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003870 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003871 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003872 }
3873
qs.xiongb37f8c42023-09-13 21:21:58 +08003874 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003875 return 0;
3876}
3877
3878
you.chen35020192022-05-06 11:30:57 +08003879int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003880{
qs.xiongad2f89d2023-01-18 13:17:41 +08003881// char lynq_disable_network_cmd[MAX_CMD];
3882// curr_status_info curr_state;
3883// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003884 int i=0;
3885 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003886
you.chen6d247052023-06-01 16:39:54 +08003887// 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 +08003888 CHECK_IDX(idx, CTRL_STA);
3889 CHECK_WPA_CTRL(CTRL_STA);
3890
you.chen6d247052023-06-01 16:39:54 +08003891// system(lynq_disable_sta_cmd);
3892 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003893 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003894
3895 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3896 if (ret != 0)
3897 {
3898 RLOGE("lynq_wifi_ap_start excute script fail");
3899 return -1;
3900 }
3901
you.chen6d247052023-06-01 16:39:54 +08003902 for (i=0; i < 30; i++) // to check if sta is realy stoped
3903 {
3904 if (inner_get_status_info_state(idx, state) != 0)
3905 {
3906 break;
3907 }
3908
3909 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3910 {
3911 break;
3912 }
3913 RLOGD("lynq_wifi_ap_start curr state %s", state);
3914 usleep(SLEEP_TIME_ON_IDLE);
3915 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003916 pthread_mutex_lock(&s_global_check_mutex);
3917 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3918 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003919 return 0;
3920// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003921}
qs.xiongfcc914b2023-07-06 21:16:20 +08003922int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3923{
3924 char LYNQ_DISABLE_CMD[128]={0};
3925 CHECK_IDX(idx, CTRL_STA);
3926 CHECK_WPA_CTRL(CTRL_STA);
3927 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3928 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3929 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3930 return 0;
3931}
qs.xiong7a105ce2022-03-02 09:43:11 -05003932
you.chen35020192022-05-06 11:30:57 +08003933//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3934// int i, count;
3935// char *p;
3936// const char * FLAG_SSID = "ssid=";
3937// const char * FLAG_SBSID = "bssid=";
3938// const char * FLAG_KEY_MGMT = "key_mgmt=";
3939// const char * FLAG_FREQ = "freq=";
3940// char lynq_sta_cmd[MAX_CMD];
3941// char *split_lines[128] = {0};
3942
3943// CHECK_WPA_CTRL(CTRL_AP);
3944
3945// sprintf(lynq_sta_cmd, "STA %s", bssid);
3946
3947// DO_REQUEST(lynq_sta_cmd);
3948
3949// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3950
3951// for(i=0; i < count; i++) {
3952// p = strstr(split_lines[i], FLAG_SSID);
3953// if (p != NULL) {
3954// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3955// continue;
3956// }
3957// }
3958
3959// lynq_get_interface_ip(idx, ap->ap_ip);
3960// lynq_ap_password_set(idx, ap->psw);
3961
3962// return 0;
3963//}
3964
3965static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3966 curr_status_info curr_state;
3967 curr_state.ap = ap;
3968 curr_state.state = NULL;
3969 return inner_get_status_info(interface, &curr_state);
3970}
3971
3972int 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 -04003973{
qs.xiong5071c802023-09-06 14:04:15 +08003974 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003975 int index, line_count;
3976 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003977 const char *lynq_first_sta_cmd = "STA-FIRST";
3978 char lynq_next_sta_cmd[MAX_CMD];
3979 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003980 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003981
you.chen35020192022-05-06 11:30:57 +08003982 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003983
you.chen35020192022-05-06 11:30:57 +08003984 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003985
you.chenb95401e2023-05-12 19:39:06 +08003986 // ap_info_s * tmp_ap;
3987 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003988 if (ap == NULL || list == NULL || len == NULL)
3989 {
3990 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003991 return -1;
3992 }
3993
you.chenb95401e2023-05-12 19:39:06 +08003994 // ap = &tmp_ap;
3995 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003996 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003997 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003998
you.chenb95401e2023-05-12 19:39:06 +08003999 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004000 {
you.chenb95401e2023-05-12 19:39:06 +08004001 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004002 return -1;
4003 }
4004
4005 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4006 lynq_ap_password_get(idx, (*ap)->psw);
4007
you.chen35020192022-05-06 11:30:57 +08004008 DO_REQUEST(lynq_first_sta_cmd);
4009
4010 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004011 while (reply_len > 0)
4012 {
4013 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004014 {
you.chen35020192022-05-06 11:30:57 +08004015 break;
4016 }
4017 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4018 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4019 strcpy(bssid[index], split_lines[0]);
4020 index++;
4021 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4022 reply_len = MAX_RET;
4023 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004024 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 +08004025 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004026 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004027 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004028 break;
4029 }
4030 }
4031
4032 *len = index;
4033
4034 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004035 for (index=0; index < *len; index++)
4036 {
you.chend2fef3f2023-02-13 10:50:35 +08004037 dev_info = &(*list)[index];
4038 memset(dev_info, 0, sizeof(device_info_s));
4039 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4040 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4041 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4042 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004043 free(bssid[index]);
4044 }
qs.xiong5071c802023-09-06 14:04:15 +08004045 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004046 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004047}
4048
you.chen35020192022-05-06 11:30:57 +08004049int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004050{
you.chen35020192022-05-06 11:30:57 +08004051 int i, count, index, count_words;
4052 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4053 char *split_lines[128] = {0};
4054 char *split_words[128] = {0};
4055 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004056
qs.xiong9fbf74e2023-03-28 13:38:22 +08004057 if (list == NULL || len == NULL)
4058 {
you.chen35020192022-05-06 11:30:57 +08004059 return -1;
4060 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004061
you.chen9ac66392022-08-06 17:01:16 +08004062 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4063 {
4064 usleep(100 * 1000);
4065 }
4066
you.chen35020192022-05-06 11:30:57 +08004067 CHECK_IDX(idx, CTRL_STA);
4068
4069 CHECK_WPA_CTRL(CTRL_STA);
4070
4071 DO_REQUEST(lynq_scan_result_cmd);
4072
4073 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4074 *len = count - 1;
4075 *list = malloc(sizeof (scan_info_s) * *len);
4076
4077 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004078 for (index=0; index <count_words; index++)
4079 {
4080 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004081 }
4082
qs.xiong9fbf74e2023-03-28 13:38:22 +08004083 for(index = 1;index < count; index++)
4084 {
4085 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004086 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004087 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4088 if (count_words < 4)
4089 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004090 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004091 //bssid / frequency / signal level / flags / ssid
4092 p = (*list) + index - 1;
4093 strcpy(p->mac, split_words[0]);
4094 p->band = convert_band_from_freq(atoi(split_words[1]));
4095 p->rssi = -1 * atoi( split_words[2]);
4096 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004097 if (count_words == 4) // ssid hided
4098 {
4099 p->ssid[0] = '\0';
4100 }
4101 else
4102 {
4103 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4104 }
you.chen35020192022-05-06 11:30:57 +08004105 }
4106
4107 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004108}
qs.xiong97fa59b2022-04-07 05:41:29 -04004109
you.chen35020192022-05-06 11:30:57 +08004110int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4111{
4112 int count, net_no, index;
4113 int net_no_list[128];
4114 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004115
you.chen35020192022-05-06 11:30:57 +08004116 char lynq_remove_cmd[MAX_CMD];
4117
qs.xiong9fbf74e2023-03-28 13:38:22 +08004118 if (ssid == NULL || *ssid == '\0')
4119 {
4120 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004121 return -1;
4122 }
4123
4124 CHECK_IDX(idx, CTRL_STA);
4125
4126 CHECK_WPA_CTRL(CTRL_STA);
4127
4128 net_no = -1;
4129 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4130
qs.xiong9fbf74e2023-03-28 13:38:22 +08004131 for (index=0; index < count; index++)
4132 {
you.chen35020192022-05-06 11:30:57 +08004133 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004134 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4135 {
you.chen35020192022-05-06 11:30:57 +08004136 net_no = net_no_list[index];
4137 break;
4138 }
4139 }
4140
qs.xiong9fbf74e2023-03-28 13:38:22 +08004141 if (net_no < 0)
4142 {
you.chen35020192022-05-06 11:30:57 +08004143 return 0;
4144 }
4145
4146 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4147
4148 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004149
4150 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4151
4152 pthread_mutex_lock(&s_global_check_mutex);
4153 lynq_sta_removeElement(net_no);
4154 pthread_mutex_unlock(&s_global_check_mutex);
4155
4156 RLOGD("%s %d",__func__,__LINE__);
4157 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004158 DO_OK_FAIL_REQUEST(cmd_save_config);
4159
4160 return 0;
4161}
4162
4163int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004164{
you.chend2fef3f2023-02-13 10:50:35 +08004165 int count, index;
you.chen35020192022-05-06 11:30:57 +08004166 int net_no_list[128];
4167 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004168 RLOGD("enter lynq_get_sta_saved_ap api\n");
4169 if (list == NULL || len == NULL)
4170 {
4171 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004172 return -1;
4173 }
4174
4175 CHECK_IDX(idx, CTRL_STA);
4176
4177// CHECK_WPA_CTRL(CTRL_STA);
4178
4179 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004180 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004181
you.chen057aac42023-04-13 14:06:58 +08004182 if (count < 0)
4183 {
4184 RLOGE("list network fail");
4185 return count;
4186 }
4187 else if (count == 0)
4188 {
4189 *list = NULL;
4190 *len = 0;
4191 return 0;
4192 }
4193
you.chen35020192022-05-06 11:30:57 +08004194 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004195 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004196 *len = count;
4197
qs.xiong9fbf74e2023-03-28 13:38:22 +08004198 for (index=0; index < count; index++)
4199 {
you.chen35020192022-05-06 11:30:57 +08004200 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004201 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004202 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004203 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004204 {
you.chen35020192022-05-06 11:30:57 +08004205 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4206 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004207 else
you.chen057aac42023-04-13 14:06:58 +08004208 {
you.chen35020192022-05-06 11:30:57 +08004209 (*list)[index].base_info.band = -1;
4210 }
you.chen057aac42023-04-13 14:06:58 +08004211 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004212 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004213 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004214 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004215 return 0;
4216}
4217
4218int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4219{
you.chen0c9bee22023-10-25 13:03:14 +08004220 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004221 {
you.chen0c9bee22023-10-25 13:03:14 +08004222 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4223 g_sta_fake_scan_finish_flag = 1;
4224 return 0;
4225 }
4226 else if (g_sta_conncet_status_flag != 0)
4227 {
4228 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004229 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004230 }
you.chen0c9bee22023-10-25 13:03:14 +08004231
qs.xiongc8d92a62023-03-29 17:36:14 +08004232 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004233 const char *lynq_scan_cmd = "SCAN";
4234
4235 CHECK_IDX(idx, CTRL_STA);
4236
4237 CHECK_WPA_CTRL(CTRL_STA);
4238
you.chen0df3e7e2023-05-10 15:56:26 +08004239 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4240 {
4241 RLOGD("tmp clear scanlist");
4242 system(clean_last_re);
4243 }
you.chen9ac66392022-08-06 17:01:16 +08004244 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004245 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004246 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4247 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004248 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004249 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4250 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004251 g_sta_scan_finish_flag = 1;
4252 return -1;
4253 }
you.chen35020192022-05-06 11:30:57 +08004254
4255 return 0;
4256}
4257
4258int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004259 if (cb == NULL)
4260 {
4261 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004262 return -1;
4263 }
4264
you.chen6d247052023-06-01 16:39:54 +08004265 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004266 g_ap_callback_priv = priv;
4267 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004268 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004269
you.chen6d247052023-06-01 16:39:54 +08004270 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004271 if (g_ap_watcher_pid == 0 )
4272 {
4273 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4274 {
4275 g_ap_watcher_pid = 0;
4276 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4277 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4278 return -1;
4279 }
4280 }
4281
4282 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4283 RLOGD("creat APWatcherTheradProc susccs");
4284
you.chen35020192022-05-06 11:30:57 +08004285 return 0;
4286}
4287
4288int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004289 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 if (g_ap_callback_priv == priv)
4291 {
you.chen35020192022-05-06 11:30:57 +08004292 g_ap_callback_func = NULL;
4293 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004294 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004295 return 0;
4296 }
you.chen6d247052023-06-01 16:39:54 +08004297 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004298 return -1;
4299}
4300
4301int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004302 if (cb == NULL)
4303 {
4304 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004305 return -1;
4306 }
4307
you.chen6d247052023-06-01 16:39:54 +08004308 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004309 g_sta_callback_priv = priv;
4310 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004311 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004312
you.chen6d247052023-06-01 16:39:54 +08004313 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004314 if (g_sta_watcher_pid == 0 ) {
4315 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4316 {
4317 g_sta_watcher_pid = 0;
4318 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4319 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4320 return -1;
4321 }
4322 }
4323
4324 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4325 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004326 return 0;
4327}
4328
4329int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004330 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004331 if (g_sta_callback_priv == priv)
4332 {
you.chen35020192022-05-06 11:30:57 +08004333 g_sta_callback_func = NULL;
4334 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004335 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004336 return 0;
4337 }
you.chen6d247052023-06-01 16:39:54 +08004338 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004339 return -1;
4340}
4341
qs.xiongfcc914b2023-07-06 21:16:20 +08004342int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4343 if (cb == NULL)
4344 {
4345 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4346 return -1;
4347 }
4348 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4349 g_sta_auto_callback_priv = priv;
4350 g_sta_auto_callback_func = cb;
4351 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4352 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4353 if (g_sta_auto_watcher_pid == 0 ) {
4354 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4355 {
4356 g_sta_auto_watcher_pid = 0;
4357 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4358 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4359 return -1;
4360 }
4361 }
4362 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4363 RLOGD("creat STAWatcherTheradProc susccs");
4364 return 0;
4365}
4366int lynq_unreg_sta_auto_event_callback(void * priv) {
4367 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4368 if (g_sta_auto_callback_priv == priv)
4369 {
4370 g_sta_auto_watcher_stop_flag = 1;
4371 if (g_sta_auto_watcher_pid != 0)
4372 {
4373 pthread_join(g_sta_auto_watcher_pid, NULL);
4374 }
4375 g_sta_auto_watcher_pid = 0;
4376 g_sta_auto_callback_func = NULL;
4377 g_sta_auto_callback_priv = NULL;
4378 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4379 return 0;
4380 }
4381 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4382 return -1;
4383}
you.chen35020192022-05-06 11:30:57 +08004384int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4385{
4386 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004387 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004388 CHECK_IDX(idx, CTRL_AP);
4389
qs.xiong9fbf74e2023-03-28 13:38:22 +08004390 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4391 {
you.chen35020192022-05-06 11:30:57 +08004392 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4393 return 0;
4394 }
4395
qs.xiong9fbf74e2023-03-28 13:38:22 +08004396 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4397 {
you.chen35020192022-05-06 11:30:57 +08004398 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4399 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004400 else
4401 {
you.chen35020192022-05-06 11:30:57 +08004402 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4403 }
4404
4405 return 0;
4406}
4407
4408int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4409 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004410 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004411 CHECK_IDX(idx, CTRL_STA);
4412
qs.xiong9fbf74e2023-03-28 13:38:22 +08004413 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4414 {
you.chen35020192022-05-06 11:30:57 +08004415 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4416 return 0;
4417 }
4418
qs.xiong9fbf74e2023-03-28 13:38:22 +08004419 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4420 {
you.chen35020192022-05-06 11:30:57 +08004421 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4422 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004423 else
4424 {
you.chen35020192022-05-06 11:30:57 +08004425 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4426 }
4427
4428 return 0;
4429}
4430
4431int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4432// CHECK_IDX(idx, CTRL_AP);
4433// int ret = 0;
4434// size_t reply_len = MAX_RET;
4435// char cmd_reply[MAX_RET]={0};
4436// const char * cmd_str = "GET country";
4437// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4438// do{
4439// if (NULL == s_lynq_wpa_ctrl) {
4440// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4441// if (NULL == s_lynq_wpa_ctrl ) {
4442// printf("wpa_ctrl_open fail\n");
4443// return -1;
4444// }
4445// }
4446// }while(0);
4447
4448// do {
4449// reply_len = MAX_RET;
4450// cmd_reply[0] = '\0';
4451// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004452// 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 +08004453// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004454// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004455// return ret;
4456// }
4457// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004458// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004459// }while(0);
4460
4461 FILE *fp;
4462 size_t i = 0;
4463 char lynq_cmd_ret[MAX_RET]={0};
4464
4465// CHECK_IDX(idx, CTRL_AP);
4466
4467 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004468 {
4469 perror("popen error!");
4470 return -1;
4471 }
you.chen35020192022-05-06 11:30:57 +08004472 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4473 {
4474 perror("fread fail!");
4475 return -1;
4476 }
4477
qs.xiong9fbf74e2023-03-28 13:38:22 +08004478 for(i=0; i < strlen(lynq_cmd_ret); i++)
4479 {
4480 if (lynq_cmd_ret[i] == ' ')
4481 {
you.chen35020192022-05-06 11:30:57 +08004482 lynq_cmd_ret[i] = '\0';
4483 break;
4484 }
4485 }
4486
4487 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004488 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004489
4490 int ret=pclose(fp);
4491 if(ret==-1)
4492 {
4493 perror("close file faild");
4494 }
4495
4496 return 0;
4497}
4498
qs.xiong44fac672023-08-29 16:15:55 +08004499
you.chen705a7ef2023-06-01 22:06:45 +08004500static int check_and_init_uci_config(char * country_code)
4501{
4502 FILE * fp;
4503 int is_different = 0;
4504 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4505 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4506 const char * commit_uci_cmd ="uci commit";
4507 char set_country_cmd[MAX_CMD];
4508 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004509
you.chen705a7ef2023-06-01 22:06:45 +08004510 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004511
you.chen705a7ef2023-06-01 22:06:45 +08004512 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004513 {
you.chen705a7ef2023-06-01 22:06:45 +08004514 if (0 != system(create_uci_cmd))
4515 {
4516 RLOGE("creat_uci_cmd fail");
4517 return -1;
4518 }
4519 is_different = 1;
4520 }
4521
4522 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4523 {
4524 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004525 return -1;
4526 }
4527
you.chen705a7ef2023-06-01 22:06:45 +08004528 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004529 {
you.chen705a7ef2023-06-01 22:06:45 +08004530 RLOGE("fread fail!");
4531 fclose(fp);
4532 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004533 }
4534
you.chen705a7ef2023-06-01 22:06:45 +08004535 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4536 {
qs.xiong44fac672023-08-29 16:15:55 +08004537 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 +08004538 is_different = 1;
4539 }
4540
4541 fclose(fp);
4542
4543 if (is_different)
4544 {
4545 if ( 0 != system(set_country_cmd))
4546 {
4547 RLOGE("set_country_cmd fail");
4548 return -1;
4549 }
4550 if (0 != system(commit_uci_cmd))
4551 {
4552 RLOGE("commmit fail");
4553 }
4554 }
4555
4556 return is_different;
4557}
4558
4559int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4560 char check_current_code[10];
4561 const char * support_country[] = {"CN", "EU"};
4562
4563 int ret,is_different, i, cc_count;
4564
4565 if (country_code == NULL || country_code[0] == '\0')
4566 {
4567 RLOGE("bad country code\n");
4568 return -1;
4569 }
4570
4571 cc_count = sizeof (support_country) / sizeof (char*);
4572 for(i=0; i < cc_count; i++)
4573 {
4574 if (strcmp(support_country[i], country_code) == 0)
4575 {
4576 break;
4577 }
4578 }
4579
4580 if (i >= cc_count)
4581 {
4582 RLOGE("unspported country code %s\n", country_code);
4583 return -1;
4584 }
4585
4586 is_different = check_and_init_uci_config(country_code);
4587 if( is_different < 0 )
4588 {
4589 RLOGE("init set uci fail\n");
4590 return -1;
4591 }
4592
4593 ret = lynq_get_country_code(idx,check_current_code);
4594 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4595 {
4596 ret = lynq_wifi_disable();
4597 if(ret != 0 )
4598 {
4599 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4600 return -1;
4601 }
4602 }
4603
4604 return 0;
you.chen35020192022-05-06 11:30:57 +08004605}
4606
4607int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4608{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004609 RLOGD("enter lynq_get_connect_ap_mac\n");
4610 if (mac == NULL)
4611 {
4612 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004613 return -1;
4614 }
4615
4616 CHECK_IDX(idx, CTRL_STA);
4617 ap_info_s ap;
4618 ap.ap_mac[0] = '\0';
4619
qs.xiong9fbf74e2023-03-28 13:38:22 +08004620 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4621 {
you.chen35020192022-05-06 11:30:57 +08004622 return -1;
4623 }
4624 strcpy(mac, ap.ap_mac);
4625
4626 return 0;
4627}
4628
4629int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4630{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004631 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004632 struct ifaddrs *ifaddr_header, *ifaddr;
4633 struct in_addr * ifa;
4634 const char * ifaName = "wlan0";
4635 if (ip == NULL)
4636 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004637 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004638 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004639 }
you.chenf58b3c92022-06-21 16:53:48 +08004640
qs.xiong9fbf74e2023-03-28 13:38:22 +08004641 if (idx == 1)
4642 {
you.chen0df3e7e2023-05-10 15:56:26 +08004643 ifaName = inner_get_ap_interface_name();
4644 if (ifaName == NULL)
4645 {
4646 RLOGE("[lynq_get_interface_ip] ap name get fail");
4647 return -1;
4648 }
you.chen9ac66392022-08-06 17:01:16 +08004649 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004650 else if (idx != 0)
4651 {
you.chen35020192022-05-06 11:30:57 +08004652 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004653 }
you.chen35020192022-05-06 11:30:57 +08004654
you.chen9ac66392022-08-06 17:01:16 +08004655 if (getifaddrs(&ifaddr_header) == -1)
4656 {
you.chen35020192022-05-06 11:30:57 +08004657 perror("getifaddrs");
4658 return -1;
4659 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004660 }
you.chen35020192022-05-06 11:30:57 +08004661
4662
you.chen9ac66392022-08-06 17:01:16 +08004663 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4664 {
4665 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004666 continue;
you.chen9ac66392022-08-06 17:01:16 +08004667 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4668 {
4669 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4670 {
4671 // is a valid IP4 Address
4672 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4673 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004674 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004675 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004676 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004677 return 0;
4678 }
4679 }
4680 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004681 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004682 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004683 return -1;
you.chen35020192022-05-06 11:30:57 +08004684}
4685
4686int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4687{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004688 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004689 int count;
4690 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004691 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004692 char *split_words[128] = {0};
4693 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4694
4695 CHECK_WPA_CTRL(idx);
4696
4697 DO_REQUEST(lynq_get_mac_cmd);
4698
qs.xiong9fbf74e2023-03-28 13:38:22 +08004699 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4700 {
4701 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004702 return -1;
4703 }
4704
4705 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4706
qs.xiong9fbf74e2023-03-28 13:38:22 +08004707 if (count < 2)
4708 {
you.chen35020192022-05-06 11:30:57 +08004709 return -1;
4710 }
4711
qs.xiong9fbf74e2023-03-28 13:38:22 +08004712 for (i=0; i < strlen(split_words[1]); i++ )
4713 {
4714 if (split_words[1][i] != ' ')
4715 {
you.chen35020192022-05-06 11:30:57 +08004716 break;
4717 }
4718 }
4719
qs.xiongdd6e44c2023-08-08 15:02:53 +08004720 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004721
4722 return 0;
4723}
4724
4725int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4726{
4727// int count;
4728// char *split_words[128] = {0};
4729// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4730
4731// if (rssi == NULL) {
4732// return -1;
4733// }
4734
4735// CHECK_IDX(idx, CTRL_STA);
4736
4737// CHECK_WPA_CTRL(CTRL_STA);
4738
4739// DO_REQUEST(lynq_get_rssi_cmd);
4740
4741// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4742// return -1;
4743// }
4744
4745// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4746
4747// if (count < 2) {
4748// return -1;
4749// }
4750
4751// *rssi = atoi(split_words[1]) * -1;
4752
you.chen35020192022-05-06 11:30:57 +08004753 char lynq_cmd_ret[MAX_RET]={0};
4754
qs.xiongff0ae0f2022-10-11 15:47:14 +08004755/*******change other cmd to get rssi*******
4756 *
4757 *wl rssi ---> wl -i wlan0 rssi
4758 *
4759 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004760 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004761 {
you.chen23c4a5f2023-04-12 16:46:00 +08004762 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004763 return -1;
4764 }
you.chen9f17e4d2022-06-06 17:18:18 +08004765 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004766/****** if got rssi is 0,means sta didn't connected any device****/
4767 if(*rssi == 0)
4768 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004769 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004770 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004771 }
you.chen35020192022-05-06 11:30:57 +08004772
4773 return 0;
4774}
4775
4776int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4777{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004778 RLOGD("enter lynq_get_connect_ap_band\n");
4779 if (band == NULL)
4780 {
you.chen35020192022-05-06 11:30:57 +08004781 return -1;
4782 }
4783
4784 CHECK_IDX(idx, CTRL_STA);
4785 ap_info_s ap;
4786 ap.band = -1;
4787
qs.xiong9fbf74e2023-03-28 13:38:22 +08004788 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4789 {
you.chen35020192022-05-06 11:30:57 +08004790 return -1;
4791 }
4792 *band = ap.band;
4793
4794 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004795}
you.chenf58b3c92022-06-21 16:53:48 +08004796
4797int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4798{
you.chenb95401e2023-05-12 19:39:06 +08004799 int ret;
4800 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004801 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004802
4803 if (ip == NULL)
4804 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004805 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004806 return -1;
4807 }
4808
4809 CHECK_IDX(idx, CTRL_STA);
4810
qs.xionge4cbf1c2023-02-28 18:22:49 +08004811 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004812 {
4813 return -1;
4814 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004815
you.chenb95401e2023-05-12 19:39:06 +08004816 ip[0] = '\0';
4817 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4818 if (ret != 0)
4819 {
4820 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4821 return -1;
4822 }
4823
4824 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4825 {
4826 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004827 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004828 if (ret != 0)
4829 {
4830 ip[0] = '\0';
4831 return 0;
4832 }
4833 else
4834 {
4835 p = strchr(ip, '\n');
4836 if (p != NULL)
4837 {
4838 *p = '\0';
4839 }
4840 }
4841 }
4842 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004843}
4844
qs.xionge02a5252023-09-20 14:00:21 +08004845int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4846{
4847 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4848 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4849}
4850
qs.xiong026c5c72022-10-17 11:15:45 +08004851int lynq_ap_connect_num(int sta_number)
4852{
4853 char lynq_limit_cmd[32]={0};
4854 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004855 if((sta_number < 1 ) && (sta_number > 15))
4856 {
4857 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004858 return -1;
4859 }
4860 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4861 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004862 if(ret != 0)
4863 {
4864 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004865 }
4866 return 0;
4867}
you.chenf58b3c92022-06-21 16:53:48 +08004868
qs.xiong77905552022-10-17 11:19:57 +08004869int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4870{
4871
4872 char lynq_wifi_acs_cmd[128]={0};
4873 char lynq_cmd_mode[128]={0};
4874 char lynq_cmd_slect[128]={0};
4875
qs.xiong9fbf74e2023-03-28 13:38:22 +08004876 if((acs_mode != 2) && (acs_mode != 5))
4877 {
qs.xiong77905552022-10-17 11:19:57 +08004878 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4879 }
4880
qs.xiong9fbf74e2023-03-28 13:38:22 +08004881 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4882 {
qs.xiong77905552022-10-17 11:19:57 +08004883 return -1;
4884 }
4885
4886 CHECK_IDX(idx, CTRL_AP);
4887
4888 CHECK_WPA_CTRL(CTRL_AP);
4889
4890 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4891 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4892 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4893
4894 DO_OK_FAIL_REQUEST(cmd_disconnect);
4895 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4896 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4897 DO_OK_FAIL_REQUEST(cmd_save_config);
4898 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4899
4900 return 0;
4901}
you.chen0f5c6432022-11-07 18:31:14 +08004902//you.chen add for tv-box start
4903static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4904 FILE *fp;
4905 //printf("to exec cmd:%s\n", str_cmd);
4906 if((fp=popen(str_cmd,"r"))==NULL)
4907 {
4908 perror("popen error!");
4909 return -1;
4910 }
4911 if((fread(str_cmd_ret,max_len,1,fp))<0)
4912 {
4913 perror("fread fail!");
4914 fclose(fp);
4915 return -1;
4916 }
4917 fclose(fp);
4918 return 0;
4919}
4920
4921static int get_netmask_length(const char* mask)
4922{
4923 int masklen=0, i=0;
4924 int netmask=0;
4925
4926 if(mask == NULL)
4927 {
4928 return 0;
4929 }
4930
4931 struct in_addr ip_addr;
4932 if( inet_aton(mask, &ip_addr) )
4933 {
4934 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004935 }else
4936 {
you.chen0f5c6432022-11-07 18:31:14 +08004937 netmask = 0;
4938 return 0;
4939 }
4940
4941 while(0 == (netmask & 0x01) && i<32)
4942 {
4943 i++;
4944 netmask = netmask>>1;
4945 }
4946 masklen = 32-i;
4947 return masklen;
4948}
4949
4950static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4951 int mask_len;
4952 char *p;
4953 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004954 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4955 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004956 return -1;
4957 p = strstr(str_cmd_ret, "Mask:");
4958 if (p == NULL)
4959 return -1;
4960 mask_len = get_netmask_length(p + 5);
4961 if (mask_len == 0)
4962 return -1;
4963 p = strstr(str_cmd_ret, "inet addr:");
4964 if (p == NULL)
4965 return -1;
4966 strcpy(tmp, p + 10);
4967 p = strstr(tmp, " ");
4968 if (p != NULL)
4969 *p = '\0';
4970 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4971 return 0;
4972}
4973
4974static void GBWWatchThreadProc() {
4975 int i,n, nloop, nmax, ncheckcount, nidlecount;
4976 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4977 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4978 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4979 char *results[16] = {0};
4980 char str_cmd[256] = {0};
4981 char str_cmd_ret[128] = {0};
4982 char dest_ip[32] = {0};
4983 lastAP1Bytes = lastAP2Bytes = 0;
4984 lastAP1Drop = lastAP2Drop = 0;
4985 lastAP1Speed = lastAP2Speed = 0;
4986 setAP1Speed = 50;
4987 setAP2Speed = 80;
4988 nloop = 0;
4989 nmax = 6;
4990 ncheckcount = nidlecount = 0;
4991
you.chen0df3e7e2023-05-10 15:56:26 +08004992 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004993 {
4994 RLOGE("------gbw thread run\n");
4995 return;
4996 }
4997
qs.xiong9fbf74e2023-03-28 13:38:22 +08004998 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004999 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5000 while (dest_ip[0] == '\0') {
5001 sleep(1);
5002 str_cmd_ret[0] = '\0';
5003 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5004 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5005 if (str_cmd_ret[n] == '\n'){
5006 str_cmd_ret[n] = '\0';
5007 break;
5008 }
5009 }
5010 if (str_cmd_ret[0] != '\0')
5011 {
5012 strcpy(dest_ip, str_cmd_ret);
5013 }
5014 }
5015
you.chenc9928582023-04-24 15:39:37 +08005016 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5017 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5018 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 +08005019 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5020 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005021 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005022 return;
5023 }
you.chenc9928582023-04-24 15:39:37 +08005024 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);
5025 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);
5026 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 +08005027
5028 while (1) {
5029 sleep(1);
5030 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005031 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5032 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5033 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005034 continue;
5035 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005036 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005037 if (n > 9) {
5038 if (strcmp(results[1], "Sent") == 0) {
5039 currAP1Bytes = atoll(results[2]);
5040 }
5041 if (strcmp(results[6], "(dropped") == 0) {
5042 currAP1Drop = atoi(results[7]);
5043 }
5044 }
5045
5046 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005047 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5048 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5049 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005050 continue;
5051 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005052 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005053 if (n > 9) {
5054 if (strcmp(results[1], "Sent") == 0) {
5055 currAP2Bytes = atoll(results[2]);
5056 }
5057 if (strcmp(results[6], "(dropped") == 0) {
5058 currAP2Drop = atoi(results[7]);
5059 }
5060 }
5061
5062 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5063 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5064 lastAP1Bytes = currAP1Bytes;
5065 lastAP2Bytes = currAP2Bytes;
5066 continue;
5067 }
5068
5069 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5070 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5071 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5072 lastAP1Speed = currAP1Speed;
5073 lastAP2Speed = currAP2Speed;
5074 lastAP1Bytes = currAP1Bytes;
5075 lastAP2Bytes = currAP2Bytes;
5076
5077 currSetAP1Speed = setAP1Speed;
5078 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5079 ncheckcount++;
5080 if (ncheckcount > 3) {
5081 ncheckcount = 0;
5082 currSetAP1Speed = 5;
5083 }
5084 }
5085 else {
5086 ncheckcount = 0;
5087 if (currAP1Speed < 5)
5088 nidlecount++;
5089 else
5090 nidlecount = 0;
5091
5092 }
5093
5094 if (nidlecount > 60 ){
5095 currSetAP1Speed = 50;
5096 }
5097
5098 if (currSetAP1Speed != setAP1Speed) {
5099 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005100 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5101 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005102 }
5103 }
5104}
5105
5106int enableGBW(const char* mac) {
5107 int i,len;
5108 char get_ipaddr_cmd[128]={0};
5109 ap_info_s *ap;
5110 device_info_s * list;
5111
5112 if (mac == NULL || g_gbw_enabled == 1)
5113 return -1;
5114 len = strlen(mac);
5115 g_gbw_mac = malloc(len + 1);
5116 for(i=0;i<len;i++) {
5117 if (mac[i] >= 'A' && mac[i] <= 'Z')
5118 {
5119 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5120 }
5121 else
5122 g_gbw_mac[i] = mac[i];
5123 }
5124 g_gbw_mac[i] = '\0';
5125 g_gbw_enabled = 1;
5126
5127 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5128 if (system(get_ipaddr_cmd) == 0) {
5129 //startGBW();
5130 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5131 for (i=0;i<len;i++) {
5132 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5133 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5134 startGBW();
5135 }
5136 free(ap);
5137 free(list);
5138 }
5139 }
5140 return 0;
5141}
5142
5143int disableGBW() {
5144 stopGBW();
5145 free(g_gbw_mac);
5146 g_gbw_mac = NULL;
5147 g_gbw_enabled = 1;
5148 return 0;
5149}
5150
5151static int startGBW() {
5152 if (g_gbw_watcher_pid != 0) {
5153 stopGBW();
5154 }
5155 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5156}
5157
5158static int stopGBW() {
5159 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005160 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005161 pthread_cancel(g_gbw_watcher_pid);
5162 pthread_join(g_gbw_watcher_pid, &retval);
5163 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005164 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5165 if (s_ap_iterface_name[0] != '\0')
5166 {
5167 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5168 system(cmd);
5169 }
you.chen0f5c6432022-11-07 18:31:14 +08005170}
5171//you.chen add for tv-box end