blob: 3848f9c9b08f3e8fb12180c970996f3e52a7ec8b [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;
137static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
138static 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.xiong31163d62023-07-11 18:54:40 +0800481 else if (strstr(msg_notify, "WoWLAN is enabled") != NULL || strstr(msg_notify, "Failed to start AP functionality") != NULL || strstr(msg_notify, "Could not connect to kernel driver") != NULL )
482 {
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
513 if (state == LYNQ_WIFI_STATUS_EGNORE)
514 {
515 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
516 {
517 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
518 }
519 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
520 {
521 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
522 }
523 }
524 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
525 {
526 s_sta_error_number = error_num;
527 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
528 {
529 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
530 pthread_cond_signal(&s_global_check_cond);
531 }
532 }
533 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
534 {
535 s_sta_status = INNER_STA_STATUS_CONNECTED;
536 pthread_cond_signal(&s_global_check_cond);
537 }
538 pthread_mutex_unlock(&s_global_check_mutex);
539}
540
qs.xiongb37f8c42023-09-13 21:21:58 +0800541static int lynq_split(char * str, int len, char delimiter, char * results[]);
542static inline char inner_convert_char(char in);
543static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
544static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
545
546
547
qs.xiong44fac672023-08-29 16:15:55 +0800548/*
549just tmp add for fix sta connect ap fail check ap connect info
550return 0 --->Current no sta device connect this AP
551*/
552static int lynq_connected_ap_sta_status() {
553
554 FILE *fp;
555 size_t i = 0;
556 int ret;
557 char lynq_cmd_ret[MAX_RET]={0};
558
559 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
560 {
561 perror("popen error!");
562 return -1;
563 }
564 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
565 {
566 perror("fread fail!");
567 ret=pclose(fp);
568 if(ret == -1)
569 perror("close file faild");
570 return -1;
571 }
572 if( strlen(lynq_cmd_ret) < MAC_LEN)
573 {
574 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
575 ret=pclose(fp);
576 if(ret==-1)
577 {
578 perror("close file faild");
579 }
580 return 0;
581 }else{
582 ret=pclose(fp);
583 if(ret==-1)
584 {
585 perror("close file faild");
586 }
587 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
588 return 1;
589 }
590}
591
592/*
593 just tmp add for fix sta connect ap fail; check fw status
594 return 1 ----> fw status error; need wl down/up
595*/
596static int check_current_fw_status() {
597
598 FILE *fp;
599 FILE *fp1;
600 size_t i = 0;
601 int ret;
602 char lynq_cmd_ret_2g[MAX_RET]={0};
603 char lynq_cmd_ret_5g[MAX_RET]={0};
604
605 const char * fw_status = "0x0096"; //0x0096 is normal fw status
606
607 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
608 {
609 perror("popen error!");
610 return -1;
611 }
612 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
613 {
614 perror("fread fail!");
615 if(pclose(fp) == -1)
616 perror("close fp file faild");
617 return -1;
618 }
619
620 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
621 {
622 perror("popen error!");
623 if(pclose(fp) == -1)
624 perror("clsoe fp file faild");
625 return -1;
626 }
627 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
628 {
629 perror("fread fail!");
630 if(pclose(fp1) == -1)
631 perror("clsoe fp1 file faild");
632 if(pclose(fp) == -1)
633 perror("clsoe fp file faild");
634 return -1;
635 }
636
637 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
638 {
639 ret=pclose(fp);
640 if(ret==-1)
641 {
642 perror("close fp file faild");
643 }
644 ret=pclose(fp1);
645 if(ret==-1)
646 {
647 perror("close fp1 file faild");
648 }
649 return 0;
650 }else
651 {
652 ret=pclose(fp);
653 if(ret==-1)
654 {
655 perror("close file faild");
656 }
657 if(pclose(fp1) == -1)
658 {
659 perror("clsoe file fp1 faild");
660 }
661 RLOGD("current fw status --error--");
662 return 1;
663 }
664}
665
qs.xiong1d4263a2023-09-06 10:46:23 +0800666/*
667eg: wl counters info
668sh-3.2# wl counters
669counters_version 30
670datalen 1648
671Slice_index: 0
672reinitreason_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)
673reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
674
675check reinit status
676return 0 ===> fw did wl reinit cmd
677*/
678static int check_current_reinit_info()
679{
680 FILE *fp;
681 int ret;
682 char lynq_cmd_ret[MAX_RET]={0};
683 char * dest;
684 char destid[3]={0};
685 if((fp=popen("wl counters","r"))==NULL)
686 {
687 perror("popen error!");
688 return -1;
689 }
690 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
691 {
692 perror("fread fail!");
693 if(pclose(fp) == -1)
694 {
695 perror("close fp file faild");
696 }
697 return -1;
698 }
699 dest = strstr(lynq_cmd_ret,"reinit ");
700 if(dest != NULL)
701 {
702 dest +=strlen("reinit ");
703 RLOGD("current get dest str is %s",dest);
704 memcpy(destid,dest,2);
705 ret = atoi(destid);
706 RLOGD("get current wl counters cmd return counts is %d",ret);
707 if( ret != 0 )
708 {
709 RLOGD("current fw did run cmd wl reinit");
710 if( pclose(fp) == -1 )
711 {
712 perror("close fp file faild");
713 }
714 return 0;
715 }
716 }
717 if( pclose(fp) == -1 )
718 {
719 perror("close fp file faild");
720 }
721 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
722 return -1;
723}
724
qs.xiong44fac672023-08-29 16:15:55 +0800725static void APTmpWatcherThreadProc() {
726
727 int i = 0;
728 int delytime = 300;
729 g_ap_tmp_watcher_stop_flag = 0;
730
731 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
732 while(1)
733 {
734 sleep(1);
735 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800736 if ( (i % 30) == 0 )
737 {
738 if ( check_current_reinit_info() == 0 )
739 {
740 system("wl reset_cnts");
741 system("wl down");
742 system("wl up");
743 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
744 }
745 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800746 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800747 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800748 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800749 {
750 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
751 {
752 system("wl down");
753 system("wl up");
754 }
qs.xiong44fac672023-08-29 16:15:55 +0800755 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800756 }
757 if ( i == delytime )
758 {
qs.xiong44fac672023-08-29 16:15:55 +0800759 i = 0;
760 }
761 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
762 {
763 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
764 return;
765 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800766
qs.xiong44fac672023-08-29 16:15:55 +0800767 }
768
769}
770
qs.xiongf0128b12023-06-29 17:29:39 +0800771static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
772{
773 char LYNQ_DISABLE_CMD[128]={0};
774
775 CHECK_IDX(idx, CTRL_STA);
776 CHECK_WPA_CTRL(CTRL_STA);
777
778 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
779 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
780 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800781
qs.xiongf0128b12023-06-29 17:29:39 +0800782 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800783
qs.xiongf0128b12023-06-29 17:29:39 +0800784}
785
qs.xiongb37f8c42023-09-13 21:21:58 +0800786static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
787{
788 char LYNQ_ENABLE_CMD[128]={0};
789
790 CHECK_IDX(idx, CTRL_STA);
791 CHECK_WPA_CTRL(CTRL_STA);
792
793
794 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
795 RLOGD("LYNQ_ENABLE_CMD is:%d\n",LYNQ_ENABLE_CMD);
796 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
797
798 return 0;
799
800}
801
802static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
803{
804
805 int index,networkid;
806
807 for ( index = 0; index < len ;index++)
808 {
809 networkid = net_no_list[index];
810 if( lynq_wifi_sta_enable_network(idx,networkid != 0) )
811 {
812 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
813 }
814 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
815 }
816 return 0;
817
818}
819
820
821/*
822 dis_net_list user disconnect list
823*/
824static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
825{
826 int count,ncount,index;
827 int flag = 0;
828 int merge_index = 0;
829 int net_no_list[128];
830
831 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
832 for( count = 0; count < index; count++)
833 {
834 for(ncount = 0; ncount < valid_num; ncount++)
835 {
836 if(net_no_list[count] == dis_net_list[ncount])
837 {
838 RLOGD("[wifi]this is history disconnect idx ----> ",net_no_list[count]);
839 flag = 1;
840 break;
841 }
842 }
843 if( flag != 1 )
844 {
845 out[merge_index] = net_no_list[count];
846 merge_index ++;
847 }
848 flag = 0;
849 }
850 * outlen =merge_index;
851 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
852 return;
853}
qs.xiongf0128b12023-06-29 17:29:39 +0800854
qs.xiong455c30b2023-04-12 11:40:02 +0800855void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
856{
857 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800858 char *wpanetid;
859 char destid[3] = {0};
860 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800861 *error = LYNQ_WAIT_CONNECT_ACTIVE;
862 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
863 {
864 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
865 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
866 return;
867 }
868
qs.xiong20202422023-09-06 18:01:18 +0800869 if (strstr(modify, "Trying to associate") != NULL)
870 {
871 RLOGD("Current sta is Trying to associate");
872 *state = LYNQ_WIFI_STATUS_EGNORE;
873 g_sta_conncet_status_flag = 1;
874 return;
875 }
876
qs.xiong455c30b2023-04-12 11:40:02 +0800877 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
878 {
879 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
880 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800881 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800882 return;
883 }
884
885 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
886 {
qs.xiongf0128b12023-06-29 17:29:39 +0800887 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
888 wpanetid = strstr(modify,"id=");
889 if ( wpanetid != NULL )
890 {
891 wpanetid +=strlen("id=");
892 memcpy(destid,wpanetid,2);
893 tmpdisid = atoi(destid);
894
895 }
qs.xiong455c30b2023-04-12 11:40:02 +0800896 pReason = strstr(modify, "reason=");
897 if (pReason != NULL)
898 {
899 pReason += strlen("reason=");
900 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
901 {
902 *error = LYNQ_TIME_OUT;
903 }
904 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
905 {
906 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800907 // tmp fix sta autoconnect connect and disconnect
908 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
909 {
910 RLOGE("stop wlan0 network %d fail",tmpdisid);
911 }
qs.xiong455c30b2023-04-12 11:40:02 +0800912 }
913 else
914 {
915 *error = LYNQ_UNSPECIFIED_REASON;
916 }
qs.xiong455c30b2023-04-12 11:40:02 +0800917 }
918 else
919 {
920 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800921 }
qs.xiongf0128b12023-06-29 17:29:39 +0800922 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800923 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800924 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800925
926 }
927
928 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
929 {
930 *error = LYNQ_NOT_FIND_AP;
931 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
932 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800933 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800934 return;
935 }
936
937
938 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
939 {
940 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
941 pReason = strstr(modify, "status_code=");
942 if (pReason != NULL)
943 {
944 pReason += strlen("status_code=");
945 if (memcmp(pReason, "17", 2) == 0)
946 {
947 *error = LYNQ_AP_UNABLE_HANDLE;
948 }
949 else if (memcmp(pReason, "1",1) == 0)
950 {
951 *error = LYNQ_UNSPECIFIED_REASON;
952 }
953 else
954 {
955 *error = LYNQ_UNSPECIFIED_REASON;
956 }
957
958 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
959 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 +0800960 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800961 return;
962 }
963 else
964 {
965 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
966 *error = LYNQ_UNSPECIFIED_REASON;
967 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
968 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
969 return;
970 }
971 }
972
973 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
974 {
975 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
976 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
977 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
978 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
979 return;
980 }
981
982 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
983 {
984 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
985 *error = LYNQ_WAIT_CONNECT_ACTIVE;
986 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
987 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
988 return;
989 }
990
you.chen32cb31e2023-04-13 14:05:45 +0800991 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800992 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800993 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800994 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
995 return;
996
997}
998
qs.xiongfcc914b2023-07-06 21:16:20 +0800999void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1000{
1001 char *pReason;
1002 char *wpanetid;
1003 char destid[3];
1004 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1005 *networkid = -1;
1006 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1007 {
1008 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1009 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1010 return;
1011 }
1012 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1013 {
1014 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1015 wpanetid = strstr(modify,"id=");
1016 if ( wpanetid != NULL )
1017 {
1018 wpanetid +=strlen("id=");
1019 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1020 if (memcpy(destid,wpanetid,2) != NULL)
1021 {
1022 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1023 *networkid = atoi(destid);
1024 RLOGD("get networkid is %d\n",*networkid);
1025 }
1026 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1027 }
1028 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1029 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1030 return;
1031 }
1032 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1033 {
1034 wpanetid = strstr(modify,"id=");
1035 if ( wpanetid != NULL )
1036 {
1037 wpanetid +=strlen("id=");
1038 if (memcpy(destid,wpanetid,2) != NULL)
1039 {
1040 *networkid = atoi(destid);
1041 RLOGD("get networkid is %d\n",*networkid);
1042 }
1043 }
1044 pReason = strstr(modify, "reason=");
1045 if (pReason != NULL)
1046 {
1047 pReason += strlen("reason=");
1048 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1049 {
1050 *error = LYNQ_TIME_OUT;
1051 }
1052 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1053 {
1054 *error = LYNQ_PSW_ERROR;
1055 }
1056 else
1057 {
1058 *error = LYNQ_UNSPECIFIED_REASON;
1059 }
1060 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1061 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1062 return;
1063 }
1064 else
1065 {
1066 *error = LYNQ_UNSPECIFIED_REASON;
1067 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1068 return;
1069 }
1070 }
1071 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1072 {
1073 wpanetid = strstr(modify,"id=");
1074 if ( wpanetid != NULL )
1075 {
1076 wpanetid +=strlen("id=");
1077 if (memcpy(destid,wpanetid,2) != NULL)
1078 {
1079 *networkid = atoi(destid);
1080 RLOGD("get networkid is %d\n",*networkid);
1081 }
1082 }
1083 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1084 pReason = strstr(modify, "status_code=");
1085 if (pReason != NULL)
1086 {
1087 pReason += strlen("status_code=");
1088 if (memcmp(pReason, "17", 2) == 0)
1089 {
1090 *error = LYNQ_AP_UNABLE_HANDLE;
1091 }
1092 else if (memcmp(pReason, "1",1) == 0)
1093 {
1094 *error = LYNQ_UNSPECIFIED_REASON;
1095 }
1096 else
1097 {
1098 *error = LYNQ_UNSPECIFIED_REASON;
1099 }
1100 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1101 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1102 return;
1103 }
1104 else
1105 {
1106 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1107 *error = LYNQ_UNSPECIFIED_REASON;
1108 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001109 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001110 return;
1111 }
1112 }
1113 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1114 {
1115 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1116 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1117 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1118 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1119 return;
1120 }
1121 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1122 {
1123 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1124 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1125 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1126 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1127 return;
1128 }
1129 RLOGD("EVENT : %s\n", modify);
1130 *error = LYNQ_UNSPECIFIED_REASON;
1131 *state = LYNQ_WIFI_STATUS_EGNORE;
1132 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1133 return;
1134}
you.chen70f377f2023-04-14 18:17:09 +08001135static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1136{
you.chen6d247052023-06-01 16:39:54 +08001137 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001138 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1139 {
1140 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1141 g_sta_callback_func(g_sta_callback_priv, state, error);
1142 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1143 }
you.chen6d247052023-06-01 16:39:54 +08001144 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001145}
qs.xiongfcc914b2023-07-06 21:16:20 +08001146static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1147{
1148 pthread_mutex_lock(&s_sta_callback_mutex);
1149 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1150 {
1151 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1152 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1153 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1154 }
1155 pthread_mutex_unlock(&s_sta_callback_mutex);
1156}
you.chen70f377f2023-04-14 18:17:09 +08001157
you.chen35020192022-05-06 11:30:57 +08001158static void STAWatcherThreadProc() {
1159 size_t len = MAX_RET;
1160 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001161 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001162 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001163 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001164
you.chen6c2dd9c2022-05-16 17:55:28 +08001165 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001166 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001167
you.chen70f377f2023-04-14 18:17:09 +08001168 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001169 while (g_sta_watcher_stop_flag == 0)
1170 {
you.chenf711c8a2023-04-13 13:49:45 +08001171 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001172 {
you.chen35020192022-05-06 11:30:57 +08001173 continue;
1174 }
you.chenf711c8a2023-04-13 13:49:45 +08001175
you.chen6c2dd9c2022-05-16 17:55:28 +08001176 memset(msg_notify, 0, MAX_RET);
1177 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001178 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001179 {
you.chen35020192022-05-06 11:30:57 +08001180 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001181 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1182 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001183 {
you.chen35020192022-05-06 11:30:57 +08001184 g_sta_scan_finish_flag = 1;
1185 }
1186
qs.xiong9fbf74e2023-03-28 13:38:22 +08001187 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001188 {
you.chen35020192022-05-06 11:30:57 +08001189 continue;
1190 }
qs.xiong455c30b2023-04-12 11:40:02 +08001191 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001192 notify_connect_status(state, error);
1193
1194 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001195 {
you.chen70f377f2023-04-14 18:17:09 +08001196 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001197 if (last_state != state)
1198 {
1199 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1200 {
1201 system_call_v("%s %s", sta_status_change_script, "connect");
1202 }
1203 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1204 {
1205 system_call_v("%s %s", sta_status_change_script, "disconnect");
1206 }
1207 }
1208
1209 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001210 }
you.chen35020192022-05-06 11:30:57 +08001211 }
1212 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001213 if (lynq_wpa_ctrl != NULL)
1214 {
you.chen92fd5d32022-05-25 10:09:47 +08001215 wpa_ctrl_detach(lynq_wpa_ctrl);
1216 wpa_ctrl_close(lynq_wpa_ctrl);
1217 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001218}
qs.xiongfcc914b2023-07-06 21:16:20 +08001219static void STAAutoWatcherThreadProc() {
1220 size_t len = MAX_RET;
1221 char msg_notify[MAX_RET];
1222 error_number_s error;
1223 lynq_wifi_sta_status_s state, last_state = -1;
1224 int idle_count = 0;
1225 int networkid;
1226 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1227 g_sta_auto_watcher_stop_flag = 0;
1228 RLOGD("STAAutoWatcherThreadProc thread started ------");
1229 while (g_sta_auto_watcher_stop_flag == 0)
1230 {
1231 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1232 {
1233 continue;
1234 }
1235 memset(msg_notify, 0, MAX_RET);
1236 len = MAX_RET;
1237 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1238 {
1239 msg_notify[len+1] = '\0';
1240 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1241 if (strstr(msg_notify, state_scan_result) != NULL)
1242 {
1243 g_sta_auto_scan_finish_flag = 1;
1244 }
1245 if (g_sta_auto_callback_func == NULL)
1246 {
1247 continue;
1248 }
1249 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1250 notify_auto_connect_status(state, error,networkid);
1251 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1252 {
1253 inner_check_connect_error(msg_notify, state, error);
1254 if (last_state != state)
1255 {
1256 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1257 {
1258 system_call_v("%s %s", sta_status_change_script, "connect");
1259 }
1260 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1261 {
1262 system_call_v("%s %s", sta_status_change_script, "disconnect");
1263 }
1264 }
1265 last_state = state;
1266 }
1267 }
1268 }
1269 if (lynq_wpa_ctrl != NULL)
1270 {
1271 wpa_ctrl_detach(lynq_wpa_ctrl);
1272 wpa_ctrl_close(lynq_wpa_ctrl);
1273 }
1274}
qs.xiongf1b525b2022-03-31 00:58:23 -04001275
you.chen70f377f2023-04-14 18:17:09 +08001276// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1277static void GlobalWatcherThreadProc()
1278{
1279 int ret, connect_timeout, service_abnormal;
1280 error_number_s error_num = -1;
1281 inner_sta_status_s sta_status;
1282 scan_info_s *scan_list = NULL;
1283 int i, scan_len=0;
1284 char connecting_ssid[64];
1285 struct timeval now;
1286
1287 RLOGD("GlobalWatcherThreadProc start to run");
1288
1289 while (1)
1290 {
1291 pthread_mutex_lock(&s_global_check_mutex);
1292 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1293 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1294 {
1295 pthread_mutex_unlock(&s_global_check_mutex);
1296 usleep(50*1000);
1297 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1298 continue;
1299 }
1300
1301 connect_timeout = 0;
1302 service_abnormal = 0;
1303 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1304 {
1305 while (1)
1306 {
1307 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1308 if (ret == ETIME)
1309 {
1310 connect_timeout = 1;
1311 }
1312 else if (ret != 0)
1313 {
1314 gettimeofday(&now,NULL);
1315 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1316 {
1317 usleep(SLEEP_TIME_ON_IDLE);
1318 continue;
1319 }
1320 connect_timeout = 1;
1321 }
1322 sta_status = s_sta_status;
1323 error_num = s_sta_error_number;
1324 s_sta_status = INNER_STA_STATUS_INIT;
1325 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1326 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1327 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1328 break;
1329 }
1330 }
1331 if (s_service_invoke_timeout_cnt > 10)
1332 {
1333 service_abnormal = 1;
1334 s_service_invoke_timeout_cnt = 0;
1335 }
1336 pthread_mutex_unlock(&s_global_check_mutex);
1337
1338 if (service_abnormal == 1)
1339 {
1340 sleep(1);
1341 RLOGE("wpa service is abnormal info app to exit");
1342 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001343
1344 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1345
you.chen70f377f2023-04-14 18:17:09 +08001346 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1347 }
1348
1349 if (sta_status == INNER_STA_STATUS_CANCEL)
1350 {
1351 continue;
1352 }
1353 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1354 {
1355 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1356 }
1357 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1358 {
1359 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1360 {
1361 for(i=0; i < scan_len;i++)
1362 {
1363 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1364 {
1365 error_num = LYNQ_AUTH_ERROR;
1366 break;
1367 }
1368 }
1369 free(scan_list);
1370 }
1371 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1372 }
1373 else if (connect_timeout == 0)
1374 {
1375 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1376 }
1377 else // wait timeout
1378 {
1379 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1380 {
1381 ; // wpa service abnormal
1382 }
1383 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1384 {
1385 RLOGD("GlobalWatcherThreadProc notify connected");
1386 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1387 }
1388 else
1389 {
1390 RLOGD("GlobalWatcherThreadProc notify timeout");
1391 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1392 }
1393 }
1394 } // while (1)
1395}
1396
qs.xiong1af5daf2022-03-14 09:12:12 -04001397int lynq_wifi_enable(void)
1398{
you.chen35020192022-05-06 11:30:57 +08001399 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001400 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001401 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001402 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1403
qs.xiong9fbf74e2023-03-28 13:38:22 +08001404 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1405 {
you.chend2fef3f2023-02-13 10:50:35 +08001406 goto out_enable;
1407 }
1408
you.chenc9928582023-04-24 15:39:37 +08001409 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001410 if (ret != 0)
1411 {
1412 //printf("service state %d\n", ret);
1413 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001414 ret = -1;
1415 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001416 }
lhfe8da902022-10-11 18:55:36 +08001417
you.chen70f377f2023-04-14 18:17:09 +08001418 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1419 {
1420 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1421 if(ret<0)
1422 {
1423 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1424 ret = -1;
1425 goto out_enable;
1426 }
1427 }
1428
you.chend2fef3f2023-02-13 10:50:35 +08001429 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1430 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1431 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1432 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1433out_enable:
1434 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001435 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001436}
1437
qs.xiong1af5daf2022-03-14 09:12:12 -04001438int lynq_wifi_disable(void)
1439{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001440 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001441 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001442 g_ap_watcher_stop_flag = 1;
1443 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001444 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001445 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001446 if (g_ap_watcher_pid != 0)
1447 pthread_join(g_ap_watcher_pid, NULL);
1448 if (g_sta_watcher_pid != 0)
1449 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001450 if (g_sta_auto_watcher_pid != 0)
1451 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001452 if (g_lynq_wpa_ctrl[0] != NULL)
1453 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1454 if (g_lynq_wpa_ctrl[1] != NULL)
1455 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001456 if (g_ap_tmp_watcher_pid != 0)
1457 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001458 g_ap_watcher_pid = 0;
1459 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001460 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001461 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001462 g_lynq_wpa_ctrl[0] = NULL;
1463 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001464 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001465 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001466 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1467 return 0;
1468}
1469
1470static inline char inner_convert_char(char in)
1471{
1472 if (in >= '0' && in <= '9')
1473 {
1474 return in - '0';
1475 }
1476 else if (in >= 'a' && in <= 'f')
1477 {
1478 return in - 'a' + 10;
1479 }
1480 else if (in >= 'A' && in <= 'F')
1481 {
1482 return in - 'A' + 10;
1483 }
1484 else
1485 {
1486 return '\xff';
1487 }
1488}
1489
1490static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1491{
1492 char *p;
1493 size_t pos = 0;
1494 if (NULL == out_ssid)
1495 return;
1496 //printf("input ssid=[%s]\n", ssid);
1497 memset(out_ssid, 0, out_ssid_len);
1498 if (NULL == ssid)
1499 return;
1500 p = strchr(ssid, '\\');
1501 if (NULL == p)
1502 {
1503 strncpy(out_ssid, ssid, out_ssid_len);
1504 //printf(" first %s\n", out_ssid);
1505 }
1506 else
1507 {
1508 pos = p - ssid;
1509 memcpy(out_ssid, ssid, pos);
1510 //printf("pos %lu -- %s\n", pos, out_ssid);
1511 for(; pos < out_ssid_len; pos ++)
1512 {
1513 if (p[0] == '\0')
1514 {
1515 //printf(" out %s\n", out_ssid);
1516 return;
1517 }
1518 else if (p[0] != '\\')
1519 {
1520 out_ssid[pos] = p[0];
1521 p += 1;
1522 }
1523 else if (p[1] == 'x' || p[1] == 'X')
1524 {
1525 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1526 p += 4;
1527 }
1528 else if (p[1] == '\\')
1529 {
1530 out_ssid[pos] = '\\';
1531 p += 2;
1532 }
1533 else if (p[1] == 't')
1534 {
1535 out_ssid[pos] = '\t';
1536 p += 2;
1537 }
1538 else if (p[1] == 'r')
1539 {
1540 out_ssid[pos] = '\r';
1541 p += 2;
1542 }
1543 else if (p[1] == 'n')
1544 {
1545 out_ssid[pos] = '\n';
1546 p += 2;
1547 }//todo find a better way to convert?
1548 }
1549 }
1550 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001551}
qs.xiong1af5daf2022-03-14 09:12:12 -04001552
you.chen35020192022-05-06 11:30:57 +08001553static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001554 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001555 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001556 RLOGD("enter inner_get_param");
1557 if (out_put == NULL)
1558 {
1559 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001560 return -1;
1561 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001562 if (param_name == NULL)
1563 {
1564 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001565 return -1;
1566 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001567 if (param_name[0] == '\0')
1568 {
1569 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001570 return -1;
1571 }
1572
1573 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1574
1575 CHECK_WPA_CTRL(interface);
1576
1577 DO_REQUEST(lynq_cmd_get);
1578
qs.xiong9fbf74e2023-03-28 13:38:22 +08001579 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1580 {
1581 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001582 return -1;
1583 }
1584
you.chena6fa5b22022-05-18 10:28:19 +08001585// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001586 if (strcmp(param_name, "ssid") == 0)
1587 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001588 if (cmd_reply[0] == '\"')
1589 {
you.chend2fef3f2023-02-13 10:50:35 +08001590 ssid_len = reply_len - 1;
1591 memcpy(out_put, cmd_reply + 1, ssid_len);
1592 if (out_put[ssid_len-1] == '\"')
1593 {
1594 out_put[ssid_len-1] = '\0';
1595 }
1596 else
1597 {
1598 out_put[ssid_len] = '\0';
1599 }
1600 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001601 else
1602 {
you.chend2fef3f2023-02-13 10:50:35 +08001603 ssid_len = reply_len / 2;
1604 for(i=0; i<ssid_len; i++)
1605 {
1606 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1607 }
1608 out_put[ssid_len] = '\0';
1609 }
1610 }
1611 else
1612 {
1613 memcpy(out_put, cmd_reply, reply_len + 1);
1614 }
you.chen35020192022-05-06 11:30:57 +08001615 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001616}
qs.xiong1af5daf2022-03-14 09:12:12 -04001617
you.chen35020192022-05-06 11:30:57 +08001618static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1619 int ret = 0;
1620 char * end = str + len - 1;
1621 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001622 while(str < end)
1623 {
1624 if (*str == delimiter)
1625 {
you.chen35020192022-05-06 11:30:57 +08001626 *str++ = '\0';
1627 results[ret++] = str;
1628 continue;
1629 }
1630 str++;
1631 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001632 if (*str == delimiter)
1633 {
you.chen35020192022-05-06 11:30:57 +08001634 *str = '\0';
1635 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001636
you.chen6ed36a62023-04-27 17:51:56 +08001637 results[ret] = NULL;
1638
you.chen35020192022-05-06 11:30:57 +08001639 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001640}
1641
you.chend2fef3f2023-02-13 10:50:35 +08001642static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1643{
1644 char * p;
1645 int ret = 0;
1646 char cmd[256]={0};
1647 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001648 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001649 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001650 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001651 ret = exec_cmd(cmd, ip, ip_len);
1652 p = strchr(ip, '\n');
1653 if (NULL != p)
1654 {
1655 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001656 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001657 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001658 return ret;
1659}
1660
you.chend2fef3f2023-02-13 10:50:35 +08001661static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001662 struct in_addr addr ={0};
1663 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001664 char cmd[64] = {0};
1665 char * p;
1666 int ret;
you.chen35020192022-05-06 11:30:57 +08001667
qs.xiong9fbf74e2023-03-28 13:38:22 +08001668 if (ip == NULL || *ip == '\0' || hostname == NULL)
1669 {
1670 RLOGE("ip == NULL or hostname == NULL");
1671 return -1;
you.chen35020192022-05-06 11:30:57 +08001672 }
1673
you.chend2fef3f2023-02-13 10:50:35 +08001674 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001675 if (inet_aton(ip, &addr) == 0)
1676 {
you.chen35020192022-05-06 11:30:57 +08001677 printf("---inet_aton fail\n");
1678 return -1;
1679 }
1680
1681 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1682
qs.xiong9fbf74e2023-03-28 13:38:22 +08001683 if (ht == NULL)
1684 {
you.chen186d3c32023-05-18 14:19:46 +08001685 hostname[0] = '\0';
1686 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1687 ret = exec_cmd(cmd, hostname, 32);
1688 if (ret == 0)
1689 {
1690 p = strchr(hostname, '\n');
1691 if (p != NULL)
1692 {
1693 *p = '\0';
1694 }
1695 return 0;
1696 }
1697 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001698 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001699 herror(NULL);
1700 return -1;
1701 }
1702
1703 strcpy(hostname, ht->h_name);
1704
1705 return 0;
1706}
1707
1708static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1709{
1710 int count, index, words_count;
1711 char * split_lines[128]= {0};
1712 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001713 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001714 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001715 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001716
1717 CHECK_WPA_CTRL(ap_sta);
1718
1719 DO_REQUEST(lynq_wifi_list_networks);
1720
1721 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1722
1723 //@todo check ssid field to compatible
1724
1725 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001726 for(index=1; index < count; index++)
1727 {
you.chen35020192022-05-06 11:30:57 +08001728 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001729 if (words_count > 2)
1730 {
you.chend2fef3f2023-02-13 10:50:35 +08001731 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001732 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1733 {
you.chen35020192022-05-06 11:30:57 +08001734 net_no_list[ret++] = atoi(split_words[0]);
1735 }
1736 }
1737 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001738 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001739 return ret;
1740}
1741
1742static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001743 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001744 CHECK_WPA_CTRL(ap_sta);
1745 const char *lynq_wifi_add_network = "ADD_NETWORK";
1746
qs.xiong9fbf74e2023-03-28 13:38:22 +08001747 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001748 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001749 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1750 {
1751 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001752 return -1;
1753 }
1754
qs.xiong9fbf74e2023-03-28 13:38:22 +08001755 for(i=0;i<reply_len;i++)
1756 {
1757 if(cmd_reply[i] == '\n')
1758 {
you.chen35020192022-05-06 11:30:57 +08001759 cmd_reply[i] = '\0';
1760 break;
1761 }
1762 }
1763 return atoi(cmd_reply);
1764}
you.chena6cd55a2022-05-08 12:20:18 +08001765
you.chen35020192022-05-06 11:30:57 +08001766static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1767{
1768 int count, index;
1769 int net_no_list[128];
1770
qs.xiong9fbf74e2023-03-28 13:38:22 +08001771 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001772 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001773 for (index=0; index < count; index++)
1774 {
1775 if (net_no_list[index] == net_no)
1776 {
you.chen35020192022-05-06 11:30:57 +08001777 return 0;
1778 }
1779 }
1780
1781 if (count >= 1)
1782 index = net_no_list[count - 1];
1783 else
1784 index = -1;
1785
qs.xiong9fbf74e2023-03-28 13:38:22 +08001786 while (index < net_no )
1787 {
you.chen35020192022-05-06 11:30:57 +08001788 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001789 if (index >= net_no)
1790 { // required network no created
1791 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001792 return 0;
1793 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001794 else if( index < 0)
1795 {
1796 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001797 return -1;
1798 }
you.chen35020192022-05-06 11:30:57 +08001799 }
1800
1801 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001802 {
1803 RLOGE("[lynq_check_network_number] network index < 0");
1804 return -1;
1805 }
1806 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001807 return 0;
1808}
1809
1810static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001811 if (freq > 5000 && freq < 6000)
1812 {
you.chen35020192022-05-06 11:30:57 +08001813 return LYNQ_WIFI_5G_band;
1814 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001815 else if (freq > 2000 && freq < 3000)
1816 {
you.chen35020192022-05-06 11:30:57 +08001817 return LYNQ_WIFI_2G_band;
1818 }
1819 return LYNQ_WIFI_2_and_5G_band;
1820}
1821
1822static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001823 if (key_mgmt != NULL)
1824 {
1825 if (memcmp( key_mgmt, "NONE", 4) == 0)
1826 {
you.chen35020192022-05-06 11:30:57 +08001827 return LYNQ_WIFI_AUTH_OPEN;
1828 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001829 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1830 {
you.chen35020192022-05-06 11:30:57 +08001831 return LYNQ_WIFI_AUTH_WEP;
1832 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001833 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1834 {
you.chen35020192022-05-06 11:30:57 +08001835 return LYNQ_WIFI_AUTH_WPA_PSK;
1836 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001837 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1838 {
you.chen35020192022-05-06 11:30:57 +08001839 return LYNQ_WIFI_AUTH_WPA2_PSK;
1840 }
1841 }
1842
1843 return -1;
1844}
1845
1846static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001847 if (flag != NULL)
1848 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001849 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 +08001850 {
1851 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001852 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001853 {
1854 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1855 }else if (strstr( flag, "WPA2-PSK") != NULL)
1856 {
you.chen35020192022-05-06 11:30:57 +08001857 return LYNQ_WIFI_AUTH_WPA2_PSK;
1858 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001859 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001860 {
you.chen35020192022-05-06 11:30:57 +08001861 return LYNQ_WIFI_AUTH_WPA_PSK;
1862 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001863 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001864 {
you.chen35020192022-05-06 11:30:57 +08001865 return LYNQ_WIFI_AUTH_WEP;
1866 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001867 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001868 {
you.chen35020192022-05-06 11:30:57 +08001869 return LYNQ_WIFI_AUTH_OPEN;
1870 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001871 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001872 {
you.chend2fef3f2023-02-13 10:50:35 +08001873 return LYNQ_WIFI_AUTH_OPEN;
1874 }
qs.xiong46f41562023-07-11 21:06:47 +08001875 else
1876 {
1877 RLOGD("convert_max_auth_from_flag not-found auth mode");
1878 }
you.chen35020192022-05-06 11:30:57 +08001879 }
1880
1881 return -1;
1882}
1883
1884static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1885 switch (bw) {
1886 case 10:
1887 return LYNQ_WIFI_BANDWIDTH_HT10;
1888 break;
1889 case 20:
1890 return LYNQ_WIFI_BANDWIDTH_HT20;
1891 break;
1892 case 40:
1893 return LYNQ_WIFI_BANDWIDTH_HT40;
1894 break;
1895 case 80:
1896 return LYNQ_WIFI_BANDWIDTH_HT80;
1897 break;
1898 default:
1899 break;
1900 }
1901
1902 return -1;
1903}
1904
you.chen70f377f2023-04-14 18:17:09 +08001905static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001906static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1907 int i, count;
1908 char *p;
1909 const char *lynq_status_cmd = "STATUS";
1910 const char * FLAG_SSID = "ssid=";
1911 const char * FLAG_SBSID = "bssid=";
1912 const char * FLAG_KEY_MGMT = "key_mgmt=";
1913 const char * FLAG_FREQ = "freq=";
1914 const char * FLAG_STATE = "wpa_state=";
1915 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001916 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001917 char *split_lines[128] = {0};
1918
1919 CHECK_WPA_CTRL(interface);
1920
qs.xiong9fbf74e2023-03-28 13:38:22 +08001921 if (curr_state == NULL)
1922 {
1923 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001924 return -1;
1925 }
1926
1927 DO_REQUEST(lynq_status_cmd);
1928
1929 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1930
1931 curr_state->net_no = -1;
1932 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001933 for(i=0; i < count; i++)
1934 {
1935 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001936 {
you.chen35020192022-05-06 11:30:57 +08001937 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001938 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001939 {
you.chend2fef3f2023-02-13 10:50:35 +08001940 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001941 ret = 0;
1942 continue;
1943 }
you.chenf58b3c92022-06-21 16:53:48 +08001944 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001945 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001946 {
you.chend2fef3f2023-02-13 10:50:35 +08001947 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 +08001948 ret = 0;
1949 continue;
1950 }
you.chen35020192022-05-06 11:30:57 +08001951 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001952 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001953 {
you.chen450d0172022-07-15 17:56:48 +08001954 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001956 ret = 0;
1957 continue;
1958 }
1959 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001960 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001961 {
you.chen35020192022-05-06 11:30:57 +08001962 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1963 ret = 0;
1964 continue;
1965 }
you.chend2fef3f2023-02-13 10:50:35 +08001966 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001967 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001968 {
you.chend2fef3f2023-02-13 10:50:35 +08001969 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1970 ret = 0;
1971 continue;
1972 }
you.chen35020192022-05-06 11:30:57 +08001973 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001974 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001975 {
you.chen35020192022-05-06 11:30:57 +08001976 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001977 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001978 {
you.chen35020192022-05-06 11:30:57 +08001979 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1980 ret = 0;
1981 continue;
1982 }
1983
1984 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001985 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001986 {
you.chen35020192022-05-06 11:30:57 +08001987 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001988 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001989 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001990 }
1991 }
1992
you.chen70f377f2023-04-14 18:17:09 +08001993 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1994 {
1995 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1996 }
1997
you.chen35020192022-05-06 11:30:57 +08001998 return ret;
1999}
2000
qs.xiongf1b525b2022-03-31 00:58:23 -04002001int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002002{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002003 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002004 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002005
qs.xiong9fbf74e2023-03-28 13:38:22 +08002006 if (ap_ssid == NULL)
2007 {
2008 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002009 return -1;
2010 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002011 else
2012 {
2013 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002014 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002015
qs.xiong9fbf74e2023-03-28 13:38:22 +08002016 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2017 {
2018 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002019 return -1;
2020 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002021
you.chen35020192022-05-06 11:30:57 +08002022 CHECK_IDX(idx, CTRL_AP);
2023
2024 CHECK_WPA_CTRL(CTRL_AP);
2025
2026 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2027
2028 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2029 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002030 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2031 return 0;
you.chen35020192022-05-06 11:30:57 +08002032
qs.xiong7a105ce2022-03-02 09:43:11 -05002033}
2034
you.chen35020192022-05-06 11:30:57 +08002035int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002036{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002037 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002038 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002039 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002040}
2041
qs.xiongc9c79f72022-10-17 15:27:18 +08002042/*****
2043 *frequency <------>channel
2044 *
2045 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2046 *
2047 *
2048 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2049 *
2050 *
2051 * */
2052static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002053 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};
2054 int i;
2055 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2056
qs.xiong69a332b2022-12-02 09:58:57 +08002057 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002058 {
2059 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002060 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002061 }
qs.xiongc00b6032022-11-29 16:28:03 +08002062
2063 if(i == arr_len)
2064 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002065 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002066 return -1;
2067 }
qs.xiongc00b6032022-11-29 16:28:03 +08002068
qs.xiongc9c79f72022-10-17 15:27:18 +08002069 return 0;
2070}
qs.xiong13673462023-02-21 19:12:54 +08002071
2072static int lynq_check_frequencyby_country_code(int input_frequency)
2073{
2074 char str_cnc[]="CN";
2075 char str_dest[20]="";
2076
2077 if( lynq_get_country_code(1,str_dest) != 0 )
2078 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002079 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002080 return -1;
2081 }
2082 if( strncmp(str_dest,str_cnc,2) != 0 )
2083 {
2084 return 0;
2085 }else if( 2473 < input_frequency && input_frequency < 5744)
2086 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002087 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002088 return -1;
2089 }
2090 return 0;
2091}
qs.xiongf1b525b2022-03-31 00:58:23 -04002092int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002093{
qs.xiongc00b6032022-11-29 16:28:03 +08002094 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002095 char lynq_wifi_frequency_cmd[128]={0};
2096 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002097 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002098 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002099 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002100 check = lynq_check_set_frequency(lynq_wifi_frequency);
2101 if(check != 0)
2102 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002103 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002104 return -1;
you.chen35020192022-05-06 11:30:57 +08002105 }
qs.xiong13673462023-02-21 19:12:54 +08002106 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2107 if(check != 0)
2108 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002109 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002110 return -1;
2111 }
2112
qs.xiongc00b6032022-11-29 16:28:03 +08002113 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2114 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002115 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002116 return -1;
2117 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002118
you.chen35020192022-05-06 11:30:57 +08002119 CHECK_IDX(idx, CTRL_AP);
2120
2121 CHECK_WPA_CTRL(CTRL_AP);
2122
2123 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2124 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2125 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2126
you.chen6c2dd9c2022-05-16 17:55:28 +08002127 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002128 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2129 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2130 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002131
qs.xiong9fbf74e2023-03-28 13:38:22 +08002132 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002133}
2134
qs.xiongf1b525b2022-03-31 00:58:23 -04002135int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002136{
you.chen35020192022-05-06 11:30:57 +08002137 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002138 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002139 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002140
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2142 {
2143 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002144 return -1;
2145 }
2146 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002147
qs.xiong9fbf74e2023-03-28 13:38:22 +08002148 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002149}
2150
qs.xiongf1b525b2022-03-31 00:58:23 -04002151int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2152{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002153 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002154 CHECK_IDX(idx, CTRL_AP);
2155 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 case LYNQ_WIFI_BANDWIDTH_HT10:
2157 {
2158 RLOGE("bandwith [%d] not support now\n", bandwidth);
2159 return -1;
2160 }
2161 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002162 {
2163 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2164 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002165 if (system(lynq_cmd_bandwith) != 0 )
2166 {
2167 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002168 return -1;
2169 }
2170 system("wl up");
2171 break;
2172 }
2173 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002174 {
qs.xiong10379192023-02-21 13:19:42 +08002175 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002176 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2177 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002178 if (system(lynq_cmd_bandwith) != 0 )
2179 {
2180 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002181 return -1;
2182 }
2183 system("wl up");
2184 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002185 }
you.chen35020192022-05-06 11:30:57 +08002186 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002187 {
qs.xiong10379192023-02-21 13:19:42 +08002188 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002189 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002190 if (system(lynq_cmd_bandwith) != 0 )
2191 {
2192 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002193 return -1;
2194 }
2195 system("wl up");
2196 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002197 }
2198 default:
you.chen35020192022-05-06 11:30:57 +08002199 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002200 RLOGE("auth type [%d] not support now\n", bandwidth);
2201 return -1;
you.chen35020192022-05-06 11:30:57 +08002202 }
2203 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002204
2205
you.chen35020192022-05-06 11:30:57 +08002206 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002207}
you.chen35020192022-05-06 11:30:57 +08002208
qs.xiongf1b525b2022-03-31 00:58:23 -04002209int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2210{
you.chen35020192022-05-06 11:30:57 +08002211 int count = 0;
2212 int index = 0;
2213 char *split_words[128] = {0};
2214 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002215 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002216 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002217
you.chen35020192022-05-06 11:30:57 +08002218 CHECK_WPA_CTRL(CTRL_AP);
2219
2220 DO_REQUEST(lynq_chanspec_cmd);
2221
2222 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2223 for(;index < count; index++) {
2224 if (strncmp(split_words[index], "bw", 2) != 0) {
2225 continue;
2226 }
2227
2228 index++;
2229 if (index >= count) {
2230 return -1;
2231 }
2232
qs.xiong9fbf74e2023-03-28 13:38:22 +08002233 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002234 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2235 return 0;
2236 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002237 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002238 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002239}
qs.xiong0fb469a2022-04-14 03:50:45 -04002240
qs.xiongf1b525b2022-03-31 00:58:23 -04002241int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002242{
you.chen35020192022-05-06 11:30:57 +08002243 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002244 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002245 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002246
you.chen35020192022-05-06 11:30:57 +08002247 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002248
qs.xiong9fbf74e2023-03-28 13:38:22 +08002249 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2250 {
you.chen35020192022-05-06 11:30:57 +08002251 return -1;
2252 }
2253
2254 system("wl down");
2255 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002256 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002257 return -1;
2258 }
2259 system("wl up");
2260 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002261}
qs.xiong0fb469a2022-04-14 03:50:45 -04002262
qs.xiongf1b525b2022-03-31 00:58:23 -04002263int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002264{
you.chen35020192022-05-06 11:30:57 +08002265 int count = 0;
2266 int index = 0;
2267 char *split_words[128] = {0};
2268 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002269 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002270 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002271
you.chen35020192022-05-06 11:30:57 +08002272 CHECK_WPA_CTRL(CTRL_AP);
2273
2274 DO_REQUEST(lynq_chanspec_cmd);
2275
2276 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002277 for(;index < count; index++)
2278 {
2279 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002280 if (strncmp(split_words[index], "channel", 2) != 0) {
2281 continue;
2282 }
2283
2284 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 if (index >= count)
2286 {
you.chen35020192022-05-06 11:30:57 +08002287 return -1;
2288 }
2289
2290 *channel = atoi(split_words[index]);
2291 return 0;
2292 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002293 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002294 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002295}
2296
2297
you.chen35020192022-05-06 11:30:57 +08002298int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002299{
you.chen6c2dd9c2022-05-16 17:55:28 +08002300 char ssid[MAX_CMD] = {0};
2301 int freq = 0;
2302 char lynq_auth_cmd[64]={0};
2303 char lynq_auth_alg_cmd[64]={0};
2304 char lynq_psk_cmd[64]={0};
2305 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002306 char lynq_ieee80211_cmd[64]={0};
2307 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002308 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002309 CHECK_IDX(idx, CTRL_AP);
2310
you.chen6c2dd9c2022-05-16 17:55:28 +08002311 CHECK_WPA_CTRL(CTRL_AP);
2312
qs.xiong9fbf74e2023-03-28 13:38:22 +08002313 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2314 {
2315 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002316 return -1;
2317 }
2318
you.chen92fd5d32022-05-25 10:09:47 +08002319 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002320 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002321 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002322 return 0;
2323 }
2324 else {
2325 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2326 ssid[0] = '\0';
2327 }
2328 lynq_wifi_ap_frequency_get(idx, &freq);
2329
2330 DO_OK_FAIL_REQUEST(cmd_disconnect);
2331 DO_OK_FAIL_REQUEST(cmd_remove_all);
2332 if (ssid[0] != '\0') {
2333 lynq_wifi_ap_ssid_set(idx, ssid);
2334 }
2335 if (freq != 0) {
2336 lynq_wifi_ap_frequency_set(idx, freq);
2337 }
2338 }
2339 }
you.chen35020192022-05-06 11:30:57 +08002340
qs.xiong9fbf74e2023-03-28 13:38:22 +08002341 switch(auth){
2342 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002343 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002344 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002345 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002346 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002347 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002348 break;
2349 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002350 case LYNQ_WIFI_AUTH_WEP:
2351 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002352 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002353 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002354 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002355 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2356
2357 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2358 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2359 break;
2360 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002361 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002362 {
2363 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2364 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2365 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2366
2367 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2368 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2369 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2370 break;
2371
2372 }
you.chen35020192022-05-06 11:30:57 +08002373 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002374 {
2375 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2376 {
you.chen35020192022-05-06 11:30:57 +08002377 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2378 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2379 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002380 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2381 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002382 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002383 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002384 }
2385// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2386// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2387 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002388
you.chen35020192022-05-06 11:30:57 +08002389 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2390 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2391 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002392 break;
2393 }
2394 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2395 {
2396 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2397 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2398 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2399 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2400
2401 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2402 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2403 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2404 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2405 break;
2406 }
2407 case LYNQ_WIFI_AUTH_WPA3_PSK:
2408 {
2409 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2410 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002411 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002412 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2413
2414 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2415 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2416 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2417 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2418 break;
2419 }
2420 default:
you.chen35020192022-05-06 11:30:57 +08002421 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 RLOGE("auth type [%d] not support now\n", auth);
2423 return -1;
you.chen35020192022-05-06 11:30:57 +08002424 }
2425 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002426 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002427
qs.xiong9fbf74e2023-03-28 13:38:22 +08002428 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002429}
2430
you.chen35020192022-05-06 11:30:57 +08002431int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002432{
you.chen35020192022-05-06 11:30:57 +08002433 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002434 char lynq_auth_alg_str[MAX_RET] = {0};
2435 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002436 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002437 CHECK_IDX(idx, CTRL_AP);
2438
qs.xiong9fbf74e2023-03-28 13:38:22 +08002439 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2440 {
2441 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002442 return -1;
2443 }
2444
qs.xiong9fbf74e2023-03-28 13:38:22 +08002445 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2446 {
2447 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2448 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002449 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002450 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002451 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002452 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002453 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2454 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002455 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002456 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002457 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002458 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002459 else
2460 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002461 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002462 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002463 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002464 }
you.chen35020192022-05-06 11:30:57 +08002465 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002466 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2467 {
2468 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2469 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002470 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002471 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002472 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002473 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2474 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002475 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002476 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002477 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002478 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002479 else
2480 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002481 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002482 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002483 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002484 }
you.chen35020192022-05-06 11:30:57 +08002485 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002486
2487 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2488 {
2489 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2490 return -1;
2491 }
2492
2493 if (memcmp(lynq_auth_str,"1",1) == 0 )
2494 {
2495 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2496 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002497 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002498 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2499 {
2500 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2501 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002502 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002503 }
2504 else
2505 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002506 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002507 *auth = -1;
2508 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002509
you.chen6c2dd9c2022-05-16 17:55:28 +08002510 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002511}
qs.xiong1af5daf2022-03-14 09:12:12 -04002512
you.chenb95401e2023-05-12 19:39:06 +08002513static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2514{
2515 char status[64];
you.chencba13492023-05-19 13:53:43 +08002516 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002517 curr_status_info curr_state;
2518
2519 CHECK_WPA_CTRL(CTRL_AP);
2520
2521 memset(status, 0, sizeof (status));
2522
2523 curr_state.ap = NULL;
2524 curr_state.state = status;
2525
2526 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002527 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002528 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002529 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002530 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002531 {
2532 return 0;
2533 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002534 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002535 {
2536 DO_REQUEST("RECONNECT");
2537 return inner_check_ap_connected(idx, retry_count+1);
2538 }
you.chencba13492023-05-19 13:53:43 +08002539 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002540 {
2541 printf("retry 10 time\n");
2542 return -1;
2543 }
2544 else
2545 {
you.chen6d247052023-06-01 16:39:54 +08002546 if (strcmp(status, STATE_DISCONNECTED) == 0)
2547 {
2548 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2549 DO_REQUEST(LYNQ_WIFI_CMD);
2550 }
you.chenb95401e2023-05-12 19:39:06 +08002551 return inner_check_ap_connected(idx, retry_count+1);
2552 }
2553 }
2554 return -1;
2555}
qs.xiong1af5daf2022-03-14 09:12:12 -04002556
qs.xiongf1b525b2022-03-31 00:58:23 -04002557int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002558{
qs.xiong5a2ba932023-09-13 16:30:21 +08002559 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002560 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002561 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2562 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2563 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002564 CHECK_IDX(idx, CTRL_AP);
2565
2566 CHECK_WPA_CTRL(CTRL_AP);
2567
you.chen0df3e7e2023-05-10 15:56:26 +08002568 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002569 {
you.chen0df3e7e2023-05-10 15:56:26 +08002570 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002571 return -1;
2572 }
you.chen35020192022-05-06 11:30:57 +08002573
qs.xiongb37f8c42023-09-13 21:21:58 +08002574 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2575 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2576
you.chen35020192022-05-06 11:30:57 +08002577 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2578 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2579
you.chenc9928582023-04-24 15:39:37 +08002580 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2581 if (ret != 0)
2582 {
2583 RLOGE("lynq_wifi_ap_start excute script fail");
2584 return -1;
2585 }
2586
you.chenb95401e2023-05-12 19:39:06 +08002587 if (inner_check_ap_connected(idx, 0) != 0)
2588 {
2589 return -1;
2590 }
2591
you.chen0df3e7e2023-05-10 15:56:26 +08002592 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002593 if (g_ap_tmp_watcher_pid == 0)
2594 {
2595 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2596 {
2597 g_ap_tmp_watcher_pid = 0;
2598 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2599 return -1;
2600 }
2601 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2602 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002603 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002604
qs.xiong9fbf74e2023-03-28 13:38:22 +08002605 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002606}
2607
qs.xiongf1b525b2022-03-31 00:58:23 -04002608int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002609{
you.chen35020192022-05-06 11:30:57 +08002610 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002611}
2612
qs.xiongf1b525b2022-03-31 00:58:23 -04002613int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002614{
you.chen35020192022-05-06 11:30:57 +08002615 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002616
you.chen35020192022-05-06 11:30:57 +08002617 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002618
you.chen35020192022-05-06 11:30:57 +08002619 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002620
you.chen35020192022-05-06 11:30:57 +08002621 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2622
2623 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2624
you.chenb4b121c2022-05-06 17:50:16 +08002625// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002626
2627 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2628 if (ret != 0)
2629 {
2630 RLOGE("lynq_wifi_ap_start excute script fail");
2631 return -1;
2632 }
qs.xiong44fac672023-08-29 16:15:55 +08002633 g_ap_tmp_watcher_stop_flag = 1;
2634 if (g_ap_tmp_watcher_pid != 0)
2635 pthread_join(g_ap_tmp_watcher_pid, NULL);
2636 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002637
qs.xiong9fbf74e2023-03-28 13:38:22 +08002638 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002639}
qs.xiong1af5daf2022-03-14 09:12:12 -04002640
qs.xiongf1b525b2022-03-31 00:58:23 -04002641int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002642{
you.chen35020192022-05-06 11:30:57 +08002643 char lynq_disable_cmd[128] = {0};
2644 char lynq_select_cmd[128] = {0};
2645 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002646 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002647 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002648
you.chen35020192022-05-06 11:30:57 +08002649 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002650 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2651 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2652
2653 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2654 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2655 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002656
qs.xiong9fbf74e2023-03-28 13:38:22 +08002657 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002658}
2659
qs.xiongf1b525b2022-03-31 00:58:23 -04002660int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002661{
you.chen35020192022-05-06 11:30:57 +08002662 char lynq_disable_cmd[128] = {0};
2663 char lynq_select_cmd[128] = {0};
2664 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002665 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002666 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002667
you.chen35020192022-05-06 11:30:57 +08002668 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002669
you.chen35020192022-05-06 11:30:57 +08002670 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2671 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2672
2673 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2674 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2675 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002676
qs.xiong9fbf74e2023-03-28 13:38:22 +08002677 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002678}
qs.xiongf1b525b2022-03-31 00:58:23 -04002679
you.chen35020192022-05-06 11:30:57 +08002680int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002681{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002682 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002683 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002684 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002685 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002686 RLOGD("enter lynq_ap_password_set");
2687 if( password == NULL )
2688 {
2689 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002690 return -1;
2691 }
2692 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002693 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002694 if(pass_len < 8 || pass_len >= 64)
2695 {
2696 RLOGE("[lynq_ap_password_set]input password len not in rage");
2697 return -1;
you.chen35020192022-05-06 11:30:57 +08002698 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002699
you.chen35020192022-05-06 11:30:57 +08002700 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002701
qs.xiong9fbf74e2023-03-28 13:38:22 +08002702 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2703 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002704 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002705 return -1;
2706 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002707 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2708 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002709 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2710 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002711 }
2712
you.chen35020192022-05-06 11:30:57 +08002713 CHECK_WPA_CTRL(CTRL_AP);
2714
qs.xiong9fbf74e2023-03-28 13:38:22 +08002715 if (auth == LYNQ_WIFI_AUTH_WEP)
2716 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002717 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002718 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2719 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2720 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2721 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2722 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2724 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002725 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 +08002726 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2727 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2728 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002729 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2730 {
2731
qs.xiongc8d92a62023-03-29 17:36:14 +08002732 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 +08002733 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002734 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002735 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2736 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2737
2738 }
2739 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002740 {
2741 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002742 return -1;
2743 }
you.chen35020192022-05-06 11:30:57 +08002744
you.chen35020192022-05-06 11:30:57 +08002745 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002746
qs.xiong9fbf74e2023-03-28 13:38:22 +08002747 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002748}
2749
you.chen35020192022-05-06 11:30:57 +08002750int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002751{
you.chen35020192022-05-06 11:30:57 +08002752 FILE * fp;
2753 int len, ret;
2754 int count, index;
2755 char *split_lines[128] = {0};
2756 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002757 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002758
you.chen35020192022-05-06 11:30:57 +08002759 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002760
you.chen35020192022-05-06 11:30:57 +08002761 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2762// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002763 if (NULL == fp)
2764 {
2765 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002766 return -1;
2767 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002768
you.chen35020192022-05-06 11:30:57 +08002769 buff = alloca(MAX_RET);
2770 fseek(fp, 0, SEEK_SET);
2771 len = fread(buff, 1, MAX_RET, fp);
2772 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002773
qs.xiong9fbf74e2023-03-28 13:38:22 +08002774 for(index=0; index < len; index ++)
2775 {
2776 if (memcmp(buff + index, "network={", 9) != 0)
2777 {
you.chen35020192022-05-06 11:30:57 +08002778 continue;
2779 }
2780 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002781 for (; index < len; index ++ )
2782 {
2783 if (buff[index] != '}')
2784 {
you.chen35020192022-05-06 11:30:57 +08002785 continue;
2786 }
2787 buff[index] = '\0';
2788 break;
2789 }
2790 len = buff + index - p;
2791 }
2792
2793 count = lynq_split(p, len, '\n', split_lines);
2794
2795 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002796 for(index=0; index < count; index++)
2797 {
you.chen35020192022-05-06 11:30:57 +08002798 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002799 if (p != NULL)
2800 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002801 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002802 if (*p == '\"')
2803 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002804 p++;
2805 }
you.chen35020192022-05-06 11:30:57 +08002806 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002807 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2808 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002809 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002810 if (*p == '\"')
2811 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002812 p++;
2813 }
2814 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002815 else
2816 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002817 continue;
you.chen35020192022-05-06 11:30:57 +08002818 }
2819
2820 strcpy(password, p);
2821
qs.xiong9fbf74e2023-03-28 13:38:22 +08002822 while(*password != '\0')
2823 {
2824 if (*password == '\"')
2825 {
you.chen35020192022-05-06 11:30:57 +08002826 *password = '\0';
2827 break;
2828 }
2829 password++;
2830 }
2831 ret = 0;
2832 break;
2833 } //end for(index=0; index < count; index++)
2834
2835 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002836}
2837
you.chen35020192022-05-06 11:30:57 +08002838static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2839 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002840 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002841
qs.xiong9fbf74e2023-03-28 13:38:22 +08002842 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2843 {
you.chen35020192022-05-06 11:30:57 +08002844 return -1;
2845 }
2846
2847 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002848
qs.xiong9fbf74e2023-03-28 13:38:22 +08002849 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2850 {
2851 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002852 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002853 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002854 {
you.chena6cd55a2022-05-08 12:20:18 +08002855 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002856 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002857 }
you.chen70f377f2023-04-14 18:17:09 +08002858 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2859 {
2860 return 0;
2861 }
you.chena6cd55a2022-05-08 12:20:18 +08002862 }
2863 }
you.chen70f377f2023-04-14 18:17:09 +08002864 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2865 {
2866 return 0;
2867 }
2868
qs.xiong9fbf74e2023-03-28 13:38:22 +08002869 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2870 {
you.chen70f377f2023-04-14 18:17:09 +08002871 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002872 return -1;
2873 }
2874 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2875 {
2876
you.chen70f377f2023-04-14 18:17:09 +08002877 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2878 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002879 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2880 {
2881
2882 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002883 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002884 }else
2885 {
you.chen70f377f2023-04-14 18:17:09 +08002886 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002887 *auth = -1;
2888 return -1;
2889 }
you.chen35020192022-05-06 11:30:57 +08002890 return 0;
2891}
2892
2893int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002894{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002896 int pass_len, net_no, count, index;
2897 char lynq_tmp_cmd[300]={0};
2898 int net_no_list[128];
2899 lynq_wifi_auth_s net_auth;
2900 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002901 if(pass_len < 8 || pass_len >= 64)
2902 {
2903 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002904 return -1;
2905 }
2906
2907 CHECK_IDX(idx, CTRL_STA);
2908
2909 net_no = -1;
2910 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2911
qs.xiong9fbf74e2023-03-28 13:38:22 +08002912 for (index=0; index < count; index++)
2913 {
you.chen35020192022-05-06 11:30:57 +08002914 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002915 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2916 {
you.chen35020192022-05-06 11:30:57 +08002917 net_no = net_no_list[index];
2918 break;
2919 }
2920 }
2921
qs.xiong9fbf74e2023-03-28 13:38:22 +08002922 if (net_no < 0)
2923 {
you.chen35020192022-05-06 11:30:57 +08002924 return -1;
2925 }
2926
2927 CHECK_WPA_CTRL(CTRL_STA);
2928
2929 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2930
2931 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2932 DO_OK_FAIL_REQUEST(cmd_save_config);
2933
2934 return 0;
2935}
2936
2937int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2938
2939 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002940 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002941 int count, index;
2942 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002943 char *buff, *p, *ssid, *ssid_end_flag;
2944 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002945 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002946
you.chen755332b2022-08-06 16:59:10 +08002947 network_len = 0;
2948 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002949 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002950
you.chen35020192022-05-06 11:30:57 +08002951 CHECK_IDX(idx, CTRL_STA);
2952
qs.xiong9fbf74e2023-03-28 13:38:22 +08002953 if (NULL == password)
2954 {
2955 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002956 return -1;
2957 }
2958
you.chen35020192022-05-06 11:30:57 +08002959 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002960 if (NULL == fp)
2961 {
2962 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002963 return -1;
2964 }
2965
you.chen6d247052023-06-01 16:39:54 +08002966 fseek(fp, 0, SEEK_END);
2967 len = ftell(fp);
2968 buff = malloc(len + 1);
2969
2970 if (buff == NULL)
2971 {
2972 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2973 return -1;
2974 }
2975
you.chen35020192022-05-06 11:30:57 +08002976 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002977 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002978 fclose(fp);
2979
qs.xiong9fbf74e2023-03-28 13:38:22 +08002980 for(index=0; index < len; index ++)
2981 {
2982 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002983 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002984 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002985 {
you.chen35020192022-05-06 11:30:57 +08002986 continue;
2987 }
you.chen6d247052023-06-01 16:39:54 +08002988 p = buff + index + 9;
2989 for (; index < len; index ++ )
2990 {
2991 if (buff[index] != '}')
2992 {
2993 continue;
2994 }
2995 buff[index] = '\0';
2996 break;
2997 }
2998 network_len = buff + index - p;
2999 break;
you.chen35020192022-05-06 11:30:57 +08003000 }
3001
qs.xiongb3f26af2023-02-17 18:41:07 +08003002 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08003003 {
3004 if (buff != NULL)
3005 {
3006 free(buff);
3007 }
3008
qs.xiongb3f26af2023-02-17 18:41:07 +08003009 return -1;
you.chen6d247052023-06-01 16:39:54 +08003010 }
qs.xiongb3f26af2023-02-17 18:41:07 +08003011
you.chend2fef3f2023-02-13 10:50:35 +08003012 ssid = strstr(p, "ssid=");
3013 if (ssid != NULL) {
3014 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003015 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08003016 {
you.chend2fef3f2023-02-13 10:50:35 +08003017 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3018 break;
3019 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003020 else
you.chen6d247052023-06-01 16:39:54 +08003021 {
you.chend2fef3f2023-02-13 10:50:35 +08003022 ssid_end_flag = strstr(ssid, "\n");
3023 if (ssid_end_flag != NULL)
3024 {
3025 ssid_len = (ssid_end_flag - ssid) / 2;
3026 for(i=0; i<ssid_len; i++)
3027 {
3028 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3029 }
3030 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3031 break;
3032 }
3033 }
you.chen35020192022-05-06 11:30:57 +08003034 }
you.chend2fef3f2023-02-13 10:50:35 +08003035
you.chen35020192022-05-06 11:30:57 +08003036 }
3037
qs.xiong9fbf74e2023-03-28 13:38:22 +08003038 if (index >= len || NULL == p || network_len <= 0)
3039 {
you.chen6d247052023-06-01 16:39:54 +08003040 if (buff != NULL)
3041 {
3042 free(buff);
3043 }
you.chen35020192022-05-06 11:30:57 +08003044 return -1;
3045 }
3046
you.chen755332b2022-08-06 16:59:10 +08003047 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08003048
3049 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003050 for(index=0; index < count; index++)
3051 {
you.chen35020192022-05-06 11:30:57 +08003052 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003053 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08003054 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003055 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003056 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08003057 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003058 p++;
3059 }
you.chen35020192022-05-06 11:30:57 +08003060 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003061 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08003062 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003063 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003064 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08003065 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003066 p++;
3067 }
3068 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003069 else
you.chen6d247052023-06-01 16:39:54 +08003070 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003071 continue;
you.chen35020192022-05-06 11:30:57 +08003072 }
3073
qs.xiong13673462023-02-21 19:12:54 +08003074 if (*p == '\"')
3075 p++;
3076 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08003077
qs.xiong13673462023-02-21 19:12:54 +08003078 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003079 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08003080 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003081 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08003082 {
you.chen35020192022-05-06 11:30:57 +08003083 *password = '\0';
3084 break;
3085 }
3086 password++;
3087 }
3088 ret = 0;
3089 break;
3090 } //end for(index=0; index < count; index++)
3091
you.chen6d247052023-06-01 16:39:54 +08003092 if (buff != NULL)
3093 {
3094 free(buff);
3095 }
3096
you.chen35020192022-05-06 11:30:57 +08003097 return ret;
3098}
3099
3100static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3101{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003102 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003103
qs.xiong9fbf74e2023-03-28 13:38:22 +08003104 if (sta_ssid == NULL)
3105 {
3106 RLOGE("sta_ssid is null\n");
3107 return -1;
you.chen35020192022-05-06 11:30:57 +08003108 }
3109
qs.xiong9fbf74e2023-03-28 13:38:22 +08003110 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003111
3112 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3113
3114 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3115// DO_OK_FAIL_REQUEST(cmd_save_config);
3116
qs.xiong9fbf74e2023-03-28 13:38:22 +08003117 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003118
3119}
3120
you.chen35020192022-05-06 11:30:57 +08003121static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003122{
you.chen35020192022-05-06 11:30:57 +08003123 char lynq_disable_cmd[128]={0};
3124 char lynq_select_cmd[128]={0};
3125
3126 CHECK_WPA_CTRL(CTRL_STA);
3127
qs.xiong9fbf74e2023-03-28 13:38:22 +08003128 if (save != 0)
3129 {
you.chenc29444e2022-06-07 18:01:16 +08003130 if (start_flag != 0)
3131 {
3132 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3133 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3134 }
3135 else
3136 {
3137 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3138 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3139 }
you.chen35020192022-05-06 11:30:57 +08003140 DO_OK_FAIL_REQUEST(cmd_save_config);
3141 }
3142
qs.xiong9fbf74e2023-03-28 13:38:22 +08003143 if (start_flag == 0)
3144 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003145 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003146 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3147 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003148 else
3149 {
you.chen35020192022-05-06 11:30:57 +08003150 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3151 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3152 }
3153
3154 return 0;
3155}
3156
3157int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3158{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003159 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003160 CHECK_IDX(idx, CTRL_STA);
3161
you.chen6c2dd9c2022-05-16 17:55:28 +08003162 curr_status_info curr_state;
3163 ap_info_s ap_info;
3164 curr_state.ap = &ap_info;
3165 curr_state.state = NULL;
3166
qs.xiong9fbf74e2023-03-28 13:38:22 +08003167 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3168 {
you.chend2fef3f2023-02-13 10:50:35 +08003169 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003170 return 0;
3171 }
3172
3173 return -1;
you.chen35020192022-05-06 11:30:57 +08003174}
3175
3176int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3177{
qs.xiong5d716d22023-09-20 20:08:39 +08003178 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003179 scan_info_s *scan_list = NULL;
3180 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003181 int scan_len=0;
3182 int save_len=0;
3183 int best_index = -1;
3184 int best_scan_index = -1;
3185 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003186 int i, j, ret;
3187
3188 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003189
3190 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003191 if (info == NULL)
3192 {
you.chen35020192022-05-06 11:30:57 +08003193 return -1;
3194 }
3195
3196 curr_status_info curr_state;
3197 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003198 char status[64];
you.chen35020192022-05-06 11:30:57 +08003199
you.chen9ac66392022-08-06 17:01:16 +08003200 memset(&ap_info, 0, sizeof (ap_info));
3201 memset(status, 0, sizeof (status));
3202
3203 curr_state.ap = &ap_info;
3204 curr_state.state = status;
3205
qs.xiong9fbf74e2023-03-28 13:38:22 +08003206 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3207 {
you.chen35020192022-05-06 11:30:57 +08003208 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003209 if (strcmp(status, STATE_COMPLETED) == 0)
3210 {
3211 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003212 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003213 }
3214 else
3215 {
3216 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003217 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003218 }
you.chen593621d2023-04-27 17:52:44 +08003219 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003220 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003221 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003222 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003223 return 0;
3224 }
3225
you.chen9ac66392022-08-06 17:01:16 +08003226 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003227 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003228 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3229 {
you.chen9ac66392022-08-06 17:01:16 +08003230 if (NULL != scan_list)
3231 {
3232 free(scan_list);
3233 }
you.chen35020192022-05-06 11:30:57 +08003234 return -1;
3235 }
3236
qs.xiong9fbf74e2023-03-28 13:38:22 +08003237 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3238 {
you.chen9ac66392022-08-06 17:01:16 +08003239 if (NULL != scan_list)
3240 {
3241 free(scan_list);
3242 }
3243 if (NULL != save_list)
3244 {
3245 free(save_list);
3246 }
you.chen35020192022-05-06 11:30:57 +08003247 return -1;
3248 }
3249
qs.xiong9fbf74e2023-03-28 13:38:22 +08003250 for (i=0; i < save_len; i++)
3251 {
3252 for (j=0; j < scan_len; j++)
3253 {
you.chen35020192022-05-06 11:30:57 +08003254 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003255 && save_list[i].base_info.auth == scan_list[j].auth)
3256 {
3257 if (best_rssi == 0)
3258 {
you.chen9ac66392022-08-06 17:01:16 +08003259 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003260 best_rssi = scan_list[j].rssi;
3261 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003262 else if (best_rssi > scan_list[j].rssi)
3263 {
you.chen35020192022-05-06 11:30:57 +08003264 best_index = i;
3265 best_scan_index = j;
3266 best_rssi = scan_list[j].rssi;
3267 }
you.chend2fef3f2023-02-13 10:50:35 +08003268 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 +08003269 break;
3270 }
3271 }
3272 }
3273
qs.xiong9fbf74e2023-03-28 13:38:22 +08003274 if (best_index >= 0)
3275 {
you.chen35020192022-05-06 11:30:57 +08003276 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003277 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 +08003278 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003279 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003280 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003281 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003282 }
3283
you.chen9ac66392022-08-06 17:01:16 +08003284 if (NULL != scan_list)
3285 {
3286 free(scan_list);
3287 }
3288 if (NULL != save_list)
3289 {
3290 free(save_list);
3291 }
3292
qs.xiong5d716d22023-09-20 20:08:39 +08003293 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003294 return ret;
you.chen35020192022-05-06 11:30:57 +08003295}
3296
3297static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3298{
qs.xiongc8d92a62023-03-29 17:36:14 +08003299 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003300 char lynq_ket_mgmt_cmd[64]={0};
3301 char lynq_pairwise_cmd[64]={0};
3302 char lynq_psk_cmd[64]={0};
3303
3304 CHECK_WPA_CTRL(CTRL_STA);
3305
qs.xiong9fbf74e2023-03-28 13:38:22 +08003306 switch(auth)
3307 {
3308 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003309 {
3310 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003311
you.chen35020192022-05-06 11:30:57 +08003312 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003313// DO_OK_FAIL_REQUEST(cmd_save_config);
3314 break;
3315 }
3316 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003317 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003318 {
3319 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3320 {
you.chen35020192022-05-06 11:30:57 +08003321 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3322 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003323 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3324 {
you.chena6cd55a2022-05-08 12:20:18 +08003325 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003326 }
3327 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3328 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003329
you.chen35020192022-05-06 11:30:57 +08003330 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3331 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3332 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003333
qs.xiong9fbf74e2023-03-28 13:38:22 +08003334 if (password != NULL)
3335 {
you.chen35020192022-05-06 11:30:57 +08003336 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3337 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003338 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003339 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003340
you.chen35020192022-05-06 11:30:57 +08003341// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 break;
3343 }
3344 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3345 {
qs.xiong3e506812023-04-06 11:08:48 +08003346 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003347 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003348 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3349 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3350
qs.xiong3e506812023-04-06 11:08:48 +08003351 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003352 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3353 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3354 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3355
3356 break;
3357 }
3358 case LYNQ_WIFI_AUTH_WPA3_PSK:
3359 {
qs.xiong3e506812023-04-06 11:08:48 +08003360 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003361 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003362 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3363 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3364
qs.xiongb37f8c42023-09-13 21:21:58 +08003365 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003366 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3367 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3368 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3369
3370 break;
3371 }
3372 default:
3373 return -1;
you.chen35020192022-05-06 11:30:57 +08003374 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003375
qs.xiong9fbf74e2023-03-28 13:38:22 +08003376 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003377}
qs.xiong7a105ce2022-03-02 09:43:11 -05003378
you.chen35020192022-05-06 11:30:57 +08003379static int inner_get_curr_net_no(int interface) {
3380 curr_status_info curr_state;
3381 curr_state.ap = NULL;
3382 curr_state.state = NULL;
3383
qs.xiong9fbf74e2023-03-28 13:38:22 +08003384 if (0 != inner_get_status_info(interface, &curr_state))
3385 {
you.chen35020192022-05-06 11:30:57 +08003386 return -1;
3387 }
3388
3389 return curr_state.net_no;
3390}
3391
3392int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003393{
you.chen35020192022-05-06 11:30:57 +08003394 int net_no;
3395 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003396
you.chen35020192022-05-06 11:30:57 +08003397 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003398
qs.xiong9fbf74e2023-03-28 13:38:22 +08003399 if (net_no < 0)
3400 {
you.chen35020192022-05-06 11:30:57 +08003401 return -1;
3402 }
3403
3404 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003405}
3406
you.chenb95401e2023-05-12 19:39:06 +08003407int 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 -05003408{
you.chen35020192022-05-06 11:30:57 +08003409 int count, net_no, index;
3410 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003411 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003412 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003413 curr_status_info curr_state;
3414 ap_info_s ap_info;
3415 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003416
qs.xiong9fbf74e2023-03-28 13:38:22 +08003417 if (ssid == NULL || *ssid == '\0')
3418 {
3419 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003420 return -1;
3421 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003422
qs.xiong9fbf74e2023-03-28 13:38:22 +08003423 if (LYNQ_WIFI_AUTH_OPEN != auth)
3424 {
3425 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003426 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003427 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003428 return -1;
3429 }
3430 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003431
you.chen70f377f2023-04-14 18:17:09 +08003432
3433 pthread_mutex_lock(&s_global_check_mutex);
3434 if (s_sta_status != INNER_STA_STATUS_INIT)
3435 {
3436 s_sta_status = INNER_STA_STATUS_CANCEL;
3437 pthread_cond_signal(&s_global_check_cond);
3438 }
3439 pthread_mutex_unlock(&s_global_check_mutex);
3440
you.chen35020192022-05-06 11:30:57 +08003441 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003442 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003443
3444 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003445 memset(&ap_info, 0, sizeof (ap_info));
3446 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003447
you.chen70f377f2023-04-14 18:17:09 +08003448 curr_state.ap = &ap_info;
3449 curr_state.state = status;
3450
3451 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003452 {
you.chen70f377f2023-04-14 18:17:09 +08003453 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3454 {
3455 net_no = curr_state.net_no;
3456 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3457 && strcmp(ap_info.psw, psw) == 0)
3458 {
3459 RLOGD("already connected\n");
3460
3461 pthread_mutex_lock(&s_global_check_mutex);
3462 s_sta_status = INNER_STA_STATUS_CONNECTED;
3463 pthread_cond_signal(&s_global_check_cond);
3464 pthread_mutex_unlock(&s_global_check_mutex);
3465 return 0;
3466 }
you.chen35020192022-05-06 11:30:57 +08003467 }
3468 }
3469
you.chen70f377f2023-04-14 18:17:09 +08003470 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003471 {
you.chen70f377f2023-04-14 18:17:09 +08003472 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3473
3474 for (index=0; index < count; index++)
3475 {
3476 net_auth = -1;
3477 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3478 {
3479 net_no = net_no_list[index];
3480 break;
3481 }
you.chen35020192022-05-06 11:30:57 +08003482 }
3483
you.chen70f377f2023-04-14 18:17:09 +08003484 if (net_no < 0)
3485 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003486 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3487 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3488 {
3489 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3490 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3491 DO_OK_FAIL_REQUEST(rm_net_cmd);
3492 }
you.chen70f377f2023-04-14 18:17:09 +08003493 net_no = lynq_add_network(CTRL_STA);
3494 if (net_no == -1)
3495 {
3496 return -1;
3497 }
3498
3499 RLOGD("net no is %d\n", net_no);
3500 if (0 != inner_set_sta_ssid(net_no, ssid))
3501 {
3502 return -1;
3503 }
you.chen35020192022-05-06 11:30:57 +08003504 }
3505 }
3506
qs.xiong9fbf74e2023-03-28 13:38:22 +08003507 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3508 {
you.chen35020192022-05-06 11:30:57 +08003509 return -1;
3510 }
3511
you.chen70f377f2023-04-14 18:17:09 +08003512
3513 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003514 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003515 usleep(200*1000);
3516
3517 ret = inner_sta_start_stop(net_no, 1, 1);
3518
3519 pthread_mutex_lock(&s_global_check_mutex);
3520 s_sta_status = INNER_STA_STATUS_CONNECTING;
3521 strcpy(s_sta_current_connecting_ssid, ssid);
3522 struct timeval now;
3523 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003524 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003525 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3526 pthread_cond_signal(&s_global_check_cond);
3527 pthread_mutex_unlock(&s_global_check_mutex);
3528 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003529}
3530
you.chenb95401e2023-05-12 19:39:06 +08003531int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3532{
3533 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3534}
3535
you.chen35020192022-05-06 11:30:57 +08003536int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003537{
you.chen35020192022-05-06 11:30:57 +08003538 ap_info_s ap;
3539 curr_status_info curr_state;
3540 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003541
qs.xiong9fbf74e2023-03-28 13:38:22 +08003542 if (ssid == NULL || *ssid == '\0')
3543 {
3544 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003545 return -1;
3546 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003547
you.chen35020192022-05-06 11:30:57 +08003548 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003549
you.chen35020192022-05-06 11:30:57 +08003550 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003551 curr_state.state = NULL;
3552
qs.xiong9fbf74e2023-03-28 13:38:22 +08003553 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3554 {
you.chen35020192022-05-06 11:30:57 +08003555 return 0;
3556 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003557
qs.xiong9fbf74e2023-03-28 13:38:22 +08003558 if (strcmp(ap.ap_ssid, ssid) != 0)
3559 {
you.chen35020192022-05-06 11:30:57 +08003560 return 0;
3561 }
3562
you.chen70f377f2023-04-14 18:17:09 +08003563 pthread_mutex_lock(&s_global_check_mutex);
3564 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3565 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003566 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003567}
qs.xiong97fa59b2022-04-07 05:41:29 -04003568
qs.xiongc93bf2b2023-08-25 10:22:08 +08003569int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3570{
3571 ap_info_s ap;
3572 curr_status_info curr_state;
3573 ap.ap_ssid[0] = '\0';
3574
3575 if (ssid == NULL || *ssid == '\0')
3576 {
3577 RLOGE("input ssid is NULL\n");
3578 return -1;
3579 }
3580
3581 CHECK_IDX(idx, CTRL_STA);
3582
3583
3584 curr_state.ap = &ap;
3585 curr_state.state = NULL;
3586
3587 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3588 {
3589 return 0;
3590 }
3591
3592 if (strcmp(ap.ap_ssid, ssid) != 0)
3593 {
3594 return 0;
3595 }
3596
3597 pthread_mutex_lock(&s_global_check_mutex);
3598 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003599 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3600 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003601 pthread_mutex_unlock(&s_global_check_mutex);
3602 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3603
3604}
3605
3606
you.chena6cd55a2022-05-08 12:20:18 +08003607int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3608{
qs.xiongb37f8c42023-09-13 21:21:58 +08003609
qs.xiongad2f89d2023-01-18 13:17:41 +08003610 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3611 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003612
you.chen35020192022-05-06 11:30:57 +08003613 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003614 CHECK_WPA_CTRL(CTRL_STA);
3615
you.chenc9928582023-04-24 15:39:37 +08003616 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3617 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003618 {
you.chenc9928582023-04-24 15:39:37 +08003619 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003620 return -1;
3621 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003622
qs.xiongad2f89d2023-01-18 13:17:41 +08003623 system(lynq_enable_sta_cmd);
3624 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003625 pthread_mutex_lock(&s_global_check_mutex);
3626 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3627 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003628 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003629}
3630
you.chen6d247052023-06-01 16:39:54 +08003631static int inner_get_status_info_state (int interface, char *state) {
3632 curr_status_info curr_state;
3633 curr_state.ap = NULL;
3634 curr_state.state = state;
3635 return inner_get_status_info(interface, &curr_state);
3636}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003637
3638int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3639{
3640
qs.xiongb37f8c42023-09-13 21:21:58 +08003641 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3642 int tmp_open_idx[128];
3643 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003644
qs.xiongb37f8c42023-09-13 21:21:58 +08003645 pthread_mutex_lock(&s_global_check_mutex);
3646 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3647 pthread_mutex_unlock(&s_global_check_mutex);
3648 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003649 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003650 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003651 }
3652
qs.xiongb37f8c42023-09-13 21:21:58 +08003653 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003654 return 0;
3655}
3656
3657
you.chen35020192022-05-06 11:30:57 +08003658int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003659{
qs.xiongad2f89d2023-01-18 13:17:41 +08003660// char lynq_disable_network_cmd[MAX_CMD];
3661// curr_status_info curr_state;
3662// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003663 int i=0;
3664 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003665
you.chen6d247052023-06-01 16:39:54 +08003666// 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 +08003667 CHECK_IDX(idx, CTRL_STA);
3668 CHECK_WPA_CTRL(CTRL_STA);
3669
you.chen6d247052023-06-01 16:39:54 +08003670// system(lynq_disable_sta_cmd);
3671 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003672 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003673
3674 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3675 if (ret != 0)
3676 {
3677 RLOGE("lynq_wifi_ap_start excute script fail");
3678 return -1;
3679 }
3680
you.chen6d247052023-06-01 16:39:54 +08003681 for (i=0; i < 30; i++) // to check if sta is realy stoped
3682 {
3683 if (inner_get_status_info_state(idx, state) != 0)
3684 {
3685 break;
3686 }
3687
3688 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3689 {
3690 break;
3691 }
3692 RLOGD("lynq_wifi_ap_start curr state %s", state);
3693 usleep(SLEEP_TIME_ON_IDLE);
3694 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003695 pthread_mutex_lock(&s_global_check_mutex);
3696 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3697 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003698 return 0;
3699// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003700}
qs.xiongfcc914b2023-07-06 21:16:20 +08003701int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3702{
3703 char LYNQ_DISABLE_CMD[128]={0};
3704 CHECK_IDX(idx, CTRL_STA);
3705 CHECK_WPA_CTRL(CTRL_STA);
3706 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3707 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3708 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3709 return 0;
3710}
qs.xiong7a105ce2022-03-02 09:43:11 -05003711
you.chen35020192022-05-06 11:30:57 +08003712//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3713// int i, count;
3714// char *p;
3715// const char * FLAG_SSID = "ssid=";
3716// const char * FLAG_SBSID = "bssid=";
3717// const char * FLAG_KEY_MGMT = "key_mgmt=";
3718// const char * FLAG_FREQ = "freq=";
3719// char lynq_sta_cmd[MAX_CMD];
3720// char *split_lines[128] = {0};
3721
3722// CHECK_WPA_CTRL(CTRL_AP);
3723
3724// sprintf(lynq_sta_cmd, "STA %s", bssid);
3725
3726// DO_REQUEST(lynq_sta_cmd);
3727
3728// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3729
3730// for(i=0; i < count; i++) {
3731// p = strstr(split_lines[i], FLAG_SSID);
3732// if (p != NULL) {
3733// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3734// continue;
3735// }
3736// }
3737
3738// lynq_get_interface_ip(idx, ap->ap_ip);
3739// lynq_ap_password_set(idx, ap->psw);
3740
3741// return 0;
3742//}
3743
3744static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3745 curr_status_info curr_state;
3746 curr_state.ap = ap;
3747 curr_state.state = NULL;
3748 return inner_get_status_info(interface, &curr_state);
3749}
3750
3751int 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 -04003752{
qs.xiong5071c802023-09-06 14:04:15 +08003753 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003754 int index, line_count;
3755 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003756 const char *lynq_first_sta_cmd = "STA-FIRST";
3757 char lynq_next_sta_cmd[MAX_CMD];
3758 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003759 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003760
you.chen35020192022-05-06 11:30:57 +08003761 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003762
you.chen35020192022-05-06 11:30:57 +08003763 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003764
you.chenb95401e2023-05-12 19:39:06 +08003765 // ap_info_s * tmp_ap;
3766 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003767 if (ap == NULL || list == NULL || len == NULL)
3768 {
3769 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003770 return -1;
3771 }
3772
you.chenb95401e2023-05-12 19:39:06 +08003773 // ap = &tmp_ap;
3774 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003775 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003776 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003777
you.chenb95401e2023-05-12 19:39:06 +08003778 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003779 {
you.chenb95401e2023-05-12 19:39:06 +08003780 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003781 return -1;
3782 }
3783
3784 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3785 lynq_ap_password_get(idx, (*ap)->psw);
3786
you.chen35020192022-05-06 11:30:57 +08003787 DO_REQUEST(lynq_first_sta_cmd);
3788
3789 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003790 while (reply_len > 0)
3791 {
3792 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003793 {
you.chen35020192022-05-06 11:30:57 +08003794 break;
3795 }
3796 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3797 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3798 strcpy(bssid[index], split_lines[0]);
3799 index++;
3800 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3801 reply_len = MAX_RET;
3802 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003803 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 +08003804 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003805 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003806 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003807 break;
3808 }
3809 }
3810
3811 *len = index;
3812
3813 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003814 for (index=0; index < *len; index++)
3815 {
you.chend2fef3f2023-02-13 10:50:35 +08003816 dev_info = &(*list)[index];
3817 memset(dev_info, 0, sizeof(device_info_s));
3818 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3819 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3820 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3821 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003822 free(bssid[index]);
3823 }
qs.xiong5071c802023-09-06 14:04:15 +08003824 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003825 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003826}
3827
you.chen35020192022-05-06 11:30:57 +08003828int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003829{
you.chen35020192022-05-06 11:30:57 +08003830 int i, count, index, count_words;
3831 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3832 char *split_lines[128] = {0};
3833 char *split_words[128] = {0};
3834 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003835
qs.xiong9fbf74e2023-03-28 13:38:22 +08003836 if (list == NULL || len == NULL)
3837 {
you.chen35020192022-05-06 11:30:57 +08003838 return -1;
3839 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003840
you.chen9ac66392022-08-06 17:01:16 +08003841 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3842 {
3843 usleep(100 * 1000);
3844 }
3845
you.chen35020192022-05-06 11:30:57 +08003846 CHECK_IDX(idx, CTRL_STA);
3847
3848 CHECK_WPA_CTRL(CTRL_STA);
3849
3850 DO_REQUEST(lynq_scan_result_cmd);
3851
3852 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3853 *len = count - 1;
3854 *list = malloc(sizeof (scan_info_s) * *len);
3855
3856 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003857 for (index=0; index <count_words; index++)
3858 {
3859 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003860 }
3861
qs.xiong9fbf74e2023-03-28 13:38:22 +08003862 for(index = 1;index < count; index++)
3863 {
3864 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003865 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003866 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3867 if (count_words < 4)
3868 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003869 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003870 //bssid / frequency / signal level / flags / ssid
3871 p = (*list) + index - 1;
3872 strcpy(p->mac, split_words[0]);
3873 p->band = convert_band_from_freq(atoi(split_words[1]));
3874 p->rssi = -1 * atoi( split_words[2]);
3875 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003876 if (count_words == 4) // ssid hided
3877 {
3878 p->ssid[0] = '\0';
3879 }
3880 else
3881 {
3882 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3883 }
you.chen35020192022-05-06 11:30:57 +08003884 }
3885
3886 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003887}
qs.xiong97fa59b2022-04-07 05:41:29 -04003888
you.chen35020192022-05-06 11:30:57 +08003889int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3890{
3891 int count, net_no, index;
3892 int net_no_list[128];
3893 lynq_wifi_auth_s net_auth;
3894 char lynq_remove_cmd[MAX_CMD];
3895
qs.xiong9fbf74e2023-03-28 13:38:22 +08003896 if (ssid == NULL || *ssid == '\0')
3897 {
3898 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003899 return -1;
3900 }
3901
3902 CHECK_IDX(idx, CTRL_STA);
3903
3904 CHECK_WPA_CTRL(CTRL_STA);
3905
3906 net_no = -1;
3907 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3908
qs.xiong9fbf74e2023-03-28 13:38:22 +08003909 for (index=0; index < count; index++)
3910 {
you.chen35020192022-05-06 11:30:57 +08003911 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003912 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3913 {
you.chen35020192022-05-06 11:30:57 +08003914 net_no = net_no_list[index];
3915 break;
3916 }
3917 }
3918
qs.xiong9fbf74e2023-03-28 13:38:22 +08003919 if (net_no < 0)
3920 {
you.chen35020192022-05-06 11:30:57 +08003921 return 0;
3922 }
3923
3924 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3925
3926 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3927 DO_OK_FAIL_REQUEST(cmd_save_config);
3928
3929 return 0;
3930}
3931
3932int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003933{
you.chend2fef3f2023-02-13 10:50:35 +08003934 int count, index;
you.chen35020192022-05-06 11:30:57 +08003935 int net_no_list[128];
3936 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003937 RLOGD("enter lynq_get_sta_saved_ap api\n");
3938 if (list == NULL || len == NULL)
3939 {
3940 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003941 return -1;
3942 }
3943
3944 CHECK_IDX(idx, CTRL_STA);
3945
3946// CHECK_WPA_CTRL(CTRL_STA);
3947
3948 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003949 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003950
you.chen057aac42023-04-13 14:06:58 +08003951 if (count < 0)
3952 {
3953 RLOGE("list network fail");
3954 return count;
3955 }
3956 else if (count == 0)
3957 {
3958 *list = NULL;
3959 *len = 0;
3960 return 0;
3961 }
3962
you.chen35020192022-05-06 11:30:57 +08003963 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003964 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003965 *len = count;
3966
qs.xiong9fbf74e2023-03-28 13:38:22 +08003967 for (index=0; index < count; index++)
3968 {
you.chen35020192022-05-06 11:30:57 +08003969 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003970 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003971 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003972 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003973 {
you.chen35020192022-05-06 11:30:57 +08003974 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3975 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003976 else
you.chen057aac42023-04-13 14:06:58 +08003977 {
you.chen35020192022-05-06 11:30:57 +08003978 (*list)[index].base_info.band = -1;
3979 }
you.chen057aac42023-04-13 14:06:58 +08003980 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003981 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003982 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003983 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003984 return 0;
3985}
3986
3987int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3988{
qs.xiong20202422023-09-06 18:01:18 +08003989 if ( g_sta_conncet_status_flag != 0 )
3990 {
3991 RLOGD("current sta is connecting dest ap");
3992 return -1;
3993 }
qs.xiongc8d92a62023-03-29 17:36:14 +08003994 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003995 const char *lynq_scan_cmd = "SCAN";
3996
3997 CHECK_IDX(idx, CTRL_STA);
3998
3999 CHECK_WPA_CTRL(CTRL_STA);
4000
you.chen0df3e7e2023-05-10 15:56:26 +08004001 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4002 {
4003 RLOGD("tmp clear scanlist");
4004 system(clean_last_re);
4005 }
you.chen9ac66392022-08-06 17:01:16 +08004006 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004007 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004008 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4009 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004010 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004011 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4012 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004013 g_sta_scan_finish_flag = 1;
4014 return -1;
4015 }
you.chen35020192022-05-06 11:30:57 +08004016
4017 return 0;
4018}
4019
4020int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004021 if (cb == NULL)
4022 {
4023 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004024 return -1;
4025 }
4026
you.chen6d247052023-06-01 16:39:54 +08004027 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004028 g_ap_callback_priv = priv;
4029 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004030 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004031
you.chen6d247052023-06-01 16:39:54 +08004032 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004033 if (g_ap_watcher_pid == 0 )
4034 {
4035 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4036 {
4037 g_ap_watcher_pid = 0;
4038 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4039 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4040 return -1;
4041 }
4042 }
4043
4044 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4045 RLOGD("creat APWatcherTheradProc susccs");
4046
you.chen35020192022-05-06 11:30:57 +08004047 return 0;
4048}
4049
4050int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004051 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004052 if (g_ap_callback_priv == priv)
4053 {
you.chen35020192022-05-06 11:30:57 +08004054 g_ap_callback_func = NULL;
4055 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004056 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004057 return 0;
4058 }
you.chen6d247052023-06-01 16:39:54 +08004059 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004060 return -1;
4061}
4062
4063int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004064 if (cb == NULL)
4065 {
4066 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004067 return -1;
4068 }
4069
you.chen6d247052023-06-01 16:39:54 +08004070 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004071 g_sta_callback_priv = priv;
4072 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004073 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004074
you.chen6d247052023-06-01 16:39:54 +08004075 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004076 if (g_sta_watcher_pid == 0 ) {
4077 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4078 {
4079 g_sta_watcher_pid = 0;
4080 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4081 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4082 return -1;
4083 }
4084 }
4085
4086 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4087 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004088 return 0;
4089}
4090
4091int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004092 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004093 if (g_sta_callback_priv == priv)
4094 {
you.chen35020192022-05-06 11:30:57 +08004095 g_sta_callback_func = NULL;
4096 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004097 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004098 return 0;
4099 }
you.chen6d247052023-06-01 16:39:54 +08004100 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004101 return -1;
4102}
4103
qs.xiongfcc914b2023-07-06 21:16:20 +08004104int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4105 if (cb == NULL)
4106 {
4107 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4108 return -1;
4109 }
4110 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4111 g_sta_auto_callback_priv = priv;
4112 g_sta_auto_callback_func = cb;
4113 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4114 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4115 if (g_sta_auto_watcher_pid == 0 ) {
4116 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4117 {
4118 g_sta_auto_watcher_pid = 0;
4119 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4120 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4121 return -1;
4122 }
4123 }
4124 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4125 RLOGD("creat STAWatcherTheradProc susccs");
4126 return 0;
4127}
4128int lynq_unreg_sta_auto_event_callback(void * priv) {
4129 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4130 if (g_sta_auto_callback_priv == priv)
4131 {
4132 g_sta_auto_watcher_stop_flag = 1;
4133 if (g_sta_auto_watcher_pid != 0)
4134 {
4135 pthread_join(g_sta_auto_watcher_pid, NULL);
4136 }
4137 g_sta_auto_watcher_pid = 0;
4138 g_sta_auto_callback_func = NULL;
4139 g_sta_auto_callback_priv = NULL;
4140 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4141 return 0;
4142 }
4143 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4144 return -1;
4145}
you.chen35020192022-05-06 11:30:57 +08004146int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4147{
4148 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004149 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004150 CHECK_IDX(idx, CTRL_AP);
4151
qs.xiong9fbf74e2023-03-28 13:38:22 +08004152 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4153 {
you.chen35020192022-05-06 11:30:57 +08004154 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4155 return 0;
4156 }
4157
qs.xiong9fbf74e2023-03-28 13:38:22 +08004158 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4159 {
you.chen35020192022-05-06 11:30:57 +08004160 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4161 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004162 else
4163 {
you.chen35020192022-05-06 11:30:57 +08004164 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4165 }
4166
4167 return 0;
4168}
4169
4170int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4171 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004172 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004173 CHECK_IDX(idx, CTRL_STA);
4174
qs.xiong9fbf74e2023-03-28 13:38:22 +08004175 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4176 {
you.chen35020192022-05-06 11:30:57 +08004177 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4178 return 0;
4179 }
4180
qs.xiong9fbf74e2023-03-28 13:38:22 +08004181 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4182 {
you.chen35020192022-05-06 11:30:57 +08004183 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4184 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004185 else
4186 {
you.chen35020192022-05-06 11:30:57 +08004187 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4188 }
4189
4190 return 0;
4191}
4192
4193int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4194// CHECK_IDX(idx, CTRL_AP);
4195// int ret = 0;
4196// size_t reply_len = MAX_RET;
4197// char cmd_reply[MAX_RET]={0};
4198// const char * cmd_str = "GET country";
4199// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4200// do{
4201// if (NULL == s_lynq_wpa_ctrl) {
4202// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4203// if (NULL == s_lynq_wpa_ctrl ) {
4204// printf("wpa_ctrl_open fail\n");
4205// return -1;
4206// }
4207// }
4208// }while(0);
4209
4210// do {
4211// reply_len = MAX_RET;
4212// cmd_reply[0] = '\0';
4213// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004214// 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 +08004215// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004216// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004217// return ret;
4218// }
4219// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004220// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004221// }while(0);
4222
4223 FILE *fp;
4224 size_t i = 0;
4225 char lynq_cmd_ret[MAX_RET]={0};
4226
4227// CHECK_IDX(idx, CTRL_AP);
4228
4229 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004230 {
4231 perror("popen error!");
4232 return -1;
4233 }
you.chen35020192022-05-06 11:30:57 +08004234 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4235 {
4236 perror("fread fail!");
4237 return -1;
4238 }
4239
qs.xiong9fbf74e2023-03-28 13:38:22 +08004240 for(i=0; i < strlen(lynq_cmd_ret); i++)
4241 {
4242 if (lynq_cmd_ret[i] == ' ')
4243 {
you.chen35020192022-05-06 11:30:57 +08004244 lynq_cmd_ret[i] = '\0';
4245 break;
4246 }
4247 }
4248
4249 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004250 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004251
4252 int ret=pclose(fp);
4253 if(ret==-1)
4254 {
4255 perror("close file faild");
4256 }
4257
4258 return 0;
4259}
4260
qs.xiong44fac672023-08-29 16:15:55 +08004261
you.chen705a7ef2023-06-01 22:06:45 +08004262static int check_and_init_uci_config(char * country_code)
4263{
4264 FILE * fp;
4265 int is_different = 0;
4266 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4267 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4268 const char * commit_uci_cmd ="uci commit";
4269 char set_country_cmd[MAX_CMD];
4270 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004271
you.chen705a7ef2023-06-01 22:06:45 +08004272 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004273
you.chen705a7ef2023-06-01 22:06:45 +08004274 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004275 {
you.chen705a7ef2023-06-01 22:06:45 +08004276 if (0 != system(create_uci_cmd))
4277 {
4278 RLOGE("creat_uci_cmd fail");
4279 return -1;
4280 }
4281 is_different = 1;
4282 }
4283
4284 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4285 {
4286 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004287 return -1;
4288 }
4289
you.chen705a7ef2023-06-01 22:06:45 +08004290 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004291 {
you.chen705a7ef2023-06-01 22:06:45 +08004292 RLOGE("fread fail!");
4293 fclose(fp);
4294 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004295 }
4296
you.chen705a7ef2023-06-01 22:06:45 +08004297 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4298 {
qs.xiong44fac672023-08-29 16:15:55 +08004299 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 +08004300 is_different = 1;
4301 }
4302
4303 fclose(fp);
4304
4305 if (is_different)
4306 {
4307 if ( 0 != system(set_country_cmd))
4308 {
4309 RLOGE("set_country_cmd fail");
4310 return -1;
4311 }
4312 if (0 != system(commit_uci_cmd))
4313 {
4314 RLOGE("commmit fail");
4315 }
4316 }
4317
4318 return is_different;
4319}
4320
4321int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4322 char check_current_code[10];
4323 const char * support_country[] = {"CN", "EU"};
4324
4325 int ret,is_different, i, cc_count;
4326
4327 if (country_code == NULL || country_code[0] == '\0')
4328 {
4329 RLOGE("bad country code\n");
4330 return -1;
4331 }
4332
4333 cc_count = sizeof (support_country) / sizeof (char*);
4334 for(i=0; i < cc_count; i++)
4335 {
4336 if (strcmp(support_country[i], country_code) == 0)
4337 {
4338 break;
4339 }
4340 }
4341
4342 if (i >= cc_count)
4343 {
4344 RLOGE("unspported country code %s\n", country_code);
4345 return -1;
4346 }
4347
4348 is_different = check_and_init_uci_config(country_code);
4349 if( is_different < 0 )
4350 {
4351 RLOGE("init set uci fail\n");
4352 return -1;
4353 }
4354
4355 ret = lynq_get_country_code(idx,check_current_code);
4356 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4357 {
4358 ret = lynq_wifi_disable();
4359 if(ret != 0 )
4360 {
4361 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4362 return -1;
4363 }
4364 }
4365
4366 return 0;
you.chen35020192022-05-06 11:30:57 +08004367}
4368
4369int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4370{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004371 RLOGD("enter lynq_get_connect_ap_mac\n");
4372 if (mac == NULL)
4373 {
4374 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004375 return -1;
4376 }
4377
4378 CHECK_IDX(idx, CTRL_STA);
4379 ap_info_s ap;
4380 ap.ap_mac[0] = '\0';
4381
qs.xiong9fbf74e2023-03-28 13:38:22 +08004382 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4383 {
you.chen35020192022-05-06 11:30:57 +08004384 return -1;
4385 }
4386 strcpy(mac, ap.ap_mac);
4387
4388 return 0;
4389}
4390
4391int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4392{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004393 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004394 struct ifaddrs *ifaddr_header, *ifaddr;
4395 struct in_addr * ifa;
4396 const char * ifaName = "wlan0";
4397 if (ip == NULL)
4398 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004399 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004400 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004401 }
you.chenf58b3c92022-06-21 16:53:48 +08004402
qs.xiong9fbf74e2023-03-28 13:38:22 +08004403 if (idx == 1)
4404 {
you.chen0df3e7e2023-05-10 15:56:26 +08004405 ifaName = inner_get_ap_interface_name();
4406 if (ifaName == NULL)
4407 {
4408 RLOGE("[lynq_get_interface_ip] ap name get fail");
4409 return -1;
4410 }
you.chen9ac66392022-08-06 17:01:16 +08004411 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004412 else if (idx != 0)
4413 {
you.chen35020192022-05-06 11:30:57 +08004414 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004415 }
you.chen35020192022-05-06 11:30:57 +08004416
you.chen9ac66392022-08-06 17:01:16 +08004417 if (getifaddrs(&ifaddr_header) == -1)
4418 {
you.chen35020192022-05-06 11:30:57 +08004419 perror("getifaddrs");
4420 return -1;
4421 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004422 }
you.chen35020192022-05-06 11:30:57 +08004423
4424
you.chen9ac66392022-08-06 17:01:16 +08004425 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4426 {
4427 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004428 continue;
you.chen9ac66392022-08-06 17:01:16 +08004429 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4430 {
4431 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4432 {
4433 // is a valid IP4 Address
4434 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4435 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004436 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004437 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004438 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004439 return 0;
4440 }
4441 }
4442 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004443 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004444 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004445 return -1;
you.chen35020192022-05-06 11:30:57 +08004446}
4447
4448int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4449{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004450 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004451 int count;
4452 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004453 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004454 char *split_words[128] = {0};
4455 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4456
4457 CHECK_WPA_CTRL(idx);
4458
4459 DO_REQUEST(lynq_get_mac_cmd);
4460
qs.xiong9fbf74e2023-03-28 13:38:22 +08004461 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4462 {
4463 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004464 return -1;
4465 }
4466
4467 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4468
qs.xiong9fbf74e2023-03-28 13:38:22 +08004469 if (count < 2)
4470 {
you.chen35020192022-05-06 11:30:57 +08004471 return -1;
4472 }
4473
qs.xiong9fbf74e2023-03-28 13:38:22 +08004474 for (i=0; i < strlen(split_words[1]); i++ )
4475 {
4476 if (split_words[1][i] != ' ')
4477 {
you.chen35020192022-05-06 11:30:57 +08004478 break;
4479 }
4480 }
4481
qs.xiongdd6e44c2023-08-08 15:02:53 +08004482 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004483
4484 return 0;
4485}
4486
4487int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4488{
4489// int count;
4490// char *split_words[128] = {0};
4491// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4492
4493// if (rssi == NULL) {
4494// return -1;
4495// }
4496
4497// CHECK_IDX(idx, CTRL_STA);
4498
4499// CHECK_WPA_CTRL(CTRL_STA);
4500
4501// DO_REQUEST(lynq_get_rssi_cmd);
4502
4503// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4504// return -1;
4505// }
4506
4507// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4508
4509// if (count < 2) {
4510// return -1;
4511// }
4512
4513// *rssi = atoi(split_words[1]) * -1;
4514
you.chen35020192022-05-06 11:30:57 +08004515 char lynq_cmd_ret[MAX_RET]={0};
4516
qs.xiongff0ae0f2022-10-11 15:47:14 +08004517/*******change other cmd to get rssi*******
4518 *
4519 *wl rssi ---> wl -i wlan0 rssi
4520 *
4521 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004522 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004523 {
you.chen23c4a5f2023-04-12 16:46:00 +08004524 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004525 return -1;
4526 }
you.chen9f17e4d2022-06-06 17:18:18 +08004527 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004528/****** if got rssi is 0,means sta didn't connected any device****/
4529 if(*rssi == 0)
4530 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004531 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004532 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004533 }
you.chen35020192022-05-06 11:30:57 +08004534
4535 return 0;
4536}
4537
4538int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4539{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004540 RLOGD("enter lynq_get_connect_ap_band\n");
4541 if (band == NULL)
4542 {
you.chen35020192022-05-06 11:30:57 +08004543 return -1;
4544 }
4545
4546 CHECK_IDX(idx, CTRL_STA);
4547 ap_info_s ap;
4548 ap.band = -1;
4549
qs.xiong9fbf74e2023-03-28 13:38:22 +08004550 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4551 {
you.chen35020192022-05-06 11:30:57 +08004552 return -1;
4553 }
4554 *band = ap.band;
4555
4556 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004557}
you.chenf58b3c92022-06-21 16:53:48 +08004558
4559int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4560{
you.chenb95401e2023-05-12 19:39:06 +08004561 int ret;
4562 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004563 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004564
4565 if (ip == NULL)
4566 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004567 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004568 return -1;
4569 }
4570
4571 CHECK_IDX(idx, CTRL_STA);
4572
qs.xionge4cbf1c2023-02-28 18:22:49 +08004573 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004574 {
4575 return -1;
4576 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004577
you.chenb95401e2023-05-12 19:39:06 +08004578 ip[0] = '\0';
4579 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4580 if (ret != 0)
4581 {
4582 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4583 return -1;
4584 }
4585
4586 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4587 {
4588 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004589 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004590 if (ret != 0)
4591 {
4592 ip[0] = '\0';
4593 return 0;
4594 }
4595 else
4596 {
4597 p = strchr(ip, '\n');
4598 if (p != NULL)
4599 {
4600 *p = '\0';
4601 }
4602 }
4603 }
4604 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004605}
4606
qs.xiong026c5c72022-10-17 11:15:45 +08004607int lynq_ap_connect_num(int sta_number)
4608{
4609 char lynq_limit_cmd[32]={0};
4610 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004611 if((sta_number < 1 ) && (sta_number > 15))
4612 {
4613 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004614 return -1;
4615 }
4616 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4617 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004618 if(ret != 0)
4619 {
4620 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004621 }
4622 return 0;
4623}
you.chenf58b3c92022-06-21 16:53:48 +08004624
qs.xiong77905552022-10-17 11:19:57 +08004625int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4626{
4627
4628 char lynq_wifi_acs_cmd[128]={0};
4629 char lynq_cmd_mode[128]={0};
4630 char lynq_cmd_slect[128]={0};
4631
qs.xiong9fbf74e2023-03-28 13:38:22 +08004632 if((acs_mode != 2) && (acs_mode != 5))
4633 {
qs.xiong77905552022-10-17 11:19:57 +08004634 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4635 }
4636
qs.xiong9fbf74e2023-03-28 13:38:22 +08004637 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4638 {
qs.xiong77905552022-10-17 11:19:57 +08004639 return -1;
4640 }
4641
4642 CHECK_IDX(idx, CTRL_AP);
4643
4644 CHECK_WPA_CTRL(CTRL_AP);
4645
4646 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4647 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4648 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4649
4650 DO_OK_FAIL_REQUEST(cmd_disconnect);
4651 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4652 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4653 DO_OK_FAIL_REQUEST(cmd_save_config);
4654 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4655
4656 return 0;
4657}
you.chen0f5c6432022-11-07 18:31:14 +08004658//you.chen add for tv-box start
4659static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4660 FILE *fp;
4661 //printf("to exec cmd:%s\n", str_cmd);
4662 if((fp=popen(str_cmd,"r"))==NULL)
4663 {
4664 perror("popen error!");
4665 return -1;
4666 }
4667 if((fread(str_cmd_ret,max_len,1,fp))<0)
4668 {
4669 perror("fread fail!");
4670 fclose(fp);
4671 return -1;
4672 }
4673 fclose(fp);
4674 return 0;
4675}
4676
4677static int get_netmask_length(const char* mask)
4678{
4679 int masklen=0, i=0;
4680 int netmask=0;
4681
4682 if(mask == NULL)
4683 {
4684 return 0;
4685 }
4686
4687 struct in_addr ip_addr;
4688 if( inet_aton(mask, &ip_addr) )
4689 {
4690 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004691 }else
4692 {
you.chen0f5c6432022-11-07 18:31:14 +08004693 netmask = 0;
4694 return 0;
4695 }
4696
4697 while(0 == (netmask & 0x01) && i<32)
4698 {
4699 i++;
4700 netmask = netmask>>1;
4701 }
4702 masklen = 32-i;
4703 return masklen;
4704}
4705
4706static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4707 int mask_len;
4708 char *p;
4709 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004710 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4711 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004712 return -1;
4713 p = strstr(str_cmd_ret, "Mask:");
4714 if (p == NULL)
4715 return -1;
4716 mask_len = get_netmask_length(p + 5);
4717 if (mask_len == 0)
4718 return -1;
4719 p = strstr(str_cmd_ret, "inet addr:");
4720 if (p == NULL)
4721 return -1;
4722 strcpy(tmp, p + 10);
4723 p = strstr(tmp, " ");
4724 if (p != NULL)
4725 *p = '\0';
4726 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4727 return 0;
4728}
4729
4730static void GBWWatchThreadProc() {
4731 int i,n, nloop, nmax, ncheckcount, nidlecount;
4732 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4733 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4734 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4735 char *results[16] = {0};
4736 char str_cmd[256] = {0};
4737 char str_cmd_ret[128] = {0};
4738 char dest_ip[32] = {0};
4739 lastAP1Bytes = lastAP2Bytes = 0;
4740 lastAP1Drop = lastAP2Drop = 0;
4741 lastAP1Speed = lastAP2Speed = 0;
4742 setAP1Speed = 50;
4743 setAP2Speed = 80;
4744 nloop = 0;
4745 nmax = 6;
4746 ncheckcount = nidlecount = 0;
4747
you.chen0df3e7e2023-05-10 15:56:26 +08004748 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004749 {
4750 RLOGE("------gbw thread run\n");
4751 return;
4752 }
4753
qs.xiong9fbf74e2023-03-28 13:38:22 +08004754 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004755 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4756 while (dest_ip[0] == '\0') {
4757 sleep(1);
4758 str_cmd_ret[0] = '\0';
4759 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4760 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4761 if (str_cmd_ret[n] == '\n'){
4762 str_cmd_ret[n] = '\0';
4763 break;
4764 }
4765 }
4766 if (str_cmd_ret[0] != '\0')
4767 {
4768 strcpy(dest_ip, str_cmd_ret);
4769 }
4770 }
4771
you.chenc9928582023-04-24 15:39:37 +08004772 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4773 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4774 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 +08004775 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4776 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004777 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004778 return;
4779 }
you.chenc9928582023-04-24 15:39:37 +08004780 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);
4781 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);
4782 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 +08004783
4784 while (1) {
4785 sleep(1);
4786 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004787 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4788 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4789 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004790 continue;
4791 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004792 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004793 if (n > 9) {
4794 if (strcmp(results[1], "Sent") == 0) {
4795 currAP1Bytes = atoll(results[2]);
4796 }
4797 if (strcmp(results[6], "(dropped") == 0) {
4798 currAP1Drop = atoi(results[7]);
4799 }
4800 }
4801
4802 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004803 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4804 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4805 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004806 continue;
4807 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004808 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004809 if (n > 9) {
4810 if (strcmp(results[1], "Sent") == 0) {
4811 currAP2Bytes = atoll(results[2]);
4812 }
4813 if (strcmp(results[6], "(dropped") == 0) {
4814 currAP2Drop = atoi(results[7]);
4815 }
4816 }
4817
4818 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4819 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4820 lastAP1Bytes = currAP1Bytes;
4821 lastAP2Bytes = currAP2Bytes;
4822 continue;
4823 }
4824
4825 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4826 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4827 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4828 lastAP1Speed = currAP1Speed;
4829 lastAP2Speed = currAP2Speed;
4830 lastAP1Bytes = currAP1Bytes;
4831 lastAP2Bytes = currAP2Bytes;
4832
4833 currSetAP1Speed = setAP1Speed;
4834 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4835 ncheckcount++;
4836 if (ncheckcount > 3) {
4837 ncheckcount = 0;
4838 currSetAP1Speed = 5;
4839 }
4840 }
4841 else {
4842 ncheckcount = 0;
4843 if (currAP1Speed < 5)
4844 nidlecount++;
4845 else
4846 nidlecount = 0;
4847
4848 }
4849
4850 if (nidlecount > 60 ){
4851 currSetAP1Speed = 50;
4852 }
4853
4854 if (currSetAP1Speed != setAP1Speed) {
4855 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004856 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4857 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004858 }
4859 }
4860}
4861
4862int enableGBW(const char* mac) {
4863 int i,len;
4864 char get_ipaddr_cmd[128]={0};
4865 ap_info_s *ap;
4866 device_info_s * list;
4867
4868 if (mac == NULL || g_gbw_enabled == 1)
4869 return -1;
4870 len = strlen(mac);
4871 g_gbw_mac = malloc(len + 1);
4872 for(i=0;i<len;i++) {
4873 if (mac[i] >= 'A' && mac[i] <= 'Z')
4874 {
4875 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4876 }
4877 else
4878 g_gbw_mac[i] = mac[i];
4879 }
4880 g_gbw_mac[i] = '\0';
4881 g_gbw_enabled = 1;
4882
4883 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4884 if (system(get_ipaddr_cmd) == 0) {
4885 //startGBW();
4886 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4887 for (i=0;i<len;i++) {
4888 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4889 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4890 startGBW();
4891 }
4892 free(ap);
4893 free(list);
4894 }
4895 }
4896 return 0;
4897}
4898
4899int disableGBW() {
4900 stopGBW();
4901 free(g_gbw_mac);
4902 g_gbw_mac = NULL;
4903 g_gbw_enabled = 1;
4904 return 0;
4905}
4906
4907static int startGBW() {
4908 if (g_gbw_watcher_pid != 0) {
4909 stopGBW();
4910 }
4911 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4912}
4913
4914static int stopGBW() {
4915 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004916 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004917 pthread_cancel(g_gbw_watcher_pid);
4918 pthread_join(g_gbw_watcher_pid, &retval);
4919 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004920 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4921 if (s_ap_iterface_name[0] != '\0')
4922 {
4923 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4924 system(cmd);
4925 }
you.chen0f5c6432022-11-07 18:31:14 +08004926}
4927//you.chen add for tv-box end