blob: 77d826e47cea299c2038b50b5c62d5aa3197ad94 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen0c9bee22023-10-25 13:03:14 +080057volatile int g_sta_fake_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
qs.xiong6ad0e822023-11-24 17:53:30 +080070
you.chen35020192022-05-06 11:30:57 +080071//const char * CTRL_PATH="/var/run/wpa_supplicant";
72const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
73//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
74const char * cmd_list_networks = "LIST_NETWORKS";
75const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080076const char * cmd_disconnect = "DISCONNECT";
77const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080078const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080079const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080080const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080081const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080082
you.chenf711c8a2023-04-13 13:49:45 +080083const char * cmd_ping = "PING";
84const char * rsp_pong = "PONG";
85const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
86const int MAX_IDLE_COUNT = 600; // 60s
87
you.chenc9928582023-04-24 15:39:37 +080088const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
89const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
90const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
91const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
92const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
93
94static char s_ap_iterface_name[64] = {0};
95
you.chend2fef3f2023-02-13 10:50:35 +080096struct local_wpa_ctrl{
97 struct wpa_ctrl *ctrl;
98 pthread_mutex_t mutex;
99};
100
qs.xiong09560402023-10-27 21:58:55 +0800101volatile int g_history_disconnect_valid_num = 0;
qs.xiongb37f8c42023-09-13 21:21:58 +0800102int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800103
you.chend2fef3f2023-02-13 10:50:35 +0800104static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800105static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
106static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800107// add for auto connect
108static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800109
110static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800111
you.chen0f5c6432022-11-07 18:31:14 +0800112//you.chen add for tv-box start
113volatile int g_gbw_enabled = 0;
114char * g_gbw_mac = NULL;
115pthread_t g_gbw_watcher_pid = 0;
116static int startGBW();
117static int stopGBW();
118//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800119
120typedef struct __curr_status_info {
121 ap_info_s *ap;
122 char * state;
123 int net_no;
124}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400125
you.chen70f377f2023-04-14 18:17:09 +0800126typedef enum {
127 INNER_STA_STATUS_INIT = 0,
128 INNER_STA_STATUS_CONNECTING,
129 INNER_STA_STATUS_ASSOCIATING,
130 INNER_STA_STATUS_ASSOCIATED,
131 INNER_STA_STATUS_CONNECTED,
132 INNER_STA_STATUS_DISCONNECTING,
133 INNER_STA_STATUS_DISCONNECTED,
134 INNER_STA_STATUS_CANCEL,
135}inner_sta_status_s;
136
137static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
138static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800139volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800140static error_number_s s_sta_error_number = -1;
141static char s_sta_current_connecting_ssid[64] = {0};
142static struct timespec s_sta_connect_timeout;
143const int MAX_CONNNECT_TIME = 15; // second
144pthread_t g_global_watcher_pid = 0;
145static int s_service_invoke_timeout_cnt=0;
146const int FAKE_MAX_INT_VALUE = 99999;
147
qs.xiong09560402023-10-27 21:58:55 +0800148static void print_disconnect_list()
149{
150 int i;
151 for( i = 0; i < g_history_disconnect_valid_num; i++ )
152 {
153 RLOGD(" list of g_history_disconnect_valid_num is %d histroy_list[%d]:%d --------- %d",g_history_disconnect_valid_num,i,g_history_disconnect_net[i],__LINE__);
154 }
155
156 return;
157}
158
159// idex ----> history_disconnect_list[x] index
160static int removeElement(int idex)
161{
162 RLOGD("into removeElement");
163 if( index < 0 )
164 {
165 RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
166 return -1;
167 }
168 RLOGD("%s line: %d g_history_disconnect_net[%d]: %d end g_history_disconnect_net[%d]:%d",__func__,__LINE__,idex,g_history_disconnect_net[idex],g_history_disconnect_valid_num-1, g_history_disconnect_net[g_history_disconnect_valid_num-1]);
169 g_history_disconnect_net[idex] = g_history_disconnect_net[g_history_disconnect_valid_num-1]; //g_history_disconnect_vaild_num -1 for get last index
170 g_history_disconnect_valid_num --;
171 RLOGD("end removeElement");
172 return 0;
173}
174static int check_history_disconenct_ap_list(int val)
175{
176 print_disconnect_list();
177 RLOGD("WIFI[check_history_disconenct_ap_list]into check_history_disconenct_ap_list && input val is %d g_history_disconnect_valid_num is %d line",val,g_history_disconnect_valid_num,__LINE__);
178 int i;
179 for( i = 0; i < g_history_disconnect_valid_num; i++)
180 {
181 if( val == g_history_disconnect_net[i] )
182 {
183 RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
184 RLOGD("end check_history_disconenct_ap_list && return network index");
185 return i;
186 }
187 }
188 RLOGD("end check_history_disconenct_ap_list && return fail,didn't need remove networkid %d from list g_history_disconnect_valid_num is %d line %d",val,g_history_disconnect_valid_num,__LINE__);
189 return -1;
190}
191
192
193static void lynq_sta_removeElement(int net_no)
194{
195 int ret;
196
197 ret = check_history_disconenct_ap_list(net_no);
198 if( ret == -1 )
199 {
200 RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
201 return;
202 }else
203 {
204 ret = removeElement(ret);
205 if( ret == 0 )
206 {
207 RLOGD("removeElement pass %s %d",__func__,__LINE__);
208 return;
209 }
210 }
211
212 return;
213}
214
you.chen70f377f2023-04-14 18:17:09 +0800215static void notify_service_invoke_fail(int error)
216{
217 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
218 pthread_mutex_lock(&s_global_check_mutex);
219 if (error == -2) //timeout
220 {
221 s_service_invoke_timeout_cnt++;
222 if (s_service_invoke_timeout_cnt > 10)
223 {
224 pthread_cond_signal(&s_global_check_cond);
225 }
226 }
227 else if (error == -1)
228 {
229 // check if can connect wpa service
230 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
231 if (lynq_wpa_ctrl == NULL)
232 {
233 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
234 pthread_cond_signal(&s_global_check_cond);
235 }
236 wpa_ctrl_close(lynq_wpa_ctrl);
237 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
238 if (lynq_wpa_ctrl == NULL)
239 {
240 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
241 pthread_cond_signal(&s_global_check_cond);
242 }
243 wpa_ctrl_close(lynq_wpa_ctrl);
244 }
245
246 pthread_mutex_unlock(&s_global_check_mutex);
247}
248
you.chenc9928582023-04-24 15:39:37 +0800249static int system_call_v(const char * fmt, ...)
250{
251 char str_cmd[256] = {0};
252 va_list args;
253 va_start(args, fmt);
254 vsprintf(str_cmd, fmt, args);
255 va_end(args);
256 printf("system call----------%s\n", str_cmd);
257 return system(str_cmd);
258}
259
you.chen0df3e7e2023-05-10 15:56:26 +0800260static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
261
262static const char * inner_get_ap_interface_name()
263{
264 char * p;
265 char cmd[128]={0};
266
267 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
268 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
269 {
270 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
271 return NULL;
272 }
273 p = strchr(s_ap_iterface_name, ' ');
274 if (NULL != p)
275 {
276 *p = '\0';
277 }
278 p = strchr(s_ap_iterface_name, '\n');
279 if (NULL != p)
280 {
281 *p = '\0';
282 }
283 if (s_ap_iterface_name[0] == '\0')
284 {
285 return NULL;
286 }
287
288 return s_ap_iterface_name;
289}
290
you.chen70f377f2023-04-14 18:17:09 +0800291static void check_tether_and_notify()
292{
293 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800294 if (inner_get_ap_interface_name() == NULL || 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chen70f377f2023-04-14 18:17:09 +0800295 {
296 return;
297 }
298 pthread_mutex_lock(&s_global_check_mutex);
299 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
300 pthread_cond_signal(&s_global_check_cond);
301 pthread_mutex_unlock(&s_global_check_mutex);
302}
303
you.chend2fef3f2023-02-13 10:50:35 +0800304static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
305 char *reply, size_t *reply_len,
306 void (*msg_cb)(char *msg, size_t len))
307{
308 int ret;
309 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800310 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800311 return -1;
312 }
313 pthread_mutex_lock(&ctrl->mutex);
314 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
315 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800316 if (ret != 0)
317 {
318 notify_service_invoke_fail(ret);
319 }
you.chend2fef3f2023-02-13 10:50:35 +0800320 return ret;
321}
322
323static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
324 int repeat_cnt;
325 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
326 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800327 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800328 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
329 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
330// printf("wait enable finish\n");
331 usleep(500 * 1000);
332 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
333 }
334 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800335 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800336 goto out_addr;
337 }
338 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
339 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
340 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800341 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800342 goto out_addr;
343 }
344 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
345 }
346 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
347out_addr:
348 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
349 return lynq_wpa_ctrl;
350}
351
qs.xiong97fa59b2022-04-07 05:41:29 -0400352#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400353{\
you.chen35020192022-05-06 11:30:57 +0800354 perror((str));\
355 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400356}
357
you.chen35020192022-05-06 11:30:57 +0800358#define CHECK_IDX(idx, type) do { \
359 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
360 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800361 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800362 return -1; \
363 } \
364 }while (0)
365
366#define CHECK_WPA_CTRL(index) int ret = 0;\
367 size_t reply_len = MAX_RET; \
368 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800369 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800370 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800371 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
372 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800373 }while(0)
374
375#define DO_REQUEST(cmd_str) do { \
376 reply_len = MAX_RET;\
377 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800378 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800379 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800380 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800381 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800382 return ret; \
383 } \
384 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800385 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800386 }while(0)
387
388#define DO_OK_FAIL_REQUEST(cmd_str) do { \
389 DO_REQUEST(cmd_str); \
390 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800391 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800392 return -1; \
393 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800394 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800395 return -1; \
396 } \
397 }while (0)
398
399
you.chenf711c8a2023-04-13 13:49:45 +0800400static int check_connection(struct wpa_ctrl * wpa_ctrl)
401{
402 size_t reply_len = MAX_RET;
403 char cmd_reply[MAX_RET]={0};
404 int ret;
405
406 RLOGD("check_connection [%p]", wpa_ctrl);
407 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
408
409 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
410 {
411 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chen70f377f2023-04-14 18:17:09 +0800412 if (ret != 0)
413 {
414 notify_service_invoke_fail(ret);
415 }
you.chenf711c8a2023-04-13 13:49:45 +0800416 return -1;
417 }
418
419 return 0;
420}
421
422/**
423 * @brief check_pending_msg
424 * @param lynq_wpa_ctrl
425 * @return 1 has msg, 0 no msg, -1 error
426 */
427static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
428{
429 int ret;
430
431 if (*pp_lynq_wpa_ctrl == NULL) // need connect
432 {
433 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
434 if (*pp_lynq_wpa_ctrl == NULL)
435 {
436 usleep(SLEEP_TIME_ON_IDLE);
437 return -1;
438 }
439
440 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
441 if (ret == 0) // attach success
442 {
443 *started_flag = 1;
444 }
445 else
446 {
you.chen70f377f2023-04-14 18:17:09 +0800447 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800448 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
449 *pp_lynq_wpa_ctrl = NULL;
450 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800451 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800452 usleep(SLEEP_TIME_ON_IDLE);
453 return -1;
454 }
455 }
456
457 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
458 if ( ret == 0) // no pending messages
459 {
460 usleep(SLEEP_TIME_ON_IDLE);
461 *idle_count += 1;
462 if (*idle_count > MAX_IDLE_COUNT)
463 {
464 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
465 {
466 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
467 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
468 *pp_lynq_wpa_ctrl = NULL;
469 *idle_count = 0;
470 return -1;
471 }
472 *idle_count = 0;
473 }
474 return 0;
475 }
476 else if ( ret == -1) // on error
477 {
478 RLOGE("[wifi error]sta wpa_ctrl_pending");
479 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
480 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
481 *pp_lynq_wpa_ctrl = NULL;
482 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800483 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800484 return -1;
485 }
486
487 *idle_count = 0;
488 return 1;
489}
490
you.chen6d247052023-06-01 16:39:54 +0800491static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
492{
493 pthread_mutex_lock(&s_ap_callback_mutex);
494 if (g_ap_callback_func != NULL)
495 g_ap_callback_func(g_ap_callback_priv, status);
496 pthread_mutex_unlock(&s_ap_callback_mutex);
497}
498
you.chen35020192022-05-06 11:30:57 +0800499static void APWatcherThreadProc() {
500 size_t len = MAX_RET;
501 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800502 int idle_count = 0;
qs.xiong6ad0e822023-11-24 17:53:30 +0800503 char *ptr = NULL;
504 char mac[32] = {0};
505 char cmd[256] = {0};
you.chen35020192022-05-06 11:30:57 +0800506
you.chen6c2dd9c2022-05-16 17:55:28 +0800507 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800508 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800509
qs.xiong9fbf74e2023-03-28 13:38:22 +0800510 while (g_ap_watcher_stop_flag == 0)
511 {
you.chenf711c8a2023-04-13 13:49:45 +0800512 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
513 {
you.chen70f377f2023-04-14 18:17:09 +0800514 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
515 {
516 check_tether_and_notify();
517 }
518
you.chen35020192022-05-06 11:30:57 +0800519 continue;
520 }
you.chenf711c8a2023-04-13 13:49:45 +0800521
you.chen6c2dd9c2022-05-16 17:55:28 +0800522 memset(msg_notify, 0, MAX_RET);
523 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800524 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800525 {
you.chen35020192022-05-06 11:30:57 +0800526 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800527 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800528 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800529 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800530 {
you.chen6d247052023-06-01 16:39:54 +0800531 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong6ad0e822023-11-24 17:53:30 +0800532 ptr = strstr(msg_notify, "AP-STA-DISCONNECTED");
533 if( ptr != NULL)
534 {
535 ptr += strlen("AP-STA-DISCONNECTED ");
536 memcpy(mac,ptr,17);
537 sprintf(cmd,"cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print \"dhcp_release ap0 \"$3\" \"$2\}' | sh",mac);
538 RLOGD("%s %d cmd is %s",__func__,__LINE__,cmd);
539 system(cmd);
540 }
541
qs.xiong9fbf74e2023-03-28 13:38:22 +0800542 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800543 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800544 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
545 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800546 {
you.chen0f5c6432022-11-07 18:31:14 +0800547 stopGBW();
548 }
549 }
you.chen35020192022-05-06 11:30:57 +0800550 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800551 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800552 {
you.chen6d247052023-06-01 16:39:54 +0800553 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800554 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800555 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800556 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
557 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800558 {
you.chen0f5c6432022-11-07 18:31:14 +0800559 startGBW();
560 }
561 }
you.chen35020192022-05-06 11:30:57 +0800562 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800563 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800564 {
565 RLOGD("APWatcherThreadProc ap------> service error");
566 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
567 }
568 else
569 {
570 RLOGD("APWatcherThreadProc ap------> going on check next msg");
571 }
you.chenf711c8a2023-04-13 13:49:45 +0800572 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800573 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
574 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800575 if (lynq_wpa_ctrl != NULL)
576 {
you.chen92fd5d32022-05-25 10:09:47 +0800577 wpa_ctrl_detach(lynq_wpa_ctrl);
578 wpa_ctrl_close(lynq_wpa_ctrl);
579 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400580}
581
you.chen70f377f2023-04-14 18:17:09 +0800582static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
583{
584 char * p;
585 const char * try_associat_flag = "Trying to associate";
586 const char * associated_flag = "Associated with ";
587
588 pthread_mutex_lock(&s_global_check_mutex);
589 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
590 {
591 pthread_mutex_unlock(&s_global_check_mutex);
592 return;
593 }
594
you.chen6e724162023-10-19 19:10:01 +0800595 // youchen@2023-10-17 add for "not notify connect fail directly" begin
596 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
597 {
598 s_sta_error_number = error_num;
599 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
600 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
601 pthread_cond_signal(&s_global_check_cond);
602 pthread_mutex_unlock(&s_global_check_mutex);
603 return;
604 }
605 // youchen@2023-10-17 add for "not notify connect fail directly" end
606
you.chen70f377f2023-04-14 18:17:09 +0800607 if (state == LYNQ_WIFI_STATUS_EGNORE)
608 {
609 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
610 {
611 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
612 }
613 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
614 {
615 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
616 }
617 }
618 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
619 {
620 s_sta_error_number = error_num;
621 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
622 {
623 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800624 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800625 pthread_cond_signal(&s_global_check_cond);
626 }
627 }
628 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
629 {
630 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800631 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800632 pthread_cond_signal(&s_global_check_cond);
633 }
634 pthread_mutex_unlock(&s_global_check_mutex);
635}
636
qs.xiongb37f8c42023-09-13 21:21:58 +0800637static int lynq_split(char * str, int len, char delimiter, char * results[]);
638static inline char inner_convert_char(char in);
639static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
640static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
641
642
643
qs.xiong44fac672023-08-29 16:15:55 +0800644/*
645just tmp add for fix sta connect ap fail check ap connect info
646return 0 --->Current no sta device connect this AP
647*/
648static int lynq_connected_ap_sta_status() {
649
650 FILE *fp;
651 size_t i = 0;
652 int ret;
653 char lynq_cmd_ret[MAX_RET]={0};
654
655 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
656 {
657 perror("popen error!");
658 return -1;
659 }
660 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
661 {
662 perror("fread fail!");
663 ret=pclose(fp);
664 if(ret == -1)
665 perror("close file faild");
666 return -1;
667 }
668 if( strlen(lynq_cmd_ret) < MAC_LEN)
669 {
670 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
671 ret=pclose(fp);
672 if(ret==-1)
673 {
674 perror("close file faild");
675 }
676 return 0;
677 }else{
678 ret=pclose(fp);
679 if(ret==-1)
680 {
681 perror("close file faild");
682 }
683 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
684 return 1;
685 }
686}
687
688/*
689 just tmp add for fix sta connect ap fail; check fw status
690 return 1 ----> fw status error; need wl down/up
691*/
692static int check_current_fw_status() {
693
694 FILE *fp;
695 FILE *fp1;
696 size_t i = 0;
697 int ret;
698 char lynq_cmd_ret_2g[MAX_RET]={0};
699 char lynq_cmd_ret_5g[MAX_RET]={0};
700
701 const char * fw_status = "0x0096"; //0x0096 is normal fw status
702
703 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
704 {
705 perror("popen error!");
706 return -1;
707 }
708 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
709 {
710 perror("fread fail!");
711 if(pclose(fp) == -1)
712 perror("close fp file faild");
713 return -1;
714 }
715
716 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
717 {
718 perror("popen error!");
719 if(pclose(fp) == -1)
720 perror("clsoe fp file faild");
721 return -1;
722 }
723 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
724 {
725 perror("fread fail!");
726 if(pclose(fp1) == -1)
727 perror("clsoe fp1 file faild");
728 if(pclose(fp) == -1)
729 perror("clsoe fp file faild");
730 return -1;
731 }
732
733 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
734 {
735 ret=pclose(fp);
736 if(ret==-1)
737 {
738 perror("close fp file faild");
739 }
740 ret=pclose(fp1);
741 if(ret==-1)
742 {
743 perror("close fp1 file faild");
744 }
745 return 0;
746 }else
747 {
748 ret=pclose(fp);
749 if(ret==-1)
750 {
751 perror("close file faild");
752 }
753 if(pclose(fp1) == -1)
754 {
755 perror("clsoe file fp1 faild");
756 }
757 RLOGD("current fw status --error--");
758 return 1;
759 }
760}
761
qs.xiong1d4263a2023-09-06 10:46:23 +0800762/*
763eg: wl counters info
764sh-3.2# wl counters
765counters_version 30
766datalen 1648
767Slice_index: 0
768reinitreason_counts: 0(0) 1(0) 2(3) 3(0) 4(0) 5(0) 6(0) 7(0) 8(0) 9(0) 10(0) 11(0) 12(0) 13(0) 14(2) 15(0) 16(0) 17(0) 18(0) 19(0) 20(0) 21(0) 22(0) 23(0) 24(0) 25(0) 26(0) 27(0) 28(0) 29(0) 30(0) 31(0) 32(0) 33(0) 34(0) 35(0) 36(0) 37(0) 38(0) 39(0) 40(0) 41(0) 42(0) 43(0) 44(0) 45(0) 46(0) 47(0) 48(0) 49(0) 50(0) 51(0) 52(0) 53(0) 54(0)
769reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
770
771check reinit status
772return 0 ===> fw did wl reinit cmd
773*/
774static int check_current_reinit_info()
775{
776 FILE *fp;
777 int ret;
778 char lynq_cmd_ret[MAX_RET]={0};
779 char * dest;
780 char destid[3]={0};
781 if((fp=popen("wl counters","r"))==NULL)
782 {
783 perror("popen error!");
784 return -1;
785 }
786 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
787 {
788 perror("fread fail!");
789 if(pclose(fp) == -1)
790 {
791 perror("close fp file faild");
792 }
793 return -1;
794 }
795 dest = strstr(lynq_cmd_ret,"reinit ");
796 if(dest != NULL)
797 {
798 dest +=strlen("reinit ");
799 RLOGD("current get dest str is %s",dest);
800 memcpy(destid,dest,2);
801 ret = atoi(destid);
802 RLOGD("get current wl counters cmd return counts is %d",ret);
803 if( ret != 0 )
804 {
805 RLOGD("current fw did run cmd wl reinit");
806 if( pclose(fp) == -1 )
807 {
808 perror("close fp file faild");
809 }
810 return 0;
811 }
812 }
813 if( pclose(fp) == -1 )
814 {
815 perror("close fp file faild");
816 }
817 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
818 return -1;
819}
820
qs.xiong44fac672023-08-29 16:15:55 +0800821static void APTmpWatcherThreadProc() {
822
823 int i = 0;
824 int delytime = 300;
825 g_ap_tmp_watcher_stop_flag = 0;
826
827 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
828 while(1)
829 {
830 sleep(1);
831 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800832 if ( (i % 30) == 0 )
833 {
834 if ( check_current_reinit_info() == 0 )
835 {
836 system("wl reset_cnts");
837 system("wl down");
838 system("wl up");
839 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
840 }
841 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800842 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800843 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800844 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800845 {
846 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
847 {
848 system("wl down");
849 system("wl up");
850 }
qs.xiong44fac672023-08-29 16:15:55 +0800851 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800852 }
853 if ( i == delytime )
854 {
qs.xiong44fac672023-08-29 16:15:55 +0800855 i = 0;
856 }
857 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
858 {
859 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
860 return;
861 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800862
qs.xiong44fac672023-08-29 16:15:55 +0800863 }
864
865}
866
qs.xiongf0128b12023-06-29 17:29:39 +0800867static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
868{
869 char LYNQ_DISABLE_CMD[128]={0};
870
871 CHECK_IDX(idx, CTRL_STA);
872 CHECK_WPA_CTRL(CTRL_STA);
873
874 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800875 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800876 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800877
qs.xiongf0128b12023-06-29 17:29:39 +0800878 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800879
qs.xiongf0128b12023-06-29 17:29:39 +0800880}
881
qs.xiongb37f8c42023-09-13 21:21:58 +0800882static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
883{
884 char LYNQ_ENABLE_CMD[128]={0};
885
886 CHECK_IDX(idx, CTRL_STA);
887 CHECK_WPA_CTRL(CTRL_STA);
888
889
890 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800891 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800892 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
893
894 return 0;
895
896}
897
898static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
899{
900
901 int index,networkid;
902
903 for ( index = 0; index < len ;index++)
904 {
905 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800906 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800907 {
908 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
909 }
910 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
911 }
912 return 0;
913
914}
915
916
917/*
918 dis_net_list user disconnect list
919*/
920static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
921{
qs.xiong09560402023-10-27 21:58:55 +0800922 RLOGD("enter %s %d",__func__,__LINE__);
923 print_disconnect_list();
qs.xiongb37f8c42023-09-13 21:21:58 +0800924 int count,ncount,index;
925 int flag = 0;
926 int merge_index = 0;
927 int net_no_list[128];
928
qs.xiong09560402023-10-27 21:58:55 +0800929 for(ncount = 0;ncount < valid_num; ncount++ )
930 {
931 RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
932 }
933
qs.xiongb37f8c42023-09-13 21:21:58 +0800934 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
935 for( count = 0; count < index; count++)
936 {
937 for(ncount = 0; ncount < valid_num; ncount++)
938 {
qs.xiong09560402023-10-27 21:58:55 +0800939 RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800940 if(net_no_list[count] == dis_net_list[ncount])
941 {
qs.xiong09560402023-10-27 21:58:55 +0800942 RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800943 flag = 1;
944 break;
945 }
946 }
947 if( flag != 1 )
948 {
949 out[merge_index] = net_no_list[count];
qs.xiong09560402023-10-27 21:58:55 +0800950 RLOGD("out[%d]: %d net_no_list[%d]: %d %d",merge_index,out[merge_index],count,net_no_list[count],__LINE__);
qs.xiongb37f8c42023-09-13 21:21:58 +0800951 merge_index ++;
952 }
953 flag = 0;
954 }
955 * outlen =merge_index;
956 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
957 return;
958}
qs.xiongf0128b12023-06-29 17:29:39 +0800959
qs.xiong455c30b2023-04-12 11:40:02 +0800960void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
961{
962 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800963 char *wpanetid;
964 char destid[3] = {0};
965 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800966 *error = LYNQ_WAIT_CONNECT_ACTIVE;
967 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
968 {
969 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
970 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
971 return;
972 }
973
qs.xiong20202422023-09-06 18:01:18 +0800974 if (strstr(modify, "Trying to associate") != NULL)
975 {
976 RLOGD("Current sta is Trying to associate");
977 *state = LYNQ_WIFI_STATUS_EGNORE;
978 g_sta_conncet_status_flag = 1;
979 return;
980 }
981
qs.xiong455c30b2023-04-12 11:40:02 +0800982 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
983 {
984 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
qs.xiong09560402023-10-27 21:58:55 +0800985 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800986 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800987 return;
988 }
989
qs.xiong1e81dfa2023-09-27 15:52:37 +0800990 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
991 {
992 *error = LYNQ_PSW_ERROR;
993 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
994 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
995 g_sta_conncet_status_flag = 0;
996 return;
997 }
qs.xiong455c30b2023-04-12 11:40:02 +0800998 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
999 {
qs.xiongf0128b12023-06-29 17:29:39 +08001000 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1001 wpanetid = strstr(modify,"id=");
1002 if ( wpanetid != NULL )
1003 {
1004 wpanetid +=strlen("id=");
1005 memcpy(destid,wpanetid,2);
1006 tmpdisid = atoi(destid);
1007
1008 }
qs.xiong455c30b2023-04-12 11:40:02 +08001009 pReason = strstr(modify, "reason=");
1010 if (pReason != NULL)
1011 {
1012 pReason += strlen("reason=");
1013 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1014 {
1015 *error = LYNQ_TIME_OUT;
1016 }
1017 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1018 {
1019 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +08001020 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +08001021 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +08001022 // you.chen@2023-10-17 only disable network during autoconnect
1023 if(tmpdisid != -1 && s_sta_status == INNER_STA_STATUS_INIT && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
qs.xiongf0128b12023-06-29 17:29:39 +08001024 {
1025 RLOGE("stop wlan0 network %d fail",tmpdisid);
1026 }
qs.xiong455c30b2023-04-12 11:40:02 +08001027 }
1028 else
1029 {
1030 *error = LYNQ_UNSPECIFIED_REASON;
1031 }
qs.xiong455c30b2023-04-12 11:40:02 +08001032 }
1033 else
1034 {
1035 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +08001036 }
qs.xiongf0128b12023-06-29 17:29:39 +08001037 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +08001038 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +08001039 return;
qs.xiong455c30b2023-04-12 11:40:02 +08001040
1041 }
1042
1043 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
1044 {
1045 *error = LYNQ_NOT_FIND_AP;
1046 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1047 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001048 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001049 return;
1050 }
1051
1052
1053 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1054 {
1055 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1056 pReason = strstr(modify, "status_code=");
1057 if (pReason != NULL)
1058 {
1059 pReason += strlen("status_code=");
1060 if (memcmp(pReason, "17", 2) == 0)
1061 {
1062 *error = LYNQ_AP_UNABLE_HANDLE;
1063 }
1064 else if (memcmp(pReason, "1",1) == 0)
1065 {
1066 *error = LYNQ_UNSPECIFIED_REASON;
1067 }
1068 else
1069 {
1070 *error = LYNQ_UNSPECIFIED_REASON;
1071 }
1072
1073 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1074 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +08001075 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +08001076 return;
1077 }
1078 else
1079 {
1080 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1081 *error = LYNQ_UNSPECIFIED_REASON;
1082 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1083 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
1084 return;
1085 }
1086 }
1087
1088 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1089 {
1090 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1091 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1092 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1093 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1094 return;
1095 }
1096
1097 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1098 {
1099 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1100 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1101 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1102 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1103 return;
1104 }
1105
qs.xiongdf637b22023-10-26 19:30:07 +08001106 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1107 if (strstr(modify, "Association request to the driver failed") != NULL)
1108 {
1109 RLOGD("Association request to the driver failed --- recover");
1110 system("wl down");
1111 system("wl up");
1112 *error = LYNQ_UNSPECIFIED_REASON;
1113 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1114 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1115 return;
1116 }
1117 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1118
1119
you.chen32cb31e2023-04-13 14:05:45 +08001120 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001121 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001122 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001123 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1124 return;
1125
1126}
1127
qs.xiongfcc914b2023-07-06 21:16:20 +08001128void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1129{
1130 char *pReason;
1131 char *wpanetid;
1132 char destid[3];
1133 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1134 *networkid = -1;
1135 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1136 {
1137 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1138 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1139 return;
1140 }
1141 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1142 {
1143 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1144 wpanetid = strstr(modify,"id=");
1145 if ( wpanetid != NULL )
1146 {
1147 wpanetid +=strlen("id=");
1148 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1149 if (memcpy(destid,wpanetid,2) != NULL)
1150 {
1151 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1152 *networkid = atoi(destid);
1153 RLOGD("get networkid is %d\n",*networkid);
1154 }
1155 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1156 }
1157 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1158 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1159 return;
1160 }
1161 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1162 {
1163 wpanetid = strstr(modify,"id=");
1164 if ( wpanetid != NULL )
1165 {
1166 wpanetid +=strlen("id=");
1167 if (memcpy(destid,wpanetid,2) != NULL)
1168 {
1169 *networkid = atoi(destid);
1170 RLOGD("get networkid is %d\n",*networkid);
1171 }
1172 }
1173 pReason = strstr(modify, "reason=");
1174 if (pReason != NULL)
1175 {
1176 pReason += strlen("reason=");
1177 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1178 {
1179 *error = LYNQ_TIME_OUT;
1180 }
1181 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1182 {
1183 *error = LYNQ_PSW_ERROR;
1184 }
1185 else
1186 {
1187 *error = LYNQ_UNSPECIFIED_REASON;
1188 }
1189 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1190 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1191 return;
1192 }
1193 else
1194 {
1195 *error = LYNQ_UNSPECIFIED_REASON;
1196 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1197 return;
1198 }
1199 }
1200 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1201 {
1202 wpanetid = strstr(modify,"id=");
1203 if ( wpanetid != NULL )
1204 {
1205 wpanetid +=strlen("id=");
1206 if (memcpy(destid,wpanetid,2) != NULL)
1207 {
1208 *networkid = atoi(destid);
1209 RLOGD("get networkid is %d\n",*networkid);
1210 }
1211 }
1212 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1213 pReason = strstr(modify, "status_code=");
1214 if (pReason != NULL)
1215 {
1216 pReason += strlen("status_code=");
1217 if (memcmp(pReason, "17", 2) == 0)
1218 {
1219 *error = LYNQ_AP_UNABLE_HANDLE;
1220 }
1221 else if (memcmp(pReason, "1",1) == 0)
1222 {
1223 *error = LYNQ_UNSPECIFIED_REASON;
1224 }
1225 else
1226 {
1227 *error = LYNQ_UNSPECIFIED_REASON;
1228 }
1229 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1230 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1231 return;
1232 }
1233 else
1234 {
1235 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1236 *error = LYNQ_UNSPECIFIED_REASON;
1237 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001238 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001239 return;
1240 }
1241 }
1242 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1243 {
1244 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1245 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1246 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1247 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1248 return;
1249 }
1250 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1251 {
1252 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1253 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1254 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1255 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1256 return;
1257 }
1258 RLOGD("EVENT : %s\n", modify);
1259 *error = LYNQ_UNSPECIFIED_REASON;
1260 *state = LYNQ_WIFI_STATUS_EGNORE;
1261 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1262 return;
1263}
you.chen70f377f2023-04-14 18:17:09 +08001264static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1265{
you.chen6d247052023-06-01 16:39:54 +08001266 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001267 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1268 {
1269 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1270 g_sta_callback_func(g_sta_callback_priv, state, error);
1271 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1272 }
you.chen6d247052023-06-01 16:39:54 +08001273 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001274}
qs.xiongfcc914b2023-07-06 21:16:20 +08001275static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1276{
1277 pthread_mutex_lock(&s_sta_callback_mutex);
1278 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1279 {
1280 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1281 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1282 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1283 }
1284 pthread_mutex_unlock(&s_sta_callback_mutex);
1285}
you.chen70f377f2023-04-14 18:17:09 +08001286
you.chen35020192022-05-06 11:30:57 +08001287static void STAWatcherThreadProc() {
1288 size_t len = MAX_RET;
1289 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001290 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001291 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001292 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001293
you.chen6c2dd9c2022-05-16 17:55:28 +08001294 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001295 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001296
you.chen70f377f2023-04-14 18:17:09 +08001297 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001298 while (g_sta_watcher_stop_flag == 0)
1299 {
you.chenf711c8a2023-04-13 13:49:45 +08001300 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001301 {
you.chen35020192022-05-06 11:30:57 +08001302 continue;
1303 }
you.chenf711c8a2023-04-13 13:49:45 +08001304
you.chen6c2dd9c2022-05-16 17:55:28 +08001305 memset(msg_notify, 0, MAX_RET);
1306 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001307 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001308 {
you.chen35020192022-05-06 11:30:57 +08001309 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001310 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1311 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001312 {
you.chen35020192022-05-06 11:30:57 +08001313 g_sta_scan_finish_flag = 1;
1314 }
1315
qs.xiong9fbf74e2023-03-28 13:38:22 +08001316 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001317 {
you.chen35020192022-05-06 11:30:57 +08001318 continue;
1319 }
qs.xiong455c30b2023-04-12 11:40:02 +08001320 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001321 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1322 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1323 {
1324 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1325 }
you.chen0c9bee22023-10-25 13:03:14 +08001326 else if (state == LYNQ_WIFI_STA_STATUS_SCAN_RESULT &&
1327 s_sta_status != INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0)
1328 {
1329 RLOGD("donot report scan result during in call connect manual");
1330 }
you.chen6e724162023-10-19 19:10:01 +08001331 else
1332 {
1333 notify_connect_status(state, error);
1334 }
1335 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001336
1337 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001338 {
you.chen70f377f2023-04-14 18:17:09 +08001339 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001340 if (last_state != state)
1341 {
1342 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1343 {
1344 system_call_v("%s %s", sta_status_change_script, "connect");
1345 }
1346 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1347 {
1348 system_call_v("%s %s", sta_status_change_script, "disconnect");
1349 }
1350 }
qs.xiong09560402023-10-27 21:58:55 +08001351
you.chenc9928582023-04-24 15:39:37 +08001352 last_state = state;
you.chen0c9bee22023-10-25 13:03:14 +08001353 if (g_sta_fake_scan_finish_flag == 1)
1354 {
1355 g_sta_fake_scan_finish_flag = 0;
1356 notify_connect_status(LYNQ_WIFI_STA_STATUS_SCAN_RESULT, 0);
1357 }
you.chen32cb31e2023-04-13 14:05:45 +08001358 }
you.chen35020192022-05-06 11:30:57 +08001359 }
1360 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001361 if (lynq_wpa_ctrl != NULL)
1362 {
you.chen92fd5d32022-05-25 10:09:47 +08001363 wpa_ctrl_detach(lynq_wpa_ctrl);
1364 wpa_ctrl_close(lynq_wpa_ctrl);
1365 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001366}
qs.xiongfcc914b2023-07-06 21:16:20 +08001367static void STAAutoWatcherThreadProc() {
1368 size_t len = MAX_RET;
1369 char msg_notify[MAX_RET];
1370 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001371 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001372 int idle_count = 0;
1373 int networkid;
1374 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1375 g_sta_auto_watcher_stop_flag = 0;
1376 RLOGD("STAAutoWatcherThreadProc thread started ------");
1377 while (g_sta_auto_watcher_stop_flag == 0)
1378 {
1379 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1380 {
1381 continue;
1382 }
1383 memset(msg_notify, 0, MAX_RET);
1384 len = MAX_RET;
1385 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1386 {
1387 msg_notify[len+1] = '\0';
1388 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1389 if (strstr(msg_notify, state_scan_result) != NULL)
1390 {
1391 g_sta_auto_scan_finish_flag = 1;
1392 }
1393 if (g_sta_auto_callback_func == NULL)
1394 {
1395 continue;
1396 }
1397 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1398 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001399 }
1400 }
1401 if (lynq_wpa_ctrl != NULL)
1402 {
1403 wpa_ctrl_detach(lynq_wpa_ctrl);
1404 wpa_ctrl_close(lynq_wpa_ctrl);
1405 }
1406}
qs.xiongf1b525b2022-03-31 00:58:23 -04001407
you.chen70f377f2023-04-14 18:17:09 +08001408// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1409static void GlobalWatcherThreadProc()
1410{
1411 int ret, connect_timeout, service_abnormal;
1412 error_number_s error_num = -1;
1413 inner_sta_status_s sta_status;
1414 scan_info_s *scan_list = NULL;
1415 int i, scan_len=0;
1416 char connecting_ssid[64];
1417 struct timeval now;
1418
1419 RLOGD("GlobalWatcherThreadProc start to run");
1420
1421 while (1)
1422 {
1423 pthread_mutex_lock(&s_global_check_mutex);
1424 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1425 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1426 {
1427 pthread_mutex_unlock(&s_global_check_mutex);
1428 usleep(50*1000);
1429 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1430 continue;
1431 }
1432
1433 connect_timeout = 0;
1434 service_abnormal = 0;
1435 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1436 {
1437 while (1)
1438 {
1439 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1440 if (ret == ETIME)
1441 {
1442 connect_timeout = 1;
1443 }
1444 else if (ret != 0)
1445 {
1446 gettimeofday(&now,NULL);
1447 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1448 {
1449 usleep(SLEEP_TIME_ON_IDLE);
1450 continue;
1451 }
1452 connect_timeout = 1;
1453 }
1454 sta_status = s_sta_status;
1455 error_num = s_sta_error_number;
1456 s_sta_status = INNER_STA_STATUS_INIT;
1457 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1458 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1459 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1460 break;
1461 }
1462 }
1463 if (s_service_invoke_timeout_cnt > 10)
1464 {
1465 service_abnormal = 1;
1466 s_service_invoke_timeout_cnt = 0;
1467 }
1468 pthread_mutex_unlock(&s_global_check_mutex);
1469
1470 if (service_abnormal == 1)
1471 {
1472 sleep(1);
1473 RLOGE("wpa service is abnormal info app to exit");
1474 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001475
1476 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1477
you.chen70f377f2023-04-14 18:17:09 +08001478 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1479 }
1480
1481 if (sta_status == INNER_STA_STATUS_CANCEL)
1482 {
1483 continue;
1484 }
1485 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1486 {
1487 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1488 }
1489 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1490 {
1491 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1492 {
1493 for(i=0; i < scan_len;i++)
1494 {
1495 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1496 {
1497 error_num = LYNQ_AUTH_ERROR;
1498 break;
1499 }
1500 }
1501 free(scan_list);
1502 }
1503 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1504 }
1505 else if (connect_timeout == 0)
1506 {
1507 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1508 }
1509 else // wait timeout
1510 {
1511 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1512 {
1513 ; // wpa service abnormal
1514 }
1515 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1516 {
1517 RLOGD("GlobalWatcherThreadProc notify connected");
1518 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1519 }
1520 else
1521 {
1522 RLOGD("GlobalWatcherThreadProc notify timeout");
1523 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1524 }
1525 }
1526 } // while (1)
1527}
1528
qs.xiong1af5daf2022-03-14 09:12:12 -04001529int lynq_wifi_enable(void)
1530{
you.chen35020192022-05-06 11:30:57 +08001531 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001532 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001533 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001534 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1535
qs.xiong9fbf74e2023-03-28 13:38:22 +08001536 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1537 {
you.chend2fef3f2023-02-13 10:50:35 +08001538 goto out_enable;
1539 }
1540
you.chenc9928582023-04-24 15:39:37 +08001541 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001542 if (ret != 0)
1543 {
1544 //printf("service state %d\n", ret);
1545 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001546 ret = -1;
1547 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001548 }
lhfe8da902022-10-11 18:55:36 +08001549
you.chen70f377f2023-04-14 18:17:09 +08001550 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1551 {
1552 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1553 if(ret<0)
1554 {
1555 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1556 ret = -1;
1557 goto out_enable;
1558 }
1559 }
1560
you.chend2fef3f2023-02-13 10:50:35 +08001561 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1562 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1563 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1564 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1565out_enable:
1566 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001567 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001568}
1569
qs.xiong1af5daf2022-03-14 09:12:12 -04001570int lynq_wifi_disable(void)
1571{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001572 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001573 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001574 g_ap_watcher_stop_flag = 1;
1575 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001576 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001577 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001578 if (g_ap_watcher_pid != 0)
1579 pthread_join(g_ap_watcher_pid, NULL);
1580 if (g_sta_watcher_pid != 0)
1581 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001582 if (g_sta_auto_watcher_pid != 0)
1583 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001584 if (g_lynq_wpa_ctrl[0] != NULL)
1585 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1586 if (g_lynq_wpa_ctrl[1] != NULL)
1587 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001588 if (g_ap_tmp_watcher_pid != 0)
1589 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001590 g_ap_watcher_pid = 0;
1591 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001592 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001593 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001594 g_lynq_wpa_ctrl[0] = NULL;
1595 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001596 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001597 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001598 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1599 return 0;
1600}
1601
1602static inline char inner_convert_char(char in)
1603{
1604 if (in >= '0' && in <= '9')
1605 {
1606 return in - '0';
1607 }
1608 else if (in >= 'a' && in <= 'f')
1609 {
1610 return in - 'a' + 10;
1611 }
1612 else if (in >= 'A' && in <= 'F')
1613 {
1614 return in - 'A' + 10;
1615 }
1616 else
1617 {
1618 return '\xff';
1619 }
1620}
1621
1622static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1623{
1624 char *p;
1625 size_t pos = 0;
1626 if (NULL == out_ssid)
1627 return;
1628 //printf("input ssid=[%s]\n", ssid);
1629 memset(out_ssid, 0, out_ssid_len);
1630 if (NULL == ssid)
1631 return;
1632 p = strchr(ssid, '\\');
1633 if (NULL == p)
1634 {
1635 strncpy(out_ssid, ssid, out_ssid_len);
1636 //printf(" first %s\n", out_ssid);
1637 }
1638 else
1639 {
1640 pos = p - ssid;
1641 memcpy(out_ssid, ssid, pos);
1642 //printf("pos %lu -- %s\n", pos, out_ssid);
1643 for(; pos < out_ssid_len; pos ++)
1644 {
1645 if (p[0] == '\0')
1646 {
1647 //printf(" out %s\n", out_ssid);
1648 return;
1649 }
1650 else if (p[0] != '\\')
1651 {
1652 out_ssid[pos] = p[0];
1653 p += 1;
1654 }
1655 else if (p[1] == 'x' || p[1] == 'X')
1656 {
1657 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1658 p += 4;
1659 }
1660 else if (p[1] == '\\')
1661 {
1662 out_ssid[pos] = '\\';
1663 p += 2;
1664 }
1665 else if (p[1] == 't')
1666 {
1667 out_ssid[pos] = '\t';
1668 p += 2;
1669 }
1670 else if (p[1] == 'r')
1671 {
1672 out_ssid[pos] = '\r';
1673 p += 2;
1674 }
1675 else if (p[1] == 'n')
1676 {
1677 out_ssid[pos] = '\n';
1678 p += 2;
1679 }//todo find a better way to convert?
1680 }
1681 }
1682 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001683}
qs.xiong1af5daf2022-03-14 09:12:12 -04001684
you.chen35020192022-05-06 11:30:57 +08001685static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001686 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001687 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001688 RLOGD("enter inner_get_param");
1689 if (out_put == NULL)
1690 {
1691 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001692 return -1;
1693 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001694 if (param_name == NULL)
1695 {
1696 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001697 return -1;
1698 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001699 if (param_name[0] == '\0')
1700 {
1701 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001702 return -1;
1703 }
1704
1705 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1706
1707 CHECK_WPA_CTRL(interface);
1708
1709 DO_REQUEST(lynq_cmd_get);
1710
qs.xiong9fbf74e2023-03-28 13:38:22 +08001711 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1712 {
1713 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001714 return -1;
1715 }
1716
you.chena6fa5b22022-05-18 10:28:19 +08001717// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001718 if (strcmp(param_name, "ssid") == 0)
1719 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001720 if (cmd_reply[0] == '\"')
1721 {
you.chend2fef3f2023-02-13 10:50:35 +08001722 ssid_len = reply_len - 1;
1723 memcpy(out_put, cmd_reply + 1, ssid_len);
1724 if (out_put[ssid_len-1] == '\"')
1725 {
1726 out_put[ssid_len-1] = '\0';
1727 }
1728 else
1729 {
1730 out_put[ssid_len] = '\0';
1731 }
1732 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001733 else
1734 {
you.chend2fef3f2023-02-13 10:50:35 +08001735 ssid_len = reply_len / 2;
1736 for(i=0; i<ssid_len; i++)
1737 {
1738 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1739 }
1740 out_put[ssid_len] = '\0';
1741 }
1742 }
1743 else
1744 {
1745 memcpy(out_put, cmd_reply, reply_len + 1);
1746 }
you.chen35020192022-05-06 11:30:57 +08001747 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001748}
qs.xiong1af5daf2022-03-14 09:12:12 -04001749
you.chen35020192022-05-06 11:30:57 +08001750static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1751 int ret = 0;
1752 char * end = str + len - 1;
1753 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001754 while(str < end)
1755 {
1756 if (*str == delimiter)
1757 {
you.chen35020192022-05-06 11:30:57 +08001758 *str++ = '\0';
1759 results[ret++] = str;
1760 continue;
1761 }
1762 str++;
1763 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001764 if (*str == delimiter)
1765 {
you.chen35020192022-05-06 11:30:57 +08001766 *str = '\0';
1767 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001768
you.chen6ed36a62023-04-27 17:51:56 +08001769 results[ret] = NULL;
1770
you.chen35020192022-05-06 11:30:57 +08001771 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001772}
qs.xiongec8bbeb2023-11-20 15:51:45 +08001773/*
1774 *add func to get conencted STA device ip from dnsmasq ap0.lease
1775 *return 0 means get ip success
1776 */
1777static int inner_get_ip_by_mac_lease(const char * mac, char * ip,int ip_len)
1778{
1779 char * p;
1780 int ret;
1781 char cmd[256]={0};
1782 if (NULL == mac || NULL == ip)
1783 return -1;
1784 memset(ip, 0, ip_len);
1785 sprintf(cmd, "cat /run/wg870/ap0.lease | grep \"%s\" | awk '{print $3}'", mac);
1786 ret = exec_cmd(cmd, ip, ip_len);
1787 if( ret == 0 )
1788 {
1789 p = strchr(ip, '\n');
1790 if (NULL != p)
1791 {
1792 *p = '\0';
1793 RLOGD("inner_get_ip_by_mac_lease %s function return is:%d", ip,ret);
1794 return ret;
1795 }else
1796 {
1797 ret = -1;
1798 }
1799 }
1800 RLOGD("%s %d function return is:%d",__func__,__LINE__,ret);
1801 return ret;
1802
1803}
qs.xiong7a105ce2022-03-02 09:43:11 -05001804
you.chend2fef3f2023-02-13 10:50:35 +08001805static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1806{
1807 char * p;
1808 int ret = 0;
1809 char cmd[256]={0};
1810 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001811 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001812 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001813 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001814 ret = exec_cmd(cmd, ip, ip_len);
1815 p = strchr(ip, '\n');
1816 if (NULL != p)
1817 {
1818 *p = '\0';
qs.xiongec8bbeb2023-11-20 15:51:45 +08001819 }else
1820 {
1821 ret = inner_get_ip_by_mac_lease(mac,ip,ip_len);
you.chen35020192022-05-06 11:30:57 +08001822 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001823 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001824 return ret;
1825}
1826
you.chend2fef3f2023-02-13 10:50:35 +08001827static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001828 struct in_addr addr ={0};
1829 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001830 char cmd[64] = {0};
1831 char * p;
1832 int ret;
you.chen35020192022-05-06 11:30:57 +08001833
qs.xiong9fbf74e2023-03-28 13:38:22 +08001834 if (ip == NULL || *ip == '\0' || hostname == NULL)
1835 {
1836 RLOGE("ip == NULL or hostname == NULL");
1837 return -1;
you.chen35020192022-05-06 11:30:57 +08001838 }
1839
you.chend2fef3f2023-02-13 10:50:35 +08001840 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001841 if (inet_aton(ip, &addr) == 0)
1842 {
you.chen35020192022-05-06 11:30:57 +08001843 printf("---inet_aton fail\n");
1844 return -1;
1845 }
1846
1847 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1848
qs.xiong9fbf74e2023-03-28 13:38:22 +08001849 if (ht == NULL)
1850 {
you.chen186d3c32023-05-18 14:19:46 +08001851 hostname[0] = '\0';
1852 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1853 ret = exec_cmd(cmd, hostname, 32);
1854 if (ret == 0)
1855 {
1856 p = strchr(hostname, '\n');
1857 if (p != NULL)
1858 {
1859 *p = '\0';
1860 }
1861 return 0;
1862 }
1863 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001865 herror(NULL);
1866 return -1;
1867 }
1868
1869 strcpy(hostname, ht->h_name);
1870
1871 return 0;
1872}
1873
1874static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1875{
1876 int count, index, words_count;
1877 char * split_lines[128]= {0};
1878 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001879 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001880 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001881 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001882
1883 CHECK_WPA_CTRL(ap_sta);
1884
1885 DO_REQUEST(lynq_wifi_list_networks);
1886
1887 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1888
1889 //@todo check ssid field to compatible
1890
1891 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001892 for(index=1; index < count; index++)
1893 {
you.chen35020192022-05-06 11:30:57 +08001894 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001895 if (words_count > 2)
1896 {
you.chend2fef3f2023-02-13 10:50:35 +08001897 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001898 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1899 {
you.chen35020192022-05-06 11:30:57 +08001900 net_no_list[ret++] = atoi(split_words[0]);
1901 }
1902 }
1903 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001904 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001905 return ret;
1906}
1907
1908static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001909 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001910 CHECK_WPA_CTRL(ap_sta);
1911 const char *lynq_wifi_add_network = "ADD_NETWORK";
1912
qs.xiong9fbf74e2023-03-28 13:38:22 +08001913 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001914 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001915 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1916 {
1917 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001918 return -1;
1919 }
1920
qs.xiong9fbf74e2023-03-28 13:38:22 +08001921 for(i=0;i<reply_len;i++)
1922 {
1923 if(cmd_reply[i] == '\n')
1924 {
you.chen35020192022-05-06 11:30:57 +08001925 cmd_reply[i] = '\0';
1926 break;
1927 }
1928 }
1929 return atoi(cmd_reply);
1930}
you.chena6cd55a2022-05-08 12:20:18 +08001931
you.chen35020192022-05-06 11:30:57 +08001932static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1933{
1934 int count, index;
1935 int net_no_list[128];
1936
qs.xiong9fbf74e2023-03-28 13:38:22 +08001937 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001938 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001939 for (index=0; index < count; index++)
1940 {
1941 if (net_no_list[index] == net_no)
1942 {
you.chen35020192022-05-06 11:30:57 +08001943 return 0;
1944 }
1945 }
1946
1947 if (count >= 1)
1948 index = net_no_list[count - 1];
1949 else
1950 index = -1;
1951
qs.xiong9fbf74e2023-03-28 13:38:22 +08001952 while (index < net_no )
1953 {
you.chen35020192022-05-06 11:30:57 +08001954 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 if (index >= net_no)
1956 { // required network no created
1957 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001958 return 0;
1959 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001960 else if( index < 0)
1961 {
1962 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001963 return -1;
1964 }
you.chen35020192022-05-06 11:30:57 +08001965 }
1966
1967 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 {
1969 RLOGE("[lynq_check_network_number] network index < 0");
1970 return -1;
1971 }
1972 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001973 return 0;
1974}
1975
1976static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001977 if (freq > 5000 && freq < 6000)
1978 {
you.chen35020192022-05-06 11:30:57 +08001979 return LYNQ_WIFI_5G_band;
1980 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001981 else if (freq > 2000 && freq < 3000)
1982 {
you.chen35020192022-05-06 11:30:57 +08001983 return LYNQ_WIFI_2G_band;
1984 }
1985 return LYNQ_WIFI_2_and_5G_band;
1986}
1987
1988static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001989 if (key_mgmt != NULL)
1990 {
1991 if (memcmp( key_mgmt, "NONE", 4) == 0)
1992 {
you.chen35020192022-05-06 11:30:57 +08001993 return LYNQ_WIFI_AUTH_OPEN;
1994 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001995 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1996 {
you.chen35020192022-05-06 11:30:57 +08001997 return LYNQ_WIFI_AUTH_WEP;
1998 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001999 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
2000 {
you.chen35020192022-05-06 11:30:57 +08002001 return LYNQ_WIFI_AUTH_WPA_PSK;
2002 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002003 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
2004 {
you.chen35020192022-05-06 11:30:57 +08002005 return LYNQ_WIFI_AUTH_WPA2_PSK;
2006 }
2007 }
2008
2009 return -1;
2010}
2011
2012static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002013 if (flag != NULL)
2014 {
qs.xiongba01e1f2023-09-06 14:14:32 +08002015 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 +08002016 {
2017 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08002018 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08002019 {
2020 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2021 }else if (strstr( flag, "WPA2-PSK") != NULL)
2022 {
you.chen35020192022-05-06 11:30:57 +08002023 return LYNQ_WIFI_AUTH_WPA2_PSK;
2024 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002025 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002026 {
you.chen35020192022-05-06 11:30:57 +08002027 return LYNQ_WIFI_AUTH_WPA_PSK;
2028 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002029 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002030 {
you.chen35020192022-05-06 11:30:57 +08002031 return LYNQ_WIFI_AUTH_WEP;
2032 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002033 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08002034 {
you.chen35020192022-05-06 11:30:57 +08002035 return LYNQ_WIFI_AUTH_OPEN;
2036 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002037 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08002038 {
you.chend2fef3f2023-02-13 10:50:35 +08002039 return LYNQ_WIFI_AUTH_OPEN;
2040 }
qs.xiong46f41562023-07-11 21:06:47 +08002041 else
2042 {
2043 RLOGD("convert_max_auth_from_flag not-found auth mode");
2044 }
you.chen35020192022-05-06 11:30:57 +08002045 }
2046
2047 return -1;
2048}
2049
2050static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
2051 switch (bw) {
2052 case 10:
2053 return LYNQ_WIFI_BANDWIDTH_HT10;
2054 break;
2055 case 20:
2056 return LYNQ_WIFI_BANDWIDTH_HT20;
2057 break;
2058 case 40:
2059 return LYNQ_WIFI_BANDWIDTH_HT40;
2060 break;
2061 case 80:
2062 return LYNQ_WIFI_BANDWIDTH_HT80;
2063 break;
2064 default:
2065 break;
2066 }
2067
2068 return -1;
2069}
2070
you.chen70f377f2023-04-14 18:17:09 +08002071static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08002072static int inner_get_status_info(int interface, curr_status_info *curr_state) {
2073 int i, count;
2074 char *p;
2075 const char *lynq_status_cmd = "STATUS";
2076 const char * FLAG_SSID = "ssid=";
2077 const char * FLAG_SBSID = "bssid=";
2078 const char * FLAG_KEY_MGMT = "key_mgmt=";
2079 const char * FLAG_FREQ = "freq=";
2080 const char * FLAG_STATE = "wpa_state=";
2081 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08002082 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08002083 char *split_lines[128] = {0};
2084
2085 CHECK_WPA_CTRL(interface);
2086
qs.xiong9fbf74e2023-03-28 13:38:22 +08002087 if (curr_state == NULL)
2088 {
2089 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08002090 return -1;
2091 }
2092
2093 DO_REQUEST(lynq_status_cmd);
2094
2095 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2096
2097 curr_state->net_no = -1;
2098 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002099 for(i=0; i < count; i++)
2100 {
2101 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002102 {
you.chen35020192022-05-06 11:30:57 +08002103 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002104 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002105 {
you.chend2fef3f2023-02-13 10:50:35 +08002106 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08002107 ret = 0;
2108 continue;
2109 }
you.chenf58b3c92022-06-21 16:53:48 +08002110 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002111 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002112 {
you.chend2fef3f2023-02-13 10:50:35 +08002113 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 +08002114 ret = 0;
2115 continue;
2116 }
you.chen35020192022-05-06 11:30:57 +08002117 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002119 {
you.chen450d0172022-07-15 17:56:48 +08002120 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002121 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08002122 ret = 0;
2123 continue;
2124 }
2125 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002126 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002127 {
you.chen35020192022-05-06 11:30:57 +08002128 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
2129 ret = 0;
2130 continue;
2131 }
you.chend2fef3f2023-02-13 10:50:35 +08002132 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002133 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002134 {
you.chend2fef3f2023-02-13 10:50:35 +08002135 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2136 ret = 0;
2137 continue;
2138 }
you.chen35020192022-05-06 11:30:57 +08002139 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002140 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002141 {
you.chen35020192022-05-06 11:30:57 +08002142 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002143 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002144 {
you.chen35020192022-05-06 11:30:57 +08002145 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2146 ret = 0;
2147 continue;
2148 }
2149
2150 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002151 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002152 {
you.chen35020192022-05-06 11:30:57 +08002153 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002154 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002156 }
2157 }
2158
you.chen70f377f2023-04-14 18:17:09 +08002159 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2160 {
2161 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2162 }
2163
you.chen35020192022-05-06 11:30:57 +08002164 return ret;
2165}
2166
qs.xiongf1b525b2022-03-31 00:58:23 -04002167int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002168{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002169 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002170 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002171
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 if (ap_ssid == NULL)
2173 {
2174 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002175 return -1;
2176 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002177 else
2178 {
2179 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002180 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002181
qs.xiong9fbf74e2023-03-28 13:38:22 +08002182 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2183 {
2184 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002185 return -1;
2186 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002187
you.chen35020192022-05-06 11:30:57 +08002188 CHECK_IDX(idx, CTRL_AP);
2189
2190 CHECK_WPA_CTRL(CTRL_AP);
2191
2192 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2193
2194 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2195 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong6ad0e822023-11-24 17:53:30 +08002196
qs.xiong9fbf74e2023-03-28 13:38:22 +08002197 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2198 return 0;
you.chen35020192022-05-06 11:30:57 +08002199
qs.xiong7a105ce2022-03-02 09:43:11 -05002200}
2201
you.chen35020192022-05-06 11:30:57 +08002202int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002203{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002204 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002205 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002206 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002207}
2208
qs.xiongc9c79f72022-10-17 15:27:18 +08002209/*****
2210 *frequency <------>channel
2211 *
2212 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2213 *
2214 *
2215 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2216 *
2217 *
2218 * */
2219static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002220 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};
2221 int i;
2222 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2223
qs.xiong69a332b2022-12-02 09:58:57 +08002224 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002225 {
2226 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002227 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002228 }
qs.xiongc00b6032022-11-29 16:28:03 +08002229
2230 if(i == arr_len)
2231 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002232 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002233 return -1;
2234 }
qs.xiongc00b6032022-11-29 16:28:03 +08002235
qs.xiongc9c79f72022-10-17 15:27:18 +08002236 return 0;
2237}
qs.xiong13673462023-02-21 19:12:54 +08002238
2239static int lynq_check_frequencyby_country_code(int input_frequency)
2240{
2241 char str_cnc[]="CN";
2242 char str_dest[20]="";
2243
2244 if( lynq_get_country_code(1,str_dest) != 0 )
2245 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002246 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002247 return -1;
2248 }
2249 if( strncmp(str_dest,str_cnc,2) != 0 )
2250 {
2251 return 0;
2252 }else if( 2473 < input_frequency && input_frequency < 5744)
2253 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002254 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002255 return -1;
2256 }
2257 return 0;
2258}
qs.xiongf1b525b2022-03-31 00:58:23 -04002259int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002260{
qs.xiongc00b6032022-11-29 16:28:03 +08002261 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002262 char lynq_wifi_frequency_cmd[128]={0};
2263 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002264 char lynq_cmd_slect[128]={0};
qs.xiongec8bbeb2023-11-20 15:51:45 +08002265 RLOGD("enter %s %d input frequency:%d",__func__,__LINE__,lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002266 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002267 check = lynq_check_set_frequency(lynq_wifi_frequency);
2268 if(check != 0)
2269 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002270 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002271 return -1;
you.chen35020192022-05-06 11:30:57 +08002272 }
qs.xiong13673462023-02-21 19:12:54 +08002273 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2274 if(check != 0)
2275 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002276 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002277 return -1;
2278 }
2279
qs.xiongc00b6032022-11-29 16:28:03 +08002280 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2281 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002282 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002283 return -1;
2284 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002285
you.chen35020192022-05-06 11:30:57 +08002286 CHECK_IDX(idx, CTRL_AP);
2287
2288 CHECK_WPA_CTRL(CTRL_AP);
2289
2290 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2291 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2292 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2293
you.chen6c2dd9c2022-05-16 17:55:28 +08002294 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002295 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2296 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2297 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong171c9ec2023-11-01 18:58:09 +08002298//@ tmp do down/up to fix 5G AP turn to 2.4G cannot be conenct
qs.xiongec8bbeb2023-11-20 15:51:45 +08002299
qs.xiong171c9ec2023-11-01 18:58:09 +08002300 system("wl down");
2301 system("wl up");
2302 RLOGD("[%s] -- run cmd down/up --%d",__func__,__LINE__);
qs.xiong7a105ce2022-03-02 09:43:11 -05002303
qs.xiong9fbf74e2023-03-28 13:38:22 +08002304 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002305}
2306
qs.xiongf1b525b2022-03-31 00:58:23 -04002307int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002308{
you.chen35020192022-05-06 11:30:57 +08002309 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002310 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002311 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002312
qs.xiong9fbf74e2023-03-28 13:38:22 +08002313 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2314 {
2315 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002316 return -1;
2317 }
2318 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002319
qs.xiong9fbf74e2023-03-28 13:38:22 +08002320 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002321}
2322
qs.xiongf1b525b2022-03-31 00:58:23 -04002323int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2324{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002325 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002326 CHECK_IDX(idx, CTRL_AP);
2327 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002328 case LYNQ_WIFI_BANDWIDTH_HT10:
2329 {
2330 RLOGE("bandwith [%d] not support now\n", bandwidth);
2331 return -1;
2332 }
2333 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002334 {
2335 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2336 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002337 if (system(lynq_cmd_bandwith) != 0 )
2338 {
2339 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002340 return -1;
2341 }
2342 system("wl up");
2343 break;
2344 }
2345 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 {
qs.xiong10379192023-02-21 13:19:42 +08002347 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002348 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2349 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002350 if (system(lynq_cmd_bandwith) != 0 )
2351 {
2352 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002353 return -1;
2354 }
2355 system("wl up");
2356 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 }
you.chen35020192022-05-06 11:30:57 +08002358 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002359 {
qs.xiong10379192023-02-21 13:19:42 +08002360 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002361 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002362 if (system(lynq_cmd_bandwith) != 0 )
2363 {
2364 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002365 return -1;
2366 }
2367 system("wl up");
2368 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002369 }
2370 default:
you.chen35020192022-05-06 11:30:57 +08002371 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 RLOGE("auth type [%d] not support now\n", bandwidth);
2373 return -1;
you.chen35020192022-05-06 11:30:57 +08002374 }
2375 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002376
2377
you.chen35020192022-05-06 11:30:57 +08002378 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002379}
you.chen35020192022-05-06 11:30:57 +08002380
qs.xiongf1b525b2022-03-31 00:58:23 -04002381int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2382{
you.chen35020192022-05-06 11:30:57 +08002383 int count = 0;
2384 int index = 0;
2385 char *split_words[128] = {0};
2386 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002387 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002388 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002389
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);
2395 for(;index < count; index++) {
2396 if (strncmp(split_words[index], "bw", 2) != 0) {
2397 continue;
2398 }
2399
2400 index++;
2401 if (index >= count) {
2402 return -1;
2403 }
2404
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002406 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2407 return 0;
2408 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002410 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002411}
qs.xiong0fb469a2022-04-14 03:50:45 -04002412
qs.xiongf1b525b2022-03-31 00:58:23 -04002413int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002414{
you.chen35020192022-05-06 11:30:57 +08002415 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002416 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002417 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002418
you.chen35020192022-05-06 11:30:57 +08002419 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002420
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2422 {
you.chen35020192022-05-06 11:30:57 +08002423 return -1;
2424 }
2425
2426 system("wl down");
2427 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002428 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002429 return -1;
2430 }
2431 system("wl up");
2432 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002433}
qs.xiong0fb469a2022-04-14 03:50:45 -04002434
qs.xiongf1b525b2022-03-31 00:58:23 -04002435int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002436{
you.chen35020192022-05-06 11:30:57 +08002437 int count = 0;
2438 int index = 0;
2439 char *split_words[128] = {0};
2440 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002441 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002442 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002443
you.chen35020192022-05-06 11:30:57 +08002444 CHECK_WPA_CTRL(CTRL_AP);
2445
2446 DO_REQUEST(lynq_chanspec_cmd);
2447
2448 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002449 for(;index < count; index++)
2450 {
2451 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002452 if (strncmp(split_words[index], "channel", 2) != 0) {
2453 continue;
2454 }
2455
2456 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002457 if (index >= count)
2458 {
you.chen35020192022-05-06 11:30:57 +08002459 return -1;
2460 }
2461
2462 *channel = atoi(split_words[index]);
2463 return 0;
2464 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002465 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002466 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002467}
2468
2469
you.chen35020192022-05-06 11:30:57 +08002470int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002471{
you.chen6c2dd9c2022-05-16 17:55:28 +08002472 char ssid[MAX_CMD] = {0};
2473 int freq = 0;
2474 char lynq_auth_cmd[64]={0};
2475 char lynq_auth_alg_cmd[64]={0};
2476 char lynq_psk_cmd[64]={0};
2477 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002478 char lynq_ieee80211_cmd[64]={0};
2479 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002480 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002481 CHECK_IDX(idx, CTRL_AP);
2482
you.chen6c2dd9c2022-05-16 17:55:28 +08002483 CHECK_WPA_CTRL(CTRL_AP);
2484
qs.xiong9fbf74e2023-03-28 13:38:22 +08002485 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2486 {
2487 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002488 return -1;
2489 }
2490
you.chen92fd5d32022-05-25 10:09:47 +08002491 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002492 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002493 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002494 return 0;
2495 }
2496 else {
2497 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2498 ssid[0] = '\0';
2499 }
2500 lynq_wifi_ap_frequency_get(idx, &freq);
2501
2502 DO_OK_FAIL_REQUEST(cmd_disconnect);
2503 DO_OK_FAIL_REQUEST(cmd_remove_all);
2504 if (ssid[0] != '\0') {
2505 lynq_wifi_ap_ssid_set(idx, ssid);
2506 }
2507 if (freq != 0) {
2508 lynq_wifi_ap_frequency_set(idx, freq);
2509 }
2510 }
2511 }
you.chen35020192022-05-06 11:30:57 +08002512
qs.xiong9fbf74e2023-03-28 13:38:22 +08002513 switch(auth){
2514 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002515 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002516 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002517 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002518 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002519 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002520 break;
2521 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002522 case LYNQ_WIFI_AUTH_WEP:
2523 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002524 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002525 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002526 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002527 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2528
2529 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2530 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2531 break;
2532 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002533 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002534 {
2535 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2536 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2537 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2538
2539 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2540 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2541 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2542 break;
2543
2544 }
you.chen35020192022-05-06 11:30:57 +08002545 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002546 {
2547 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2548 {
you.chen35020192022-05-06 11:30:57 +08002549 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2550 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2551 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002552 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2553 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002554 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002555 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002556 }
2557// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2558// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2559 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002560
you.chen35020192022-05-06 11:30:57 +08002561 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2562 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2563 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002564 break;
2565 }
2566 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2567 {
2568 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2569 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2570 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2571 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2572
2573 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2574 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2575 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2576 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2577 break;
2578 }
2579 case LYNQ_WIFI_AUTH_WPA3_PSK:
2580 {
2581 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2582 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002583 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002584 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2585
2586 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2587 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2588 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2589 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2590 break;
2591 }
2592 default:
you.chen35020192022-05-06 11:30:57 +08002593 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002594 RLOGE("auth type [%d] not support now\n", auth);
2595 return -1;
you.chen35020192022-05-06 11:30:57 +08002596 }
2597 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002598 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002599
qs.xiong9fbf74e2023-03-28 13:38:22 +08002600 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002601}
2602
you.chen35020192022-05-06 11:30:57 +08002603int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002604{
you.chen35020192022-05-06 11:30:57 +08002605 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002606 char lynq_auth_alg_str[MAX_RET] = {0};
2607 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002608 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002609 CHECK_IDX(idx, CTRL_AP);
2610
qs.xiong9fbf74e2023-03-28 13:38:22 +08002611 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2612 {
2613 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002614 return -1;
2615 }
2616
qs.xiong9fbf74e2023-03-28 13:38:22 +08002617 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2618 {
2619 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2620 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002621 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002622 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002623 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002624 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002625 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2626 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002627 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002628 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002629 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002630 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002631 else
2632 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002633 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002634 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002635 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002636 }
you.chen35020192022-05-06 11:30:57 +08002637 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002638 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2639 {
2640 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2641 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002642 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002643 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002644 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002645 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2646 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002647 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002648 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002649 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002650 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002651 else
2652 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002653 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002654 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002655 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002656 }
you.chen35020192022-05-06 11:30:57 +08002657 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002658
2659 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2660 {
2661 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2662 return -1;
2663 }
2664
2665 if (memcmp(lynq_auth_str,"1",1) == 0 )
2666 {
2667 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2668 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002669 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002670 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2671 {
2672 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2673 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002674 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002675 }
2676 else
2677 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002678 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002679 *auth = -1;
2680 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002681
you.chen6c2dd9c2022-05-16 17:55:28 +08002682 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002683}
qs.xiong1af5daf2022-03-14 09:12:12 -04002684
you.chenb95401e2023-05-12 19:39:06 +08002685static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2686{
2687 char status[64];
you.chencba13492023-05-19 13:53:43 +08002688 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002689 curr_status_info curr_state;
2690
2691 CHECK_WPA_CTRL(CTRL_AP);
2692
2693 memset(status, 0, sizeof (status));
2694
2695 curr_state.ap = NULL;
2696 curr_state.state = status;
2697
2698 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002699 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002700 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002701 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002702 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002703 {
2704 return 0;
2705 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002706 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002707 {
2708 DO_REQUEST("RECONNECT");
2709 return inner_check_ap_connected(idx, retry_count+1);
2710 }
you.chencba13492023-05-19 13:53:43 +08002711 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002712 {
2713 printf("retry 10 time\n");
2714 return -1;
2715 }
2716 else
2717 {
you.chen6d247052023-06-01 16:39:54 +08002718 if (strcmp(status, STATE_DISCONNECTED) == 0)
2719 {
2720 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2721 DO_REQUEST(LYNQ_WIFI_CMD);
2722 }
you.chenb95401e2023-05-12 19:39:06 +08002723 return inner_check_ap_connected(idx, retry_count+1);
2724 }
2725 }
2726 return -1;
2727}
qs.xiong1af5daf2022-03-14 09:12:12 -04002728
qs.xiongf1b525b2022-03-31 00:58:23 -04002729int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002730{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002731 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002732 char LYNQ_WIFI_CMD[128]={0};
you.chen35020192022-05-06 11:30:57 +08002733
qs.xiongec8bbeb2023-11-20 15:51:45 +08002734 CHECK_IDX(idx, CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002735 CHECK_WPA_CTRL(CTRL_AP);
2736
you.chen0df3e7e2023-05-10 15:56:26 +08002737 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002738 {
you.chen0df3e7e2023-05-10 15:56:26 +08002739 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002740 return -1;
2741 }
you.chen35020192022-05-06 11:30:57 +08002742
qs.xiongb37f8c42023-09-13 21:21:58 +08002743
you.chen35020192022-05-06 11:30:57 +08002744 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2745 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2746
you.chenc9928582023-04-24 15:39:37 +08002747 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2748 if (ret != 0)
2749 {
2750 RLOGE("lynq_wifi_ap_start excute script fail");
2751 return -1;
2752 }
2753
you.chenb95401e2023-05-12 19:39:06 +08002754 if (inner_check_ap_connected(idx, 0) != 0)
2755 {
2756 return -1;
2757 }
2758
you.chen0df3e7e2023-05-10 15:56:26 +08002759 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002760 if (g_ap_tmp_watcher_pid == 0)
2761 {
2762 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2763 {
2764 g_ap_tmp_watcher_pid = 0;
2765 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2766 return -1;
2767 }
2768 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2769 }
qs.xiongb37f8c42023-09-13 21:21:58 +08002770
qs.xiongec8bbeb2023-11-20 15:51:45 +08002771 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002772 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002773}
2774
qs.xiongf1b525b2022-03-31 00:58:23 -04002775int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002776{
you.chen35020192022-05-06 11:30:57 +08002777 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002778}
2779
qs.xiongf1b525b2022-03-31 00:58:23 -04002780int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002781{
qs.xiongec8bbeb2023-11-20 15:51:45 +08002782 RLOGD("enter %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08002783 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002784
you.chen35020192022-05-06 11:30:57 +08002785 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002786
you.chen35020192022-05-06 11:30:57 +08002787 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002788
you.chen35020192022-05-06 11:30:57 +08002789 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2790
2791 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2792
you.chenc9928582023-04-24 15:39:37 +08002793
2794 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2795 if (ret != 0)
2796 {
2797 RLOGE("lynq_wifi_ap_start excute script fail");
2798 return -1;
2799 }
qs.xiong44fac672023-08-29 16:15:55 +08002800 g_ap_tmp_watcher_stop_flag = 1;
2801 if (g_ap_tmp_watcher_pid != 0)
2802 pthread_join(g_ap_tmp_watcher_pid, NULL);
2803 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002804
qs.xiongec8bbeb2023-11-20 15:51:45 +08002805 RLOGD("end %s %d",__func__,__LINE__);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002806 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002807}
qs.xiong1af5daf2022-03-14 09:12:12 -04002808
qs.xiongf1b525b2022-03-31 00:58:23 -04002809int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002810{
you.chen35020192022-05-06 11:30:57 +08002811 char lynq_disable_cmd[128] = {0};
2812 char lynq_select_cmd[128] = {0};
2813 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002814 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002815 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002816
you.chen35020192022-05-06 11:30:57 +08002817 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002818 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2819 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2820
2821 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2822 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2823 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002824
qs.xiong9fbf74e2023-03-28 13:38:22 +08002825 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002826}
2827
qs.xiongf1b525b2022-03-31 00:58:23 -04002828int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002829{
you.chen35020192022-05-06 11:30:57 +08002830 char lynq_disable_cmd[128] = {0};
2831 char lynq_select_cmd[128] = {0};
2832 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002833 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002834 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002835
you.chen35020192022-05-06 11:30:57 +08002836 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002837
you.chen35020192022-05-06 11:30:57 +08002838 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2839 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2840
2841 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2842 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2843 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002844
qs.xiong9fbf74e2023-03-28 13:38:22 +08002845 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002846}
qs.xiongf1b525b2022-03-31 00:58:23 -04002847
you.chen35020192022-05-06 11:30:57 +08002848int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002849{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002850 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002851 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002852 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002853 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002854 RLOGD("enter lynq_ap_password_set");
2855 if( password == NULL )
2856 {
2857 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002858 return -1;
2859 }
2860 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002861 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002862 if(pass_len < 8 || pass_len >= 64)
2863 {
2864 RLOGE("[lynq_ap_password_set]input password len not in rage");
2865 return -1;
you.chen35020192022-05-06 11:30:57 +08002866 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002867
you.chen35020192022-05-06 11:30:57 +08002868 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002869
qs.xiong9fbf74e2023-03-28 13:38:22 +08002870 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2871 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002872 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002873 return -1;
2874 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002875 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2876 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002877 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2878 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002879 }
2880
you.chen35020192022-05-06 11:30:57 +08002881 CHECK_WPA_CTRL(CTRL_AP);
2882
qs.xiong9fbf74e2023-03-28 13:38:22 +08002883 if (auth == LYNQ_WIFI_AUTH_WEP)
2884 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002885 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002886 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2887 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2888 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2889 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2890 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002891 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2892 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002893 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 +08002894 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2895 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2896 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002897 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2898 {
2899
qs.xiongc8d92a62023-03-29 17:36:14 +08002900 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 +08002901 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002902 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2904 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2905
2906 }
2907 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002908 {
2909 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002910 return -1;
2911 }
you.chen35020192022-05-06 11:30:57 +08002912
you.chen35020192022-05-06 11:30:57 +08002913 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002914
qs.xiong9fbf74e2023-03-28 13:38:22 +08002915 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002916}
2917
you.chen35020192022-05-06 11:30:57 +08002918int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002919{
you.chen35020192022-05-06 11:30:57 +08002920 FILE * fp;
2921 int len, ret;
2922 int count, index;
2923 char *split_lines[128] = {0};
2924 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002926
you.chen35020192022-05-06 11:30:57 +08002927 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002928
you.chen35020192022-05-06 11:30:57 +08002929 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2930// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002931 if (NULL == fp)
2932 {
2933 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002934 return -1;
2935 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002936
you.chen35020192022-05-06 11:30:57 +08002937 buff = alloca(MAX_RET);
2938 fseek(fp, 0, SEEK_SET);
2939 len = fread(buff, 1, MAX_RET, fp);
2940 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002941
qs.xiong9fbf74e2023-03-28 13:38:22 +08002942 for(index=0; index < len; index ++)
2943 {
2944 if (memcmp(buff + index, "network={", 9) != 0)
2945 {
you.chen35020192022-05-06 11:30:57 +08002946 continue;
2947 }
2948 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002949 for (; index < len; index ++ )
2950 {
2951 if (buff[index] != '}')
2952 {
you.chen35020192022-05-06 11:30:57 +08002953 continue;
2954 }
2955 buff[index] = '\0';
2956 break;
2957 }
2958 len = buff + index - p;
2959 }
2960
2961 count = lynq_split(p, len, '\n', split_lines);
2962
2963 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002964 for(index=0; index < count; index++)
2965 {
you.chen35020192022-05-06 11:30:57 +08002966 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002967 if (p != NULL)
2968 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002969 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002970 if (*p == '\"')
2971 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002972 p++;
2973 }
you.chen35020192022-05-06 11:30:57 +08002974 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2976 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002977 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002978 if (*p == '\"')
2979 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002980 p++;
2981 }
2982 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002983 else
2984 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002985 continue;
you.chen35020192022-05-06 11:30:57 +08002986 }
2987
2988 strcpy(password, p);
2989
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 while(*password != '\0')
2991 {
2992 if (*password == '\"')
2993 {
you.chen35020192022-05-06 11:30:57 +08002994 *password = '\0';
2995 break;
2996 }
2997 password++;
2998 }
2999 ret = 0;
3000 break;
3001 } //end for(index=0; index < count; index++)
3002
3003 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003004}
3005
you.chen35020192022-05-06 11:30:57 +08003006static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
3007 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08003008 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003009
qs.xiong9fbf74e2023-03-28 13:38:22 +08003010 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
3011 {
you.chen35020192022-05-06 11:30:57 +08003012 return -1;
3013 }
3014
3015 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08003016
qs.xiong9fbf74e2023-03-28 13:38:22 +08003017 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
3018 {
3019 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08003020 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003021 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08003022 {
you.chena6cd55a2022-05-08 12:20:18 +08003023 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003024 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08003025 }
you.chen70f377f2023-04-14 18:17:09 +08003026 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
3027 {
3028 return 0;
3029 }
you.chena6cd55a2022-05-08 12:20:18 +08003030 }
3031 }
you.chen70f377f2023-04-14 18:17:09 +08003032 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
3033 {
3034 return 0;
3035 }
3036
qs.xiong9fbf74e2023-03-28 13:38:22 +08003037 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
3038 {
you.chen70f377f2023-04-14 18:17:09 +08003039 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003040 return -1;
3041 }
3042 if ( strncmp(lynq_auth_str,"1",1) == 0 )
3043 {
3044
you.chen70f377f2023-04-14 18:17:09 +08003045 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
3046 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003047 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
3048 {
3049
3050 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08003051 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003052 }else
3053 {
you.chen70f377f2023-04-14 18:17:09 +08003054 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003055 *auth = -1;
3056 return -1;
3057 }
you.chen35020192022-05-06 11:30:57 +08003058 return 0;
3059}
3060
3061int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05003062{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003063 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003064 int pass_len, net_no, count, index;
3065 char lynq_tmp_cmd[300]={0};
3066 int net_no_list[128];
3067 lynq_wifi_auth_s net_auth;
3068 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003069 if(pass_len < 8 || pass_len >= 64)
3070 {
3071 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08003072 return -1;
3073 }
3074
3075 CHECK_IDX(idx, CTRL_STA);
3076
3077 net_no = -1;
3078 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
3079
qs.xiong9fbf74e2023-03-28 13:38:22 +08003080 for (index=0; index < count; index++)
3081 {
you.chen35020192022-05-06 11:30:57 +08003082 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003083 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
3084 {
you.chen35020192022-05-06 11:30:57 +08003085 net_no = net_no_list[index];
3086 break;
3087 }
3088 }
3089
qs.xiong9fbf74e2023-03-28 13:38:22 +08003090 if (net_no < 0)
3091 {
you.chen35020192022-05-06 11:30:57 +08003092 return -1;
3093 }
3094
3095 CHECK_WPA_CTRL(CTRL_STA);
3096
3097 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
3098
3099 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
3100 DO_OK_FAIL_REQUEST(cmd_save_config);
3101
3102 return 0;
3103}
3104
qs.xiongb5dab082023-10-13 14:43:41 +08003105/**
3106* buff data
3107* buff_len size of buff
3108* idx sta
3109* *ap ap info for find ssid && password
3110* password return password
3111*
3112*/
3113static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
3114
3115 int ret, network_len, i, ssid_len,curr_auth;
3116 int count, index,org_index;
3117 char *split_lines[128] = {0};
3118 char *p, *ssid, *ssid_end_flag,*ptr;
3119 char tmp_ssid[128]={0};
3120 char tmp_auth[24]={0};
3121
3122 org_index = 0;
3123 network_len = 0;
3124 p = NULL;
3125
3126 CHECK_IDX(idx, CTRL_STA);
3127
3128 while(1){
3129 network_len = 0;
3130 p == NULL;
3131 for(; org_index < buff_len; org_index ++)
3132 {
3133 for(; org_index < buff_len; org_index ++)
3134 {
3135 if (memcmp(buff + org_index, "network={", 9) != 0)
3136 {
3137 continue;
3138 }
3139 p = buff + org_index + 9;
3140
3141 for (; org_index < buff_len; org_index ++ )
3142 {
3143 if (buff[org_index] != '}')
3144 {
3145 continue;
3146 }
3147 buff[org_index] = '\0';
3148 break;
3149 }
3150 network_len = buff + org_index - p;
3151 break;
3152 }
3153
3154 if (p == NULL)
3155 {
3156 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3157 return -1;
3158 }
3159
3160 ssid = strstr(p, "ssid=");
3161 if (ssid != NULL) {
3162 ssid += strlen("ssid=");
3163 if (ssid[0] == '\"')
3164 {
3165 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3166 {
qs.xiongb5dab082023-10-13 14:43:41 +08003167 break;
3168 }
qs.xiongb5dab082023-10-13 14:43:41 +08003169 }
3170 else
3171 {
3172 ssid_end_flag = strstr(ssid, "\n");
3173 if (ssid_end_flag != NULL)
3174 {
3175 ssid_len = (ssid_end_flag - ssid) / 2;
3176 for(i=0; i<ssid_len; i++)
3177 {
3178 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3179 }
3180 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3181 {
qs.xiongb5dab082023-10-13 14:43:41 +08003182 break;
3183 }
3184 }
3185 }
3186 }
3187
3188 }
3189
3190 if (org_index >= buff_len || NULL == p || network_len <= 0)
3191 {
3192
3193 if (buff != NULL)
3194 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3195 return -1;
3196 }
3197
3198 count = lynq_split(p, network_len, '\n', split_lines);
3199 ret = -1;
3200 for( index=0; index < count; index++ )
3201 {
3202 p = strstr(split_lines[index], "key_mgmt=");
qs.xiongb5dab082023-10-13 14:43:41 +08003203 if(p != NULL)
3204 {
3205 p += 9;
3206 if(memcmp(p,"SAE",3) == 0)
3207 {
3208 curr_auth = 5;
3209 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3210 {
3211 curr_auth = 4;
3212 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3213 {
3214 curr_auth = 3;
3215 }else if(memcmp(p,"NONE",4) == 0 )
3216 {
3217 curr_auth = 0;
3218 }else{
3219 curr_auth = 1;
3220 }
qs.xiongb5dab082023-10-13 14:43:41 +08003221 if( curr_auth < 1 || curr_auth > 6)
3222 {
3223 ret = -1;
3224 }
3225 break;
3226 }
3227 }
3228 if( curr_auth == 0)
3229 {
3230 return 0;
3231 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3232 {
3233 for(index=0; index < count; index++)
3234 {
3235 /*get psw info*/
3236
3237 p = strstr(split_lines[index], "psk=");
3238 if (p != NULL)
3239 {
3240 p += 4;
3241 if (*p == '\"')
3242 {
3243 p++;
3244 }
3245 }
3246 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3247 {
3248 p += 9;
3249 if (*p == '\"')
3250 {
3251 p++;
3252 }
3253 }
3254 else
3255 {
3256 continue;
3257 }
3258
3259 if (*p == '\"')
3260 p++;
3261 strncpy(password, p, 64);
3262 p = password;
3263 while(password - p < 64 && *password != '\0')
3264 {
3265 if (*password == '\"')
3266 {
3267 *password = '\0';
qs.xiongb5dab082023-10-13 14:43:41 +08003268 ret = 0;
3269 break;
3270 }
3271 password++;
3272 }
3273 break;
3274 }
3275 break;
3276 }
3277 }
3278
3279 return ret;
3280}
3281
3282
3283
you.chen35020192022-05-06 11:30:57 +08003284int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3285
3286 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003287 int len, ret;
3288 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003289 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003290
qs.xiongb5dab082023-10-13 14:43:41 +08003291 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003292 CHECK_IDX(idx, CTRL_STA);
3293
qs.xiong9fbf74e2023-03-28 13:38:22 +08003294 if (NULL == password)
3295 {
3296 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003297 return -1;
3298 }
3299
you.chen35020192022-05-06 11:30:57 +08003300 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003301 if (NULL == fp)
3302 {
3303 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003304 return -1;
3305 }
3306
you.chen6d247052023-06-01 16:39:54 +08003307 fseek(fp, 0, SEEK_END);
3308 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003309 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003310
qs.xiongb5dab082023-10-13 14:43:41 +08003311 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003312 {
3313 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3314 return -1;
3315 }
3316
you.chen35020192022-05-06 11:30:57 +08003317 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003318 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003319 fclose(fp);
3320
qs.xiongb5dab082023-10-13 14:43:41 +08003321
3322 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3323
3324 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003325 {
qs.xiongb5dab082023-10-13 14:43:41 +08003326 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3327 free(info_buff);
3328 return 0;
you.chen35020192022-05-06 11:30:57 +08003329 }
qs.xiongb5dab082023-10-13 14:43:41 +08003330 else{
3331 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003332 return -1;
3333 }
3334
you.chen35020192022-05-06 11:30:57 +08003335}
3336
qs.xiongb5dab082023-10-13 14:43:41 +08003337
you.chen35020192022-05-06 11:30:57 +08003338static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3339{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003340 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003341
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 if (sta_ssid == NULL)
3343 {
3344 RLOGE("sta_ssid is null\n");
3345 return -1;
you.chen35020192022-05-06 11:30:57 +08003346 }
3347
qs.xiong9fbf74e2023-03-28 13:38:22 +08003348 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003349
3350 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3351
3352 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3353// DO_OK_FAIL_REQUEST(cmd_save_config);
3354
qs.xiong9fbf74e2023-03-28 13:38:22 +08003355 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003356
3357}
3358
you.chen35020192022-05-06 11:30:57 +08003359static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003360{
you.chen35020192022-05-06 11:30:57 +08003361 char lynq_disable_cmd[128]={0};
3362 char lynq_select_cmd[128]={0};
3363
3364 CHECK_WPA_CTRL(CTRL_STA);
3365
qs.xiong9fbf74e2023-03-28 13:38:22 +08003366 if (save != 0)
3367 {
you.chenc29444e2022-06-07 18:01:16 +08003368 if (start_flag != 0)
3369 {
3370 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3371 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3372 }
3373 else
3374 {
3375 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3376 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3377 }
you.chen35020192022-05-06 11:30:57 +08003378 DO_OK_FAIL_REQUEST(cmd_save_config);
3379 }
3380
qs.xiong9fbf74e2023-03-28 13:38:22 +08003381 if (start_flag == 0)
3382 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003383 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003384 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3385 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003386 else
3387 {
you.chen35020192022-05-06 11:30:57 +08003388 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3389 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3390 }
3391
3392 return 0;
3393}
3394
3395int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3396{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003397 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003398 CHECK_IDX(idx, CTRL_STA);
3399
you.chen6c2dd9c2022-05-16 17:55:28 +08003400 curr_status_info curr_state;
3401 ap_info_s ap_info;
3402 curr_state.ap = &ap_info;
3403 curr_state.state = NULL;
3404
qs.xiong9fbf74e2023-03-28 13:38:22 +08003405 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3406 {
you.chend2fef3f2023-02-13 10:50:35 +08003407 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003408 return 0;
3409 }
3410
3411 return -1;
you.chen35020192022-05-06 11:30:57 +08003412}
3413
3414int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3415{
qs.xiong5d716d22023-09-20 20:08:39 +08003416 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003417 scan_info_s *scan_list = NULL;
3418 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003419 int scan_len=0;
3420 int save_len=0;
3421 int best_index = -1;
3422 int best_scan_index = -1;
3423 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003424 int i, j, ret;
3425
3426 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003427
3428 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003429 if (info == NULL)
3430 {
you.chen35020192022-05-06 11:30:57 +08003431 return -1;
3432 }
3433
3434 curr_status_info curr_state;
3435 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003436 char status[64];
you.chen35020192022-05-06 11:30:57 +08003437
you.chen9ac66392022-08-06 17:01:16 +08003438 memset(&ap_info, 0, sizeof (ap_info));
3439 memset(status, 0, sizeof (status));
3440
3441 curr_state.ap = &ap_info;
3442 curr_state.state = status;
3443
qs.xiong9fbf74e2023-03-28 13:38:22 +08003444 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3445 {
you.chen35020192022-05-06 11:30:57 +08003446 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003447 if (strcmp(status, STATE_COMPLETED) == 0)
3448 {
3449 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003450 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003451 }
3452 else
3453 {
3454 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003455 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003456 }
you.chen593621d2023-04-27 17:52:44 +08003457 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003458 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003459 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003460 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003461 return 0;
3462 }
3463
you.chen9ac66392022-08-06 17:01:16 +08003464 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003465 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003466 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3467 {
you.chen9ac66392022-08-06 17:01:16 +08003468 if (NULL != scan_list)
3469 {
3470 free(scan_list);
3471 }
you.chen35020192022-05-06 11:30:57 +08003472 return -1;
3473 }
3474
qs.xiong9fbf74e2023-03-28 13:38:22 +08003475 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3476 {
you.chen9ac66392022-08-06 17:01:16 +08003477 if (NULL != scan_list)
3478 {
3479 free(scan_list);
3480 }
3481 if (NULL != save_list)
3482 {
3483 free(save_list);
3484 }
you.chen35020192022-05-06 11:30:57 +08003485 return -1;
3486 }
3487
qs.xiong9fbf74e2023-03-28 13:38:22 +08003488 for (i=0; i < save_len; i++)
3489 {
3490 for (j=0; j < scan_len; j++)
3491 {
you.chen35020192022-05-06 11:30:57 +08003492 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003493 && save_list[i].base_info.auth == scan_list[j].auth)
3494 {
3495 if (best_rssi == 0)
3496 {
you.chen9ac66392022-08-06 17:01:16 +08003497 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003498 best_rssi = scan_list[j].rssi;
3499 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003500 else if (best_rssi > scan_list[j].rssi)
3501 {
you.chen35020192022-05-06 11:30:57 +08003502 best_index = i;
3503 best_scan_index = j;
3504 best_rssi = scan_list[j].rssi;
3505 }
you.chend2fef3f2023-02-13 10:50:35 +08003506 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 +08003507 break;
3508 }
3509 }
3510 }
3511
qs.xiong9fbf74e2023-03-28 13:38:22 +08003512 if (best_index >= 0)
3513 {
you.chen35020192022-05-06 11:30:57 +08003514 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003515 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 +08003516 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003517 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003518 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003519 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003520 }
3521
you.chen9ac66392022-08-06 17:01:16 +08003522 if (NULL != scan_list)
3523 {
3524 free(scan_list);
3525 }
3526 if (NULL != save_list)
3527 {
3528 free(save_list);
3529 }
3530
qs.xiong5d716d22023-09-20 20:08:39 +08003531 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003532 return ret;
you.chen35020192022-05-06 11:30:57 +08003533}
3534
3535static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3536{
qs.xiongc8d92a62023-03-29 17:36:14 +08003537 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003538 char lynq_ket_mgmt_cmd[64]={0};
3539 char lynq_pairwise_cmd[64]={0};
3540 char lynq_psk_cmd[64]={0};
3541
3542 CHECK_WPA_CTRL(CTRL_STA);
3543
qs.xiong9fbf74e2023-03-28 13:38:22 +08003544 switch(auth)
3545 {
3546 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003547 {
3548 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003549
you.chen35020192022-05-06 11:30:57 +08003550 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003551// DO_OK_FAIL_REQUEST(cmd_save_config);
3552 break;
3553 }
3554 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003555 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003556 {
3557 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3558 {
you.chen35020192022-05-06 11:30:57 +08003559 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3560 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003561 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3562 {
you.chena6cd55a2022-05-08 12:20:18 +08003563 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003564 }
3565 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3566 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003567
you.chen35020192022-05-06 11:30:57 +08003568 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3569 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3570 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003571
qs.xiong9fbf74e2023-03-28 13:38:22 +08003572 if (password != NULL)
3573 {
you.chen35020192022-05-06 11:30:57 +08003574 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3575 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003576 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003577 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003578
you.chen35020192022-05-06 11:30:57 +08003579// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003580 break;
3581 }
3582 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3583 {
qs.xiong3e506812023-04-06 11:08:48 +08003584 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003585 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003586 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3587 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3588
qs.xiong3e506812023-04-06 11:08:48 +08003589 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003590 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3591 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3592 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3593
3594 break;
3595 }
3596 case LYNQ_WIFI_AUTH_WPA3_PSK:
3597 {
qs.xiong3e506812023-04-06 11:08:48 +08003598 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003599 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003600 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3601 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3602
qs.xiongb37f8c42023-09-13 21:21:58 +08003603 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003604 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3605 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3606 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3607
3608 break;
3609 }
3610 default:
3611 return -1;
you.chen35020192022-05-06 11:30:57 +08003612 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003613
qs.xiong9fbf74e2023-03-28 13:38:22 +08003614 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003615}
qs.xiong7a105ce2022-03-02 09:43:11 -05003616
you.chen35020192022-05-06 11:30:57 +08003617static int inner_get_curr_net_no(int interface) {
3618 curr_status_info curr_state;
3619 curr_state.ap = NULL;
3620 curr_state.state = NULL;
3621
qs.xiong9fbf74e2023-03-28 13:38:22 +08003622 if (0 != inner_get_status_info(interface, &curr_state))
3623 {
you.chen35020192022-05-06 11:30:57 +08003624 return -1;
3625 }
3626
3627 return curr_state.net_no;
3628}
3629
3630int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003631{
you.chen35020192022-05-06 11:30:57 +08003632 int net_no;
3633 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003634
you.chen35020192022-05-06 11:30:57 +08003635 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003636
qs.xiong9fbf74e2023-03-28 13:38:22 +08003637 if (net_no < 0)
3638 {
you.chen35020192022-05-06 11:30:57 +08003639 return -1;
3640 }
3641
3642 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003643}
3644
you.chenb95401e2023-05-12 19:39:06 +08003645int 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 -05003646{
you.chen35020192022-05-06 11:30:57 +08003647 int count, net_no, index;
3648 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003649 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003650 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003651 curr_status_info curr_state;
3652 ap_info_s ap_info;
3653 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003654
qs.xiong9fbf74e2023-03-28 13:38:22 +08003655 if (ssid == NULL || *ssid == '\0')
3656 {
3657 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003658 return -1;
3659 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003660
qs.xiong9fbf74e2023-03-28 13:38:22 +08003661 if (LYNQ_WIFI_AUTH_OPEN != auth)
3662 {
3663 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003664 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003665 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003666 return -1;
3667 }
3668 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003669
you.chen70f377f2023-04-14 18:17:09 +08003670
3671 pthread_mutex_lock(&s_global_check_mutex);
3672 if (s_sta_status != INNER_STA_STATUS_INIT)
3673 {
3674 s_sta_status = INNER_STA_STATUS_CANCEL;
3675 pthread_cond_signal(&s_global_check_cond);
3676 }
3677 pthread_mutex_unlock(&s_global_check_mutex);
3678
you.chen35020192022-05-06 11:30:57 +08003679 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003680 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003681
3682 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003683 memset(&ap_info, 0, sizeof (ap_info));
3684 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003685
you.chen70f377f2023-04-14 18:17:09 +08003686 curr_state.ap = &ap_info;
3687 curr_state.state = status;
3688
3689 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003690 {
you.chen70f377f2023-04-14 18:17:09 +08003691 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3692 {
3693 net_no = curr_state.net_no;
3694 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3695 && strcmp(ap_info.psw, psw) == 0)
3696 {
3697 RLOGD("already connected\n");
3698
3699 pthread_mutex_lock(&s_global_check_mutex);
3700 s_sta_status = INNER_STA_STATUS_CONNECTED;
qs.xiong09560402023-10-27 21:58:55 +08003701 lynq_sta_removeElement(net_no);
you.chen70f377f2023-04-14 18:17:09 +08003702 pthread_cond_signal(&s_global_check_cond);
3703 pthread_mutex_unlock(&s_global_check_mutex);
3704 return 0;
3705 }
you.chen35020192022-05-06 11:30:57 +08003706 }
3707 }
3708
you.chen70f377f2023-04-14 18:17:09 +08003709 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003710 {
you.chen70f377f2023-04-14 18:17:09 +08003711 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3712
3713 for (index=0; index < count; index++)
3714 {
3715 net_auth = -1;
3716 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3717 {
3718 net_no = net_no_list[index];
3719 break;
3720 }
you.chen35020192022-05-06 11:30:57 +08003721 }
3722
you.chen70f377f2023-04-14 18:17:09 +08003723 if (net_no < 0)
3724 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003725 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3726 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3727 {
3728 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3729 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3730 DO_OK_FAIL_REQUEST(rm_net_cmd);
3731 }
you.chen70f377f2023-04-14 18:17:09 +08003732 net_no = lynq_add_network(CTRL_STA);
3733 if (net_no == -1)
3734 {
3735 return -1;
3736 }
3737
3738 RLOGD("net no is %d\n", net_no);
3739 if (0 != inner_set_sta_ssid(net_no, ssid))
3740 {
3741 return -1;
3742 }
you.chen35020192022-05-06 11:30:57 +08003743 }
3744 }
3745
qs.xiong9fbf74e2023-03-28 13:38:22 +08003746 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3747 {
you.chen35020192022-05-06 11:30:57 +08003748 return -1;
3749 }
3750
you.chen70f377f2023-04-14 18:17:09 +08003751
3752 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003753 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003754 usleep(200*1000);
3755
qs.xiong09560402023-10-27 21:58:55 +08003756 pthread_mutex_lock(&s_global_check_mutex);
3757 lynq_sta_removeElement(net_no);
3758 pthread_mutex_unlock(&s_global_check_mutex);
3759
you.chen70f377f2023-04-14 18:17:09 +08003760 ret = inner_sta_start_stop(net_no, 1, 1);
3761
3762 pthread_mutex_lock(&s_global_check_mutex);
3763 s_sta_status = INNER_STA_STATUS_CONNECTING;
you.chen0c9bee22023-10-25 13:03:14 +08003764 g_sta_conncet_status_flag = 1;
you.chen70f377f2023-04-14 18:17:09 +08003765 strcpy(s_sta_current_connecting_ssid, ssid);
3766 struct timeval now;
3767 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003768 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003769 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3770 pthread_cond_signal(&s_global_check_cond);
3771 pthread_mutex_unlock(&s_global_check_mutex);
3772 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003773}
3774
you.chenb95401e2023-05-12 19:39:06 +08003775int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3776{
3777 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3778}
3779
you.chen35020192022-05-06 11:30:57 +08003780int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003781{
you.chen35020192022-05-06 11:30:57 +08003782 ap_info_s ap;
3783 curr_status_info curr_state;
3784 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003785
qs.xiong9fbf74e2023-03-28 13:38:22 +08003786 if (ssid == NULL || *ssid == '\0')
3787 {
3788 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003789 return -1;
3790 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003791
you.chen35020192022-05-06 11:30:57 +08003792 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003793
you.chen35020192022-05-06 11:30:57 +08003794 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003795 curr_state.state = NULL;
3796
qs.xiong9fbf74e2023-03-28 13:38:22 +08003797 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3798 {
you.chen35020192022-05-06 11:30:57 +08003799 return 0;
3800 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003801
qs.xiong9fbf74e2023-03-28 13:38:22 +08003802 if (strcmp(ap.ap_ssid, ssid) != 0)
3803 {
you.chen35020192022-05-06 11:30:57 +08003804 return 0;
3805 }
3806
you.chen70f377f2023-04-14 18:17:09 +08003807 pthread_mutex_lock(&s_global_check_mutex);
3808 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3809 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003810 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003811}
qs.xiong97fa59b2022-04-07 05:41:29 -04003812
qs.xiongc93bf2b2023-08-25 10:22:08 +08003813int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3814{
qs.xiong09560402023-10-27 21:58:55 +08003815 int i,check_history_idx_flag;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003816 ap_info_s ap;
3817 curr_status_info curr_state;
3818 ap.ap_ssid[0] = '\0';
qs.xiong09560402023-10-27 21:58:55 +08003819 check_history_idx_flag = 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003820
3821 if (ssid == NULL || *ssid == '\0')
3822 {
3823 RLOGE("input ssid is NULL\n");
3824 return -1;
3825 }
3826
3827 CHECK_IDX(idx, CTRL_STA);
3828
3829
3830 curr_state.ap = &ap;
3831 curr_state.state = NULL;
3832
3833 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3834 {
3835 return 0;
3836 }
3837
3838 if (strcmp(ap.ap_ssid, ssid) != 0)
3839 {
3840 return 0;
3841 }
3842
3843 pthread_mutex_lock(&s_global_check_mutex);
3844 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiong09560402023-10-27 21:58:55 +08003845 RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
3846 for( i = 0; i< g_history_disconnect_valid_num ; i++)
3847 {
3848 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);
3849 if( g_history_disconnect_net[i] == curr_state.net_no)
3850 {
3851 RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
3852 check_history_idx_flag = 1;
3853 break;
3854 }
3855 }
3856 if ( check_history_idx_flag == 0)
3857 {
3858 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__);
3859 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3860 g_history_disconnect_valid_num++;
3861 }
3862 RLOGD("%s %d",__func__,__LINE__);
3863 print_disconnect_list();
qs.xiongc93bf2b2023-08-25 10:22:08 +08003864 pthread_mutex_unlock(&s_global_check_mutex);
3865 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3866
3867}
3868
3869
you.chena6cd55a2022-05-08 12:20:18 +08003870int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3871{
qs.xiongb37f8c42023-09-13 21:21:58 +08003872
qs.xiongad2f89d2023-01-18 13:17:41 +08003873 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3874 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003875
you.chen35020192022-05-06 11:30:57 +08003876 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003877 CHECK_WPA_CTRL(CTRL_STA);
3878
you.chenc9928582023-04-24 15:39:37 +08003879 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3880 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003881 {
you.chenc9928582023-04-24 15:39:37 +08003882 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003883 return -1;
3884 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003885
qs.xiongad2f89d2023-01-18 13:17:41 +08003886 system(lynq_enable_sta_cmd);
3887 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003888 pthread_mutex_lock(&s_global_check_mutex);
3889 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003890 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003891 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003892 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003893}
3894
you.chen6d247052023-06-01 16:39:54 +08003895static int inner_get_status_info_state (int interface, char *state) {
3896 curr_status_info curr_state;
3897 curr_state.ap = NULL;
3898 curr_state.state = state;
3899 return inner_get_status_info(interface, &curr_state);
3900}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003901
3902int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3903{
3904
qs.xiongb37f8c42023-09-13 21:21:58 +08003905 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3906 int tmp_open_idx[128];
3907 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003908
qs.xiongb37f8c42023-09-13 21:21:58 +08003909 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003910 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003911 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3912 pthread_mutex_unlock(&s_global_check_mutex);
3913 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003914 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003915 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003916 }
3917
qs.xiongb37f8c42023-09-13 21:21:58 +08003918 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003919 return 0;
3920}
3921
3922
you.chen35020192022-05-06 11:30:57 +08003923int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003924{
qs.xiongec8bbeb2023-11-20 15:51:45 +08003925 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08003926 int i=0;
3927 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003928
you.chena6cd55a2022-05-08 12:20:18 +08003929 CHECK_IDX(idx, CTRL_STA);
3930 CHECK_WPA_CTRL(CTRL_STA);
3931
you.chen6d247052023-06-01 16:39:54 +08003932 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003933 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003934
3935 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3936 if (ret != 0)
3937 {
3938 RLOGE("lynq_wifi_ap_start excute script fail");
3939 return -1;
3940 }
3941
you.chen6d247052023-06-01 16:39:54 +08003942 for (i=0; i < 30; i++) // to check if sta is realy stoped
3943 {
3944 if (inner_get_status_info_state(idx, state) != 0)
3945 {
3946 break;
3947 }
3948
3949 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3950 {
3951 break;
3952 }
3953 RLOGD("lynq_wifi_ap_start curr state %s", state);
3954 usleep(SLEEP_TIME_ON_IDLE);
3955 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003956 pthread_mutex_lock(&s_global_check_mutex);
3957 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3958 pthread_mutex_unlock(&s_global_check_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08003959 RLOGD("end %s %d",__func__,__LINE__);
you.chena6cd55a2022-05-08 12:20:18 +08003960 return 0;
3961// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003962}
qs.xiongfcc914b2023-07-06 21:16:20 +08003963int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3964{
3965 char LYNQ_DISABLE_CMD[128]={0};
3966 CHECK_IDX(idx, CTRL_STA);
3967 CHECK_WPA_CTRL(CTRL_STA);
3968 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3969 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3970 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3971 return 0;
3972}
qs.xiong7a105ce2022-03-02 09:43:11 -05003973
you.chen35020192022-05-06 11:30:57 +08003974//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3975// int i, count;
3976// char *p;
3977// const char * FLAG_SSID = "ssid=";
3978// const char * FLAG_SBSID = "bssid=";
3979// const char * FLAG_KEY_MGMT = "key_mgmt=";
3980// const char * FLAG_FREQ = "freq=";
3981// char lynq_sta_cmd[MAX_CMD];
3982// char *split_lines[128] = {0};
3983
3984// CHECK_WPA_CTRL(CTRL_AP);
3985
3986// sprintf(lynq_sta_cmd, "STA %s", bssid);
3987
3988// DO_REQUEST(lynq_sta_cmd);
3989
3990// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3991
3992// for(i=0; i < count; i++) {
3993// p = strstr(split_lines[i], FLAG_SSID);
3994// if (p != NULL) {
3995// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3996// continue;
3997// }
3998// }
3999
4000// lynq_get_interface_ip(idx, ap->ap_ip);
4001// lynq_ap_password_set(idx, ap->psw);
4002
4003// return 0;
4004//}
4005
4006static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
4007 curr_status_info curr_state;
4008 curr_state.ap = ap;
4009 curr_state.state = NULL;
4010 return inner_get_status_info(interface, &curr_state);
4011}
4012
4013int 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 -04004014{
qs.xiong5071c802023-09-06 14:04:15 +08004015 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08004016 int index, line_count;
4017 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08004018 const char *lynq_first_sta_cmd = "STA-FIRST";
4019 char lynq_next_sta_cmd[MAX_CMD];
4020 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08004021 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04004022
you.chen35020192022-05-06 11:30:57 +08004023 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004024
you.chen35020192022-05-06 11:30:57 +08004025 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04004026
you.chenb95401e2023-05-12 19:39:06 +08004027 // ap_info_s * tmp_ap;
4028 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004029 if (ap == NULL || list == NULL || len == NULL)
4030 {
4031 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08004032 return -1;
4033 }
4034
you.chenb95401e2023-05-12 19:39:06 +08004035 // ap = &tmp_ap;
4036 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08004037 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08004038 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08004039
you.chenb95401e2023-05-12 19:39:06 +08004040 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08004041 {
you.chenb95401e2023-05-12 19:39:06 +08004042 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08004043 return -1;
4044 }
4045
4046 lynq_get_interface_ip(idx, (*ap)->ap_ip);
4047 lynq_ap_password_get(idx, (*ap)->psw);
4048
you.chen35020192022-05-06 11:30:57 +08004049 DO_REQUEST(lynq_first_sta_cmd);
4050
4051 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004052 while (reply_len > 0)
4053 {
4054 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004055 {
you.chen35020192022-05-06 11:30:57 +08004056 break;
4057 }
4058 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4059 bssid[index] = malloc(strlen(split_lines[0]) + 1);
4060 strcpy(bssid[index], split_lines[0]);
4061 index++;
4062 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
4063 reply_len = MAX_RET;
4064 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08004065 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 +08004066 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08004067 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004068 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08004069 break;
4070 }
4071 }
4072
4073 *len = index;
4074
4075 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08004076 for (index=0; index < *len; index++)
4077 {
you.chend2fef3f2023-02-13 10:50:35 +08004078 dev_info = &(*list)[index];
4079 memset(dev_info, 0, sizeof(device_info_s));
4080 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
4081 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
4082 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
4083 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08004084 free(bssid[index]);
4085 }
qs.xiong5071c802023-09-06 14:04:15 +08004086 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08004087 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004088}
4089
you.chen35020192022-05-06 11:30:57 +08004090int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04004091{
you.chen35020192022-05-06 11:30:57 +08004092 int i, count, index, count_words;
4093 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
4094 char *split_lines[128] = {0};
4095 char *split_words[128] = {0};
4096 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04004097
qs.xiong9fbf74e2023-03-28 13:38:22 +08004098 if (list == NULL || len == NULL)
4099 {
you.chen35020192022-05-06 11:30:57 +08004100 return -1;
4101 }
qs.xiong97fa59b2022-04-07 05:41:29 -04004102
you.chen9ac66392022-08-06 17:01:16 +08004103 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
4104 {
4105 usleep(100 * 1000);
4106 }
4107
you.chen35020192022-05-06 11:30:57 +08004108 CHECK_IDX(idx, CTRL_STA);
4109
4110 CHECK_WPA_CTRL(CTRL_STA);
4111
4112 DO_REQUEST(lynq_scan_result_cmd);
4113
4114 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
4115 *len = count - 1;
4116 *list = malloc(sizeof (scan_info_s) * *len);
4117
4118 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08004119 for (index=0; index <count_words; index++)
4120 {
4121 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08004122 }
4123
qs.xiong9fbf74e2023-03-28 13:38:22 +08004124 for(index = 1;index < count; index++)
4125 {
4126 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08004127 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08004128 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
4129 if (count_words < 4)
4130 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004131 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08004132 //bssid / frequency / signal level / flags / ssid
4133 p = (*list) + index - 1;
4134 strcpy(p->mac, split_words[0]);
4135 p->band = convert_band_from_freq(atoi(split_words[1]));
4136 p->rssi = -1 * atoi( split_words[2]);
4137 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08004138 if (count_words == 4) // ssid hided
4139 {
4140 p->ssid[0] = '\0';
4141 }
4142 else
4143 {
4144 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
4145 }
you.chen35020192022-05-06 11:30:57 +08004146 }
4147
4148 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004149}
qs.xiong97fa59b2022-04-07 05:41:29 -04004150
you.chen35020192022-05-06 11:30:57 +08004151int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
4152{
4153 int count, net_no, index;
4154 int net_no_list[128];
4155 lynq_wifi_auth_s net_auth;
qs.xiong09560402023-10-27 21:58:55 +08004156
you.chen35020192022-05-06 11:30:57 +08004157 char lynq_remove_cmd[MAX_CMD];
4158
qs.xiong9fbf74e2023-03-28 13:38:22 +08004159 if (ssid == NULL || *ssid == '\0')
4160 {
4161 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004162 return -1;
4163 }
4164
4165 CHECK_IDX(idx, CTRL_STA);
4166
4167 CHECK_WPA_CTRL(CTRL_STA);
4168
4169 net_no = -1;
4170 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4171
qs.xiong9fbf74e2023-03-28 13:38:22 +08004172 for (index=0; index < count; index++)
4173 {
you.chen35020192022-05-06 11:30:57 +08004174 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004175 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4176 {
you.chen35020192022-05-06 11:30:57 +08004177 net_no = net_no_list[index];
4178 break;
4179 }
4180 }
4181
qs.xiong9fbf74e2023-03-28 13:38:22 +08004182 if (net_no < 0)
4183 {
you.chen35020192022-05-06 11:30:57 +08004184 return 0;
4185 }
4186
4187 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4188
4189 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
qs.xiong09560402023-10-27 21:58:55 +08004190
4191 RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
4192
4193 pthread_mutex_lock(&s_global_check_mutex);
4194 lynq_sta_removeElement(net_no);
4195 pthread_mutex_unlock(&s_global_check_mutex);
4196
4197 RLOGD("%s %d",__func__,__LINE__);
4198 print_disconnect_list();
you.chen35020192022-05-06 11:30:57 +08004199 DO_OK_FAIL_REQUEST(cmd_save_config);
4200
4201 return 0;
4202}
4203
4204int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004205{
you.chend2fef3f2023-02-13 10:50:35 +08004206 int count, index;
you.chen35020192022-05-06 11:30:57 +08004207 int net_no_list[128];
4208 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004209 RLOGD("enter lynq_get_sta_saved_ap api\n");
4210 if (list == NULL || len == NULL)
4211 {
4212 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004213 return -1;
4214 }
4215
4216 CHECK_IDX(idx, CTRL_STA);
4217
4218// CHECK_WPA_CTRL(CTRL_STA);
4219
4220 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004221 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004222
you.chen057aac42023-04-13 14:06:58 +08004223 if (count < 0)
4224 {
4225 RLOGE("list network fail");
4226 return count;
4227 }
4228 else if (count == 0)
4229 {
4230 *list = NULL;
4231 *len = 0;
4232 return 0;
4233 }
4234
you.chen35020192022-05-06 11:30:57 +08004235 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004236 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004237 *len = count;
4238
qs.xiong9fbf74e2023-03-28 13:38:22 +08004239 for (index=0; index < count; index++)
4240 {
you.chen35020192022-05-06 11:30:57 +08004241 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004242 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004243 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004244 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004245 {
you.chen35020192022-05-06 11:30:57 +08004246 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4247 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004248 else
you.chen057aac42023-04-13 14:06:58 +08004249 {
you.chen35020192022-05-06 11:30:57 +08004250 (*list)[index].base_info.band = -1;
4251 }
you.chen057aac42023-04-13 14:06:58 +08004252 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004253 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004254 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004255 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004256 return 0;
4257}
4258
4259int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4260{
you.chen0c9bee22023-10-25 13:03:14 +08004261 if ( s_sta_status == INNER_STA_STATUS_INIT && g_sta_conncet_status_flag != 0 )
qs.xiong20202422023-09-06 18:01:18 +08004262 {
you.chen0c9bee22023-10-25 13:03:14 +08004263 RLOGD("current sta is autoconnecting dest ap,fake scan result");
4264 g_sta_fake_scan_finish_flag = 1;
4265 return 0;
4266 }
4267 else if (g_sta_conncet_status_flag != 0)
4268 {
4269 RLOGD("current sta is connecting dest ap, don't scan");
qs.xiongba5b5f22023-09-19 14:55:34 +08004270 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004271 }
you.chen0c9bee22023-10-25 13:03:14 +08004272
qs.xiongc8d92a62023-03-29 17:36:14 +08004273 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004274 const char *lynq_scan_cmd = "SCAN";
4275
4276 CHECK_IDX(idx, CTRL_STA);
4277
4278 CHECK_WPA_CTRL(CTRL_STA);
4279
you.chen0df3e7e2023-05-10 15:56:26 +08004280 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4281 {
4282 RLOGD("tmp clear scanlist");
4283 system(clean_last_re);
4284 }
you.chen9ac66392022-08-06 17:01:16 +08004285 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004286 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004287 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4288 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004289 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4291 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004292 g_sta_scan_finish_flag = 1;
4293 return -1;
4294 }
you.chen35020192022-05-06 11:30:57 +08004295
4296 return 0;
4297}
4298
4299int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004300 if (cb == NULL)
4301 {
4302 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004303 return -1;
4304 }
4305
you.chen6d247052023-06-01 16:39:54 +08004306 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004307 g_ap_callback_priv = priv;
4308 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004309 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004310
you.chen6d247052023-06-01 16:39:54 +08004311 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004312 if (g_ap_watcher_pid == 0 )
4313 {
4314 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4315 {
4316 g_ap_watcher_pid = 0;
4317 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4318 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4319 return -1;
4320 }
4321 }
4322
4323 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4324 RLOGD("creat APWatcherTheradProc susccs");
4325
you.chen35020192022-05-06 11:30:57 +08004326 return 0;
4327}
4328
4329int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004330 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004331 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004332 if (g_ap_callback_priv == priv)
4333 {
you.chen35020192022-05-06 11:30:57 +08004334 g_ap_callback_func = NULL;
4335 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004336 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004337 RLOGD("unreg ap callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004338 return 0;
4339 }
you.chen6d247052023-06-01 16:39:54 +08004340 pthread_mutex_unlock(&s_ap_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004341 RLOGE("unreg ap callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004342 return -1;
4343}
4344
4345int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004346 if (cb == NULL)
4347 {
4348 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004349 return -1;
4350 }
4351
you.chen6d247052023-06-01 16:39:54 +08004352 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004353 g_sta_callback_priv = priv;
4354 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004355 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004356
you.chen6d247052023-06-01 16:39:54 +08004357 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004358 if (g_sta_watcher_pid == 0 ) {
4359 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4360 {
4361 g_sta_watcher_pid = 0;
4362 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4363 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4364 return -1;
4365 }
4366 }
4367
4368 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4369 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004370 return 0;
4371}
4372
4373int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongec8bbeb2023-11-20 15:51:45 +08004374 RLOGD("enter %s %d",__func__,__LINE__);
you.chen6d247052023-06-01 16:39:54 +08004375 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004376 if (g_sta_callback_priv == priv)
4377 {
you.chen35020192022-05-06 11:30:57 +08004378 g_sta_callback_func = NULL;
4379 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004380 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004381 RLOGD("unreg sta callback pass %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004382 return 0;
4383 }
you.chen6d247052023-06-01 16:39:54 +08004384 pthread_mutex_unlock(&s_sta_callback_mutex);
qs.xiongec8bbeb2023-11-20 15:51:45 +08004385 RLOGE("unreg sta callback fail %s %d",__func__,__LINE__);
you.chen35020192022-05-06 11:30:57 +08004386 return -1;
4387}
4388
qs.xiongfcc914b2023-07-06 21:16:20 +08004389int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4390 if (cb == NULL)
4391 {
4392 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4393 return -1;
4394 }
4395 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4396 g_sta_auto_callback_priv = priv;
4397 g_sta_auto_callback_func = cb;
4398 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4399 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4400 if (g_sta_auto_watcher_pid == 0 ) {
4401 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4402 {
4403 g_sta_auto_watcher_pid = 0;
4404 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4405 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4406 return -1;
4407 }
4408 }
4409 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4410 RLOGD("creat STAWatcherTheradProc susccs");
4411 return 0;
4412}
4413int lynq_unreg_sta_auto_event_callback(void * priv) {
4414 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4415 if (g_sta_auto_callback_priv == priv)
4416 {
4417 g_sta_auto_watcher_stop_flag = 1;
4418 if (g_sta_auto_watcher_pid != 0)
4419 {
4420 pthread_join(g_sta_auto_watcher_pid, NULL);
4421 }
4422 g_sta_auto_watcher_pid = 0;
4423 g_sta_auto_callback_func = NULL;
4424 g_sta_auto_callback_priv = NULL;
4425 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4426 return 0;
4427 }
4428 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4429 return -1;
4430}
you.chen35020192022-05-06 11:30:57 +08004431int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4432{
4433 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004434 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004435 CHECK_IDX(idx, CTRL_AP);
4436
qs.xiong9fbf74e2023-03-28 13:38:22 +08004437 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4438 {
you.chen35020192022-05-06 11:30:57 +08004439 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4440 return 0;
4441 }
4442
qs.xiong9fbf74e2023-03-28 13:38:22 +08004443 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4444 {
you.chen35020192022-05-06 11:30:57 +08004445 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4446 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004447 else
4448 {
you.chen35020192022-05-06 11:30:57 +08004449 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4450 }
4451
4452 return 0;
4453}
4454
4455int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4456 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004457 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004458 CHECK_IDX(idx, CTRL_STA);
4459
qs.xiong9fbf74e2023-03-28 13:38:22 +08004460 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4461 {
you.chen35020192022-05-06 11:30:57 +08004462 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4463 return 0;
4464 }
4465
qs.xiong9fbf74e2023-03-28 13:38:22 +08004466 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4467 {
you.chen35020192022-05-06 11:30:57 +08004468 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4469 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004470 else
4471 {
you.chen35020192022-05-06 11:30:57 +08004472 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4473 }
4474
4475 return 0;
4476}
4477
4478int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4479// CHECK_IDX(idx, CTRL_AP);
4480// int ret = 0;
4481// size_t reply_len = MAX_RET;
4482// char cmd_reply[MAX_RET]={0};
4483// const char * cmd_str = "GET country";
4484// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4485// do{
4486// if (NULL == s_lynq_wpa_ctrl) {
4487// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4488// if (NULL == s_lynq_wpa_ctrl ) {
4489// printf("wpa_ctrl_open fail\n");
4490// return -1;
4491// }
4492// }
4493// }while(0);
4494
4495// do {
4496// reply_len = MAX_RET;
4497// cmd_reply[0] = '\0';
4498// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004499// 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 +08004500// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004501// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004502// return ret;
4503// }
4504// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004505// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004506// }while(0);
4507
4508 FILE *fp;
4509 size_t i = 0;
4510 char lynq_cmd_ret[MAX_RET]={0};
4511
4512// CHECK_IDX(idx, CTRL_AP);
4513
4514 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004515 {
4516 perror("popen error!");
4517 return -1;
4518 }
you.chen35020192022-05-06 11:30:57 +08004519 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4520 {
4521 perror("fread fail!");
4522 return -1;
4523 }
4524
qs.xiong9fbf74e2023-03-28 13:38:22 +08004525 for(i=0; i < strlen(lynq_cmd_ret); i++)
4526 {
4527 if (lynq_cmd_ret[i] == ' ')
4528 {
you.chen35020192022-05-06 11:30:57 +08004529 lynq_cmd_ret[i] = '\0';
4530 break;
4531 }
4532 }
4533
4534 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004535 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004536
4537 int ret=pclose(fp);
4538 if(ret==-1)
4539 {
4540 perror("close file faild");
4541 }
4542
4543 return 0;
4544}
4545
qs.xiong44fac672023-08-29 16:15:55 +08004546
you.chen705a7ef2023-06-01 22:06:45 +08004547static int check_and_init_uci_config(char * country_code)
4548{
4549 FILE * fp;
4550 int is_different = 0;
4551 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4552 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4553 const char * commit_uci_cmd ="uci commit";
4554 char set_country_cmd[MAX_CMD];
4555 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004556
you.chen705a7ef2023-06-01 22:06:45 +08004557 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004558
you.chen705a7ef2023-06-01 22:06:45 +08004559 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004560 {
you.chen705a7ef2023-06-01 22:06:45 +08004561 if (0 != system(create_uci_cmd))
4562 {
4563 RLOGE("creat_uci_cmd fail");
4564 return -1;
4565 }
4566 is_different = 1;
4567 }
4568
4569 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4570 {
4571 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004572 return -1;
4573 }
4574
you.chen705a7ef2023-06-01 22:06:45 +08004575 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004576 {
you.chen705a7ef2023-06-01 22:06:45 +08004577 RLOGE("fread fail!");
4578 fclose(fp);
4579 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004580 }
4581
you.chen705a7ef2023-06-01 22:06:45 +08004582 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4583 {
qs.xiong44fac672023-08-29 16:15:55 +08004584 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 +08004585 is_different = 1;
4586 }
4587
4588 fclose(fp);
4589
4590 if (is_different)
4591 {
4592 if ( 0 != system(set_country_cmd))
4593 {
4594 RLOGE("set_country_cmd fail");
4595 return -1;
4596 }
4597 if (0 != system(commit_uci_cmd))
4598 {
4599 RLOGE("commmit fail");
4600 }
4601 }
4602
4603 return is_different;
4604}
4605
4606int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4607 char check_current_code[10];
4608 const char * support_country[] = {"CN", "EU"};
4609
4610 int ret,is_different, i, cc_count;
4611
4612 if (country_code == NULL || country_code[0] == '\0')
4613 {
4614 RLOGE("bad country code\n");
4615 return -1;
4616 }
4617
4618 cc_count = sizeof (support_country) / sizeof (char*);
4619 for(i=0; i < cc_count; i++)
4620 {
4621 if (strcmp(support_country[i], country_code) == 0)
4622 {
4623 break;
4624 }
4625 }
4626
4627 if (i >= cc_count)
4628 {
4629 RLOGE("unspported country code %s\n", country_code);
4630 return -1;
4631 }
4632
4633 is_different = check_and_init_uci_config(country_code);
4634 if( is_different < 0 )
4635 {
4636 RLOGE("init set uci fail\n");
4637 return -1;
4638 }
4639
4640 ret = lynq_get_country_code(idx,check_current_code);
4641 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4642 {
4643 ret = lynq_wifi_disable();
4644 if(ret != 0 )
4645 {
4646 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4647 return -1;
4648 }
4649 }
4650
4651 return 0;
you.chen35020192022-05-06 11:30:57 +08004652}
4653
4654int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4655{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004656 RLOGD("enter lynq_get_connect_ap_mac\n");
4657 if (mac == NULL)
4658 {
4659 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004660 return -1;
4661 }
4662
4663 CHECK_IDX(idx, CTRL_STA);
4664 ap_info_s ap;
4665 ap.ap_mac[0] = '\0';
4666
qs.xiong9fbf74e2023-03-28 13:38:22 +08004667 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4668 {
you.chen35020192022-05-06 11:30:57 +08004669 return -1;
4670 }
4671 strcpy(mac, ap.ap_mac);
4672
4673 return 0;
4674}
4675
4676int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4677{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004678 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004679 struct ifaddrs *ifaddr_header, *ifaddr;
4680 struct in_addr * ifa;
4681 const char * ifaName = "wlan0";
4682 if (ip == NULL)
4683 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004684 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004685 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004686 }
you.chenf58b3c92022-06-21 16:53:48 +08004687
qs.xiong9fbf74e2023-03-28 13:38:22 +08004688 if (idx == 1)
4689 {
you.chen0df3e7e2023-05-10 15:56:26 +08004690 ifaName = inner_get_ap_interface_name();
4691 if (ifaName == NULL)
4692 {
4693 RLOGE("[lynq_get_interface_ip] ap name get fail");
4694 return -1;
4695 }
you.chen9ac66392022-08-06 17:01:16 +08004696 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004697 else if (idx != 0)
4698 {
you.chen35020192022-05-06 11:30:57 +08004699 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004700 }
you.chen35020192022-05-06 11:30:57 +08004701
you.chen9ac66392022-08-06 17:01:16 +08004702 if (getifaddrs(&ifaddr_header) == -1)
4703 {
you.chen35020192022-05-06 11:30:57 +08004704 perror("getifaddrs");
4705 return -1;
4706 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004707 }
you.chen35020192022-05-06 11:30:57 +08004708
4709
you.chen9ac66392022-08-06 17:01:16 +08004710 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4711 {
4712 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004713 continue;
you.chen9ac66392022-08-06 17:01:16 +08004714 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4715 {
4716 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4717 {
4718 // is a valid IP4 Address
4719 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4720 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004721 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004722 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004723 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004724 return 0;
4725 }
4726 }
4727 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004728 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004729 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004730 return -1;
you.chen35020192022-05-06 11:30:57 +08004731}
4732
4733int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4734{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004735 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004736 int count;
4737 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004738 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004739 char *split_words[128] = {0};
4740 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4741
4742 CHECK_WPA_CTRL(idx);
4743
4744 DO_REQUEST(lynq_get_mac_cmd);
4745
qs.xiong9fbf74e2023-03-28 13:38:22 +08004746 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4747 {
4748 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004749 return -1;
4750 }
4751
4752 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4753
qs.xiong9fbf74e2023-03-28 13:38:22 +08004754 if (count < 2)
4755 {
you.chen35020192022-05-06 11:30:57 +08004756 return -1;
4757 }
4758
qs.xiong9fbf74e2023-03-28 13:38:22 +08004759 for (i=0; i < strlen(split_words[1]); i++ )
4760 {
4761 if (split_words[1][i] != ' ')
4762 {
you.chen35020192022-05-06 11:30:57 +08004763 break;
4764 }
4765 }
4766
qs.xiongdd6e44c2023-08-08 15:02:53 +08004767 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004768
4769 return 0;
4770}
4771
4772int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4773{
4774// int count;
4775// char *split_words[128] = {0};
4776// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4777
4778// if (rssi == NULL) {
4779// return -1;
4780// }
4781
4782// CHECK_IDX(idx, CTRL_STA);
4783
4784// CHECK_WPA_CTRL(CTRL_STA);
4785
4786// DO_REQUEST(lynq_get_rssi_cmd);
4787
4788// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4789// return -1;
4790// }
4791
4792// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4793
4794// if (count < 2) {
4795// return -1;
4796// }
4797
4798// *rssi = atoi(split_words[1]) * -1;
4799
you.chen35020192022-05-06 11:30:57 +08004800 char lynq_cmd_ret[MAX_RET]={0};
4801
qs.xiongff0ae0f2022-10-11 15:47:14 +08004802/*******change other cmd to get rssi*******
4803 *
4804 *wl rssi ---> wl -i wlan0 rssi
4805 *
4806 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004807 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004808 {
you.chen23c4a5f2023-04-12 16:46:00 +08004809 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004810 return -1;
4811 }
you.chen9f17e4d2022-06-06 17:18:18 +08004812 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004813/****** if got rssi is 0,means sta didn't connected any device****/
4814 if(*rssi == 0)
4815 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004816 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004817 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004818 }
you.chen35020192022-05-06 11:30:57 +08004819
4820 return 0;
4821}
4822
4823int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4824{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004825 RLOGD("enter lynq_get_connect_ap_band\n");
4826 if (band == NULL)
4827 {
you.chen35020192022-05-06 11:30:57 +08004828 return -1;
4829 }
4830
4831 CHECK_IDX(idx, CTRL_STA);
4832 ap_info_s ap;
4833 ap.band = -1;
4834
qs.xiong9fbf74e2023-03-28 13:38:22 +08004835 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4836 {
you.chen35020192022-05-06 11:30:57 +08004837 return -1;
4838 }
4839 *band = ap.band;
4840
4841 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004842}
you.chenf58b3c92022-06-21 16:53:48 +08004843
4844int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4845{
you.chenb95401e2023-05-12 19:39:06 +08004846 int ret;
4847 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004848 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004849
4850 if (ip == NULL)
4851 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004852 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004853 return -1;
4854 }
4855
4856 CHECK_IDX(idx, CTRL_STA);
4857
qs.xionge4cbf1c2023-02-28 18:22:49 +08004858 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004859 {
4860 return -1;
4861 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004862
you.chenb95401e2023-05-12 19:39:06 +08004863 ip[0] = '\0';
4864 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4865 if (ret != 0)
4866 {
4867 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4868 return -1;
4869 }
4870
4871 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4872 {
4873 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004874 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004875 if (ret != 0)
4876 {
4877 ip[0] = '\0';
4878 return 0;
4879 }
4880 else
4881 {
4882 p = strchr(ip, '\n');
4883 if (p != NULL)
4884 {
4885 *p = '\0';
4886 }
4887 }
4888 }
4889 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004890}
4891
qs.xionge02a5252023-09-20 14:00:21 +08004892int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4893{
4894 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4895 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4896}
4897
qs.xiong026c5c72022-10-17 11:15:45 +08004898int lynq_ap_connect_num(int sta_number)
4899{
4900 char lynq_limit_cmd[32]={0};
4901 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004902 if((sta_number < 1 ) && (sta_number > 15))
4903 {
4904 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004905 return -1;
4906 }
4907 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4908 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004909 if(ret != 0)
4910 {
4911 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004912 }
4913 return 0;
4914}
you.chenf58b3c92022-06-21 16:53:48 +08004915
qs.xiong77905552022-10-17 11:19:57 +08004916int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4917{
4918
4919 char lynq_wifi_acs_cmd[128]={0};
4920 char lynq_cmd_mode[128]={0};
4921 char lynq_cmd_slect[128]={0};
4922
qs.xiong9fbf74e2023-03-28 13:38:22 +08004923 if((acs_mode != 2) && (acs_mode != 5))
4924 {
qs.xiong77905552022-10-17 11:19:57 +08004925 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4926 }
4927
qs.xiong9fbf74e2023-03-28 13:38:22 +08004928 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4929 {
qs.xiong77905552022-10-17 11:19:57 +08004930 return -1;
4931 }
4932
4933 CHECK_IDX(idx, CTRL_AP);
4934
4935 CHECK_WPA_CTRL(CTRL_AP);
4936
4937 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4938 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4939 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4940
4941 DO_OK_FAIL_REQUEST(cmd_disconnect);
4942 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4943 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4944 DO_OK_FAIL_REQUEST(cmd_save_config);
4945 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4946
4947 return 0;
4948}
you.chen0f5c6432022-11-07 18:31:14 +08004949//you.chen add for tv-box start
4950static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4951 FILE *fp;
4952 //printf("to exec cmd:%s\n", str_cmd);
4953 if((fp=popen(str_cmd,"r"))==NULL)
4954 {
4955 perror("popen error!");
4956 return -1;
4957 }
4958 if((fread(str_cmd_ret,max_len,1,fp))<0)
4959 {
4960 perror("fread fail!");
4961 fclose(fp);
4962 return -1;
4963 }
4964 fclose(fp);
4965 return 0;
4966}
4967
4968static int get_netmask_length(const char* mask)
4969{
4970 int masklen=0, i=0;
4971 int netmask=0;
4972
4973 if(mask == NULL)
4974 {
4975 return 0;
4976 }
4977
4978 struct in_addr ip_addr;
4979 if( inet_aton(mask, &ip_addr) )
4980 {
4981 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004982 }else
4983 {
you.chen0f5c6432022-11-07 18:31:14 +08004984 netmask = 0;
4985 return 0;
4986 }
4987
4988 while(0 == (netmask & 0x01) && i<32)
4989 {
4990 i++;
4991 netmask = netmask>>1;
4992 }
4993 masklen = 32-i;
4994 return masklen;
4995}
4996
4997static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4998 int mask_len;
4999 char *p;
5000 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08005001 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
5002 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08005003 return -1;
5004 p = strstr(str_cmd_ret, "Mask:");
5005 if (p == NULL)
5006 return -1;
5007 mask_len = get_netmask_length(p + 5);
5008 if (mask_len == 0)
5009 return -1;
5010 p = strstr(str_cmd_ret, "inet addr:");
5011 if (p == NULL)
5012 return -1;
5013 strcpy(tmp, p + 10);
5014 p = strstr(tmp, " ");
5015 if (p != NULL)
5016 *p = '\0';
5017 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
5018 return 0;
5019}
5020
5021static void GBWWatchThreadProc() {
5022 int i,n, nloop, nmax, ncheckcount, nidlecount;
5023 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
5024 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
5025 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
5026 char *results[16] = {0};
5027 char str_cmd[256] = {0};
5028 char str_cmd_ret[128] = {0};
5029 char dest_ip[32] = {0};
5030 lastAP1Bytes = lastAP2Bytes = 0;
5031 lastAP1Drop = lastAP2Drop = 0;
5032 lastAP1Speed = lastAP2Speed = 0;
5033 setAP1Speed = 50;
5034 setAP2Speed = 80;
5035 nloop = 0;
5036 nmax = 6;
5037 ncheckcount = nidlecount = 0;
5038
you.chen0df3e7e2023-05-10 15:56:26 +08005039 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08005040 {
5041 RLOGE("------gbw thread run\n");
5042 return;
5043 }
5044
qs.xiong9fbf74e2023-03-28 13:38:22 +08005045 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08005046 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
5047 while (dest_ip[0] == '\0') {
5048 sleep(1);
5049 str_cmd_ret[0] = '\0';
5050 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
5051 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
5052 if (str_cmd_ret[n] == '\n'){
5053 str_cmd_ret[n] = '\0';
5054 break;
5055 }
5056 }
5057 if (str_cmd_ret[0] != '\0')
5058 {
5059 strcpy(dest_ip, str_cmd_ret);
5060 }
5061 }
5062
you.chenc9928582023-04-24 15:39:37 +08005063 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
5064 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
5065 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 +08005066 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
5067 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08005068 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08005069 return;
5070 }
you.chenc9928582023-04-24 15:39:37 +08005071 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);
5072 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);
5073 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 +08005074
5075 while (1) {
5076 sleep(1);
5077 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005078 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5079 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
5080 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005081 continue;
5082 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005083 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005084 if (n > 9) {
5085 if (strcmp(results[1], "Sent") == 0) {
5086 currAP1Bytes = atoll(results[2]);
5087 }
5088 if (strcmp(results[6], "(dropped") == 0) {
5089 currAP1Drop = atoi(results[7]);
5090 }
5091 }
5092
5093 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08005094 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
5095 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
5096 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08005097 continue;
5098 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08005099 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08005100 if (n > 9) {
5101 if (strcmp(results[1], "Sent") == 0) {
5102 currAP2Bytes = atoll(results[2]);
5103 }
5104 if (strcmp(results[6], "(dropped") == 0) {
5105 currAP2Drop = atoi(results[7]);
5106 }
5107 }
5108
5109 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
5110 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
5111 lastAP1Bytes = currAP1Bytes;
5112 lastAP2Bytes = currAP2Bytes;
5113 continue;
5114 }
5115
5116 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
5117 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
5118 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
5119 lastAP1Speed = currAP1Speed;
5120 lastAP2Speed = currAP2Speed;
5121 lastAP1Bytes = currAP1Bytes;
5122 lastAP2Bytes = currAP2Bytes;
5123
5124 currSetAP1Speed = setAP1Speed;
5125 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
5126 ncheckcount++;
5127 if (ncheckcount > 3) {
5128 ncheckcount = 0;
5129 currSetAP1Speed = 5;
5130 }
5131 }
5132 else {
5133 ncheckcount = 0;
5134 if (currAP1Speed < 5)
5135 nidlecount++;
5136 else
5137 nidlecount = 0;
5138
5139 }
5140
5141 if (nidlecount > 60 ){
5142 currSetAP1Speed = 50;
5143 }
5144
5145 if (currSetAP1Speed != setAP1Speed) {
5146 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08005147 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
5148 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08005149 }
5150 }
5151}
5152
5153int enableGBW(const char* mac) {
5154 int i,len;
5155 char get_ipaddr_cmd[128]={0};
5156 ap_info_s *ap;
5157 device_info_s * list;
5158
5159 if (mac == NULL || g_gbw_enabled == 1)
5160 return -1;
5161 len = strlen(mac);
5162 g_gbw_mac = malloc(len + 1);
5163 for(i=0;i<len;i++) {
5164 if (mac[i] >= 'A' && mac[i] <= 'Z')
5165 {
5166 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
5167 }
5168 else
5169 g_gbw_mac[i] = mac[i];
5170 }
5171 g_gbw_mac[i] = '\0';
5172 g_gbw_enabled = 1;
5173
5174 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
5175 if (system(get_ipaddr_cmd) == 0) {
5176 //startGBW();
5177 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5178 for (i=0;i<len;i++) {
5179 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5180 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5181 startGBW();
5182 }
5183 free(ap);
5184 free(list);
5185 }
5186 }
5187 return 0;
5188}
5189
5190int disableGBW() {
5191 stopGBW();
5192 free(g_gbw_mac);
5193 g_gbw_mac = NULL;
5194 g_gbw_enabled = 1;
5195 return 0;
5196}
5197
5198static int startGBW() {
5199 if (g_gbw_watcher_pid != 0) {
5200 stopGBW();
5201 }
5202 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5203}
5204
5205static int stopGBW() {
5206 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005207 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005208 pthread_cancel(g_gbw_watcher_pid);
5209 pthread_join(g_gbw_watcher_pid, &retval);
5210 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005211 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5212 if (s_ap_iterface_name[0] != '\0')
5213 {
5214 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5215 system(cmd);
5216 }
you.chen0f5c6432022-11-07 18:31:14 +08005217}
5218//you.chen add for tv-box end