blob: c7313885feeb86865c4210176ea62a12f23a0ae3 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen35020192022-05-06 11:30:57 +080057
qs.xiongfcc914b2023-07-06 21:16:20 +080058pthread_t g_sta_auto_watcher_pid = 0;
59volatile int g_sta_auto_watcher_stop_flag = 0;
60volatile int g_sta_auto_scan_finish_flag = 1;
61volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080062void * g_ap_callback_priv = NULL;
63AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
64void * g_sta_callback_priv = NULL;
65STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080066void * g_sta_auto_callback_priv = NULL;
67STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080068
69//const char * CTRL_PATH="/var/run/wpa_supplicant";
70const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
71//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
72const char * cmd_list_networks = "LIST_NETWORKS";
73const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080074const char * cmd_disconnect = "DISCONNECT";
75const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080076const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080077const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080078const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080079const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080080
you.chenf711c8a2023-04-13 13:49:45 +080081const char * cmd_ping = "PING";
82const char * rsp_pong = "PONG";
83const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
84const int MAX_IDLE_COUNT = 600; // 60s
85
you.chenc9928582023-04-24 15:39:37 +080086const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
87const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
88const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
89const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
90const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
91
92static char s_ap_iterface_name[64] = {0};
93
you.chend2fef3f2023-02-13 10:50:35 +080094struct local_wpa_ctrl{
95 struct wpa_ctrl *ctrl;
96 pthread_mutex_t mutex;
97};
98
qs.xiongb37f8c42023-09-13 21:21:58 +080099int g_history_disconnect_valid_num = 0;
100int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800101
you.chend2fef3f2023-02-13 10:50:35 +0800102static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800103static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
104static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800105// add for auto connect
106static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800107
108static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800109
you.chen0f5c6432022-11-07 18:31:14 +0800110//you.chen add for tv-box start
111volatile int g_gbw_enabled = 0;
112char * g_gbw_mac = NULL;
113pthread_t g_gbw_watcher_pid = 0;
114static int startGBW();
115static int stopGBW();
116//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800117
118typedef struct __curr_status_info {
119 ap_info_s *ap;
120 char * state;
121 int net_no;
122}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400123
you.chen70f377f2023-04-14 18:17:09 +0800124typedef enum {
125 INNER_STA_STATUS_INIT = 0,
126 INNER_STA_STATUS_CONNECTING,
127 INNER_STA_STATUS_ASSOCIATING,
128 INNER_STA_STATUS_ASSOCIATED,
129 INNER_STA_STATUS_CONNECTED,
130 INNER_STA_STATUS_DISCONNECTING,
131 INNER_STA_STATUS_DISCONNECTED,
132 INNER_STA_STATUS_CANCEL,
133}inner_sta_status_s;
134
135static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
136static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800137volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800138static error_number_s s_sta_error_number = -1;
139static char s_sta_current_connecting_ssid[64] = {0};
140static struct timespec s_sta_connect_timeout;
141const int MAX_CONNNECT_TIME = 15; // second
142pthread_t g_global_watcher_pid = 0;
143static int s_service_invoke_timeout_cnt=0;
144const int FAKE_MAX_INT_VALUE = 99999;
145
146static void notify_service_invoke_fail(int error)
147{
148 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
149 pthread_mutex_lock(&s_global_check_mutex);
150 if (error == -2) //timeout
151 {
152 s_service_invoke_timeout_cnt++;
153 if (s_service_invoke_timeout_cnt > 10)
154 {
155 pthread_cond_signal(&s_global_check_cond);
156 }
157 }
158 else if (error == -1)
159 {
160 // check if can connect wpa service
161 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
162 if (lynq_wpa_ctrl == NULL)
163 {
164 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
165 pthread_cond_signal(&s_global_check_cond);
166 }
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
169 if (lynq_wpa_ctrl == NULL)
170 {
171 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
172 pthread_cond_signal(&s_global_check_cond);
173 }
174 wpa_ctrl_close(lynq_wpa_ctrl);
175 }
176
177 pthread_mutex_unlock(&s_global_check_mutex);
178}
179
you.chenc9928582023-04-24 15:39:37 +0800180static int system_call_v(const char * fmt, ...)
181{
182 char str_cmd[256] = {0};
183 va_list args;
184 va_start(args, fmt);
185 vsprintf(str_cmd, fmt, args);
186 va_end(args);
187 printf("system call----------%s\n", str_cmd);
188 return system(str_cmd);
189}
190
you.chen0df3e7e2023-05-10 15:56:26 +0800191static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
192
193static const char * inner_get_ap_interface_name()
194{
195 char * p;
196 char cmd[128]={0};
197
198 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
199 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
200 {
201 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
202 return NULL;
203 }
204 p = strchr(s_ap_iterface_name, ' ');
205 if (NULL != p)
206 {
207 *p = '\0';
208 }
209 p = strchr(s_ap_iterface_name, '\n');
210 if (NULL != p)
211 {
212 *p = '\0';
213 }
214 if (s_ap_iterface_name[0] == '\0')
215 {
216 return NULL;
217 }
218
219 return s_ap_iterface_name;
220}
221
you.chen70f377f2023-04-14 18:17:09 +0800222static void check_tether_and_notify()
223{
224 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800225 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 +0800226 {
227 return;
228 }
229 pthread_mutex_lock(&s_global_check_mutex);
230 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
231 pthread_cond_signal(&s_global_check_cond);
232 pthread_mutex_unlock(&s_global_check_mutex);
233}
234
you.chend2fef3f2023-02-13 10:50:35 +0800235static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
236 char *reply, size_t *reply_len,
237 void (*msg_cb)(char *msg, size_t len))
238{
239 int ret;
240 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800241 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800242 return -1;
243 }
244 pthread_mutex_lock(&ctrl->mutex);
245 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
246 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800247 if (ret != 0)
248 {
249 notify_service_invoke_fail(ret);
250 }
you.chend2fef3f2023-02-13 10:50:35 +0800251 return ret;
252}
253
254static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
255 int repeat_cnt;
256 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
257 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800258 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800259 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
260 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
261// printf("wait enable finish\n");
262 usleep(500 * 1000);
263 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
264 }
265 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800266 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800267 goto out_addr;
268 }
269 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
270 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
271 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800272 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800273 goto out_addr;
274 }
275 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
276 }
277 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
278out_addr:
279 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
280 return lynq_wpa_ctrl;
281}
282
qs.xiong97fa59b2022-04-07 05:41:29 -0400283#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400284{\
you.chen35020192022-05-06 11:30:57 +0800285 perror((str));\
286 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400287}
288
you.chen35020192022-05-06 11:30:57 +0800289#define CHECK_IDX(idx, type) do { \
290 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
291 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800292 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800293 return -1; \
294 } \
295 }while (0)
296
297#define CHECK_WPA_CTRL(index) int ret = 0;\
298 size_t reply_len = MAX_RET; \
299 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800300 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800301 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800302 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
303 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800304 }while(0)
305
306#define DO_REQUEST(cmd_str) do { \
307 reply_len = MAX_RET;\
308 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800310 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 +0800311 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800312 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800313 return ret; \
314 } \
315 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800316 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800317 }while(0)
318
319#define DO_OK_FAIL_REQUEST(cmd_str) do { \
320 DO_REQUEST(cmd_str); \
321 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800322 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800323 return -1; \
324 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800325 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800326 return -1; \
327 } \
328 }while (0)
329
330
you.chenf711c8a2023-04-13 13:49:45 +0800331static int check_connection(struct wpa_ctrl * wpa_ctrl)
332{
333 size_t reply_len = MAX_RET;
334 char cmd_reply[MAX_RET]={0};
335 int ret;
336
337 RLOGD("check_connection [%p]", wpa_ctrl);
338 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
339
340 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
341 {
342 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 +0800343 if (ret != 0)
344 {
345 notify_service_invoke_fail(ret);
346 }
you.chenf711c8a2023-04-13 13:49:45 +0800347 return -1;
348 }
349
350 return 0;
351}
352
353/**
354 * @brief check_pending_msg
355 * @param lynq_wpa_ctrl
356 * @return 1 has msg, 0 no msg, -1 error
357 */
358static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
359{
360 int ret;
361
362 if (*pp_lynq_wpa_ctrl == NULL) // need connect
363 {
364 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
365 if (*pp_lynq_wpa_ctrl == NULL)
366 {
367 usleep(SLEEP_TIME_ON_IDLE);
368 return -1;
369 }
370
371 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
372 if (ret == 0) // attach success
373 {
374 *started_flag = 1;
375 }
376 else
377 {
you.chen70f377f2023-04-14 18:17:09 +0800378 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800379 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
380 *pp_lynq_wpa_ctrl = NULL;
381 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800382 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800383 usleep(SLEEP_TIME_ON_IDLE);
384 return -1;
385 }
386 }
387
388 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
389 if ( ret == 0) // no pending messages
390 {
391 usleep(SLEEP_TIME_ON_IDLE);
392 *idle_count += 1;
393 if (*idle_count > MAX_IDLE_COUNT)
394 {
395 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
396 {
397 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
398 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
399 *pp_lynq_wpa_ctrl = NULL;
400 *idle_count = 0;
401 return -1;
402 }
403 *idle_count = 0;
404 }
405 return 0;
406 }
407 else if ( ret == -1) // on error
408 {
409 RLOGE("[wifi error]sta wpa_ctrl_pending");
410 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
411 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
412 *pp_lynq_wpa_ctrl = NULL;
413 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800414 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800415 return -1;
416 }
417
418 *idle_count = 0;
419 return 1;
420}
421
you.chen6d247052023-06-01 16:39:54 +0800422static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
423{
424 pthread_mutex_lock(&s_ap_callback_mutex);
425 if (g_ap_callback_func != NULL)
426 g_ap_callback_func(g_ap_callback_priv, status);
427 pthread_mutex_unlock(&s_ap_callback_mutex);
428}
429
you.chen35020192022-05-06 11:30:57 +0800430static void APWatcherThreadProc() {
431 size_t len = MAX_RET;
432 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800433 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800434
you.chen6c2dd9c2022-05-16 17:55:28 +0800435 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800436 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800437
qs.xiong9fbf74e2023-03-28 13:38:22 +0800438 while (g_ap_watcher_stop_flag == 0)
439 {
you.chenf711c8a2023-04-13 13:49:45 +0800440 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
441 {
you.chen70f377f2023-04-14 18:17:09 +0800442 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
443 {
444 check_tether_and_notify();
445 }
446
you.chen35020192022-05-06 11:30:57 +0800447 continue;
448 }
you.chenf711c8a2023-04-13 13:49:45 +0800449
you.chen6c2dd9c2022-05-16 17:55:28 +0800450 memset(msg_notify, 0, MAX_RET);
451 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800452 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800453 {
you.chen35020192022-05-06 11:30:57 +0800454 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800455 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800456 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800457 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800458 {
you.chen6d247052023-06-01 16:39:54 +0800459 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800460 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800461 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800462 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
463 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800464 {
you.chen0f5c6432022-11-07 18:31:14 +0800465 stopGBW();
466 }
467 }
you.chen35020192022-05-06 11:30:57 +0800468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800469 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800470 {
you.chen6d247052023-06-01 16:39:54 +0800471 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800472 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800473 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800474 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
475 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800476 {
you.chen0f5c6432022-11-07 18:31:14 +0800477 startGBW();
478 }
479 }
you.chen35020192022-05-06 11:30:57 +0800480 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800481 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800482 {
483 RLOGD("APWatcherThreadProc ap------> service error");
484 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
485 }
486 else
487 {
488 RLOGD("APWatcherThreadProc ap------> going on check next msg");
489 }
you.chenf711c8a2023-04-13 13:49:45 +0800490 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800491 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
492 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800493 if (lynq_wpa_ctrl != NULL)
494 {
you.chen92fd5d32022-05-25 10:09:47 +0800495 wpa_ctrl_detach(lynq_wpa_ctrl);
496 wpa_ctrl_close(lynq_wpa_ctrl);
497 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400498}
499
you.chen70f377f2023-04-14 18:17:09 +0800500static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
501{
502 char * p;
503 const char * try_associat_flag = "Trying to associate";
504 const char * associated_flag = "Associated with ";
505
506 pthread_mutex_lock(&s_global_check_mutex);
507 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
508 {
509 pthread_mutex_unlock(&s_global_check_mutex);
510 return;
511 }
512
you.chen6e724162023-10-19 19:10:01 +0800513 // youchen@2023-10-17 add for "not notify connect fail directly" begin
514 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
515 {
516 s_sta_error_number = error_num;
517 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
518 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
519 pthread_cond_signal(&s_global_check_cond);
520 pthread_mutex_unlock(&s_global_check_mutex);
521 return;
522 }
523 // youchen@2023-10-17 add for "not notify connect fail directly" end
524
you.chen70f377f2023-04-14 18:17:09 +0800525 if (state == LYNQ_WIFI_STATUS_EGNORE)
526 {
527 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
528 {
529 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
530 }
531 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
532 {
533 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
534 }
535 }
536 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
537 {
538 s_sta_error_number = error_num;
539 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
540 {
541 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800542 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 +0800543 pthread_cond_signal(&s_global_check_cond);
544 }
545 }
546 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
547 {
548 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800549 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 +0800550 pthread_cond_signal(&s_global_check_cond);
551 }
552 pthread_mutex_unlock(&s_global_check_mutex);
553}
554
qs.xiongb37f8c42023-09-13 21:21:58 +0800555static int lynq_split(char * str, int len, char delimiter, char * results[]);
556static inline char inner_convert_char(char in);
557static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
558static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
559
560
561
qs.xiong44fac672023-08-29 16:15:55 +0800562/*
563just tmp add for fix sta connect ap fail check ap connect info
564return 0 --->Current no sta device connect this AP
565*/
566static int lynq_connected_ap_sta_status() {
567
568 FILE *fp;
569 size_t i = 0;
570 int ret;
571 char lynq_cmd_ret[MAX_RET]={0};
572
573 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
574 {
575 perror("popen error!");
576 return -1;
577 }
578 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
579 {
580 perror("fread fail!");
581 ret=pclose(fp);
582 if(ret == -1)
583 perror("close file faild");
584 return -1;
585 }
586 if( strlen(lynq_cmd_ret) < MAC_LEN)
587 {
588 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
589 ret=pclose(fp);
590 if(ret==-1)
591 {
592 perror("close file faild");
593 }
594 return 0;
595 }else{
596 ret=pclose(fp);
597 if(ret==-1)
598 {
599 perror("close file faild");
600 }
601 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
602 return 1;
603 }
604}
605
606/*
607 just tmp add for fix sta connect ap fail; check fw status
608 return 1 ----> fw status error; need wl down/up
609*/
610static int check_current_fw_status() {
611
612 FILE *fp;
613 FILE *fp1;
614 size_t i = 0;
615 int ret;
616 char lynq_cmd_ret_2g[MAX_RET]={0};
617 char lynq_cmd_ret_5g[MAX_RET]={0};
618
619 const char * fw_status = "0x0096"; //0x0096 is normal fw status
620
621 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
622 {
623 perror("popen error!");
624 return -1;
625 }
626 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
627 {
628 perror("fread fail!");
629 if(pclose(fp) == -1)
630 perror("close fp file faild");
631 return -1;
632 }
633
634 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
635 {
636 perror("popen error!");
637 if(pclose(fp) == -1)
638 perror("clsoe fp file faild");
639 return -1;
640 }
641 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
642 {
643 perror("fread fail!");
644 if(pclose(fp1) == -1)
645 perror("clsoe fp1 file faild");
646 if(pclose(fp) == -1)
647 perror("clsoe fp file faild");
648 return -1;
649 }
650
651 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
652 {
653 ret=pclose(fp);
654 if(ret==-1)
655 {
656 perror("close fp file faild");
657 }
658 ret=pclose(fp1);
659 if(ret==-1)
660 {
661 perror("close fp1 file faild");
662 }
663 return 0;
664 }else
665 {
666 ret=pclose(fp);
667 if(ret==-1)
668 {
669 perror("close file faild");
670 }
671 if(pclose(fp1) == -1)
672 {
673 perror("clsoe file fp1 faild");
674 }
675 RLOGD("current fw status --error--");
676 return 1;
677 }
678}
679
qs.xiong1d4263a2023-09-06 10:46:23 +0800680/*
681eg: wl counters info
682sh-3.2# wl counters
683counters_version 30
684datalen 1648
685Slice_index: 0
686reinitreason_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)
687reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
688
689check reinit status
690return 0 ===> fw did wl reinit cmd
691*/
692static int check_current_reinit_info()
693{
694 FILE *fp;
695 int ret;
696 char lynq_cmd_ret[MAX_RET]={0};
697 char * dest;
698 char destid[3]={0};
699 if((fp=popen("wl counters","r"))==NULL)
700 {
701 perror("popen error!");
702 return -1;
703 }
704 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
705 {
706 perror("fread fail!");
707 if(pclose(fp) == -1)
708 {
709 perror("close fp file faild");
710 }
711 return -1;
712 }
713 dest = strstr(lynq_cmd_ret,"reinit ");
714 if(dest != NULL)
715 {
716 dest +=strlen("reinit ");
717 RLOGD("current get dest str is %s",dest);
718 memcpy(destid,dest,2);
719 ret = atoi(destid);
720 RLOGD("get current wl counters cmd return counts is %d",ret);
721 if( ret != 0 )
722 {
723 RLOGD("current fw did run cmd wl reinit");
724 if( pclose(fp) == -1 )
725 {
726 perror("close fp file faild");
727 }
728 return 0;
729 }
730 }
731 if( pclose(fp) == -1 )
732 {
733 perror("close fp file faild");
734 }
735 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
736 return -1;
737}
738
qs.xiong44fac672023-08-29 16:15:55 +0800739static void APTmpWatcherThreadProc() {
740
741 int i = 0;
742 int delytime = 300;
743 g_ap_tmp_watcher_stop_flag = 0;
744
745 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
746 while(1)
747 {
748 sleep(1);
749 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800750 if ( (i % 30) == 0 )
751 {
752 if ( check_current_reinit_info() == 0 )
753 {
754 system("wl reset_cnts");
755 system("wl down");
756 system("wl up");
757 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
758 }
759 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800760 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800761 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800762 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800763 {
764 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
765 {
766 system("wl down");
767 system("wl up");
768 }
qs.xiong44fac672023-08-29 16:15:55 +0800769 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800770 }
771 if ( i == delytime )
772 {
qs.xiong44fac672023-08-29 16:15:55 +0800773 i = 0;
774 }
775 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
776 {
777 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
778 return;
779 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800780
qs.xiong44fac672023-08-29 16:15:55 +0800781 }
782
783}
784
qs.xiongf0128b12023-06-29 17:29:39 +0800785static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
786{
787 char LYNQ_DISABLE_CMD[128]={0};
788
789 CHECK_IDX(idx, CTRL_STA);
790 CHECK_WPA_CTRL(CTRL_STA);
791
792 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800793 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800794 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800795
qs.xiongf0128b12023-06-29 17:29:39 +0800796 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800797
qs.xiongf0128b12023-06-29 17:29:39 +0800798}
799
qs.xiongb37f8c42023-09-13 21:21:58 +0800800static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
801{
802 char LYNQ_ENABLE_CMD[128]={0};
803
804 CHECK_IDX(idx, CTRL_STA);
805 CHECK_WPA_CTRL(CTRL_STA);
806
807
808 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800809 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800810 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
811
812 return 0;
813
814}
815
816static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
817{
818
819 int index,networkid;
820
821 for ( index = 0; index < len ;index++)
822 {
823 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800824 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800825 {
826 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
827 }
828 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
829 }
830 return 0;
831
832}
833
834
835/*
836 dis_net_list user disconnect list
837*/
838static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
839{
840 int count,ncount,index;
841 int flag = 0;
842 int merge_index = 0;
843 int net_no_list[128];
844
845 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
846 for( count = 0; count < index; count++)
847 {
848 for(ncount = 0; ncount < valid_num; ncount++)
849 {
850 if(net_no_list[count] == dis_net_list[ncount])
851 {
852 RLOGD("[wifi]this is history disconnect idx ----> ",net_no_list[count]);
853 flag = 1;
854 break;
855 }
856 }
857 if( flag != 1 )
858 {
859 out[merge_index] = net_no_list[count];
860 merge_index ++;
861 }
862 flag = 0;
863 }
864 * outlen =merge_index;
865 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
866 return;
867}
qs.xiongf0128b12023-06-29 17:29:39 +0800868
qs.xiong455c30b2023-04-12 11:40:02 +0800869void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
870{
871 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800872 char *wpanetid;
873 char destid[3] = {0};
874 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800875 *error = LYNQ_WAIT_CONNECT_ACTIVE;
876 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
877 {
878 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
879 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
880 return;
881 }
882
qs.xiong20202422023-09-06 18:01:18 +0800883 if (strstr(modify, "Trying to associate") != NULL)
884 {
885 RLOGD("Current sta is Trying to associate");
886 *state = LYNQ_WIFI_STATUS_EGNORE;
887 g_sta_conncet_status_flag = 1;
888 return;
889 }
890
qs.xiong455c30b2023-04-12 11:40:02 +0800891 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
892 {
893 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
894 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800895 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800896 return;
897 }
898
qs.xiong1e81dfa2023-09-27 15:52:37 +0800899 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
900 {
901 *error = LYNQ_PSW_ERROR;
902 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
903 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
904 g_sta_conncet_status_flag = 0;
905 return;
906 }
qs.xiong455c30b2023-04-12 11:40:02 +0800907 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
908 {
qs.xiongf0128b12023-06-29 17:29:39 +0800909 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
910 wpanetid = strstr(modify,"id=");
911 if ( wpanetid != NULL )
912 {
913 wpanetid +=strlen("id=");
914 memcpy(destid,wpanetid,2);
915 tmpdisid = atoi(destid);
916
917 }
qs.xiong455c30b2023-04-12 11:40:02 +0800918 pReason = strstr(modify, "reason=");
919 if (pReason != NULL)
920 {
921 pReason += strlen("reason=");
922 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
923 {
924 *error = LYNQ_TIME_OUT;
925 }
926 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
927 {
928 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +0800929 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +0800930 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +0800931 // you.chen@2023-10-17 only disable network during autoconnect
932 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 +0800933 {
934 RLOGE("stop wlan0 network %d fail",tmpdisid);
935 }
qs.xiong455c30b2023-04-12 11:40:02 +0800936 }
937 else
938 {
939 *error = LYNQ_UNSPECIFIED_REASON;
940 }
qs.xiong455c30b2023-04-12 11:40:02 +0800941 }
942 else
943 {
944 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800945 }
qs.xiongf0128b12023-06-29 17:29:39 +0800946 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800947 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800948 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800949
950 }
951
952 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
953 {
954 *error = LYNQ_NOT_FIND_AP;
955 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
956 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800957 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800958 return;
959 }
960
961
962 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
963 {
964 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
965 pReason = strstr(modify, "status_code=");
966 if (pReason != NULL)
967 {
968 pReason += strlen("status_code=");
969 if (memcmp(pReason, "17", 2) == 0)
970 {
971 *error = LYNQ_AP_UNABLE_HANDLE;
972 }
973 else if (memcmp(pReason, "1",1) == 0)
974 {
975 *error = LYNQ_UNSPECIFIED_REASON;
976 }
977 else
978 {
979 *error = LYNQ_UNSPECIFIED_REASON;
980 }
981
982 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
983 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 +0800984 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800985 return;
986 }
987 else
988 {
989 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
990 *error = LYNQ_UNSPECIFIED_REASON;
991 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
992 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
993 return;
994 }
995 }
996
997 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
998 {
999 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1000 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1001 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1002 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1003 return;
1004 }
1005
1006 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1007 {
1008 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1009 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1010 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1011 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1012 return;
1013 }
1014
qs.xiongdf637b22023-10-26 19:30:07 +08001015 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed
1016 if (strstr(modify, "Association request to the driver failed") != NULL)
1017 {
1018 RLOGD("Association request to the driver failed --- recover");
1019 system("wl down");
1020 system("wl up");
1021 *error = LYNQ_UNSPECIFIED_REASON;
1022 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1023 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1024 return;
1025 }
1026 // add by qs.xiong 20231026 tmp fix sta association request to the driver failed end
1027
1028
you.chen32cb31e2023-04-13 14:05:45 +08001029 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001030 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001031 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001032 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1033 return;
1034
1035}
1036
qs.xiongfcc914b2023-07-06 21:16:20 +08001037void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1038{
1039 char *pReason;
1040 char *wpanetid;
1041 char destid[3];
1042 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1043 *networkid = -1;
1044 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1045 {
1046 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1047 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1048 return;
1049 }
1050 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1051 {
1052 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1053 wpanetid = strstr(modify,"id=");
1054 if ( wpanetid != NULL )
1055 {
1056 wpanetid +=strlen("id=");
1057 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1058 if (memcpy(destid,wpanetid,2) != NULL)
1059 {
1060 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1061 *networkid = atoi(destid);
1062 RLOGD("get networkid is %d\n",*networkid);
1063 }
1064 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1065 }
1066 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1067 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1068 return;
1069 }
1070 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1071 {
1072 wpanetid = strstr(modify,"id=");
1073 if ( wpanetid != NULL )
1074 {
1075 wpanetid +=strlen("id=");
1076 if (memcpy(destid,wpanetid,2) != NULL)
1077 {
1078 *networkid = atoi(destid);
1079 RLOGD("get networkid is %d\n",*networkid);
1080 }
1081 }
1082 pReason = strstr(modify, "reason=");
1083 if (pReason != NULL)
1084 {
1085 pReason += strlen("reason=");
1086 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1087 {
1088 *error = LYNQ_TIME_OUT;
1089 }
1090 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1091 {
1092 *error = LYNQ_PSW_ERROR;
1093 }
1094 else
1095 {
1096 *error = LYNQ_UNSPECIFIED_REASON;
1097 }
1098 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1099 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1100 return;
1101 }
1102 else
1103 {
1104 *error = LYNQ_UNSPECIFIED_REASON;
1105 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1106 return;
1107 }
1108 }
1109 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1110 {
1111 wpanetid = strstr(modify,"id=");
1112 if ( wpanetid != NULL )
1113 {
1114 wpanetid +=strlen("id=");
1115 if (memcpy(destid,wpanetid,2) != NULL)
1116 {
1117 *networkid = atoi(destid);
1118 RLOGD("get networkid is %d\n",*networkid);
1119 }
1120 }
1121 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1122 pReason = strstr(modify, "status_code=");
1123 if (pReason != NULL)
1124 {
1125 pReason += strlen("status_code=");
1126 if (memcmp(pReason, "17", 2) == 0)
1127 {
1128 *error = LYNQ_AP_UNABLE_HANDLE;
1129 }
1130 else if (memcmp(pReason, "1",1) == 0)
1131 {
1132 *error = LYNQ_UNSPECIFIED_REASON;
1133 }
1134 else
1135 {
1136 *error = LYNQ_UNSPECIFIED_REASON;
1137 }
1138 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1139 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1140 return;
1141 }
1142 else
1143 {
1144 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1145 *error = LYNQ_UNSPECIFIED_REASON;
1146 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001147 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001148 return;
1149 }
1150 }
1151 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1152 {
1153 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1154 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1155 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1156 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1157 return;
1158 }
1159 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1160 {
1161 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1162 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1163 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1164 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1165 return;
1166 }
1167 RLOGD("EVENT : %s\n", modify);
1168 *error = LYNQ_UNSPECIFIED_REASON;
1169 *state = LYNQ_WIFI_STATUS_EGNORE;
1170 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1171 return;
1172}
you.chen70f377f2023-04-14 18:17:09 +08001173static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1174{
you.chen6d247052023-06-01 16:39:54 +08001175 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001176 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1177 {
1178 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1179 g_sta_callback_func(g_sta_callback_priv, state, error);
1180 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1181 }
you.chen6d247052023-06-01 16:39:54 +08001182 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001183}
qs.xiongfcc914b2023-07-06 21:16:20 +08001184static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1185{
1186 pthread_mutex_lock(&s_sta_callback_mutex);
1187 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1188 {
1189 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1190 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1191 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1192 }
1193 pthread_mutex_unlock(&s_sta_callback_mutex);
1194}
you.chen70f377f2023-04-14 18:17:09 +08001195
you.chen35020192022-05-06 11:30:57 +08001196static void STAWatcherThreadProc() {
1197 size_t len = MAX_RET;
1198 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001199 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001200 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001201 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001202
you.chen6c2dd9c2022-05-16 17:55:28 +08001203 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001204 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001205
you.chen70f377f2023-04-14 18:17:09 +08001206 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001207 while (g_sta_watcher_stop_flag == 0)
1208 {
you.chenf711c8a2023-04-13 13:49:45 +08001209 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001210 {
you.chen35020192022-05-06 11:30:57 +08001211 continue;
1212 }
you.chenf711c8a2023-04-13 13:49:45 +08001213
you.chen6c2dd9c2022-05-16 17:55:28 +08001214 memset(msg_notify, 0, MAX_RET);
1215 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001216 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001217 {
you.chen35020192022-05-06 11:30:57 +08001218 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001219 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1220 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001221 {
you.chen35020192022-05-06 11:30:57 +08001222 g_sta_scan_finish_flag = 1;
1223 }
1224
qs.xiong9fbf74e2023-03-28 13:38:22 +08001225 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001226 {
you.chen35020192022-05-06 11:30:57 +08001227 continue;
1228 }
qs.xiong455c30b2023-04-12 11:40:02 +08001229 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001230 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1231 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1232 {
1233 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1234 }
1235 else
1236 {
1237 notify_connect_status(state, error);
1238 }
1239 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001240
1241 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001242 {
you.chen70f377f2023-04-14 18:17:09 +08001243 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001244 if (last_state != state)
1245 {
1246 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1247 {
1248 system_call_v("%s %s", sta_status_change_script, "connect");
1249 }
1250 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1251 {
1252 system_call_v("%s %s", sta_status_change_script, "disconnect");
1253 }
1254 }
you.chenc9928582023-04-24 15:39:37 +08001255 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001256 }
you.chen35020192022-05-06 11:30:57 +08001257 }
1258 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001259 if (lynq_wpa_ctrl != NULL)
1260 {
you.chen92fd5d32022-05-25 10:09:47 +08001261 wpa_ctrl_detach(lynq_wpa_ctrl);
1262 wpa_ctrl_close(lynq_wpa_ctrl);
1263 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001264}
qs.xiongfcc914b2023-07-06 21:16:20 +08001265static void STAAutoWatcherThreadProc() {
1266 size_t len = MAX_RET;
1267 char msg_notify[MAX_RET];
1268 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001269 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001270 int idle_count = 0;
1271 int networkid;
1272 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1273 g_sta_auto_watcher_stop_flag = 0;
1274 RLOGD("STAAutoWatcherThreadProc thread started ------");
1275 while (g_sta_auto_watcher_stop_flag == 0)
1276 {
1277 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1278 {
1279 continue;
1280 }
1281 memset(msg_notify, 0, MAX_RET);
1282 len = MAX_RET;
1283 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1284 {
1285 msg_notify[len+1] = '\0';
1286 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1287 if (strstr(msg_notify, state_scan_result) != NULL)
1288 {
1289 g_sta_auto_scan_finish_flag = 1;
1290 }
1291 if (g_sta_auto_callback_func == NULL)
1292 {
1293 continue;
1294 }
1295 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1296 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001297 }
1298 }
1299 if (lynq_wpa_ctrl != NULL)
1300 {
1301 wpa_ctrl_detach(lynq_wpa_ctrl);
1302 wpa_ctrl_close(lynq_wpa_ctrl);
1303 }
1304}
qs.xiongf1b525b2022-03-31 00:58:23 -04001305
you.chen70f377f2023-04-14 18:17:09 +08001306// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1307static void GlobalWatcherThreadProc()
1308{
1309 int ret, connect_timeout, service_abnormal;
1310 error_number_s error_num = -1;
1311 inner_sta_status_s sta_status;
1312 scan_info_s *scan_list = NULL;
1313 int i, scan_len=0;
1314 char connecting_ssid[64];
1315 struct timeval now;
1316
1317 RLOGD("GlobalWatcherThreadProc start to run");
1318
1319 while (1)
1320 {
1321 pthread_mutex_lock(&s_global_check_mutex);
1322 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1323 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1324 {
1325 pthread_mutex_unlock(&s_global_check_mutex);
1326 usleep(50*1000);
1327 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1328 continue;
1329 }
1330
1331 connect_timeout = 0;
1332 service_abnormal = 0;
1333 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1334 {
1335 while (1)
1336 {
1337 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1338 if (ret == ETIME)
1339 {
1340 connect_timeout = 1;
1341 }
1342 else if (ret != 0)
1343 {
1344 gettimeofday(&now,NULL);
1345 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1346 {
1347 usleep(SLEEP_TIME_ON_IDLE);
1348 continue;
1349 }
1350 connect_timeout = 1;
1351 }
1352 sta_status = s_sta_status;
1353 error_num = s_sta_error_number;
1354 s_sta_status = INNER_STA_STATUS_INIT;
1355 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1356 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1357 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1358 break;
1359 }
1360 }
1361 if (s_service_invoke_timeout_cnt > 10)
1362 {
1363 service_abnormal = 1;
1364 s_service_invoke_timeout_cnt = 0;
1365 }
1366 pthread_mutex_unlock(&s_global_check_mutex);
1367
1368 if (service_abnormal == 1)
1369 {
1370 sleep(1);
1371 RLOGE("wpa service is abnormal info app to exit");
1372 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001373
1374 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1375
you.chen70f377f2023-04-14 18:17:09 +08001376 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1377 }
1378
1379 if (sta_status == INNER_STA_STATUS_CANCEL)
1380 {
1381 continue;
1382 }
1383 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1384 {
1385 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1386 }
1387 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1388 {
1389 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1390 {
1391 for(i=0; i < scan_len;i++)
1392 {
1393 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1394 {
1395 error_num = LYNQ_AUTH_ERROR;
1396 break;
1397 }
1398 }
1399 free(scan_list);
1400 }
1401 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1402 }
1403 else if (connect_timeout == 0)
1404 {
1405 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1406 }
1407 else // wait timeout
1408 {
1409 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1410 {
1411 ; // wpa service abnormal
1412 }
1413 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1414 {
1415 RLOGD("GlobalWatcherThreadProc notify connected");
1416 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1417 }
1418 else
1419 {
1420 RLOGD("GlobalWatcherThreadProc notify timeout");
1421 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1422 }
1423 }
1424 } // while (1)
1425}
1426
qs.xiong1af5daf2022-03-14 09:12:12 -04001427int lynq_wifi_enable(void)
1428{
you.chen35020192022-05-06 11:30:57 +08001429 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001430 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001431 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001432 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1433
qs.xiong9fbf74e2023-03-28 13:38:22 +08001434 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1435 {
you.chend2fef3f2023-02-13 10:50:35 +08001436 goto out_enable;
1437 }
1438
you.chenc9928582023-04-24 15:39:37 +08001439 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001440 if (ret != 0)
1441 {
1442 //printf("service state %d\n", ret);
1443 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001444 ret = -1;
1445 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001446 }
lhfe8da902022-10-11 18:55:36 +08001447
you.chen70f377f2023-04-14 18:17:09 +08001448 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1449 {
1450 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1451 if(ret<0)
1452 {
1453 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1454 ret = -1;
1455 goto out_enable;
1456 }
1457 }
1458
you.chend2fef3f2023-02-13 10:50:35 +08001459 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1460 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1461 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1462 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1463out_enable:
1464 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001465 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001466}
1467
qs.xiong1af5daf2022-03-14 09:12:12 -04001468int lynq_wifi_disable(void)
1469{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001470 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001471 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001472 g_ap_watcher_stop_flag = 1;
1473 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001474 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001475 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001476 if (g_ap_watcher_pid != 0)
1477 pthread_join(g_ap_watcher_pid, NULL);
1478 if (g_sta_watcher_pid != 0)
1479 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001480 if (g_sta_auto_watcher_pid != 0)
1481 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001482 if (g_lynq_wpa_ctrl[0] != NULL)
1483 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1484 if (g_lynq_wpa_ctrl[1] != NULL)
1485 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001486 if (g_ap_tmp_watcher_pid != 0)
1487 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001488 g_ap_watcher_pid = 0;
1489 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001490 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001491 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001492 g_lynq_wpa_ctrl[0] = NULL;
1493 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001494 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001495 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001496 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1497 return 0;
1498}
1499
1500static inline char inner_convert_char(char in)
1501{
1502 if (in >= '0' && in <= '9')
1503 {
1504 return in - '0';
1505 }
1506 else if (in >= 'a' && in <= 'f')
1507 {
1508 return in - 'a' + 10;
1509 }
1510 else if (in >= 'A' && in <= 'F')
1511 {
1512 return in - 'A' + 10;
1513 }
1514 else
1515 {
1516 return '\xff';
1517 }
1518}
1519
1520static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1521{
1522 char *p;
1523 size_t pos = 0;
1524 if (NULL == out_ssid)
1525 return;
1526 //printf("input ssid=[%s]\n", ssid);
1527 memset(out_ssid, 0, out_ssid_len);
1528 if (NULL == ssid)
1529 return;
1530 p = strchr(ssid, '\\');
1531 if (NULL == p)
1532 {
1533 strncpy(out_ssid, ssid, out_ssid_len);
1534 //printf(" first %s\n", out_ssid);
1535 }
1536 else
1537 {
1538 pos = p - ssid;
1539 memcpy(out_ssid, ssid, pos);
1540 //printf("pos %lu -- %s\n", pos, out_ssid);
1541 for(; pos < out_ssid_len; pos ++)
1542 {
1543 if (p[0] == '\0')
1544 {
1545 //printf(" out %s\n", out_ssid);
1546 return;
1547 }
1548 else if (p[0] != '\\')
1549 {
1550 out_ssid[pos] = p[0];
1551 p += 1;
1552 }
1553 else if (p[1] == 'x' || p[1] == 'X')
1554 {
1555 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1556 p += 4;
1557 }
1558 else if (p[1] == '\\')
1559 {
1560 out_ssid[pos] = '\\';
1561 p += 2;
1562 }
1563 else if (p[1] == 't')
1564 {
1565 out_ssid[pos] = '\t';
1566 p += 2;
1567 }
1568 else if (p[1] == 'r')
1569 {
1570 out_ssid[pos] = '\r';
1571 p += 2;
1572 }
1573 else if (p[1] == 'n')
1574 {
1575 out_ssid[pos] = '\n';
1576 p += 2;
1577 }//todo find a better way to convert?
1578 }
1579 }
1580 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001581}
qs.xiong1af5daf2022-03-14 09:12:12 -04001582
you.chen35020192022-05-06 11:30:57 +08001583static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001584 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001585 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001586 RLOGD("enter inner_get_param");
1587 if (out_put == NULL)
1588 {
1589 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001590 return -1;
1591 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001592 if (param_name == NULL)
1593 {
1594 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001595 return -1;
1596 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001597 if (param_name[0] == '\0')
1598 {
1599 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001600 return -1;
1601 }
1602
1603 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1604
1605 CHECK_WPA_CTRL(interface);
1606
1607 DO_REQUEST(lynq_cmd_get);
1608
qs.xiong9fbf74e2023-03-28 13:38:22 +08001609 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1610 {
1611 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001612 return -1;
1613 }
1614
you.chena6fa5b22022-05-18 10:28:19 +08001615// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001616 if (strcmp(param_name, "ssid") == 0)
1617 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001618 if (cmd_reply[0] == '\"')
1619 {
you.chend2fef3f2023-02-13 10:50:35 +08001620 ssid_len = reply_len - 1;
1621 memcpy(out_put, cmd_reply + 1, ssid_len);
1622 if (out_put[ssid_len-1] == '\"')
1623 {
1624 out_put[ssid_len-1] = '\0';
1625 }
1626 else
1627 {
1628 out_put[ssid_len] = '\0';
1629 }
1630 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001631 else
1632 {
you.chend2fef3f2023-02-13 10:50:35 +08001633 ssid_len = reply_len / 2;
1634 for(i=0; i<ssid_len; i++)
1635 {
1636 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1637 }
1638 out_put[ssid_len] = '\0';
1639 }
1640 }
1641 else
1642 {
1643 memcpy(out_put, cmd_reply, reply_len + 1);
1644 }
you.chen35020192022-05-06 11:30:57 +08001645 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001646}
qs.xiong1af5daf2022-03-14 09:12:12 -04001647
you.chen35020192022-05-06 11:30:57 +08001648static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1649 int ret = 0;
1650 char * end = str + len - 1;
1651 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001652 while(str < end)
1653 {
1654 if (*str == delimiter)
1655 {
you.chen35020192022-05-06 11:30:57 +08001656 *str++ = '\0';
1657 results[ret++] = str;
1658 continue;
1659 }
1660 str++;
1661 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001662 if (*str == delimiter)
1663 {
you.chen35020192022-05-06 11:30:57 +08001664 *str = '\0';
1665 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001666
you.chen6ed36a62023-04-27 17:51:56 +08001667 results[ret] = NULL;
1668
you.chen35020192022-05-06 11:30:57 +08001669 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001670}
1671
you.chend2fef3f2023-02-13 10:50:35 +08001672static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1673{
1674 char * p;
1675 int ret = 0;
1676 char cmd[256]={0};
1677 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001678 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001679 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001680 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001681 ret = exec_cmd(cmd, ip, ip_len);
1682 p = strchr(ip, '\n');
1683 if (NULL != p)
1684 {
1685 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001686 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001687 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001688 return ret;
1689}
1690
you.chend2fef3f2023-02-13 10:50:35 +08001691static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001692 struct in_addr addr ={0};
1693 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001694 char cmd[64] = {0};
1695 char * p;
1696 int ret;
you.chen35020192022-05-06 11:30:57 +08001697
qs.xiong9fbf74e2023-03-28 13:38:22 +08001698 if (ip == NULL || *ip == '\0' || hostname == NULL)
1699 {
1700 RLOGE("ip == NULL or hostname == NULL");
1701 return -1;
you.chen35020192022-05-06 11:30:57 +08001702 }
1703
you.chend2fef3f2023-02-13 10:50:35 +08001704 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001705 if (inet_aton(ip, &addr) == 0)
1706 {
you.chen35020192022-05-06 11:30:57 +08001707 printf("---inet_aton fail\n");
1708 return -1;
1709 }
1710
1711 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1712
qs.xiong9fbf74e2023-03-28 13:38:22 +08001713 if (ht == NULL)
1714 {
you.chen186d3c32023-05-18 14:19:46 +08001715 hostname[0] = '\0';
1716 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1717 ret = exec_cmd(cmd, hostname, 32);
1718 if (ret == 0)
1719 {
1720 p = strchr(hostname, '\n');
1721 if (p != NULL)
1722 {
1723 *p = '\0';
1724 }
1725 return 0;
1726 }
1727 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001728 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001729 herror(NULL);
1730 return -1;
1731 }
1732
1733 strcpy(hostname, ht->h_name);
1734
1735 return 0;
1736}
1737
1738static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1739{
1740 int count, index, words_count;
1741 char * split_lines[128]= {0};
1742 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001743 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001744 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001745 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001746
1747 CHECK_WPA_CTRL(ap_sta);
1748
1749 DO_REQUEST(lynq_wifi_list_networks);
1750
1751 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1752
1753 //@todo check ssid field to compatible
1754
1755 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 for(index=1; index < count; index++)
1757 {
you.chen35020192022-05-06 11:30:57 +08001758 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001759 if (words_count > 2)
1760 {
you.chend2fef3f2023-02-13 10:50:35 +08001761 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001762 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1763 {
you.chen35020192022-05-06 11:30:57 +08001764 net_no_list[ret++] = atoi(split_words[0]);
1765 }
1766 }
1767 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001768 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001769 return ret;
1770}
1771
1772static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001773 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001774 CHECK_WPA_CTRL(ap_sta);
1775 const char *lynq_wifi_add_network = "ADD_NETWORK";
1776
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001778 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001779 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1780 {
1781 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001782 return -1;
1783 }
1784
qs.xiong9fbf74e2023-03-28 13:38:22 +08001785 for(i=0;i<reply_len;i++)
1786 {
1787 if(cmd_reply[i] == '\n')
1788 {
you.chen35020192022-05-06 11:30:57 +08001789 cmd_reply[i] = '\0';
1790 break;
1791 }
1792 }
1793 return atoi(cmd_reply);
1794}
you.chena6cd55a2022-05-08 12:20:18 +08001795
you.chen35020192022-05-06 11:30:57 +08001796static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1797{
1798 int count, index;
1799 int net_no_list[128];
1800
qs.xiong9fbf74e2023-03-28 13:38:22 +08001801 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001802 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001803 for (index=0; index < count; index++)
1804 {
1805 if (net_no_list[index] == net_no)
1806 {
you.chen35020192022-05-06 11:30:57 +08001807 return 0;
1808 }
1809 }
1810
1811 if (count >= 1)
1812 index = net_no_list[count - 1];
1813 else
1814 index = -1;
1815
qs.xiong9fbf74e2023-03-28 13:38:22 +08001816 while (index < net_no )
1817 {
you.chen35020192022-05-06 11:30:57 +08001818 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001819 if (index >= net_no)
1820 { // required network no created
1821 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001822 return 0;
1823 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001824 else if( index < 0)
1825 {
1826 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001827 return -1;
1828 }
you.chen35020192022-05-06 11:30:57 +08001829 }
1830
1831 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001832 {
1833 RLOGE("[lynq_check_network_number] network index < 0");
1834 return -1;
1835 }
1836 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001837 return 0;
1838}
1839
1840static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001841 if (freq > 5000 && freq < 6000)
1842 {
you.chen35020192022-05-06 11:30:57 +08001843 return LYNQ_WIFI_5G_band;
1844 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001845 else if (freq > 2000 && freq < 3000)
1846 {
you.chen35020192022-05-06 11:30:57 +08001847 return LYNQ_WIFI_2G_band;
1848 }
1849 return LYNQ_WIFI_2_and_5G_band;
1850}
1851
1852static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001853 if (key_mgmt != NULL)
1854 {
1855 if (memcmp( key_mgmt, "NONE", 4) == 0)
1856 {
you.chen35020192022-05-06 11:30:57 +08001857 return LYNQ_WIFI_AUTH_OPEN;
1858 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001859 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1860 {
you.chen35020192022-05-06 11:30:57 +08001861 return LYNQ_WIFI_AUTH_WEP;
1862 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001863 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1864 {
you.chen35020192022-05-06 11:30:57 +08001865 return LYNQ_WIFI_AUTH_WPA_PSK;
1866 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001867 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1868 {
you.chen35020192022-05-06 11:30:57 +08001869 return LYNQ_WIFI_AUTH_WPA2_PSK;
1870 }
1871 }
1872
1873 return -1;
1874}
1875
1876static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001877 if (flag != NULL)
1878 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001879 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 +08001880 {
1881 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001882 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001883 {
1884 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1885 }else if (strstr( flag, "WPA2-PSK") != NULL)
1886 {
you.chen35020192022-05-06 11:30:57 +08001887 return LYNQ_WIFI_AUTH_WPA2_PSK;
1888 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001889 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001890 {
you.chen35020192022-05-06 11:30:57 +08001891 return LYNQ_WIFI_AUTH_WPA_PSK;
1892 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001893 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001894 {
you.chen35020192022-05-06 11:30:57 +08001895 return LYNQ_WIFI_AUTH_WEP;
1896 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001897 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001898 {
you.chen35020192022-05-06 11:30:57 +08001899 return LYNQ_WIFI_AUTH_OPEN;
1900 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001901 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001902 {
you.chend2fef3f2023-02-13 10:50:35 +08001903 return LYNQ_WIFI_AUTH_OPEN;
1904 }
qs.xiong46f41562023-07-11 21:06:47 +08001905 else
1906 {
1907 RLOGD("convert_max_auth_from_flag not-found auth mode");
1908 }
you.chen35020192022-05-06 11:30:57 +08001909 }
1910
1911 return -1;
1912}
1913
1914static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1915 switch (bw) {
1916 case 10:
1917 return LYNQ_WIFI_BANDWIDTH_HT10;
1918 break;
1919 case 20:
1920 return LYNQ_WIFI_BANDWIDTH_HT20;
1921 break;
1922 case 40:
1923 return LYNQ_WIFI_BANDWIDTH_HT40;
1924 break;
1925 case 80:
1926 return LYNQ_WIFI_BANDWIDTH_HT80;
1927 break;
1928 default:
1929 break;
1930 }
1931
1932 return -1;
1933}
1934
you.chen70f377f2023-04-14 18:17:09 +08001935static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001936static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1937 int i, count;
1938 char *p;
1939 const char *lynq_status_cmd = "STATUS";
1940 const char * FLAG_SSID = "ssid=";
1941 const char * FLAG_SBSID = "bssid=";
1942 const char * FLAG_KEY_MGMT = "key_mgmt=";
1943 const char * FLAG_FREQ = "freq=";
1944 const char * FLAG_STATE = "wpa_state=";
1945 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001946 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001947 char *split_lines[128] = {0};
1948
1949 CHECK_WPA_CTRL(interface);
1950
qs.xiong9fbf74e2023-03-28 13:38:22 +08001951 if (curr_state == NULL)
1952 {
1953 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001954 return -1;
1955 }
1956
1957 DO_REQUEST(lynq_status_cmd);
1958
1959 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1960
1961 curr_state->net_no = -1;
1962 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001963 for(i=0; i < count; i++)
1964 {
1965 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001966 {
you.chen35020192022-05-06 11:30:57 +08001967 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001969 {
you.chend2fef3f2023-02-13 10:50:35 +08001970 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001971 ret = 0;
1972 continue;
1973 }
you.chenf58b3c92022-06-21 16:53:48 +08001974 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001976 {
you.chend2fef3f2023-02-13 10:50:35 +08001977 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 +08001978 ret = 0;
1979 continue;
1980 }
you.chen35020192022-05-06 11:30:57 +08001981 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001982 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001983 {
you.chen450d0172022-07-15 17:56:48 +08001984 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001985 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001986 ret = 0;
1987 continue;
1988 }
1989 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001991 {
you.chen35020192022-05-06 11:30:57 +08001992 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1993 ret = 0;
1994 continue;
1995 }
you.chend2fef3f2023-02-13 10:50:35 +08001996 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001997 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001998 {
you.chend2fef3f2023-02-13 10:50:35 +08001999 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
2000 ret = 0;
2001 continue;
2002 }
you.chen35020192022-05-06 11:30:57 +08002003 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002005 {
you.chen35020192022-05-06 11:30:57 +08002006 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002007 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08002008 {
you.chen35020192022-05-06 11:30:57 +08002009 strcpy(curr_state->state, p + strlen(FLAG_STATE));
2010 ret = 0;
2011 continue;
2012 }
2013
2014 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002016 {
you.chen35020192022-05-06 11:30:57 +08002017 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002018 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002019 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002020 }
2021 }
2022
you.chen70f377f2023-04-14 18:17:09 +08002023 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2024 {
2025 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2026 }
2027
you.chen35020192022-05-06 11:30:57 +08002028 return ret;
2029}
2030
qs.xiongf1b525b2022-03-31 00:58:23 -04002031int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002032{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002033 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002034 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002035
qs.xiong9fbf74e2023-03-28 13:38:22 +08002036 if (ap_ssid == NULL)
2037 {
2038 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002039 return -1;
2040 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002041 else
2042 {
2043 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002044 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002045
qs.xiong9fbf74e2023-03-28 13:38:22 +08002046 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2047 {
2048 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002049 return -1;
2050 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002051
you.chen35020192022-05-06 11:30:57 +08002052 CHECK_IDX(idx, CTRL_AP);
2053
2054 CHECK_WPA_CTRL(CTRL_AP);
2055
2056 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2057
2058 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2059 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002060 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2061 return 0;
you.chen35020192022-05-06 11:30:57 +08002062
qs.xiong7a105ce2022-03-02 09:43:11 -05002063}
2064
you.chen35020192022-05-06 11:30:57 +08002065int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002066{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002067 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002068 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002069 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002070}
2071
qs.xiongc9c79f72022-10-17 15:27:18 +08002072/*****
2073 *frequency <------>channel
2074 *
2075 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2076 *
2077 *
2078 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2079 *
2080 *
2081 * */
2082static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002083 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};
2084 int i;
2085 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2086
qs.xiong69a332b2022-12-02 09:58:57 +08002087 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002088 {
2089 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002090 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002091 }
qs.xiongc00b6032022-11-29 16:28:03 +08002092
2093 if(i == arr_len)
2094 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002095 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002096 return -1;
2097 }
qs.xiongc00b6032022-11-29 16:28:03 +08002098
qs.xiongc9c79f72022-10-17 15:27:18 +08002099 return 0;
2100}
qs.xiong13673462023-02-21 19:12:54 +08002101
2102static int lynq_check_frequencyby_country_code(int input_frequency)
2103{
2104 char str_cnc[]="CN";
2105 char str_dest[20]="";
2106
2107 if( lynq_get_country_code(1,str_dest) != 0 )
2108 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002109 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002110 return -1;
2111 }
2112 if( strncmp(str_dest,str_cnc,2) != 0 )
2113 {
2114 return 0;
2115 }else if( 2473 < input_frequency && input_frequency < 5744)
2116 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002117 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002118 return -1;
2119 }
2120 return 0;
2121}
qs.xiongf1b525b2022-03-31 00:58:23 -04002122int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002123{
qs.xiongc00b6032022-11-29 16:28:03 +08002124 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002125 char lynq_wifi_frequency_cmd[128]={0};
2126 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002127 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002128 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002129 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002130 check = lynq_check_set_frequency(lynq_wifi_frequency);
2131 if(check != 0)
2132 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002133 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002134 return -1;
you.chen35020192022-05-06 11:30:57 +08002135 }
qs.xiong13673462023-02-21 19:12:54 +08002136 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2137 if(check != 0)
2138 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002139 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002140 return -1;
2141 }
2142
qs.xiongc00b6032022-11-29 16:28:03 +08002143 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2144 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002145 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002146 return -1;
2147 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002148
you.chen35020192022-05-06 11:30:57 +08002149 CHECK_IDX(idx, CTRL_AP);
2150
2151 CHECK_WPA_CTRL(CTRL_AP);
2152
2153 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2154 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2155 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2156
you.chen6c2dd9c2022-05-16 17:55:28 +08002157 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002158 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2159 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2160 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002161
qs.xiong9fbf74e2023-03-28 13:38:22 +08002162 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002163}
2164
qs.xiongf1b525b2022-03-31 00:58:23 -04002165int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002166{
you.chen35020192022-05-06 11:30:57 +08002167 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002168 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002169 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002170
qs.xiong9fbf74e2023-03-28 13:38:22 +08002171 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2172 {
2173 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002174 return -1;
2175 }
2176 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002177
qs.xiong9fbf74e2023-03-28 13:38:22 +08002178 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002179}
2180
qs.xiongf1b525b2022-03-31 00:58:23 -04002181int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2182{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002183 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002184 CHECK_IDX(idx, CTRL_AP);
2185 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002186 case LYNQ_WIFI_BANDWIDTH_HT10:
2187 {
2188 RLOGE("bandwith [%d] not support now\n", bandwidth);
2189 return -1;
2190 }
2191 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002192 {
2193 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2194 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002195 if (system(lynq_cmd_bandwith) != 0 )
2196 {
2197 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002198 return -1;
2199 }
2200 system("wl up");
2201 break;
2202 }
2203 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002204 {
qs.xiong10379192023-02-21 13:19:42 +08002205 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002206 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2207 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002208 if (system(lynq_cmd_bandwith) != 0 )
2209 {
2210 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002211 return -1;
2212 }
2213 system("wl up");
2214 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002215 }
you.chen35020192022-05-06 11:30:57 +08002216 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002217 {
qs.xiong10379192023-02-21 13:19:42 +08002218 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002219 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002220 if (system(lynq_cmd_bandwith) != 0 )
2221 {
2222 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002223 return -1;
2224 }
2225 system("wl up");
2226 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002227 }
2228 default:
you.chen35020192022-05-06 11:30:57 +08002229 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002230 RLOGE("auth type [%d] not support now\n", bandwidth);
2231 return -1;
you.chen35020192022-05-06 11:30:57 +08002232 }
2233 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002234
2235
you.chen35020192022-05-06 11:30:57 +08002236 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002237}
you.chen35020192022-05-06 11:30:57 +08002238
qs.xiongf1b525b2022-03-31 00:58:23 -04002239int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2240{
you.chen35020192022-05-06 11:30:57 +08002241 int count = 0;
2242 int index = 0;
2243 char *split_words[128] = {0};
2244 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002245 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002246 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002247
you.chen35020192022-05-06 11:30:57 +08002248 CHECK_WPA_CTRL(CTRL_AP);
2249
2250 DO_REQUEST(lynq_chanspec_cmd);
2251
2252 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2253 for(;index < count; index++) {
2254 if (strncmp(split_words[index], "bw", 2) != 0) {
2255 continue;
2256 }
2257
2258 index++;
2259 if (index >= count) {
2260 return -1;
2261 }
2262
qs.xiong9fbf74e2023-03-28 13:38:22 +08002263 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002264 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2265 return 0;
2266 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002267 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002268 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002269}
qs.xiong0fb469a2022-04-14 03:50:45 -04002270
qs.xiongf1b525b2022-03-31 00:58:23 -04002271int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002272{
you.chen35020192022-05-06 11:30:57 +08002273 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002274 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002275 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002276
you.chen35020192022-05-06 11:30:57 +08002277 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002278
qs.xiong9fbf74e2023-03-28 13:38:22 +08002279 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2280 {
you.chen35020192022-05-06 11:30:57 +08002281 return -1;
2282 }
2283
2284 system("wl down");
2285 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002286 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002287 return -1;
2288 }
2289 system("wl up");
2290 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002291}
qs.xiong0fb469a2022-04-14 03:50:45 -04002292
qs.xiongf1b525b2022-03-31 00:58:23 -04002293int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002294{
you.chen35020192022-05-06 11:30:57 +08002295 int count = 0;
2296 int index = 0;
2297 char *split_words[128] = {0};
2298 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002299 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002300 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002301
you.chen35020192022-05-06 11:30:57 +08002302 CHECK_WPA_CTRL(CTRL_AP);
2303
2304 DO_REQUEST(lynq_chanspec_cmd);
2305
2306 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002307 for(;index < count; index++)
2308 {
2309 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002310 if (strncmp(split_words[index], "channel", 2) != 0) {
2311 continue;
2312 }
2313
2314 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 if (index >= count)
2316 {
you.chen35020192022-05-06 11:30:57 +08002317 return -1;
2318 }
2319
2320 *channel = atoi(split_words[index]);
2321 return 0;
2322 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002323 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002324 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002325}
2326
2327
you.chen35020192022-05-06 11:30:57 +08002328int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002329{
you.chen6c2dd9c2022-05-16 17:55:28 +08002330 char ssid[MAX_CMD] = {0};
2331 int freq = 0;
2332 char lynq_auth_cmd[64]={0};
2333 char lynq_auth_alg_cmd[64]={0};
2334 char lynq_psk_cmd[64]={0};
2335 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002336 char lynq_ieee80211_cmd[64]={0};
2337 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002338 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002339 CHECK_IDX(idx, CTRL_AP);
2340
you.chen6c2dd9c2022-05-16 17:55:28 +08002341 CHECK_WPA_CTRL(CTRL_AP);
2342
qs.xiong9fbf74e2023-03-28 13:38:22 +08002343 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2344 {
2345 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002346 return -1;
2347 }
2348
you.chen92fd5d32022-05-25 10:09:47 +08002349 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002350 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002351 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002352 return 0;
2353 }
2354 else {
2355 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2356 ssid[0] = '\0';
2357 }
2358 lynq_wifi_ap_frequency_get(idx, &freq);
2359
2360 DO_OK_FAIL_REQUEST(cmd_disconnect);
2361 DO_OK_FAIL_REQUEST(cmd_remove_all);
2362 if (ssid[0] != '\0') {
2363 lynq_wifi_ap_ssid_set(idx, ssid);
2364 }
2365 if (freq != 0) {
2366 lynq_wifi_ap_frequency_set(idx, freq);
2367 }
2368 }
2369 }
you.chen35020192022-05-06 11:30:57 +08002370
qs.xiong9fbf74e2023-03-28 13:38:22 +08002371 switch(auth){
2372 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002373 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002374 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002375 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002376 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002377 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002378 break;
2379 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002380 case LYNQ_WIFI_AUTH_WEP:
2381 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002382 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002383 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002384 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002385 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2386
2387 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2388 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2389 break;
2390 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002391 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002392 {
2393 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2394 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2395 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2396
2397 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2398 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2399 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2400 break;
2401
2402 }
you.chen35020192022-05-06 11:30:57 +08002403 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002404 {
2405 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2406 {
you.chen35020192022-05-06 11:30:57 +08002407 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2408 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2409 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002410 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2411 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002412 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002413 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002414 }
2415// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2416// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2417 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002418
you.chen35020192022-05-06 11:30:57 +08002419 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2420 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2421 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 break;
2423 }
2424 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2425 {
2426 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2427 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2428 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2429 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2430
2431 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2432 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2433 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2434 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2435 break;
2436 }
2437 case LYNQ_WIFI_AUTH_WPA3_PSK:
2438 {
2439 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2440 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002441 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002442 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2443
2444 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2445 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2446 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2447 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2448 break;
2449 }
2450 default:
you.chen35020192022-05-06 11:30:57 +08002451 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002452 RLOGE("auth type [%d] not support now\n", auth);
2453 return -1;
you.chen35020192022-05-06 11:30:57 +08002454 }
2455 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002456 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002457
qs.xiong9fbf74e2023-03-28 13:38:22 +08002458 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002459}
2460
you.chen35020192022-05-06 11:30:57 +08002461int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002462{
you.chen35020192022-05-06 11:30:57 +08002463 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002464 char lynq_auth_alg_str[MAX_RET] = {0};
2465 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002466 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002467 CHECK_IDX(idx, CTRL_AP);
2468
qs.xiong9fbf74e2023-03-28 13:38:22 +08002469 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2470 {
2471 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002472 return -1;
2473 }
2474
qs.xiong9fbf74e2023-03-28 13:38:22 +08002475 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2476 {
2477 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2478 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002479 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002480 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002481 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002482 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002483 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2484 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002485 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002486 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002487 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002488 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002489 else
2490 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002491 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002492 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002493 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002494 }
you.chen35020192022-05-06 11:30:57 +08002495 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002496 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2497 {
2498 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2499 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002500 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002501 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002502 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002503 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2504 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002505 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002506 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002507 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002508 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002509 else
2510 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002511 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002512 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002513 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002514 }
you.chen35020192022-05-06 11:30:57 +08002515 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002516
2517 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2518 {
2519 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2520 return -1;
2521 }
2522
2523 if (memcmp(lynq_auth_str,"1",1) == 0 )
2524 {
2525 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2526 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002527 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002528 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2529 {
2530 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2531 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002532 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002533 }
2534 else
2535 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002536 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002537 *auth = -1;
2538 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002539
you.chen6c2dd9c2022-05-16 17:55:28 +08002540 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002541}
qs.xiong1af5daf2022-03-14 09:12:12 -04002542
you.chenb95401e2023-05-12 19:39:06 +08002543static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2544{
2545 char status[64];
you.chencba13492023-05-19 13:53:43 +08002546 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002547 curr_status_info curr_state;
2548
2549 CHECK_WPA_CTRL(CTRL_AP);
2550
2551 memset(status, 0, sizeof (status));
2552
2553 curr_state.ap = NULL;
2554 curr_state.state = status;
2555
2556 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002557 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002558 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002559 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002560 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002561 {
2562 return 0;
2563 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002564 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002565 {
2566 DO_REQUEST("RECONNECT");
2567 return inner_check_ap_connected(idx, retry_count+1);
2568 }
you.chencba13492023-05-19 13:53:43 +08002569 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002570 {
2571 printf("retry 10 time\n");
2572 return -1;
2573 }
2574 else
2575 {
you.chen6d247052023-06-01 16:39:54 +08002576 if (strcmp(status, STATE_DISCONNECTED) == 0)
2577 {
2578 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2579 DO_REQUEST(LYNQ_WIFI_CMD);
2580 }
you.chenb95401e2023-05-12 19:39:06 +08002581 return inner_check_ap_connected(idx, retry_count+1);
2582 }
2583 }
2584 return -1;
2585}
qs.xiong1af5daf2022-03-14 09:12:12 -04002586
qs.xiongf1b525b2022-03-31 00:58:23 -04002587int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002588{
qs.xiong5a2ba932023-09-13 16:30:21 +08002589 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002590 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002591 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2592 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2593 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002594 CHECK_IDX(idx, CTRL_AP);
2595
2596 CHECK_WPA_CTRL(CTRL_AP);
2597
you.chen0df3e7e2023-05-10 15:56:26 +08002598 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002599 {
you.chen0df3e7e2023-05-10 15:56:26 +08002600 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002601 return -1;
2602 }
you.chen35020192022-05-06 11:30:57 +08002603
qs.xiongb37f8c42023-09-13 21:21:58 +08002604 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2605 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2606
you.chen35020192022-05-06 11:30:57 +08002607 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2608 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2609
you.chenc9928582023-04-24 15:39:37 +08002610 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2611 if (ret != 0)
2612 {
2613 RLOGE("lynq_wifi_ap_start excute script fail");
2614 return -1;
2615 }
2616
you.chenb95401e2023-05-12 19:39:06 +08002617 if (inner_check_ap_connected(idx, 0) != 0)
2618 {
2619 return -1;
2620 }
2621
you.chen0df3e7e2023-05-10 15:56:26 +08002622 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002623 if (g_ap_tmp_watcher_pid == 0)
2624 {
2625 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2626 {
2627 g_ap_tmp_watcher_pid = 0;
2628 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2629 return -1;
2630 }
2631 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2632 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002633 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002634
qs.xiong9fbf74e2023-03-28 13:38:22 +08002635 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002636}
2637
qs.xiongf1b525b2022-03-31 00:58:23 -04002638int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002639{
you.chen35020192022-05-06 11:30:57 +08002640 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002641}
2642
qs.xiongf1b525b2022-03-31 00:58:23 -04002643int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002644{
you.chen35020192022-05-06 11:30:57 +08002645 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002646
you.chen35020192022-05-06 11:30:57 +08002647 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002648
you.chen35020192022-05-06 11:30:57 +08002649 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002650
you.chen35020192022-05-06 11:30:57 +08002651 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2652
2653 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2654
you.chenb4b121c2022-05-06 17:50:16 +08002655// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002656
2657 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2658 if (ret != 0)
2659 {
2660 RLOGE("lynq_wifi_ap_start excute script fail");
2661 return -1;
2662 }
qs.xiong44fac672023-08-29 16:15:55 +08002663 g_ap_tmp_watcher_stop_flag = 1;
2664 if (g_ap_tmp_watcher_pid != 0)
2665 pthread_join(g_ap_tmp_watcher_pid, NULL);
2666 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002667
qs.xiong9fbf74e2023-03-28 13:38:22 +08002668 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002669}
qs.xiong1af5daf2022-03-14 09:12:12 -04002670
qs.xiongf1b525b2022-03-31 00:58:23 -04002671int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002672{
you.chen35020192022-05-06 11:30:57 +08002673 char lynq_disable_cmd[128] = {0};
2674 char lynq_select_cmd[128] = {0};
2675 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002676 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002677 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002678
you.chen35020192022-05-06 11:30:57 +08002679 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002680 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2681 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2682
2683 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2684 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2685 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002686
qs.xiong9fbf74e2023-03-28 13:38:22 +08002687 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002688}
2689
qs.xiongf1b525b2022-03-31 00:58:23 -04002690int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002691{
you.chen35020192022-05-06 11:30:57 +08002692 char lynq_disable_cmd[128] = {0};
2693 char lynq_select_cmd[128] = {0};
2694 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002695 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002696 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002697
you.chen35020192022-05-06 11:30:57 +08002698 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002699
you.chen35020192022-05-06 11:30:57 +08002700 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2701 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2702
2703 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2704 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2705 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002706
qs.xiong9fbf74e2023-03-28 13:38:22 +08002707 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002708}
qs.xiongf1b525b2022-03-31 00:58:23 -04002709
you.chen35020192022-05-06 11:30:57 +08002710int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002711{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002712 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002713 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002714 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002715 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002716 RLOGD("enter lynq_ap_password_set");
2717 if( password == NULL )
2718 {
2719 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002720 return -1;
2721 }
2722 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002723 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002724 if(pass_len < 8 || pass_len >= 64)
2725 {
2726 RLOGE("[lynq_ap_password_set]input password len not in rage");
2727 return -1;
you.chen35020192022-05-06 11:30:57 +08002728 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002729
you.chen35020192022-05-06 11:30:57 +08002730 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002731
qs.xiong9fbf74e2023-03-28 13:38:22 +08002732 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2733 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002734 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002735 return -1;
2736 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002737 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2738 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002739 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2740 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002741 }
2742
you.chen35020192022-05-06 11:30:57 +08002743 CHECK_WPA_CTRL(CTRL_AP);
2744
qs.xiong9fbf74e2023-03-28 13:38:22 +08002745 if (auth == LYNQ_WIFI_AUTH_WEP)
2746 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002747 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002748 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2749 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2750 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2751 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2752 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002753 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2754 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002755 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 +08002756 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2757 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2758 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002759 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2760 {
2761
qs.xiongc8d92a62023-03-29 17:36:14 +08002762 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 +08002763 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002764 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002765 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2766 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2767
2768 }
2769 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002770 {
2771 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002772 return -1;
2773 }
you.chen35020192022-05-06 11:30:57 +08002774
you.chen35020192022-05-06 11:30:57 +08002775 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002776
qs.xiong9fbf74e2023-03-28 13:38:22 +08002777 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002778}
2779
you.chen35020192022-05-06 11:30:57 +08002780int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002781{
you.chen35020192022-05-06 11:30:57 +08002782 FILE * fp;
2783 int len, ret;
2784 int count, index;
2785 char *split_lines[128] = {0};
2786 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002787 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002788
you.chen35020192022-05-06 11:30:57 +08002789 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002790
you.chen35020192022-05-06 11:30:57 +08002791 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2792// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002793 if (NULL == fp)
2794 {
2795 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002796 return -1;
2797 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002798
you.chen35020192022-05-06 11:30:57 +08002799 buff = alloca(MAX_RET);
2800 fseek(fp, 0, SEEK_SET);
2801 len = fread(buff, 1, MAX_RET, fp);
2802 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002803
qs.xiong9fbf74e2023-03-28 13:38:22 +08002804 for(index=0; index < len; index ++)
2805 {
2806 if (memcmp(buff + index, "network={", 9) != 0)
2807 {
you.chen35020192022-05-06 11:30:57 +08002808 continue;
2809 }
2810 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002811 for (; index < len; index ++ )
2812 {
2813 if (buff[index] != '}')
2814 {
you.chen35020192022-05-06 11:30:57 +08002815 continue;
2816 }
2817 buff[index] = '\0';
2818 break;
2819 }
2820 len = buff + index - p;
2821 }
2822
2823 count = lynq_split(p, len, '\n', split_lines);
2824
2825 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002826 for(index=0; index < count; index++)
2827 {
you.chen35020192022-05-06 11:30:57 +08002828 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002829 if (p != NULL)
2830 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002831 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002832 if (*p == '\"')
2833 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002834 p++;
2835 }
you.chen35020192022-05-06 11:30:57 +08002836 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002837 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2838 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002839 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002840 if (*p == '\"')
2841 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002842 p++;
2843 }
2844 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002845 else
2846 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002847 continue;
you.chen35020192022-05-06 11:30:57 +08002848 }
2849
2850 strcpy(password, p);
2851
qs.xiong9fbf74e2023-03-28 13:38:22 +08002852 while(*password != '\0')
2853 {
2854 if (*password == '\"')
2855 {
you.chen35020192022-05-06 11:30:57 +08002856 *password = '\0';
2857 break;
2858 }
2859 password++;
2860 }
2861 ret = 0;
2862 break;
2863 } //end for(index=0; index < count; index++)
2864
2865 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002866}
2867
you.chen35020192022-05-06 11:30:57 +08002868static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2869 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002870 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002871
qs.xiong9fbf74e2023-03-28 13:38:22 +08002872 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2873 {
you.chen35020192022-05-06 11:30:57 +08002874 return -1;
2875 }
2876
2877 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002878
qs.xiong9fbf74e2023-03-28 13:38:22 +08002879 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2880 {
2881 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002882 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002883 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002884 {
you.chena6cd55a2022-05-08 12:20:18 +08002885 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002886 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002887 }
you.chen70f377f2023-04-14 18:17:09 +08002888 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2889 {
2890 return 0;
2891 }
you.chena6cd55a2022-05-08 12:20:18 +08002892 }
2893 }
you.chen70f377f2023-04-14 18:17:09 +08002894 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2895 {
2896 return 0;
2897 }
2898
qs.xiong9fbf74e2023-03-28 13:38:22 +08002899 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2900 {
you.chen70f377f2023-04-14 18:17:09 +08002901 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002902 return -1;
2903 }
2904 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2905 {
2906
you.chen70f377f2023-04-14 18:17:09 +08002907 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2908 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002909 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2910 {
2911
2912 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002913 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002914 }else
2915 {
you.chen70f377f2023-04-14 18:17:09 +08002916 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 *auth = -1;
2918 return -1;
2919 }
you.chen35020192022-05-06 11:30:57 +08002920 return 0;
2921}
2922
2923int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002924{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002926 int pass_len, net_no, count, index;
2927 char lynq_tmp_cmd[300]={0};
2928 int net_no_list[128];
2929 lynq_wifi_auth_s net_auth;
2930 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002931 if(pass_len < 8 || pass_len >= 64)
2932 {
2933 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002934 return -1;
2935 }
2936
2937 CHECK_IDX(idx, CTRL_STA);
2938
2939 net_no = -1;
2940 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2941
qs.xiong9fbf74e2023-03-28 13:38:22 +08002942 for (index=0; index < count; index++)
2943 {
you.chen35020192022-05-06 11:30:57 +08002944 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002945 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2946 {
you.chen35020192022-05-06 11:30:57 +08002947 net_no = net_no_list[index];
2948 break;
2949 }
2950 }
2951
qs.xiong9fbf74e2023-03-28 13:38:22 +08002952 if (net_no < 0)
2953 {
you.chen35020192022-05-06 11:30:57 +08002954 return -1;
2955 }
2956
2957 CHECK_WPA_CTRL(CTRL_STA);
2958
2959 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2960
2961 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2962 DO_OK_FAIL_REQUEST(cmd_save_config);
2963
2964 return 0;
2965}
2966
qs.xiongb5dab082023-10-13 14:43:41 +08002967/**
2968* buff data
2969* buff_len size of buff
2970* idx sta
2971* *ap ap info for find ssid && password
2972* password return password
2973*
2974*/
2975static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
2976
2977 int ret, network_len, i, ssid_len,curr_auth;
2978 int count, index,org_index;
2979 char *split_lines[128] = {0};
2980 char *p, *ssid, *ssid_end_flag,*ptr;
2981 char tmp_ssid[128]={0};
2982 char tmp_auth[24]={0};
2983
2984 org_index = 0;
2985 network_len = 0;
2986 p = NULL;
2987
2988 CHECK_IDX(idx, CTRL_STA);
2989
2990 while(1){
2991 network_len = 0;
2992 p == NULL;
2993 for(; org_index < buff_len; org_index ++)
2994 {
2995 for(; org_index < buff_len; org_index ++)
2996 {
2997 if (memcmp(buff + org_index, "network={", 9) != 0)
2998 {
2999 continue;
3000 }
3001 p = buff + org_index + 9;
3002
3003 for (; org_index < buff_len; org_index ++ )
3004 {
3005 if (buff[org_index] != '}')
3006 {
3007 continue;
3008 }
3009 buff[org_index] = '\0';
3010 break;
3011 }
3012 network_len = buff + org_index - p;
3013 break;
3014 }
3015
3016 if (p == NULL)
3017 {
3018 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3019 return -1;
3020 }
3021
3022 ssid = strstr(p, "ssid=");
3023 if (ssid != NULL) {
3024 ssid += strlen("ssid=");
3025 if (ssid[0] == '\"')
3026 {
3027 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3028 {
3029 RLOGD("-----curr_get ssid form config is %s",ssid);
3030 break;
3031 }
3032 RLOGD("-----countine to find dest ssid %s ---curr_get ssid from config is %s",ap->ap_ssid,ssid);
3033 }
3034 else
3035 {
3036 ssid_end_flag = strstr(ssid, "\n");
3037 if (ssid_end_flag != NULL)
3038 {
3039 ssid_len = (ssid_end_flag - ssid) / 2;
3040 for(i=0; i<ssid_len; i++)
3041 {
3042 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3043 }
3044 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3045 {
3046 RLOGD("curr_ssid is(from config) ---- %s ap_info ssid --->",tmp_ssid,ap->ap_ssid);
3047 break;
3048 }
3049 }
3050 }
3051 }
3052
3053 }
3054
3055 if (org_index >= buff_len || NULL == p || network_len <= 0)
3056 {
3057
3058 if (buff != NULL)
3059 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3060 return -1;
3061 }
3062
3063 count = lynq_split(p, network_len, '\n', split_lines);
3064 ret = -1;
3065 for( index=0; index < count; index++ )
3066 {
3067 p = strstr(split_lines[index], "key_mgmt=");
3068 RLOGD("current p str ------- %s",p);
3069 if(p != NULL)
3070 {
3071 p += 9;
3072 if(memcmp(p,"SAE",3) == 0)
3073 {
3074 curr_auth = 5;
3075 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3076 {
3077 curr_auth = 4;
3078 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3079 {
3080 curr_auth = 3;
3081 }else if(memcmp(p,"NONE",4) == 0 )
3082 {
3083 curr_auth = 0;
3084 }else{
3085 curr_auth = 1;
3086 }
3087 RLOGD("************curret_get_auth is %d ssid is %s",curr_auth,ap->ap_ssid);
3088 if( curr_auth < 1 || curr_auth > 6)
3089 {
3090 ret = -1;
3091 }
3092 break;
3093 }
3094 }
3095 if( curr_auth == 0)
3096 {
3097 return 0;
3098 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3099 {
3100 for(index=0; index < count; index++)
3101 {
3102 /*get psw info*/
3103
3104 p = strstr(split_lines[index], "psk=");
3105 if (p != NULL)
3106 {
3107 p += 4;
3108 if (*p == '\"')
3109 {
3110 p++;
3111 }
3112 }
3113 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3114 {
3115 p += 9;
3116 if (*p == '\"')
3117 {
3118 p++;
3119 }
3120 }
3121 else
3122 {
3123 continue;
3124 }
3125
3126 if (*p == '\"')
3127 p++;
3128 strncpy(password, p, 64);
3129 p = password;
3130 while(password - p < 64 && *password != '\0')
3131 {
3132 if (*password == '\"')
3133 {
3134 *password = '\0';
3135 RLOGD("---------password------- p:: %s",p);
3136 ret = 0;
3137 break;
3138 }
3139 password++;
3140 }
3141 break;
3142 }
3143 break;
3144 }
3145 }
3146
3147 return ret;
3148}
3149
3150
3151
you.chen35020192022-05-06 11:30:57 +08003152int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3153
3154 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003155 int len, ret;
3156 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003157 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003158
qs.xiongb5dab082023-10-13 14:43:41 +08003159 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003160 CHECK_IDX(idx, CTRL_STA);
3161
qs.xiong9fbf74e2023-03-28 13:38:22 +08003162 if (NULL == password)
3163 {
3164 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003165 return -1;
3166 }
3167
you.chen35020192022-05-06 11:30:57 +08003168 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003169 if (NULL == fp)
3170 {
3171 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003172 return -1;
3173 }
3174
you.chen6d247052023-06-01 16:39:54 +08003175 fseek(fp, 0, SEEK_END);
3176 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003177 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003178
qs.xiongb5dab082023-10-13 14:43:41 +08003179 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003180 {
3181 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3182 return -1;
3183 }
3184
you.chen35020192022-05-06 11:30:57 +08003185 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003186 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003187 fclose(fp);
3188
qs.xiongb5dab082023-10-13 14:43:41 +08003189
3190 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3191
3192 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003193 {
qs.xiongb5dab082023-10-13 14:43:41 +08003194 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3195 free(info_buff);
3196 return 0;
you.chen35020192022-05-06 11:30:57 +08003197 }
qs.xiongb5dab082023-10-13 14:43:41 +08003198 else{
3199 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003200 return -1;
3201 }
3202
you.chen35020192022-05-06 11:30:57 +08003203}
3204
qs.xiongb5dab082023-10-13 14:43:41 +08003205
you.chen35020192022-05-06 11:30:57 +08003206static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3207{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003208 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003209
qs.xiong9fbf74e2023-03-28 13:38:22 +08003210 if (sta_ssid == NULL)
3211 {
3212 RLOGE("sta_ssid is null\n");
3213 return -1;
you.chen35020192022-05-06 11:30:57 +08003214 }
3215
qs.xiong9fbf74e2023-03-28 13:38:22 +08003216 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003217
3218 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3219
3220 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3221// DO_OK_FAIL_REQUEST(cmd_save_config);
3222
qs.xiong9fbf74e2023-03-28 13:38:22 +08003223 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003224
3225}
3226
you.chen35020192022-05-06 11:30:57 +08003227static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003228{
you.chen35020192022-05-06 11:30:57 +08003229 char lynq_disable_cmd[128]={0};
3230 char lynq_select_cmd[128]={0};
3231
3232 CHECK_WPA_CTRL(CTRL_STA);
3233
qs.xiong9fbf74e2023-03-28 13:38:22 +08003234 if (save != 0)
3235 {
you.chenc29444e2022-06-07 18:01:16 +08003236 if (start_flag != 0)
3237 {
3238 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3239 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3240 }
3241 else
3242 {
3243 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3244 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3245 }
you.chen35020192022-05-06 11:30:57 +08003246 DO_OK_FAIL_REQUEST(cmd_save_config);
3247 }
3248
qs.xiong9fbf74e2023-03-28 13:38:22 +08003249 if (start_flag == 0)
3250 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003251 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003252 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3253 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003254 else
3255 {
you.chen35020192022-05-06 11:30:57 +08003256 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3257 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3258 }
3259
3260 return 0;
3261}
3262
3263int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3264{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003265 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003266 CHECK_IDX(idx, CTRL_STA);
3267
you.chen6c2dd9c2022-05-16 17:55:28 +08003268 curr_status_info curr_state;
3269 ap_info_s ap_info;
3270 curr_state.ap = &ap_info;
3271 curr_state.state = NULL;
3272
qs.xiong9fbf74e2023-03-28 13:38:22 +08003273 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3274 {
you.chend2fef3f2023-02-13 10:50:35 +08003275 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003276 return 0;
3277 }
3278
3279 return -1;
you.chen35020192022-05-06 11:30:57 +08003280}
3281
3282int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3283{
qs.xiong5d716d22023-09-20 20:08:39 +08003284 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003285 scan_info_s *scan_list = NULL;
3286 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003287 int scan_len=0;
3288 int save_len=0;
3289 int best_index = -1;
3290 int best_scan_index = -1;
3291 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003292 int i, j, ret;
3293
3294 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003295
3296 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003297 if (info == NULL)
3298 {
you.chen35020192022-05-06 11:30:57 +08003299 return -1;
3300 }
3301
3302 curr_status_info curr_state;
3303 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003304 char status[64];
you.chen35020192022-05-06 11:30:57 +08003305
you.chen9ac66392022-08-06 17:01:16 +08003306 memset(&ap_info, 0, sizeof (ap_info));
3307 memset(status, 0, sizeof (status));
3308
3309 curr_state.ap = &ap_info;
3310 curr_state.state = status;
3311
qs.xiong9fbf74e2023-03-28 13:38:22 +08003312 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3313 {
you.chen35020192022-05-06 11:30:57 +08003314 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003315 if (strcmp(status, STATE_COMPLETED) == 0)
3316 {
3317 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003318 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003319 }
3320 else
3321 {
3322 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003323 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003324 }
you.chen593621d2023-04-27 17:52:44 +08003325 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003326 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003327 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003328 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003329 return 0;
3330 }
3331
you.chen9ac66392022-08-06 17:01:16 +08003332 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003333 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003334 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3335 {
you.chen9ac66392022-08-06 17:01:16 +08003336 if (NULL != scan_list)
3337 {
3338 free(scan_list);
3339 }
you.chen35020192022-05-06 11:30:57 +08003340 return -1;
3341 }
3342
qs.xiong9fbf74e2023-03-28 13:38:22 +08003343 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3344 {
you.chen9ac66392022-08-06 17:01:16 +08003345 if (NULL != scan_list)
3346 {
3347 free(scan_list);
3348 }
3349 if (NULL != save_list)
3350 {
3351 free(save_list);
3352 }
you.chen35020192022-05-06 11:30:57 +08003353 return -1;
3354 }
3355
qs.xiong9fbf74e2023-03-28 13:38:22 +08003356 for (i=0; i < save_len; i++)
3357 {
3358 for (j=0; j < scan_len; j++)
3359 {
you.chen35020192022-05-06 11:30:57 +08003360 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003361 && save_list[i].base_info.auth == scan_list[j].auth)
3362 {
3363 if (best_rssi == 0)
3364 {
you.chen9ac66392022-08-06 17:01:16 +08003365 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003366 best_rssi = scan_list[j].rssi;
3367 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003368 else if (best_rssi > scan_list[j].rssi)
3369 {
you.chen35020192022-05-06 11:30:57 +08003370 best_index = i;
3371 best_scan_index = j;
3372 best_rssi = scan_list[j].rssi;
3373 }
you.chend2fef3f2023-02-13 10:50:35 +08003374 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 +08003375 break;
3376 }
3377 }
3378 }
3379
qs.xiong9fbf74e2023-03-28 13:38:22 +08003380 if (best_index >= 0)
3381 {
you.chen35020192022-05-06 11:30:57 +08003382 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003383 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 +08003384 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003385 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003386 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003387 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003388 }
3389
you.chen9ac66392022-08-06 17:01:16 +08003390 if (NULL != scan_list)
3391 {
3392 free(scan_list);
3393 }
3394 if (NULL != save_list)
3395 {
3396 free(save_list);
3397 }
3398
qs.xiong5d716d22023-09-20 20:08:39 +08003399 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003400 return ret;
you.chen35020192022-05-06 11:30:57 +08003401}
3402
3403static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3404{
qs.xiongc8d92a62023-03-29 17:36:14 +08003405 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003406 char lynq_ket_mgmt_cmd[64]={0};
3407 char lynq_pairwise_cmd[64]={0};
3408 char lynq_psk_cmd[64]={0};
3409
3410 CHECK_WPA_CTRL(CTRL_STA);
3411
qs.xiong9fbf74e2023-03-28 13:38:22 +08003412 switch(auth)
3413 {
3414 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003415 {
3416 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003417
you.chen35020192022-05-06 11:30:57 +08003418 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003419// DO_OK_FAIL_REQUEST(cmd_save_config);
3420 break;
3421 }
3422 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003423 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003424 {
3425 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3426 {
you.chen35020192022-05-06 11:30:57 +08003427 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3428 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003429 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3430 {
you.chena6cd55a2022-05-08 12:20:18 +08003431 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003432 }
3433 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3434 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003435
you.chen35020192022-05-06 11:30:57 +08003436 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3437 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3438 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003439
qs.xiong9fbf74e2023-03-28 13:38:22 +08003440 if (password != NULL)
3441 {
you.chen35020192022-05-06 11:30:57 +08003442 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3443 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003444 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003445 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003446
you.chen35020192022-05-06 11:30:57 +08003447// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003448 break;
3449 }
3450 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3451 {
qs.xiong3e506812023-04-06 11:08:48 +08003452 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003453 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003454 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3455 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3456
qs.xiong3e506812023-04-06 11:08:48 +08003457 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003458 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3459 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3460 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3461
3462 break;
3463 }
3464 case LYNQ_WIFI_AUTH_WPA3_PSK:
3465 {
qs.xiong3e506812023-04-06 11:08:48 +08003466 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003467 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3469 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3470
qs.xiongb37f8c42023-09-13 21:21:58 +08003471 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003472 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3473 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3474 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3475
3476 break;
3477 }
3478 default:
3479 return -1;
you.chen35020192022-05-06 11:30:57 +08003480 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003481
qs.xiong9fbf74e2023-03-28 13:38:22 +08003482 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003483}
qs.xiong7a105ce2022-03-02 09:43:11 -05003484
you.chen35020192022-05-06 11:30:57 +08003485static int inner_get_curr_net_no(int interface) {
3486 curr_status_info curr_state;
3487 curr_state.ap = NULL;
3488 curr_state.state = NULL;
3489
qs.xiong9fbf74e2023-03-28 13:38:22 +08003490 if (0 != inner_get_status_info(interface, &curr_state))
3491 {
you.chen35020192022-05-06 11:30:57 +08003492 return -1;
3493 }
3494
3495 return curr_state.net_no;
3496}
3497
3498int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003499{
you.chen35020192022-05-06 11:30:57 +08003500 int net_no;
3501 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003502
you.chen35020192022-05-06 11:30:57 +08003503 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003504
qs.xiong9fbf74e2023-03-28 13:38:22 +08003505 if (net_no < 0)
3506 {
you.chen35020192022-05-06 11:30:57 +08003507 return -1;
3508 }
3509
3510 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003511}
3512
you.chenb95401e2023-05-12 19:39:06 +08003513int 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 -05003514{
you.chen35020192022-05-06 11:30:57 +08003515 int count, net_no, index;
3516 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003517 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003518 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003519 curr_status_info curr_state;
3520 ap_info_s ap_info;
3521 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003522
qs.xiong9fbf74e2023-03-28 13:38:22 +08003523 if (ssid == NULL || *ssid == '\0')
3524 {
3525 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003526 return -1;
3527 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003528
qs.xiong9fbf74e2023-03-28 13:38:22 +08003529 if (LYNQ_WIFI_AUTH_OPEN != auth)
3530 {
3531 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003532 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003533 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003534 return -1;
3535 }
3536 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003537
you.chen70f377f2023-04-14 18:17:09 +08003538
3539 pthread_mutex_lock(&s_global_check_mutex);
3540 if (s_sta_status != INNER_STA_STATUS_INIT)
3541 {
3542 s_sta_status = INNER_STA_STATUS_CANCEL;
3543 pthread_cond_signal(&s_global_check_cond);
3544 }
3545 pthread_mutex_unlock(&s_global_check_mutex);
3546
you.chen35020192022-05-06 11:30:57 +08003547 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003548 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003549
3550 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003551 memset(&ap_info, 0, sizeof (ap_info));
3552 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003553
you.chen70f377f2023-04-14 18:17:09 +08003554 curr_state.ap = &ap_info;
3555 curr_state.state = status;
3556
3557 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003558 {
you.chen70f377f2023-04-14 18:17:09 +08003559 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3560 {
3561 net_no = curr_state.net_no;
3562 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3563 && strcmp(ap_info.psw, psw) == 0)
3564 {
3565 RLOGD("already connected\n");
3566
3567 pthread_mutex_lock(&s_global_check_mutex);
3568 s_sta_status = INNER_STA_STATUS_CONNECTED;
3569 pthread_cond_signal(&s_global_check_cond);
3570 pthread_mutex_unlock(&s_global_check_mutex);
3571 return 0;
3572 }
you.chen35020192022-05-06 11:30:57 +08003573 }
3574 }
3575
you.chen70f377f2023-04-14 18:17:09 +08003576 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003577 {
you.chen70f377f2023-04-14 18:17:09 +08003578 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3579
3580 for (index=0; index < count; index++)
3581 {
3582 net_auth = -1;
3583 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3584 {
3585 net_no = net_no_list[index];
3586 break;
3587 }
you.chen35020192022-05-06 11:30:57 +08003588 }
3589
you.chen70f377f2023-04-14 18:17:09 +08003590 if (net_no < 0)
3591 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003592 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3593 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3594 {
3595 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3596 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3597 DO_OK_FAIL_REQUEST(rm_net_cmd);
3598 }
you.chen70f377f2023-04-14 18:17:09 +08003599 net_no = lynq_add_network(CTRL_STA);
3600 if (net_no == -1)
3601 {
3602 return -1;
3603 }
3604
3605 RLOGD("net no is %d\n", net_no);
3606 if (0 != inner_set_sta_ssid(net_no, ssid))
3607 {
3608 return -1;
3609 }
you.chen35020192022-05-06 11:30:57 +08003610 }
3611 }
3612
qs.xiong9fbf74e2023-03-28 13:38:22 +08003613 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3614 {
you.chen35020192022-05-06 11:30:57 +08003615 return -1;
3616 }
3617
you.chen70f377f2023-04-14 18:17:09 +08003618
3619 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003620 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003621 usleep(200*1000);
3622
3623 ret = inner_sta_start_stop(net_no, 1, 1);
3624
3625 pthread_mutex_lock(&s_global_check_mutex);
3626 s_sta_status = INNER_STA_STATUS_CONNECTING;
3627 strcpy(s_sta_current_connecting_ssid, ssid);
3628 struct timeval now;
3629 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003630 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003631 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3632 pthread_cond_signal(&s_global_check_cond);
3633 pthread_mutex_unlock(&s_global_check_mutex);
3634 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003635}
3636
you.chenb95401e2023-05-12 19:39:06 +08003637int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3638{
3639 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3640}
3641
you.chen35020192022-05-06 11:30:57 +08003642int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003643{
you.chen35020192022-05-06 11:30:57 +08003644 ap_info_s ap;
3645 curr_status_info curr_state;
3646 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003647
qs.xiong9fbf74e2023-03-28 13:38:22 +08003648 if (ssid == NULL || *ssid == '\0')
3649 {
3650 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003651 return -1;
3652 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003653
you.chen35020192022-05-06 11:30:57 +08003654 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003655
you.chen35020192022-05-06 11:30:57 +08003656 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003657 curr_state.state = NULL;
3658
qs.xiong9fbf74e2023-03-28 13:38:22 +08003659 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3660 {
you.chen35020192022-05-06 11:30:57 +08003661 return 0;
3662 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003663
qs.xiong9fbf74e2023-03-28 13:38:22 +08003664 if (strcmp(ap.ap_ssid, ssid) != 0)
3665 {
you.chen35020192022-05-06 11:30:57 +08003666 return 0;
3667 }
3668
you.chen70f377f2023-04-14 18:17:09 +08003669 pthread_mutex_lock(&s_global_check_mutex);
3670 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3671 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003672 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003673}
qs.xiong97fa59b2022-04-07 05:41:29 -04003674
qs.xiongc93bf2b2023-08-25 10:22:08 +08003675int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3676{
3677 ap_info_s ap;
3678 curr_status_info curr_state;
3679 ap.ap_ssid[0] = '\0';
3680
3681 if (ssid == NULL || *ssid == '\0')
3682 {
3683 RLOGE("input ssid is NULL\n");
3684 return -1;
3685 }
3686
3687 CHECK_IDX(idx, CTRL_STA);
3688
3689
3690 curr_state.ap = &ap;
3691 curr_state.state = NULL;
3692
3693 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3694 {
3695 return 0;
3696 }
3697
3698 if (strcmp(ap.ap_ssid, ssid) != 0)
3699 {
3700 return 0;
3701 }
3702
3703 pthread_mutex_lock(&s_global_check_mutex);
3704 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003705 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3706 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003707 pthread_mutex_unlock(&s_global_check_mutex);
3708 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3709
3710}
3711
3712
you.chena6cd55a2022-05-08 12:20:18 +08003713int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3714{
qs.xiongb37f8c42023-09-13 21:21:58 +08003715
qs.xiongad2f89d2023-01-18 13:17:41 +08003716 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3717 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003718
you.chen35020192022-05-06 11:30:57 +08003719 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003720 CHECK_WPA_CTRL(CTRL_STA);
3721
you.chenc9928582023-04-24 15:39:37 +08003722 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3723 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003724 {
you.chenc9928582023-04-24 15:39:37 +08003725 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003726 return -1;
3727 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003728
qs.xiongad2f89d2023-01-18 13:17:41 +08003729 system(lynq_enable_sta_cmd);
3730 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003731 pthread_mutex_lock(&s_global_check_mutex);
3732 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003733 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003734 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003735 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003736}
3737
you.chen6d247052023-06-01 16:39:54 +08003738static int inner_get_status_info_state (int interface, char *state) {
3739 curr_status_info curr_state;
3740 curr_state.ap = NULL;
3741 curr_state.state = state;
3742 return inner_get_status_info(interface, &curr_state);
3743}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003744
3745int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3746{
3747
qs.xiongb37f8c42023-09-13 21:21:58 +08003748 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3749 int tmp_open_idx[128];
3750 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003751
qs.xiongb37f8c42023-09-13 21:21:58 +08003752 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003753 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003754 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3755 pthread_mutex_unlock(&s_global_check_mutex);
3756 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003757 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003758 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003759 }
3760
qs.xiongb37f8c42023-09-13 21:21:58 +08003761 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003762 return 0;
3763}
3764
3765
you.chen35020192022-05-06 11:30:57 +08003766int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003767{
qs.xiongad2f89d2023-01-18 13:17:41 +08003768// char lynq_disable_network_cmd[MAX_CMD];
3769// curr_status_info curr_state;
3770// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003771 int i=0;
3772 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003773
you.chen6d247052023-06-01 16:39:54 +08003774// const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08003775 CHECK_IDX(idx, CTRL_STA);
3776 CHECK_WPA_CTRL(CTRL_STA);
3777
you.chen6d247052023-06-01 16:39:54 +08003778// system(lynq_disable_sta_cmd);
3779 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003780 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003781
3782 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3783 if (ret != 0)
3784 {
3785 RLOGE("lynq_wifi_ap_start excute script fail");
3786 return -1;
3787 }
3788
you.chen6d247052023-06-01 16:39:54 +08003789 for (i=0; i < 30; i++) // to check if sta is realy stoped
3790 {
3791 if (inner_get_status_info_state(idx, state) != 0)
3792 {
3793 break;
3794 }
3795
3796 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3797 {
3798 break;
3799 }
3800 RLOGD("lynq_wifi_ap_start curr state %s", state);
3801 usleep(SLEEP_TIME_ON_IDLE);
3802 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003803 pthread_mutex_lock(&s_global_check_mutex);
3804 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3805 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003806 return 0;
3807// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003808}
qs.xiongfcc914b2023-07-06 21:16:20 +08003809int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3810{
3811 char LYNQ_DISABLE_CMD[128]={0};
3812 CHECK_IDX(idx, CTRL_STA);
3813 CHECK_WPA_CTRL(CTRL_STA);
3814 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3815 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3816 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3817 return 0;
3818}
qs.xiong7a105ce2022-03-02 09:43:11 -05003819
you.chen35020192022-05-06 11:30:57 +08003820//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3821// int i, count;
3822// char *p;
3823// const char * FLAG_SSID = "ssid=";
3824// const char * FLAG_SBSID = "bssid=";
3825// const char * FLAG_KEY_MGMT = "key_mgmt=";
3826// const char * FLAG_FREQ = "freq=";
3827// char lynq_sta_cmd[MAX_CMD];
3828// char *split_lines[128] = {0};
3829
3830// CHECK_WPA_CTRL(CTRL_AP);
3831
3832// sprintf(lynq_sta_cmd, "STA %s", bssid);
3833
3834// DO_REQUEST(lynq_sta_cmd);
3835
3836// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3837
3838// for(i=0; i < count; i++) {
3839// p = strstr(split_lines[i], FLAG_SSID);
3840// if (p != NULL) {
3841// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3842// continue;
3843// }
3844// }
3845
3846// lynq_get_interface_ip(idx, ap->ap_ip);
3847// lynq_ap_password_set(idx, ap->psw);
3848
3849// return 0;
3850//}
3851
3852static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3853 curr_status_info curr_state;
3854 curr_state.ap = ap;
3855 curr_state.state = NULL;
3856 return inner_get_status_info(interface, &curr_state);
3857}
3858
3859int 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 -04003860{
qs.xiong5071c802023-09-06 14:04:15 +08003861 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003862 int index, line_count;
3863 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003864 const char *lynq_first_sta_cmd = "STA-FIRST";
3865 char lynq_next_sta_cmd[MAX_CMD];
3866 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003867 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003868
you.chen35020192022-05-06 11:30:57 +08003869 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003870
you.chen35020192022-05-06 11:30:57 +08003871 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003872
you.chenb95401e2023-05-12 19:39:06 +08003873 // ap_info_s * tmp_ap;
3874 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003875 if (ap == NULL || list == NULL || len == NULL)
3876 {
3877 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003878 return -1;
3879 }
3880
you.chenb95401e2023-05-12 19:39:06 +08003881 // ap = &tmp_ap;
3882 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003883 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003884 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003885
you.chenb95401e2023-05-12 19:39:06 +08003886 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003887 {
you.chenb95401e2023-05-12 19:39:06 +08003888 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003889 return -1;
3890 }
3891
3892 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3893 lynq_ap_password_get(idx, (*ap)->psw);
3894
you.chen35020192022-05-06 11:30:57 +08003895 DO_REQUEST(lynq_first_sta_cmd);
3896
3897 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003898 while (reply_len > 0)
3899 {
3900 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003901 {
you.chen35020192022-05-06 11:30:57 +08003902 break;
3903 }
3904 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3905 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3906 strcpy(bssid[index], split_lines[0]);
3907 index++;
3908 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3909 reply_len = MAX_RET;
3910 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003911 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 +08003912 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003913 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003914 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003915 break;
3916 }
3917 }
3918
3919 *len = index;
3920
3921 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003922 for (index=0; index < *len; index++)
3923 {
you.chend2fef3f2023-02-13 10:50:35 +08003924 dev_info = &(*list)[index];
3925 memset(dev_info, 0, sizeof(device_info_s));
3926 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3927 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3928 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3929 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003930 free(bssid[index]);
3931 }
qs.xiong5071c802023-09-06 14:04:15 +08003932 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003933 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003934}
3935
you.chen35020192022-05-06 11:30:57 +08003936int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003937{
you.chen35020192022-05-06 11:30:57 +08003938 int i, count, index, count_words;
3939 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3940 char *split_lines[128] = {0};
3941 char *split_words[128] = {0};
3942 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003943
qs.xiong9fbf74e2023-03-28 13:38:22 +08003944 if (list == NULL || len == NULL)
3945 {
you.chen35020192022-05-06 11:30:57 +08003946 return -1;
3947 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003948
you.chen9ac66392022-08-06 17:01:16 +08003949 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3950 {
3951 usleep(100 * 1000);
3952 }
3953
you.chen35020192022-05-06 11:30:57 +08003954 CHECK_IDX(idx, CTRL_STA);
3955
3956 CHECK_WPA_CTRL(CTRL_STA);
3957
3958 DO_REQUEST(lynq_scan_result_cmd);
3959
3960 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3961 *len = count - 1;
3962 *list = malloc(sizeof (scan_info_s) * *len);
3963
3964 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003965 for (index=0; index <count_words; index++)
3966 {
3967 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003968 }
3969
qs.xiong9fbf74e2023-03-28 13:38:22 +08003970 for(index = 1;index < count; index++)
3971 {
3972 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003973 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003974 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3975 if (count_words < 4)
3976 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003977 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003978 //bssid / frequency / signal level / flags / ssid
3979 p = (*list) + index - 1;
3980 strcpy(p->mac, split_words[0]);
3981 p->band = convert_band_from_freq(atoi(split_words[1]));
3982 p->rssi = -1 * atoi( split_words[2]);
3983 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003984 if (count_words == 4) // ssid hided
3985 {
3986 p->ssid[0] = '\0';
3987 }
3988 else
3989 {
3990 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3991 }
you.chen35020192022-05-06 11:30:57 +08003992 }
3993
3994 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003995}
qs.xiong97fa59b2022-04-07 05:41:29 -04003996
you.chen35020192022-05-06 11:30:57 +08003997int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3998{
3999 int count, net_no, index;
4000 int net_no_list[128];
4001 lynq_wifi_auth_s net_auth;
4002 char lynq_remove_cmd[MAX_CMD];
4003
qs.xiong9fbf74e2023-03-28 13:38:22 +08004004 if (ssid == NULL || *ssid == '\0')
4005 {
4006 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08004007 return -1;
4008 }
4009
4010 CHECK_IDX(idx, CTRL_STA);
4011
4012 CHECK_WPA_CTRL(CTRL_STA);
4013
4014 net_no = -1;
4015 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4016
qs.xiong9fbf74e2023-03-28 13:38:22 +08004017 for (index=0; index < count; index++)
4018 {
you.chen35020192022-05-06 11:30:57 +08004019 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004020 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4021 {
you.chen35020192022-05-06 11:30:57 +08004022 net_no = net_no_list[index];
4023 break;
4024 }
4025 }
4026
qs.xiong9fbf74e2023-03-28 13:38:22 +08004027 if (net_no < 0)
4028 {
you.chen35020192022-05-06 11:30:57 +08004029 return 0;
4030 }
4031
4032 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4033
4034 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
4035 DO_OK_FAIL_REQUEST(cmd_save_config);
4036
4037 return 0;
4038}
4039
4040int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004041{
you.chend2fef3f2023-02-13 10:50:35 +08004042 int count, index;
you.chen35020192022-05-06 11:30:57 +08004043 int net_no_list[128];
4044 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004045 RLOGD("enter lynq_get_sta_saved_ap api\n");
4046 if (list == NULL || len == NULL)
4047 {
4048 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004049 return -1;
4050 }
4051
4052 CHECK_IDX(idx, CTRL_STA);
4053
4054// CHECK_WPA_CTRL(CTRL_STA);
4055
4056 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004057 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004058
you.chen057aac42023-04-13 14:06:58 +08004059 if (count < 0)
4060 {
4061 RLOGE("list network fail");
4062 return count;
4063 }
4064 else if (count == 0)
4065 {
4066 *list = NULL;
4067 *len = 0;
4068 return 0;
4069 }
4070
you.chen35020192022-05-06 11:30:57 +08004071 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004072 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004073 *len = count;
4074
qs.xiong9fbf74e2023-03-28 13:38:22 +08004075 for (index=0; index < count; index++)
4076 {
you.chen35020192022-05-06 11:30:57 +08004077 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004078 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004079 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004080 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004081 {
you.chen35020192022-05-06 11:30:57 +08004082 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4083 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004084 else
you.chen057aac42023-04-13 14:06:58 +08004085 {
you.chen35020192022-05-06 11:30:57 +08004086 (*list)[index].base_info.band = -1;
4087 }
you.chen057aac42023-04-13 14:06:58 +08004088 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004089 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004090 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004091 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004092 return 0;
4093}
4094
4095int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4096{
qs.xiong20202422023-09-06 18:01:18 +08004097 if ( g_sta_conncet_status_flag != 0 )
4098 {
4099 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08004100 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004101 }
qs.xiongc8d92a62023-03-29 17:36:14 +08004102 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004103 const char *lynq_scan_cmd = "SCAN";
4104
4105 CHECK_IDX(idx, CTRL_STA);
4106
4107 CHECK_WPA_CTRL(CTRL_STA);
4108
you.chen0df3e7e2023-05-10 15:56:26 +08004109 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4110 {
4111 RLOGD("tmp clear scanlist");
4112 system(clean_last_re);
4113 }
you.chen9ac66392022-08-06 17:01:16 +08004114 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004115 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004116 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4117 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004118 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004119 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4120 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004121 g_sta_scan_finish_flag = 1;
4122 return -1;
4123 }
you.chen35020192022-05-06 11:30:57 +08004124
4125 return 0;
4126}
4127
4128int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004129 if (cb == NULL)
4130 {
4131 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004132 return -1;
4133 }
4134
you.chen6d247052023-06-01 16:39:54 +08004135 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004136 g_ap_callback_priv = priv;
4137 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004138 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004139
you.chen6d247052023-06-01 16:39:54 +08004140 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004141 if (g_ap_watcher_pid == 0 )
4142 {
4143 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4144 {
4145 g_ap_watcher_pid = 0;
4146 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4147 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4148 return -1;
4149 }
4150 }
4151
4152 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4153 RLOGD("creat APWatcherTheradProc susccs");
4154
you.chen35020192022-05-06 11:30:57 +08004155 return 0;
4156}
4157
4158int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004159 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004160 if (g_ap_callback_priv == priv)
4161 {
you.chen35020192022-05-06 11:30:57 +08004162 g_ap_callback_func = NULL;
4163 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004164 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004165 return 0;
4166 }
you.chen6d247052023-06-01 16:39:54 +08004167 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004168 return -1;
4169}
4170
4171int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004172 if (cb == NULL)
4173 {
4174 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004175 return -1;
4176 }
4177
you.chen6d247052023-06-01 16:39:54 +08004178 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004179 g_sta_callback_priv = priv;
4180 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004181 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004182
you.chen6d247052023-06-01 16:39:54 +08004183 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004184 if (g_sta_watcher_pid == 0 ) {
4185 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4186 {
4187 g_sta_watcher_pid = 0;
4188 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4189 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4190 return -1;
4191 }
4192 }
4193
4194 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4195 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004196 return 0;
4197}
4198
4199int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004200 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004201 if (g_sta_callback_priv == priv)
4202 {
you.chen35020192022-05-06 11:30:57 +08004203 g_sta_callback_func = NULL;
4204 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004205 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004206 return 0;
4207 }
you.chen6d247052023-06-01 16:39:54 +08004208 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004209 return -1;
4210}
4211
qs.xiongfcc914b2023-07-06 21:16:20 +08004212int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4213 if (cb == NULL)
4214 {
4215 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4216 return -1;
4217 }
4218 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4219 g_sta_auto_callback_priv = priv;
4220 g_sta_auto_callback_func = cb;
4221 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4222 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4223 if (g_sta_auto_watcher_pid == 0 ) {
4224 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4225 {
4226 g_sta_auto_watcher_pid = 0;
4227 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4228 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4229 return -1;
4230 }
4231 }
4232 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4233 RLOGD("creat STAWatcherTheradProc susccs");
4234 return 0;
4235}
4236int lynq_unreg_sta_auto_event_callback(void * priv) {
4237 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4238 if (g_sta_auto_callback_priv == priv)
4239 {
4240 g_sta_auto_watcher_stop_flag = 1;
4241 if (g_sta_auto_watcher_pid != 0)
4242 {
4243 pthread_join(g_sta_auto_watcher_pid, NULL);
4244 }
4245 g_sta_auto_watcher_pid = 0;
4246 g_sta_auto_callback_func = NULL;
4247 g_sta_auto_callback_priv = NULL;
4248 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4249 return 0;
4250 }
4251 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4252 return -1;
4253}
you.chen35020192022-05-06 11:30:57 +08004254int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4255{
4256 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004257 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004258 CHECK_IDX(idx, CTRL_AP);
4259
qs.xiong9fbf74e2023-03-28 13:38:22 +08004260 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4261 {
you.chen35020192022-05-06 11:30:57 +08004262 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4263 return 0;
4264 }
4265
qs.xiong9fbf74e2023-03-28 13:38:22 +08004266 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4267 {
you.chen35020192022-05-06 11:30:57 +08004268 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4269 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004270 else
4271 {
you.chen35020192022-05-06 11:30:57 +08004272 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4273 }
4274
4275 return 0;
4276}
4277
4278int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4279 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004280 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004281 CHECK_IDX(idx, CTRL_STA);
4282
qs.xiong9fbf74e2023-03-28 13:38:22 +08004283 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4284 {
you.chen35020192022-05-06 11:30:57 +08004285 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4286 return 0;
4287 }
4288
qs.xiong9fbf74e2023-03-28 13:38:22 +08004289 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4290 {
you.chen35020192022-05-06 11:30:57 +08004291 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4292 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004293 else
4294 {
you.chen35020192022-05-06 11:30:57 +08004295 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4296 }
4297
4298 return 0;
4299}
4300
4301int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4302// CHECK_IDX(idx, CTRL_AP);
4303// int ret = 0;
4304// size_t reply_len = MAX_RET;
4305// char cmd_reply[MAX_RET]={0};
4306// const char * cmd_str = "GET country";
4307// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4308// do{
4309// if (NULL == s_lynq_wpa_ctrl) {
4310// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4311// if (NULL == s_lynq_wpa_ctrl ) {
4312// printf("wpa_ctrl_open fail\n");
4313// return -1;
4314// }
4315// }
4316// }while(0);
4317
4318// do {
4319// reply_len = MAX_RET;
4320// cmd_reply[0] = '\0';
4321// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004322// 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 +08004323// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004324// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004325// return ret;
4326// }
4327// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004328// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004329// }while(0);
4330
4331 FILE *fp;
4332 size_t i = 0;
4333 char lynq_cmd_ret[MAX_RET]={0};
4334
4335// CHECK_IDX(idx, CTRL_AP);
4336
4337 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004338 {
4339 perror("popen error!");
4340 return -1;
4341 }
you.chen35020192022-05-06 11:30:57 +08004342 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4343 {
4344 perror("fread fail!");
4345 return -1;
4346 }
4347
qs.xiong9fbf74e2023-03-28 13:38:22 +08004348 for(i=0; i < strlen(lynq_cmd_ret); i++)
4349 {
4350 if (lynq_cmd_ret[i] == ' ')
4351 {
you.chen35020192022-05-06 11:30:57 +08004352 lynq_cmd_ret[i] = '\0';
4353 break;
4354 }
4355 }
4356
4357 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004358 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004359
4360 int ret=pclose(fp);
4361 if(ret==-1)
4362 {
4363 perror("close file faild");
4364 }
4365
4366 return 0;
4367}
4368
qs.xiong44fac672023-08-29 16:15:55 +08004369
you.chen705a7ef2023-06-01 22:06:45 +08004370static int check_and_init_uci_config(char * country_code)
4371{
4372 FILE * fp;
4373 int is_different = 0;
4374 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4375 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4376 const char * commit_uci_cmd ="uci commit";
4377 char set_country_cmd[MAX_CMD];
4378 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004379
you.chen705a7ef2023-06-01 22:06:45 +08004380 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004381
you.chen705a7ef2023-06-01 22:06:45 +08004382 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004383 {
you.chen705a7ef2023-06-01 22:06:45 +08004384 if (0 != system(create_uci_cmd))
4385 {
4386 RLOGE("creat_uci_cmd fail");
4387 return -1;
4388 }
4389 is_different = 1;
4390 }
4391
4392 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4393 {
4394 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004395 return -1;
4396 }
4397
you.chen705a7ef2023-06-01 22:06:45 +08004398 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004399 {
you.chen705a7ef2023-06-01 22:06:45 +08004400 RLOGE("fread fail!");
4401 fclose(fp);
4402 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004403 }
4404
you.chen705a7ef2023-06-01 22:06:45 +08004405 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4406 {
qs.xiong44fac672023-08-29 16:15:55 +08004407 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 +08004408 is_different = 1;
4409 }
4410
4411 fclose(fp);
4412
4413 if (is_different)
4414 {
4415 if ( 0 != system(set_country_cmd))
4416 {
4417 RLOGE("set_country_cmd fail");
4418 return -1;
4419 }
4420 if (0 != system(commit_uci_cmd))
4421 {
4422 RLOGE("commmit fail");
4423 }
4424 }
4425
4426 return is_different;
4427}
4428
4429int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4430 char check_current_code[10];
4431 const char * support_country[] = {"CN", "EU"};
4432
4433 int ret,is_different, i, cc_count;
4434
4435 if (country_code == NULL || country_code[0] == '\0')
4436 {
4437 RLOGE("bad country code\n");
4438 return -1;
4439 }
4440
4441 cc_count = sizeof (support_country) / sizeof (char*);
4442 for(i=0; i < cc_count; i++)
4443 {
4444 if (strcmp(support_country[i], country_code) == 0)
4445 {
4446 break;
4447 }
4448 }
4449
4450 if (i >= cc_count)
4451 {
4452 RLOGE("unspported country code %s\n", country_code);
4453 return -1;
4454 }
4455
4456 is_different = check_and_init_uci_config(country_code);
4457 if( is_different < 0 )
4458 {
4459 RLOGE("init set uci fail\n");
4460 return -1;
4461 }
4462
4463 ret = lynq_get_country_code(idx,check_current_code);
4464 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4465 {
4466 ret = lynq_wifi_disable();
4467 if(ret != 0 )
4468 {
4469 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4470 return -1;
4471 }
4472 }
4473
4474 return 0;
you.chen35020192022-05-06 11:30:57 +08004475}
4476
4477int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4478{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004479 RLOGD("enter lynq_get_connect_ap_mac\n");
4480 if (mac == NULL)
4481 {
4482 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004483 return -1;
4484 }
4485
4486 CHECK_IDX(idx, CTRL_STA);
4487 ap_info_s ap;
4488 ap.ap_mac[0] = '\0';
4489
qs.xiong9fbf74e2023-03-28 13:38:22 +08004490 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4491 {
you.chen35020192022-05-06 11:30:57 +08004492 return -1;
4493 }
4494 strcpy(mac, ap.ap_mac);
4495
4496 return 0;
4497}
4498
4499int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4500{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004501 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004502 struct ifaddrs *ifaddr_header, *ifaddr;
4503 struct in_addr * ifa;
4504 const char * ifaName = "wlan0";
4505 if (ip == NULL)
4506 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004507 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004508 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004509 }
you.chenf58b3c92022-06-21 16:53:48 +08004510
qs.xiong9fbf74e2023-03-28 13:38:22 +08004511 if (idx == 1)
4512 {
you.chen0df3e7e2023-05-10 15:56:26 +08004513 ifaName = inner_get_ap_interface_name();
4514 if (ifaName == NULL)
4515 {
4516 RLOGE("[lynq_get_interface_ip] ap name get fail");
4517 return -1;
4518 }
you.chen9ac66392022-08-06 17:01:16 +08004519 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004520 else if (idx != 0)
4521 {
you.chen35020192022-05-06 11:30:57 +08004522 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004523 }
you.chen35020192022-05-06 11:30:57 +08004524
you.chen9ac66392022-08-06 17:01:16 +08004525 if (getifaddrs(&ifaddr_header) == -1)
4526 {
you.chen35020192022-05-06 11:30:57 +08004527 perror("getifaddrs");
4528 return -1;
4529 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004530 }
you.chen35020192022-05-06 11:30:57 +08004531
4532
you.chen9ac66392022-08-06 17:01:16 +08004533 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4534 {
4535 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004536 continue;
you.chen9ac66392022-08-06 17:01:16 +08004537 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4538 {
4539 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4540 {
4541 // is a valid IP4 Address
4542 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4543 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004544 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004545 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004546 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004547 return 0;
4548 }
4549 }
4550 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004551 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004552 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004553 return -1;
you.chen35020192022-05-06 11:30:57 +08004554}
4555
4556int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4557{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004558 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004559 int count;
4560 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004561 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004562 char *split_words[128] = {0};
4563 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4564
4565 CHECK_WPA_CTRL(idx);
4566
4567 DO_REQUEST(lynq_get_mac_cmd);
4568
qs.xiong9fbf74e2023-03-28 13:38:22 +08004569 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4570 {
4571 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004572 return -1;
4573 }
4574
4575 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4576
qs.xiong9fbf74e2023-03-28 13:38:22 +08004577 if (count < 2)
4578 {
you.chen35020192022-05-06 11:30:57 +08004579 return -1;
4580 }
4581
qs.xiong9fbf74e2023-03-28 13:38:22 +08004582 for (i=0; i < strlen(split_words[1]); i++ )
4583 {
4584 if (split_words[1][i] != ' ')
4585 {
you.chen35020192022-05-06 11:30:57 +08004586 break;
4587 }
4588 }
4589
qs.xiongdd6e44c2023-08-08 15:02:53 +08004590 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004591
4592 return 0;
4593}
4594
4595int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4596{
4597// int count;
4598// char *split_words[128] = {0};
4599// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4600
4601// if (rssi == NULL) {
4602// return -1;
4603// }
4604
4605// CHECK_IDX(idx, CTRL_STA);
4606
4607// CHECK_WPA_CTRL(CTRL_STA);
4608
4609// DO_REQUEST(lynq_get_rssi_cmd);
4610
4611// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4612// return -1;
4613// }
4614
4615// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4616
4617// if (count < 2) {
4618// return -1;
4619// }
4620
4621// *rssi = atoi(split_words[1]) * -1;
4622
you.chen35020192022-05-06 11:30:57 +08004623 char lynq_cmd_ret[MAX_RET]={0};
4624
qs.xiongff0ae0f2022-10-11 15:47:14 +08004625/*******change other cmd to get rssi*******
4626 *
4627 *wl rssi ---> wl -i wlan0 rssi
4628 *
4629 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004630 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004631 {
you.chen23c4a5f2023-04-12 16:46:00 +08004632 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004633 return -1;
4634 }
you.chen9f17e4d2022-06-06 17:18:18 +08004635 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004636/****** if got rssi is 0,means sta didn't connected any device****/
4637 if(*rssi == 0)
4638 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004639 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004640 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004641 }
you.chen35020192022-05-06 11:30:57 +08004642
4643 return 0;
4644}
4645
4646int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4647{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004648 RLOGD("enter lynq_get_connect_ap_band\n");
4649 if (band == NULL)
4650 {
you.chen35020192022-05-06 11:30:57 +08004651 return -1;
4652 }
4653
4654 CHECK_IDX(idx, CTRL_STA);
4655 ap_info_s ap;
4656 ap.band = -1;
4657
qs.xiong9fbf74e2023-03-28 13:38:22 +08004658 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4659 {
you.chen35020192022-05-06 11:30:57 +08004660 return -1;
4661 }
4662 *band = ap.band;
4663
4664 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004665}
you.chenf58b3c92022-06-21 16:53:48 +08004666
4667int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4668{
you.chenb95401e2023-05-12 19:39:06 +08004669 int ret;
4670 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004671 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004672
4673 if (ip == NULL)
4674 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004675 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004676 return -1;
4677 }
4678
4679 CHECK_IDX(idx, CTRL_STA);
4680
qs.xionge4cbf1c2023-02-28 18:22:49 +08004681 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004682 {
4683 return -1;
4684 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004685
you.chenb95401e2023-05-12 19:39:06 +08004686 ip[0] = '\0';
4687 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4688 if (ret != 0)
4689 {
4690 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4691 return -1;
4692 }
4693
4694 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4695 {
4696 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004697 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004698 if (ret != 0)
4699 {
4700 ip[0] = '\0';
4701 return 0;
4702 }
4703 else
4704 {
4705 p = strchr(ip, '\n');
4706 if (p != NULL)
4707 {
4708 *p = '\0';
4709 }
4710 }
4711 }
4712 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004713}
4714
qs.xionge02a5252023-09-20 14:00:21 +08004715int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4716{
4717 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4718 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4719}
4720
qs.xiong026c5c72022-10-17 11:15:45 +08004721int lynq_ap_connect_num(int sta_number)
4722{
4723 char lynq_limit_cmd[32]={0};
4724 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004725 if((sta_number < 1 ) && (sta_number > 15))
4726 {
4727 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004728 return -1;
4729 }
4730 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4731 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004732 if(ret != 0)
4733 {
4734 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004735 }
4736 return 0;
4737}
you.chenf58b3c92022-06-21 16:53:48 +08004738
qs.xiong77905552022-10-17 11:19:57 +08004739int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4740{
4741
4742 char lynq_wifi_acs_cmd[128]={0};
4743 char lynq_cmd_mode[128]={0};
4744 char lynq_cmd_slect[128]={0};
4745
qs.xiong9fbf74e2023-03-28 13:38:22 +08004746 if((acs_mode != 2) && (acs_mode != 5))
4747 {
qs.xiong77905552022-10-17 11:19:57 +08004748 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4749 }
4750
qs.xiong9fbf74e2023-03-28 13:38:22 +08004751 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4752 {
qs.xiong77905552022-10-17 11:19:57 +08004753 return -1;
4754 }
4755
4756 CHECK_IDX(idx, CTRL_AP);
4757
4758 CHECK_WPA_CTRL(CTRL_AP);
4759
4760 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4761 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4762 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4763
4764 DO_OK_FAIL_REQUEST(cmd_disconnect);
4765 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4766 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4767 DO_OK_FAIL_REQUEST(cmd_save_config);
4768 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4769
4770 return 0;
4771}
you.chen0f5c6432022-11-07 18:31:14 +08004772//you.chen add for tv-box start
4773static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4774 FILE *fp;
4775 //printf("to exec cmd:%s\n", str_cmd);
4776 if((fp=popen(str_cmd,"r"))==NULL)
4777 {
4778 perror("popen error!");
4779 return -1;
4780 }
4781 if((fread(str_cmd_ret,max_len,1,fp))<0)
4782 {
4783 perror("fread fail!");
4784 fclose(fp);
4785 return -1;
4786 }
4787 fclose(fp);
4788 return 0;
4789}
4790
4791static int get_netmask_length(const char* mask)
4792{
4793 int masklen=0, i=0;
4794 int netmask=0;
4795
4796 if(mask == NULL)
4797 {
4798 return 0;
4799 }
4800
4801 struct in_addr ip_addr;
4802 if( inet_aton(mask, &ip_addr) )
4803 {
4804 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004805 }else
4806 {
you.chen0f5c6432022-11-07 18:31:14 +08004807 netmask = 0;
4808 return 0;
4809 }
4810
4811 while(0 == (netmask & 0x01) && i<32)
4812 {
4813 i++;
4814 netmask = netmask>>1;
4815 }
4816 masklen = 32-i;
4817 return masklen;
4818}
4819
4820static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4821 int mask_len;
4822 char *p;
4823 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004824 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4825 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004826 return -1;
4827 p = strstr(str_cmd_ret, "Mask:");
4828 if (p == NULL)
4829 return -1;
4830 mask_len = get_netmask_length(p + 5);
4831 if (mask_len == 0)
4832 return -1;
4833 p = strstr(str_cmd_ret, "inet addr:");
4834 if (p == NULL)
4835 return -1;
4836 strcpy(tmp, p + 10);
4837 p = strstr(tmp, " ");
4838 if (p != NULL)
4839 *p = '\0';
4840 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4841 return 0;
4842}
4843
4844static void GBWWatchThreadProc() {
4845 int i,n, nloop, nmax, ncheckcount, nidlecount;
4846 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4847 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4848 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4849 char *results[16] = {0};
4850 char str_cmd[256] = {0};
4851 char str_cmd_ret[128] = {0};
4852 char dest_ip[32] = {0};
4853 lastAP1Bytes = lastAP2Bytes = 0;
4854 lastAP1Drop = lastAP2Drop = 0;
4855 lastAP1Speed = lastAP2Speed = 0;
4856 setAP1Speed = 50;
4857 setAP2Speed = 80;
4858 nloop = 0;
4859 nmax = 6;
4860 ncheckcount = nidlecount = 0;
4861
you.chen0df3e7e2023-05-10 15:56:26 +08004862 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004863 {
4864 RLOGE("------gbw thread run\n");
4865 return;
4866 }
4867
qs.xiong9fbf74e2023-03-28 13:38:22 +08004868 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004869 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4870 while (dest_ip[0] == '\0') {
4871 sleep(1);
4872 str_cmd_ret[0] = '\0';
4873 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4874 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4875 if (str_cmd_ret[n] == '\n'){
4876 str_cmd_ret[n] = '\0';
4877 break;
4878 }
4879 }
4880 if (str_cmd_ret[0] != '\0')
4881 {
4882 strcpy(dest_ip, str_cmd_ret);
4883 }
4884 }
4885
you.chenc9928582023-04-24 15:39:37 +08004886 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4887 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4888 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 +08004889 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4890 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004891 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004892 return;
4893 }
you.chenc9928582023-04-24 15:39:37 +08004894 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);
4895 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);
4896 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 +08004897
4898 while (1) {
4899 sleep(1);
4900 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004901 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4902 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4903 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004904 continue;
4905 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004906 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004907 if (n > 9) {
4908 if (strcmp(results[1], "Sent") == 0) {
4909 currAP1Bytes = atoll(results[2]);
4910 }
4911 if (strcmp(results[6], "(dropped") == 0) {
4912 currAP1Drop = atoi(results[7]);
4913 }
4914 }
4915
4916 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004917 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4918 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4919 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004920 continue;
4921 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004922 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004923 if (n > 9) {
4924 if (strcmp(results[1], "Sent") == 0) {
4925 currAP2Bytes = atoll(results[2]);
4926 }
4927 if (strcmp(results[6], "(dropped") == 0) {
4928 currAP2Drop = atoi(results[7]);
4929 }
4930 }
4931
4932 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4933 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4934 lastAP1Bytes = currAP1Bytes;
4935 lastAP2Bytes = currAP2Bytes;
4936 continue;
4937 }
4938
4939 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4940 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4941 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4942 lastAP1Speed = currAP1Speed;
4943 lastAP2Speed = currAP2Speed;
4944 lastAP1Bytes = currAP1Bytes;
4945 lastAP2Bytes = currAP2Bytes;
4946
4947 currSetAP1Speed = setAP1Speed;
4948 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4949 ncheckcount++;
4950 if (ncheckcount > 3) {
4951 ncheckcount = 0;
4952 currSetAP1Speed = 5;
4953 }
4954 }
4955 else {
4956 ncheckcount = 0;
4957 if (currAP1Speed < 5)
4958 nidlecount++;
4959 else
4960 nidlecount = 0;
4961
4962 }
4963
4964 if (nidlecount > 60 ){
4965 currSetAP1Speed = 50;
4966 }
4967
4968 if (currSetAP1Speed != setAP1Speed) {
4969 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004970 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4971 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004972 }
4973 }
4974}
4975
4976int enableGBW(const char* mac) {
4977 int i,len;
4978 char get_ipaddr_cmd[128]={0};
4979 ap_info_s *ap;
4980 device_info_s * list;
4981
4982 if (mac == NULL || g_gbw_enabled == 1)
4983 return -1;
4984 len = strlen(mac);
4985 g_gbw_mac = malloc(len + 1);
4986 for(i=0;i<len;i++) {
4987 if (mac[i] >= 'A' && mac[i] <= 'Z')
4988 {
4989 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4990 }
4991 else
4992 g_gbw_mac[i] = mac[i];
4993 }
4994 g_gbw_mac[i] = '\0';
4995 g_gbw_enabled = 1;
4996
4997 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4998 if (system(get_ipaddr_cmd) == 0) {
4999 //startGBW();
5000 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
5001 for (i=0;i<len;i++) {
5002 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
5003 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
5004 startGBW();
5005 }
5006 free(ap);
5007 free(list);
5008 }
5009 }
5010 return 0;
5011}
5012
5013int disableGBW() {
5014 stopGBW();
5015 free(g_gbw_mac);
5016 g_gbw_mac = NULL;
5017 g_gbw_enabled = 1;
5018 return 0;
5019}
5020
5021static int startGBW() {
5022 if (g_gbw_watcher_pid != 0) {
5023 stopGBW();
5024 }
5025 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5026}
5027
5028static int stopGBW() {
5029 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005030 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005031 pthread_cancel(g_gbw_watcher_pid);
5032 pthread_join(g_gbw_watcher_pid, &retval);
5033 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005034 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5035 if (s_ap_iterface_name[0] != '\0')
5036 {
5037 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5038 system(cmd);
5039 }
you.chen0f5c6432022-11-07 18:31:14 +08005040}
5041//you.chen add for tv-box end