blob: 25e4badab9f216de8a01b81a13214541cc3e0973 [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.xiongbaec30f2023-09-20 13:10:15 +0800481 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800482 {
483 RLOGD("APWatcherThreadProc ap------> service error");
484 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
485 }
486 else
487 {
488 RLOGD("APWatcherThreadProc ap------> going on check next msg");
489 }
you.chenf711c8a2023-04-13 13:49:45 +0800490 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800491 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
492 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800493 if (lynq_wpa_ctrl != NULL)
494 {
you.chen92fd5d32022-05-25 10:09:47 +0800495 wpa_ctrl_detach(lynq_wpa_ctrl);
496 wpa_ctrl_close(lynq_wpa_ctrl);
497 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400498}
499
you.chen70f377f2023-04-14 18:17:09 +0800500static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
501{
502 char * p;
503 const char * try_associat_flag = "Trying to associate";
504 const char * associated_flag = "Associated with ";
505
506 pthread_mutex_lock(&s_global_check_mutex);
507 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
508 {
509 pthread_mutex_unlock(&s_global_check_mutex);
510 return;
511 }
512
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];
qs.xiongb8518cd2023-09-22 18:43:42 +0800810 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800811 {
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.xiong7d24bf52023-09-20 13:22:35 +0800907 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +0800908 // tmp fix sta autoconnect connect and disconnect
909 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
910 {
911 RLOGE("stop wlan0 network %d fail",tmpdisid);
912 }
qs.xiong455c30b2023-04-12 11:40:02 +0800913 }
914 else
915 {
916 *error = LYNQ_UNSPECIFIED_REASON;
917 }
qs.xiong455c30b2023-04-12 11:40:02 +0800918 }
919 else
920 {
921 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800922 }
qs.xiongf0128b12023-06-29 17:29:39 +0800923 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800924 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800925 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800926
927 }
928
929 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
930 {
931 *error = LYNQ_NOT_FIND_AP;
932 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
933 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800934 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800935 return;
936 }
937
938
939 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
940 {
941 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
942 pReason = strstr(modify, "status_code=");
943 if (pReason != NULL)
944 {
945 pReason += strlen("status_code=");
946 if (memcmp(pReason, "17", 2) == 0)
947 {
948 *error = LYNQ_AP_UNABLE_HANDLE;
949 }
950 else if (memcmp(pReason, "1",1) == 0)
951 {
952 *error = LYNQ_UNSPECIFIED_REASON;
953 }
954 else
955 {
956 *error = LYNQ_UNSPECIFIED_REASON;
957 }
958
959 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
960 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 +0800961 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800962 return;
963 }
964 else
965 {
966 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
967 *error = LYNQ_UNSPECIFIED_REASON;
968 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
969 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
970 return;
971 }
972 }
973
974 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
975 {
976 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
977 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
978 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
979 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
980 return;
981 }
982
983 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
984 {
985 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
986 *error = LYNQ_WAIT_CONNECT_ACTIVE;
987 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
988 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
989 return;
990 }
991
you.chen32cb31e2023-04-13 14:05:45 +0800992 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800993 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800994 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800995 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
996 return;
997
998}
999
qs.xiongfcc914b2023-07-06 21:16:20 +08001000void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1001{
1002 char *pReason;
1003 char *wpanetid;
1004 char destid[3];
1005 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1006 *networkid = -1;
1007 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1008 {
1009 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1010 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1011 return;
1012 }
1013 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1014 {
1015 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1016 wpanetid = strstr(modify,"id=");
1017 if ( wpanetid != NULL )
1018 {
1019 wpanetid +=strlen("id=");
1020 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1021 if (memcpy(destid,wpanetid,2) != NULL)
1022 {
1023 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1024 *networkid = atoi(destid);
1025 RLOGD("get networkid is %d\n",*networkid);
1026 }
1027 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1028 }
1029 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1030 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1031 return;
1032 }
1033 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1034 {
1035 wpanetid = strstr(modify,"id=");
1036 if ( wpanetid != NULL )
1037 {
1038 wpanetid +=strlen("id=");
1039 if (memcpy(destid,wpanetid,2) != NULL)
1040 {
1041 *networkid = atoi(destid);
1042 RLOGD("get networkid is %d\n",*networkid);
1043 }
1044 }
1045 pReason = strstr(modify, "reason=");
1046 if (pReason != NULL)
1047 {
1048 pReason += strlen("reason=");
1049 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1050 {
1051 *error = LYNQ_TIME_OUT;
1052 }
1053 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1054 {
1055 *error = LYNQ_PSW_ERROR;
1056 }
1057 else
1058 {
1059 *error = LYNQ_UNSPECIFIED_REASON;
1060 }
1061 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1062 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1063 return;
1064 }
1065 else
1066 {
1067 *error = LYNQ_UNSPECIFIED_REASON;
1068 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1069 return;
1070 }
1071 }
1072 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1073 {
1074 wpanetid = strstr(modify,"id=");
1075 if ( wpanetid != NULL )
1076 {
1077 wpanetid +=strlen("id=");
1078 if (memcpy(destid,wpanetid,2) != NULL)
1079 {
1080 *networkid = atoi(destid);
1081 RLOGD("get networkid is %d\n",*networkid);
1082 }
1083 }
1084 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1085 pReason = strstr(modify, "status_code=");
1086 if (pReason != NULL)
1087 {
1088 pReason += strlen("status_code=");
1089 if (memcmp(pReason, "17", 2) == 0)
1090 {
1091 *error = LYNQ_AP_UNABLE_HANDLE;
1092 }
1093 else if (memcmp(pReason, "1",1) == 0)
1094 {
1095 *error = LYNQ_UNSPECIFIED_REASON;
1096 }
1097 else
1098 {
1099 *error = LYNQ_UNSPECIFIED_REASON;
1100 }
1101 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1102 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1103 return;
1104 }
1105 else
1106 {
1107 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1108 *error = LYNQ_UNSPECIFIED_REASON;
1109 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001110 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001111 return;
1112 }
1113 }
1114 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1115 {
1116 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1117 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1118 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1119 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1120 return;
1121 }
1122 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1123 {
1124 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1125 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1126 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1127 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1128 return;
1129 }
1130 RLOGD("EVENT : %s\n", modify);
1131 *error = LYNQ_UNSPECIFIED_REASON;
1132 *state = LYNQ_WIFI_STATUS_EGNORE;
1133 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1134 return;
1135}
you.chen70f377f2023-04-14 18:17:09 +08001136static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1137{
you.chen6d247052023-06-01 16:39:54 +08001138 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001139 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1140 {
1141 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1142 g_sta_callback_func(g_sta_callback_priv, state, error);
1143 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1144 }
you.chen6d247052023-06-01 16:39:54 +08001145 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001146}
qs.xiongfcc914b2023-07-06 21:16:20 +08001147static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1148{
1149 pthread_mutex_lock(&s_sta_callback_mutex);
1150 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1151 {
1152 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1153 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1154 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1155 }
1156 pthread_mutex_unlock(&s_sta_callback_mutex);
1157}
you.chen70f377f2023-04-14 18:17:09 +08001158
you.chen35020192022-05-06 11:30:57 +08001159static void STAWatcherThreadProc() {
1160 size_t len = MAX_RET;
1161 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001162 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001163 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001164 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001165
you.chen6c2dd9c2022-05-16 17:55:28 +08001166 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001167 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001168
you.chen70f377f2023-04-14 18:17:09 +08001169 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001170 while (g_sta_watcher_stop_flag == 0)
1171 {
you.chenf711c8a2023-04-13 13:49:45 +08001172 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001173 {
you.chen35020192022-05-06 11:30:57 +08001174 continue;
1175 }
you.chenf711c8a2023-04-13 13:49:45 +08001176
you.chen6c2dd9c2022-05-16 17:55:28 +08001177 memset(msg_notify, 0, MAX_RET);
1178 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001179 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001180 {
you.chen35020192022-05-06 11:30:57 +08001181 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001182 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1183 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001184 {
you.chen35020192022-05-06 11:30:57 +08001185 g_sta_scan_finish_flag = 1;
1186 }
1187
qs.xiong9fbf74e2023-03-28 13:38:22 +08001188 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001189 {
you.chen35020192022-05-06 11:30:57 +08001190 continue;
1191 }
qs.xiong455c30b2023-04-12 11:40:02 +08001192 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001193 notify_connect_status(state, error);
1194
1195 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001196 {
you.chen70f377f2023-04-14 18:17:09 +08001197 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001198 if (last_state != state)
1199 {
1200 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1201 {
1202 system_call_v("%s %s", sta_status_change_script, "connect");
1203 }
1204 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1205 {
1206 system_call_v("%s %s", sta_status_change_script, "disconnect");
1207 }
1208 }
1209
1210 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001211 }
you.chen35020192022-05-06 11:30:57 +08001212 }
1213 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001214 if (lynq_wpa_ctrl != NULL)
1215 {
you.chen92fd5d32022-05-25 10:09:47 +08001216 wpa_ctrl_detach(lynq_wpa_ctrl);
1217 wpa_ctrl_close(lynq_wpa_ctrl);
1218 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001219}
qs.xiongfcc914b2023-07-06 21:16:20 +08001220static void STAAutoWatcherThreadProc() {
1221 size_t len = MAX_RET;
1222 char msg_notify[MAX_RET];
1223 error_number_s error;
1224 lynq_wifi_sta_status_s state, last_state = -1;
1225 int idle_count = 0;
1226 int networkid;
1227 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1228 g_sta_auto_watcher_stop_flag = 0;
1229 RLOGD("STAAutoWatcherThreadProc thread started ------");
1230 while (g_sta_auto_watcher_stop_flag == 0)
1231 {
1232 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1233 {
1234 continue;
1235 }
1236 memset(msg_notify, 0, MAX_RET);
1237 len = MAX_RET;
1238 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1239 {
1240 msg_notify[len+1] = '\0';
1241 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1242 if (strstr(msg_notify, state_scan_result) != NULL)
1243 {
1244 g_sta_auto_scan_finish_flag = 1;
1245 }
1246 if (g_sta_auto_callback_func == NULL)
1247 {
1248 continue;
1249 }
1250 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1251 notify_auto_connect_status(state, error,networkid);
1252 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1253 {
1254 inner_check_connect_error(msg_notify, state, error);
1255 if (last_state != state)
1256 {
1257 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1258 {
1259 system_call_v("%s %s", sta_status_change_script, "connect");
1260 }
1261 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1262 {
1263 system_call_v("%s %s", sta_status_change_script, "disconnect");
1264 }
1265 }
1266 last_state = state;
1267 }
1268 }
1269 }
1270 if (lynq_wpa_ctrl != NULL)
1271 {
1272 wpa_ctrl_detach(lynq_wpa_ctrl);
1273 wpa_ctrl_close(lynq_wpa_ctrl);
1274 }
1275}
qs.xiongf1b525b2022-03-31 00:58:23 -04001276
you.chen70f377f2023-04-14 18:17:09 +08001277// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1278static void GlobalWatcherThreadProc()
1279{
1280 int ret, connect_timeout, service_abnormal;
1281 error_number_s error_num = -1;
1282 inner_sta_status_s sta_status;
1283 scan_info_s *scan_list = NULL;
1284 int i, scan_len=0;
1285 char connecting_ssid[64];
1286 struct timeval now;
1287
1288 RLOGD("GlobalWatcherThreadProc start to run");
1289
1290 while (1)
1291 {
1292 pthread_mutex_lock(&s_global_check_mutex);
1293 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1294 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1295 {
1296 pthread_mutex_unlock(&s_global_check_mutex);
1297 usleep(50*1000);
1298 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1299 continue;
1300 }
1301
1302 connect_timeout = 0;
1303 service_abnormal = 0;
1304 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1305 {
1306 while (1)
1307 {
1308 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1309 if (ret == ETIME)
1310 {
1311 connect_timeout = 1;
1312 }
1313 else if (ret != 0)
1314 {
1315 gettimeofday(&now,NULL);
1316 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1317 {
1318 usleep(SLEEP_TIME_ON_IDLE);
1319 continue;
1320 }
1321 connect_timeout = 1;
1322 }
1323 sta_status = s_sta_status;
1324 error_num = s_sta_error_number;
1325 s_sta_status = INNER_STA_STATUS_INIT;
1326 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1327 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1328 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1329 break;
1330 }
1331 }
1332 if (s_service_invoke_timeout_cnt > 10)
1333 {
1334 service_abnormal = 1;
1335 s_service_invoke_timeout_cnt = 0;
1336 }
1337 pthread_mutex_unlock(&s_global_check_mutex);
1338
1339 if (service_abnormal == 1)
1340 {
1341 sleep(1);
1342 RLOGE("wpa service is abnormal info app to exit");
1343 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001344
1345 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1346
you.chen70f377f2023-04-14 18:17:09 +08001347 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1348 }
1349
1350 if (sta_status == INNER_STA_STATUS_CANCEL)
1351 {
1352 continue;
1353 }
1354 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1355 {
1356 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1357 }
1358 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1359 {
1360 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1361 {
1362 for(i=0; i < scan_len;i++)
1363 {
1364 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1365 {
1366 error_num = LYNQ_AUTH_ERROR;
1367 break;
1368 }
1369 }
1370 free(scan_list);
1371 }
1372 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1373 }
1374 else if (connect_timeout == 0)
1375 {
1376 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1377 }
1378 else // wait timeout
1379 {
1380 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1381 {
1382 ; // wpa service abnormal
1383 }
1384 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1385 {
1386 RLOGD("GlobalWatcherThreadProc notify connected");
1387 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1388 }
1389 else
1390 {
1391 RLOGD("GlobalWatcherThreadProc notify timeout");
1392 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1393 }
1394 }
1395 } // while (1)
1396}
1397
qs.xiong1af5daf2022-03-14 09:12:12 -04001398int lynq_wifi_enable(void)
1399{
you.chen35020192022-05-06 11:30:57 +08001400 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001401 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001402 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001403 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1404
qs.xiong9fbf74e2023-03-28 13:38:22 +08001405 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1406 {
you.chend2fef3f2023-02-13 10:50:35 +08001407 goto out_enable;
1408 }
1409
you.chenc9928582023-04-24 15:39:37 +08001410 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001411 if (ret != 0)
1412 {
1413 //printf("service state %d\n", ret);
1414 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001415 ret = -1;
1416 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001417 }
lhfe8da902022-10-11 18:55:36 +08001418
you.chen70f377f2023-04-14 18:17:09 +08001419 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1420 {
1421 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1422 if(ret<0)
1423 {
1424 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1425 ret = -1;
1426 goto out_enable;
1427 }
1428 }
1429
you.chend2fef3f2023-02-13 10:50:35 +08001430 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1431 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1432 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1433 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1434out_enable:
1435 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001436 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001437}
1438
qs.xiong1af5daf2022-03-14 09:12:12 -04001439int lynq_wifi_disable(void)
1440{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001441 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001442 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001443 g_ap_watcher_stop_flag = 1;
1444 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001445 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001446 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001447 if (g_ap_watcher_pid != 0)
1448 pthread_join(g_ap_watcher_pid, NULL);
1449 if (g_sta_watcher_pid != 0)
1450 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001451 if (g_sta_auto_watcher_pid != 0)
1452 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001453 if (g_lynq_wpa_ctrl[0] != NULL)
1454 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1455 if (g_lynq_wpa_ctrl[1] != NULL)
1456 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001457 if (g_ap_tmp_watcher_pid != 0)
1458 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001459 g_ap_watcher_pid = 0;
1460 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001461 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001462 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001463 g_lynq_wpa_ctrl[0] = NULL;
1464 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001465 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001466 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001467 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1468 return 0;
1469}
1470
1471static inline char inner_convert_char(char in)
1472{
1473 if (in >= '0' && in <= '9')
1474 {
1475 return in - '0';
1476 }
1477 else if (in >= 'a' && in <= 'f')
1478 {
1479 return in - 'a' + 10;
1480 }
1481 else if (in >= 'A' && in <= 'F')
1482 {
1483 return in - 'A' + 10;
1484 }
1485 else
1486 {
1487 return '\xff';
1488 }
1489}
1490
1491static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1492{
1493 char *p;
1494 size_t pos = 0;
1495 if (NULL == out_ssid)
1496 return;
1497 //printf("input ssid=[%s]\n", ssid);
1498 memset(out_ssid, 0, out_ssid_len);
1499 if (NULL == ssid)
1500 return;
1501 p = strchr(ssid, '\\');
1502 if (NULL == p)
1503 {
1504 strncpy(out_ssid, ssid, out_ssid_len);
1505 //printf(" first %s\n", out_ssid);
1506 }
1507 else
1508 {
1509 pos = p - ssid;
1510 memcpy(out_ssid, ssid, pos);
1511 //printf("pos %lu -- %s\n", pos, out_ssid);
1512 for(; pos < out_ssid_len; pos ++)
1513 {
1514 if (p[0] == '\0')
1515 {
1516 //printf(" out %s\n", out_ssid);
1517 return;
1518 }
1519 else if (p[0] != '\\')
1520 {
1521 out_ssid[pos] = p[0];
1522 p += 1;
1523 }
1524 else if (p[1] == 'x' || p[1] == 'X')
1525 {
1526 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1527 p += 4;
1528 }
1529 else if (p[1] == '\\')
1530 {
1531 out_ssid[pos] = '\\';
1532 p += 2;
1533 }
1534 else if (p[1] == 't')
1535 {
1536 out_ssid[pos] = '\t';
1537 p += 2;
1538 }
1539 else if (p[1] == 'r')
1540 {
1541 out_ssid[pos] = '\r';
1542 p += 2;
1543 }
1544 else if (p[1] == 'n')
1545 {
1546 out_ssid[pos] = '\n';
1547 p += 2;
1548 }//todo find a better way to convert?
1549 }
1550 }
1551 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001552}
qs.xiong1af5daf2022-03-14 09:12:12 -04001553
you.chen35020192022-05-06 11:30:57 +08001554static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001555 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001556 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001557 RLOGD("enter inner_get_param");
1558 if (out_put == NULL)
1559 {
1560 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001561 return -1;
1562 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001563 if (param_name == NULL)
1564 {
1565 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001566 return -1;
1567 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001568 if (param_name[0] == '\0')
1569 {
1570 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001571 return -1;
1572 }
1573
1574 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1575
1576 CHECK_WPA_CTRL(interface);
1577
1578 DO_REQUEST(lynq_cmd_get);
1579
qs.xiong9fbf74e2023-03-28 13:38:22 +08001580 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1581 {
1582 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001583 return -1;
1584 }
1585
you.chena6fa5b22022-05-18 10:28:19 +08001586// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001587 if (strcmp(param_name, "ssid") == 0)
1588 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001589 if (cmd_reply[0] == '\"')
1590 {
you.chend2fef3f2023-02-13 10:50:35 +08001591 ssid_len = reply_len - 1;
1592 memcpy(out_put, cmd_reply + 1, ssid_len);
1593 if (out_put[ssid_len-1] == '\"')
1594 {
1595 out_put[ssid_len-1] = '\0';
1596 }
1597 else
1598 {
1599 out_put[ssid_len] = '\0';
1600 }
1601 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001602 else
1603 {
you.chend2fef3f2023-02-13 10:50:35 +08001604 ssid_len = reply_len / 2;
1605 for(i=0; i<ssid_len; i++)
1606 {
1607 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1608 }
1609 out_put[ssid_len] = '\0';
1610 }
1611 }
1612 else
1613 {
1614 memcpy(out_put, cmd_reply, reply_len + 1);
1615 }
you.chen35020192022-05-06 11:30:57 +08001616 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001617}
qs.xiong1af5daf2022-03-14 09:12:12 -04001618
you.chen35020192022-05-06 11:30:57 +08001619static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1620 int ret = 0;
1621 char * end = str + len - 1;
1622 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001623 while(str < end)
1624 {
1625 if (*str == delimiter)
1626 {
you.chen35020192022-05-06 11:30:57 +08001627 *str++ = '\0';
1628 results[ret++] = str;
1629 continue;
1630 }
1631 str++;
1632 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001633 if (*str == delimiter)
1634 {
you.chen35020192022-05-06 11:30:57 +08001635 *str = '\0';
1636 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001637
you.chen6ed36a62023-04-27 17:51:56 +08001638 results[ret] = NULL;
1639
you.chen35020192022-05-06 11:30:57 +08001640 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001641}
1642
you.chend2fef3f2023-02-13 10:50:35 +08001643static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1644{
1645 char * p;
1646 int ret = 0;
1647 char cmd[256]={0};
1648 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001649 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001650 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001651 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001652 ret = exec_cmd(cmd, ip, ip_len);
1653 p = strchr(ip, '\n');
1654 if (NULL != p)
1655 {
1656 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001657 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001658 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001659 return ret;
1660}
1661
you.chend2fef3f2023-02-13 10:50:35 +08001662static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001663 struct in_addr addr ={0};
1664 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001665 char cmd[64] = {0};
1666 char * p;
1667 int ret;
you.chen35020192022-05-06 11:30:57 +08001668
qs.xiong9fbf74e2023-03-28 13:38:22 +08001669 if (ip == NULL || *ip == '\0' || hostname == NULL)
1670 {
1671 RLOGE("ip == NULL or hostname == NULL");
1672 return -1;
you.chen35020192022-05-06 11:30:57 +08001673 }
1674
you.chend2fef3f2023-02-13 10:50:35 +08001675 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001676 if (inet_aton(ip, &addr) == 0)
1677 {
you.chen35020192022-05-06 11:30:57 +08001678 printf("---inet_aton fail\n");
1679 return -1;
1680 }
1681
1682 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1683
qs.xiong9fbf74e2023-03-28 13:38:22 +08001684 if (ht == NULL)
1685 {
you.chen186d3c32023-05-18 14:19:46 +08001686 hostname[0] = '\0';
1687 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1688 ret = exec_cmd(cmd, hostname, 32);
1689 if (ret == 0)
1690 {
1691 p = strchr(hostname, '\n');
1692 if (p != NULL)
1693 {
1694 *p = '\0';
1695 }
1696 return 0;
1697 }
1698 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001699 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001700 herror(NULL);
1701 return -1;
1702 }
1703
1704 strcpy(hostname, ht->h_name);
1705
1706 return 0;
1707}
1708
1709static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1710{
1711 int count, index, words_count;
1712 char * split_lines[128]= {0};
1713 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001714 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001715 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001716 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001717
1718 CHECK_WPA_CTRL(ap_sta);
1719
1720 DO_REQUEST(lynq_wifi_list_networks);
1721
1722 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1723
1724 //@todo check ssid field to compatible
1725
1726 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001727 for(index=1; index < count; index++)
1728 {
you.chen35020192022-05-06 11:30:57 +08001729 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001730 if (words_count > 2)
1731 {
you.chend2fef3f2023-02-13 10:50:35 +08001732 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001733 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1734 {
you.chen35020192022-05-06 11:30:57 +08001735 net_no_list[ret++] = atoi(split_words[0]);
1736 }
1737 }
1738 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001739 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001740 return ret;
1741}
1742
1743static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001744 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001745 CHECK_WPA_CTRL(ap_sta);
1746 const char *lynq_wifi_add_network = "ADD_NETWORK";
1747
qs.xiong9fbf74e2023-03-28 13:38:22 +08001748 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001749 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001750 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1751 {
1752 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001753 return -1;
1754 }
1755
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 for(i=0;i<reply_len;i++)
1757 {
1758 if(cmd_reply[i] == '\n')
1759 {
you.chen35020192022-05-06 11:30:57 +08001760 cmd_reply[i] = '\0';
1761 break;
1762 }
1763 }
1764 return atoi(cmd_reply);
1765}
you.chena6cd55a2022-05-08 12:20:18 +08001766
you.chen35020192022-05-06 11:30:57 +08001767static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1768{
1769 int count, index;
1770 int net_no_list[128];
1771
qs.xiong9fbf74e2023-03-28 13:38:22 +08001772 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001773 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001774 for (index=0; index < count; index++)
1775 {
1776 if (net_no_list[index] == net_no)
1777 {
you.chen35020192022-05-06 11:30:57 +08001778 return 0;
1779 }
1780 }
1781
1782 if (count >= 1)
1783 index = net_no_list[count - 1];
1784 else
1785 index = -1;
1786
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 while (index < net_no )
1788 {
you.chen35020192022-05-06 11:30:57 +08001789 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001790 if (index >= net_no)
1791 { // required network no created
1792 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001793 return 0;
1794 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001795 else if( index < 0)
1796 {
1797 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001798 return -1;
1799 }
you.chen35020192022-05-06 11:30:57 +08001800 }
1801
1802 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001803 {
1804 RLOGE("[lynq_check_network_number] network index < 0");
1805 return -1;
1806 }
1807 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001808 return 0;
1809}
1810
1811static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001812 if (freq > 5000 && freq < 6000)
1813 {
you.chen35020192022-05-06 11:30:57 +08001814 return LYNQ_WIFI_5G_band;
1815 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001816 else if (freq > 2000 && freq < 3000)
1817 {
you.chen35020192022-05-06 11:30:57 +08001818 return LYNQ_WIFI_2G_band;
1819 }
1820 return LYNQ_WIFI_2_and_5G_band;
1821}
1822
1823static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001824 if (key_mgmt != NULL)
1825 {
1826 if (memcmp( key_mgmt, "NONE", 4) == 0)
1827 {
you.chen35020192022-05-06 11:30:57 +08001828 return LYNQ_WIFI_AUTH_OPEN;
1829 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001830 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1831 {
you.chen35020192022-05-06 11:30:57 +08001832 return LYNQ_WIFI_AUTH_WEP;
1833 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001834 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1835 {
you.chen35020192022-05-06 11:30:57 +08001836 return LYNQ_WIFI_AUTH_WPA_PSK;
1837 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001838 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1839 {
you.chen35020192022-05-06 11:30:57 +08001840 return LYNQ_WIFI_AUTH_WPA2_PSK;
1841 }
1842 }
1843
1844 return -1;
1845}
1846
1847static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001848 if (flag != NULL)
1849 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001850 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 +08001851 {
1852 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001853 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001854 {
1855 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1856 }else if (strstr( flag, "WPA2-PSK") != NULL)
1857 {
you.chen35020192022-05-06 11:30:57 +08001858 return LYNQ_WIFI_AUTH_WPA2_PSK;
1859 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001860 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001861 {
you.chen35020192022-05-06 11:30:57 +08001862 return LYNQ_WIFI_AUTH_WPA_PSK;
1863 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001865 {
you.chen35020192022-05-06 11:30:57 +08001866 return LYNQ_WIFI_AUTH_WEP;
1867 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001868 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001869 {
you.chen35020192022-05-06 11:30:57 +08001870 return LYNQ_WIFI_AUTH_OPEN;
1871 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001872 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001873 {
you.chend2fef3f2023-02-13 10:50:35 +08001874 return LYNQ_WIFI_AUTH_OPEN;
1875 }
qs.xiong46f41562023-07-11 21:06:47 +08001876 else
1877 {
1878 RLOGD("convert_max_auth_from_flag not-found auth mode");
1879 }
you.chen35020192022-05-06 11:30:57 +08001880 }
1881
1882 return -1;
1883}
1884
1885static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1886 switch (bw) {
1887 case 10:
1888 return LYNQ_WIFI_BANDWIDTH_HT10;
1889 break;
1890 case 20:
1891 return LYNQ_WIFI_BANDWIDTH_HT20;
1892 break;
1893 case 40:
1894 return LYNQ_WIFI_BANDWIDTH_HT40;
1895 break;
1896 case 80:
1897 return LYNQ_WIFI_BANDWIDTH_HT80;
1898 break;
1899 default:
1900 break;
1901 }
1902
1903 return -1;
1904}
1905
you.chen70f377f2023-04-14 18:17:09 +08001906static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001907static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1908 int i, count;
1909 char *p;
1910 const char *lynq_status_cmd = "STATUS";
1911 const char * FLAG_SSID = "ssid=";
1912 const char * FLAG_SBSID = "bssid=";
1913 const char * FLAG_KEY_MGMT = "key_mgmt=";
1914 const char * FLAG_FREQ = "freq=";
1915 const char * FLAG_STATE = "wpa_state=";
1916 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001917 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001918 char *split_lines[128] = {0};
1919
1920 CHECK_WPA_CTRL(interface);
1921
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 if (curr_state == NULL)
1923 {
1924 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001925 return -1;
1926 }
1927
1928 DO_REQUEST(lynq_status_cmd);
1929
1930 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1931
1932 curr_state->net_no = -1;
1933 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 for(i=0; i < count; i++)
1935 {
1936 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001937 {
you.chen35020192022-05-06 11:30:57 +08001938 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001939 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001940 {
you.chend2fef3f2023-02-13 10:50:35 +08001941 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001942 ret = 0;
1943 continue;
1944 }
you.chenf58b3c92022-06-21 16:53:48 +08001945 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001946 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001947 {
you.chend2fef3f2023-02-13 10:50:35 +08001948 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 +08001949 ret = 0;
1950 continue;
1951 }
you.chen35020192022-05-06 11:30:57 +08001952 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001953 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001954 {
you.chen450d0172022-07-15 17:56:48 +08001955 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001956 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001957 ret = 0;
1958 continue;
1959 }
1960 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001961 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001962 {
you.chen35020192022-05-06 11:30:57 +08001963 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1964 ret = 0;
1965 continue;
1966 }
you.chend2fef3f2023-02-13 10:50:35 +08001967 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001969 {
you.chend2fef3f2023-02-13 10:50:35 +08001970 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1971 ret = 0;
1972 continue;
1973 }
you.chen35020192022-05-06 11:30:57 +08001974 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001976 {
you.chen35020192022-05-06 11:30:57 +08001977 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001978 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001979 {
you.chen35020192022-05-06 11:30:57 +08001980 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1981 ret = 0;
1982 continue;
1983 }
1984
1985 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001986 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001987 {
you.chen35020192022-05-06 11:30:57 +08001988 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001989 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001991 }
1992 }
1993
you.chen70f377f2023-04-14 18:17:09 +08001994 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1995 {
1996 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1997 }
1998
you.chen35020192022-05-06 11:30:57 +08001999 return ret;
2000}
2001
qs.xiongf1b525b2022-03-31 00:58:23 -04002002int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002003{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002005 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002006
qs.xiong9fbf74e2023-03-28 13:38:22 +08002007 if (ap_ssid == NULL)
2008 {
2009 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002010 return -1;
2011 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002012 else
2013 {
2014 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002015 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002016
qs.xiong9fbf74e2023-03-28 13:38:22 +08002017 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2018 {
2019 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002020 return -1;
2021 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002022
you.chen35020192022-05-06 11:30:57 +08002023 CHECK_IDX(idx, CTRL_AP);
2024
2025 CHECK_WPA_CTRL(CTRL_AP);
2026
2027 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2028
2029 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2030 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002031 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2032 return 0;
you.chen35020192022-05-06 11:30:57 +08002033
qs.xiong7a105ce2022-03-02 09:43:11 -05002034}
2035
you.chen35020192022-05-06 11:30:57 +08002036int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002037{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002038 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002039 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002040 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002041}
2042
qs.xiongc9c79f72022-10-17 15:27:18 +08002043/*****
2044 *frequency <------>channel
2045 *
2046 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2047 *
2048 *
2049 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2050 *
2051 *
2052 * */
2053static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002054 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};
2055 int i;
2056 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2057
qs.xiong69a332b2022-12-02 09:58:57 +08002058 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002059 {
2060 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002061 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002062 }
qs.xiongc00b6032022-11-29 16:28:03 +08002063
2064 if(i == arr_len)
2065 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002066 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002067 return -1;
2068 }
qs.xiongc00b6032022-11-29 16:28:03 +08002069
qs.xiongc9c79f72022-10-17 15:27:18 +08002070 return 0;
2071}
qs.xiong13673462023-02-21 19:12:54 +08002072
2073static int lynq_check_frequencyby_country_code(int input_frequency)
2074{
2075 char str_cnc[]="CN";
2076 char str_dest[20]="";
2077
2078 if( lynq_get_country_code(1,str_dest) != 0 )
2079 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002080 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002081 return -1;
2082 }
2083 if( strncmp(str_dest,str_cnc,2) != 0 )
2084 {
2085 return 0;
2086 }else if( 2473 < input_frequency && input_frequency < 5744)
2087 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002088 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002089 return -1;
2090 }
2091 return 0;
2092}
qs.xiongf1b525b2022-03-31 00:58:23 -04002093int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002094{
qs.xiongc00b6032022-11-29 16:28:03 +08002095 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002096 char lynq_wifi_frequency_cmd[128]={0};
2097 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002098 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002099 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002100 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002101 check = lynq_check_set_frequency(lynq_wifi_frequency);
2102 if(check != 0)
2103 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002104 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002105 return -1;
you.chen35020192022-05-06 11:30:57 +08002106 }
qs.xiong13673462023-02-21 19:12:54 +08002107 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2108 if(check != 0)
2109 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002111 return -1;
2112 }
2113
qs.xiongc00b6032022-11-29 16:28:03 +08002114 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2115 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002116 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002117 return -1;
2118 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002119
you.chen35020192022-05-06 11:30:57 +08002120 CHECK_IDX(idx, CTRL_AP);
2121
2122 CHECK_WPA_CTRL(CTRL_AP);
2123
2124 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2125 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2126 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2127
you.chen6c2dd9c2022-05-16 17:55:28 +08002128 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002129 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2130 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2131 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002132
qs.xiong9fbf74e2023-03-28 13:38:22 +08002133 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002134}
2135
qs.xiongf1b525b2022-03-31 00:58:23 -04002136int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002137{
you.chen35020192022-05-06 11:30:57 +08002138 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002139 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002140 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002141
qs.xiong9fbf74e2023-03-28 13:38:22 +08002142 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2143 {
2144 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002145 return -1;
2146 }
2147 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002148
qs.xiong9fbf74e2023-03-28 13:38:22 +08002149 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002150}
2151
qs.xiongf1b525b2022-03-31 00:58:23 -04002152int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2153{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002154 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002155 CHECK_IDX(idx, CTRL_AP);
2156 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 case LYNQ_WIFI_BANDWIDTH_HT10:
2158 {
2159 RLOGE("bandwith [%d] not support now\n", bandwidth);
2160 return -1;
2161 }
2162 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002163 {
2164 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2165 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002166 if (system(lynq_cmd_bandwith) != 0 )
2167 {
2168 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002169 return -1;
2170 }
2171 system("wl up");
2172 break;
2173 }
2174 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002175 {
qs.xiong10379192023-02-21 13:19:42 +08002176 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002177 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2178 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002179 if (system(lynq_cmd_bandwith) != 0 )
2180 {
2181 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002182 return -1;
2183 }
2184 system("wl up");
2185 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002186 }
you.chen35020192022-05-06 11:30:57 +08002187 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002188 {
qs.xiong10379192023-02-21 13:19:42 +08002189 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002190 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002191 if (system(lynq_cmd_bandwith) != 0 )
2192 {
2193 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002194 return -1;
2195 }
2196 system("wl up");
2197 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002198 }
2199 default:
you.chen35020192022-05-06 11:30:57 +08002200 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002201 RLOGE("auth type [%d] not support now\n", bandwidth);
2202 return -1;
you.chen35020192022-05-06 11:30:57 +08002203 }
2204 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002205
2206
you.chen35020192022-05-06 11:30:57 +08002207 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002208}
you.chen35020192022-05-06 11:30:57 +08002209
qs.xiongf1b525b2022-03-31 00:58:23 -04002210int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2211{
you.chen35020192022-05-06 11:30:57 +08002212 int count = 0;
2213 int index = 0;
2214 char *split_words[128] = {0};
2215 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002216 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002217 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002218
you.chen35020192022-05-06 11:30:57 +08002219 CHECK_WPA_CTRL(CTRL_AP);
2220
2221 DO_REQUEST(lynq_chanspec_cmd);
2222
2223 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2224 for(;index < count; index++) {
2225 if (strncmp(split_words[index], "bw", 2) != 0) {
2226 continue;
2227 }
2228
2229 index++;
2230 if (index >= count) {
2231 return -1;
2232 }
2233
qs.xiong9fbf74e2023-03-28 13:38:22 +08002234 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002235 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2236 return 0;
2237 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002238 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002239 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002240}
qs.xiong0fb469a2022-04-14 03:50:45 -04002241
qs.xiongf1b525b2022-03-31 00:58:23 -04002242int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002243{
you.chen35020192022-05-06 11:30:57 +08002244 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002245 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002246 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002247
you.chen35020192022-05-06 11:30:57 +08002248 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002249
qs.xiong9fbf74e2023-03-28 13:38:22 +08002250 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2251 {
you.chen35020192022-05-06 11:30:57 +08002252 return -1;
2253 }
2254
2255 system("wl down");
2256 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002257 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002258 return -1;
2259 }
2260 system("wl up");
2261 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002262}
qs.xiong0fb469a2022-04-14 03:50:45 -04002263
qs.xiongf1b525b2022-03-31 00:58:23 -04002264int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002265{
you.chen35020192022-05-06 11:30:57 +08002266 int count = 0;
2267 int index = 0;
2268 char *split_words[128] = {0};
2269 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002270 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002271 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002272
you.chen35020192022-05-06 11:30:57 +08002273 CHECK_WPA_CTRL(CTRL_AP);
2274
2275 DO_REQUEST(lynq_chanspec_cmd);
2276
2277 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002278 for(;index < count; index++)
2279 {
2280 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002281 if (strncmp(split_words[index], "channel", 2) != 0) {
2282 continue;
2283 }
2284
2285 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002286 if (index >= count)
2287 {
you.chen35020192022-05-06 11:30:57 +08002288 return -1;
2289 }
2290
2291 *channel = atoi(split_words[index]);
2292 return 0;
2293 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002294 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002295 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002296}
2297
2298
you.chen35020192022-05-06 11:30:57 +08002299int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002300{
you.chen6c2dd9c2022-05-16 17:55:28 +08002301 char ssid[MAX_CMD] = {0};
2302 int freq = 0;
2303 char lynq_auth_cmd[64]={0};
2304 char lynq_auth_alg_cmd[64]={0};
2305 char lynq_psk_cmd[64]={0};
2306 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002307 char lynq_ieee80211_cmd[64]={0};
2308 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002309 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002310 CHECK_IDX(idx, CTRL_AP);
2311
you.chen6c2dd9c2022-05-16 17:55:28 +08002312 CHECK_WPA_CTRL(CTRL_AP);
2313
qs.xiong9fbf74e2023-03-28 13:38:22 +08002314 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2315 {
2316 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002317 return -1;
2318 }
2319
you.chen92fd5d32022-05-25 10:09:47 +08002320 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002321 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002322 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002323 return 0;
2324 }
2325 else {
2326 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2327 ssid[0] = '\0';
2328 }
2329 lynq_wifi_ap_frequency_get(idx, &freq);
2330
2331 DO_OK_FAIL_REQUEST(cmd_disconnect);
2332 DO_OK_FAIL_REQUEST(cmd_remove_all);
2333 if (ssid[0] != '\0') {
2334 lynq_wifi_ap_ssid_set(idx, ssid);
2335 }
2336 if (freq != 0) {
2337 lynq_wifi_ap_frequency_set(idx, freq);
2338 }
2339 }
2340 }
you.chen35020192022-05-06 11:30:57 +08002341
qs.xiong9fbf74e2023-03-28 13:38:22 +08002342 switch(auth){
2343 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002344 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002345 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002346 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002347 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002348 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002349 break;
2350 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002351 case LYNQ_WIFI_AUTH_WEP:
2352 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002353 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002354 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002355 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002356 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2357
2358 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2359 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2360 break;
2361 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002362 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002363 {
2364 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2365 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2366 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2367
2368 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2369 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2370 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2371 break;
2372
2373 }
you.chen35020192022-05-06 11:30:57 +08002374 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002375 {
2376 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2377 {
you.chen35020192022-05-06 11:30:57 +08002378 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2379 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2380 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002381 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2382 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002383 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002384 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002385 }
2386// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2387// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2388 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002389
you.chen35020192022-05-06 11:30:57 +08002390 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2391 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2392 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002393 break;
2394 }
2395 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2396 {
2397 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2398 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2399 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2400 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2401
2402 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2403 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2404 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2405 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2406 break;
2407 }
2408 case LYNQ_WIFI_AUTH_WPA3_PSK:
2409 {
2410 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2411 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002412 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002413 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2414
2415 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2416 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2417 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2418 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2419 break;
2420 }
2421 default:
you.chen35020192022-05-06 11:30:57 +08002422 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002423 RLOGE("auth type [%d] not support now\n", auth);
2424 return -1;
you.chen35020192022-05-06 11:30:57 +08002425 }
2426 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002427 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002428
qs.xiong9fbf74e2023-03-28 13:38:22 +08002429 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002430}
2431
you.chen35020192022-05-06 11:30:57 +08002432int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002433{
you.chen35020192022-05-06 11:30:57 +08002434 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002435 char lynq_auth_alg_str[MAX_RET] = {0};
2436 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002437 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002438 CHECK_IDX(idx, CTRL_AP);
2439
qs.xiong9fbf74e2023-03-28 13:38:22 +08002440 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2441 {
2442 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002443 return -1;
2444 }
2445
qs.xiong9fbf74e2023-03-28 13:38:22 +08002446 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2447 {
2448 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2449 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002450 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002451 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002452 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002453 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002454 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2455 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002456 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002457 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002458 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002459 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002460 else
2461 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002462 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002463 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002464 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002465 }
you.chen35020192022-05-06 11:30:57 +08002466 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002467 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2468 {
2469 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2470 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002471 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002472 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002473 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002474 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2475 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002476 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002477 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002478 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002479 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002480 else
2481 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002482 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002483 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002484 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002485 }
you.chen35020192022-05-06 11:30:57 +08002486 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002487
2488 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2489 {
2490 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2491 return -1;
2492 }
2493
2494 if (memcmp(lynq_auth_str,"1",1) == 0 )
2495 {
2496 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2497 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002498 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002499 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2500 {
2501 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2502 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002503 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002504 }
2505 else
2506 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002507 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002508 *auth = -1;
2509 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002510
you.chen6c2dd9c2022-05-16 17:55:28 +08002511 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002512}
qs.xiong1af5daf2022-03-14 09:12:12 -04002513
you.chenb95401e2023-05-12 19:39:06 +08002514static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2515{
2516 char status[64];
you.chencba13492023-05-19 13:53:43 +08002517 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002518 curr_status_info curr_state;
2519
2520 CHECK_WPA_CTRL(CTRL_AP);
2521
2522 memset(status, 0, sizeof (status));
2523
2524 curr_state.ap = NULL;
2525 curr_state.state = status;
2526
2527 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002528 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002529 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002530 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002531 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002532 {
2533 return 0;
2534 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002535 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002536 {
2537 DO_REQUEST("RECONNECT");
2538 return inner_check_ap_connected(idx, retry_count+1);
2539 }
you.chencba13492023-05-19 13:53:43 +08002540 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002541 {
2542 printf("retry 10 time\n");
2543 return -1;
2544 }
2545 else
2546 {
you.chen6d247052023-06-01 16:39:54 +08002547 if (strcmp(status, STATE_DISCONNECTED) == 0)
2548 {
2549 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2550 DO_REQUEST(LYNQ_WIFI_CMD);
2551 }
you.chenb95401e2023-05-12 19:39:06 +08002552 return inner_check_ap_connected(idx, retry_count+1);
2553 }
2554 }
2555 return -1;
2556}
qs.xiong1af5daf2022-03-14 09:12:12 -04002557
qs.xiongf1b525b2022-03-31 00:58:23 -04002558int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002559{
qs.xiong5a2ba932023-09-13 16:30:21 +08002560 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002561 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002562 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2563 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2564 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002565 CHECK_IDX(idx, CTRL_AP);
2566
2567 CHECK_WPA_CTRL(CTRL_AP);
2568
you.chen0df3e7e2023-05-10 15:56:26 +08002569 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002570 {
you.chen0df3e7e2023-05-10 15:56:26 +08002571 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002572 return -1;
2573 }
you.chen35020192022-05-06 11:30:57 +08002574
qs.xiongb37f8c42023-09-13 21:21:58 +08002575 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2576 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2577
you.chen35020192022-05-06 11:30:57 +08002578 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2579 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2580
you.chenc9928582023-04-24 15:39:37 +08002581 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2582 if (ret != 0)
2583 {
2584 RLOGE("lynq_wifi_ap_start excute script fail");
2585 return -1;
2586 }
2587
you.chenb95401e2023-05-12 19:39:06 +08002588 if (inner_check_ap_connected(idx, 0) != 0)
2589 {
2590 return -1;
2591 }
2592
you.chen0df3e7e2023-05-10 15:56:26 +08002593 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002594 if (g_ap_tmp_watcher_pid == 0)
2595 {
2596 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2597 {
2598 g_ap_tmp_watcher_pid = 0;
2599 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2600 return -1;
2601 }
2602 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2603 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002604 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002605
qs.xiong9fbf74e2023-03-28 13:38:22 +08002606 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002607}
2608
qs.xiongf1b525b2022-03-31 00:58:23 -04002609int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002610{
you.chen35020192022-05-06 11:30:57 +08002611 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002612}
2613
qs.xiongf1b525b2022-03-31 00:58:23 -04002614int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002615{
you.chen35020192022-05-06 11:30:57 +08002616 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002617
you.chen35020192022-05-06 11:30:57 +08002618 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002619
you.chen35020192022-05-06 11:30:57 +08002620 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002621
you.chen35020192022-05-06 11:30:57 +08002622 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2623
2624 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2625
you.chenb4b121c2022-05-06 17:50:16 +08002626// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002627
2628 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2629 if (ret != 0)
2630 {
2631 RLOGE("lynq_wifi_ap_start excute script fail");
2632 return -1;
2633 }
qs.xiong44fac672023-08-29 16:15:55 +08002634 g_ap_tmp_watcher_stop_flag = 1;
2635 if (g_ap_tmp_watcher_pid != 0)
2636 pthread_join(g_ap_tmp_watcher_pid, NULL);
2637 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002638
qs.xiong9fbf74e2023-03-28 13:38:22 +08002639 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002640}
qs.xiong1af5daf2022-03-14 09:12:12 -04002641
qs.xiongf1b525b2022-03-31 00:58:23 -04002642int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002643{
you.chen35020192022-05-06 11:30:57 +08002644 char lynq_disable_cmd[128] = {0};
2645 char lynq_select_cmd[128] = {0};
2646 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002648 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002649
you.chen35020192022-05-06 11:30:57 +08002650 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002651 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2652 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2653
2654 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2655 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2656 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002657
qs.xiong9fbf74e2023-03-28 13:38:22 +08002658 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002659}
2660
qs.xiongf1b525b2022-03-31 00:58:23 -04002661int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002662{
you.chen35020192022-05-06 11:30:57 +08002663 char lynq_disable_cmd[128] = {0};
2664 char lynq_select_cmd[128] = {0};
2665 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002666 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002667 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002668
you.chen35020192022-05-06 11:30:57 +08002669 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002670
you.chen35020192022-05-06 11:30:57 +08002671 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2672 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2673
2674 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2675 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2676 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002677
qs.xiong9fbf74e2023-03-28 13:38:22 +08002678 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002679}
qs.xiongf1b525b2022-03-31 00:58:23 -04002680
you.chen35020192022-05-06 11:30:57 +08002681int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002682{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002683 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002684 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002685 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002686 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002687 RLOGD("enter lynq_ap_password_set");
2688 if( password == NULL )
2689 {
2690 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002691 return -1;
2692 }
2693 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002694 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002695 if(pass_len < 8 || pass_len >= 64)
2696 {
2697 RLOGE("[lynq_ap_password_set]input password len not in rage");
2698 return -1;
you.chen35020192022-05-06 11:30:57 +08002699 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002700
you.chen35020192022-05-06 11:30:57 +08002701 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002702
qs.xiong9fbf74e2023-03-28 13:38:22 +08002703 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2704 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002705 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002706 return -1;
2707 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002708 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2709 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002710 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2711 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002712 }
2713
you.chen35020192022-05-06 11:30:57 +08002714 CHECK_WPA_CTRL(CTRL_AP);
2715
qs.xiong9fbf74e2023-03-28 13:38:22 +08002716 if (auth == LYNQ_WIFI_AUTH_WEP)
2717 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002718 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002719 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2720 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2721 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2722 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2723 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002724 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2725 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002726 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 +08002727 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2728 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2729 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002730 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2731 {
2732
qs.xiongc8d92a62023-03-29 17:36:14 +08002733 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 +08002734 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002735 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002736 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2737 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2738
2739 }
2740 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002741 {
2742 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002743 return -1;
2744 }
you.chen35020192022-05-06 11:30:57 +08002745
you.chen35020192022-05-06 11:30:57 +08002746 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002747
qs.xiong9fbf74e2023-03-28 13:38:22 +08002748 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002749}
2750
you.chen35020192022-05-06 11:30:57 +08002751int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002752{
you.chen35020192022-05-06 11:30:57 +08002753 FILE * fp;
2754 int len, ret;
2755 int count, index;
2756 char *split_lines[128] = {0};
2757 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002758 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002759
you.chen35020192022-05-06 11:30:57 +08002760 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002761
you.chen35020192022-05-06 11:30:57 +08002762 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2763// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002764 if (NULL == fp)
2765 {
2766 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002767 return -1;
2768 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002769
you.chen35020192022-05-06 11:30:57 +08002770 buff = alloca(MAX_RET);
2771 fseek(fp, 0, SEEK_SET);
2772 len = fread(buff, 1, MAX_RET, fp);
2773 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002774
qs.xiong9fbf74e2023-03-28 13:38:22 +08002775 for(index=0; index < len; index ++)
2776 {
2777 if (memcmp(buff + index, "network={", 9) != 0)
2778 {
you.chen35020192022-05-06 11:30:57 +08002779 continue;
2780 }
2781 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002782 for (; index < len; index ++ )
2783 {
2784 if (buff[index] != '}')
2785 {
you.chen35020192022-05-06 11:30:57 +08002786 continue;
2787 }
2788 buff[index] = '\0';
2789 break;
2790 }
2791 len = buff + index - p;
2792 }
2793
2794 count = lynq_split(p, len, '\n', split_lines);
2795
2796 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002797 for(index=0; index < count; index++)
2798 {
you.chen35020192022-05-06 11:30:57 +08002799 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002800 if (p != NULL)
2801 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002802 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002803 if (*p == '\"')
2804 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002805 p++;
2806 }
you.chen35020192022-05-06 11:30:57 +08002807 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002808 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2809 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002810 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002811 if (*p == '\"')
2812 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002813 p++;
2814 }
2815 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002816 else
2817 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002818 continue;
you.chen35020192022-05-06 11:30:57 +08002819 }
2820
2821 strcpy(password, p);
2822
qs.xiong9fbf74e2023-03-28 13:38:22 +08002823 while(*password != '\0')
2824 {
2825 if (*password == '\"')
2826 {
you.chen35020192022-05-06 11:30:57 +08002827 *password = '\0';
2828 break;
2829 }
2830 password++;
2831 }
2832 ret = 0;
2833 break;
2834 } //end for(index=0; index < count; index++)
2835
2836 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002837}
2838
you.chen35020192022-05-06 11:30:57 +08002839static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2840 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002841 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002842
qs.xiong9fbf74e2023-03-28 13:38:22 +08002843 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2844 {
you.chen35020192022-05-06 11:30:57 +08002845 return -1;
2846 }
2847
2848 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002849
qs.xiong9fbf74e2023-03-28 13:38:22 +08002850 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2851 {
2852 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002853 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002854 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002855 {
you.chena6cd55a2022-05-08 12:20:18 +08002856 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002857 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002858 }
you.chen70f377f2023-04-14 18:17:09 +08002859 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2860 {
2861 return 0;
2862 }
you.chena6cd55a2022-05-08 12:20:18 +08002863 }
2864 }
you.chen70f377f2023-04-14 18:17:09 +08002865 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2866 {
2867 return 0;
2868 }
2869
qs.xiong9fbf74e2023-03-28 13:38:22 +08002870 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2871 {
you.chen70f377f2023-04-14 18:17:09 +08002872 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002873 return -1;
2874 }
2875 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2876 {
2877
you.chen70f377f2023-04-14 18:17:09 +08002878 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2879 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002880 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2881 {
2882
2883 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002884 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002885 }else
2886 {
you.chen70f377f2023-04-14 18:17:09 +08002887 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 *auth = -1;
2889 return -1;
2890 }
you.chen35020192022-05-06 11:30:57 +08002891 return 0;
2892}
2893
2894int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002895{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002896 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002897 int pass_len, net_no, count, index;
2898 char lynq_tmp_cmd[300]={0};
2899 int net_no_list[128];
2900 lynq_wifi_auth_s net_auth;
2901 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002902 if(pass_len < 8 || pass_len >= 64)
2903 {
2904 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002905 return -1;
2906 }
2907
2908 CHECK_IDX(idx, CTRL_STA);
2909
2910 net_no = -1;
2911 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2912
qs.xiong9fbf74e2023-03-28 13:38:22 +08002913 for (index=0; index < count; index++)
2914 {
you.chen35020192022-05-06 11:30:57 +08002915 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002916 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2917 {
you.chen35020192022-05-06 11:30:57 +08002918 net_no = net_no_list[index];
2919 break;
2920 }
2921 }
2922
qs.xiong9fbf74e2023-03-28 13:38:22 +08002923 if (net_no < 0)
2924 {
you.chen35020192022-05-06 11:30:57 +08002925 return -1;
2926 }
2927
2928 CHECK_WPA_CTRL(CTRL_STA);
2929
2930 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2931
2932 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2933 DO_OK_FAIL_REQUEST(cmd_save_config);
2934
2935 return 0;
2936}
2937
2938int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2939
2940 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002941 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002942 int count, index;
2943 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002944 char *buff, *p, *ssid, *ssid_end_flag;
2945 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002946 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002947
you.chen755332b2022-08-06 16:59:10 +08002948 network_len = 0;
2949 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002950 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002951
you.chen35020192022-05-06 11:30:57 +08002952 CHECK_IDX(idx, CTRL_STA);
2953
qs.xiong9fbf74e2023-03-28 13:38:22 +08002954 if (NULL == password)
2955 {
2956 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002957 return -1;
2958 }
2959
you.chen35020192022-05-06 11:30:57 +08002960 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002961 if (NULL == fp)
2962 {
2963 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002964 return -1;
2965 }
2966
you.chen6d247052023-06-01 16:39:54 +08002967 fseek(fp, 0, SEEK_END);
2968 len = ftell(fp);
2969 buff = malloc(len + 1);
2970
2971 if (buff == NULL)
2972 {
2973 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2974 return -1;
2975 }
2976
you.chen35020192022-05-06 11:30:57 +08002977 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002978 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002979 fclose(fp);
2980
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981 for(index=0; index < len; index ++)
2982 {
2983 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002984 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002985 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002986 {
you.chen35020192022-05-06 11:30:57 +08002987 continue;
2988 }
you.chen6d247052023-06-01 16:39:54 +08002989 p = buff + index + 9;
2990 for (; index < len; index ++ )
2991 {
2992 if (buff[index] != '}')
2993 {
2994 continue;
2995 }
2996 buff[index] = '\0';
2997 break;
2998 }
2999 network_len = buff + index - p;
3000 break;
you.chen35020192022-05-06 11:30:57 +08003001 }
3002
qs.xiongb3f26af2023-02-17 18:41:07 +08003003 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08003004 {
3005 if (buff != NULL)
3006 {
3007 free(buff);
3008 }
3009
qs.xiongb3f26af2023-02-17 18:41:07 +08003010 return -1;
you.chen6d247052023-06-01 16:39:54 +08003011 }
qs.xiongb3f26af2023-02-17 18:41:07 +08003012
you.chend2fef3f2023-02-13 10:50:35 +08003013 ssid = strstr(p, "ssid=");
3014 if (ssid != NULL) {
3015 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003016 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08003017 {
you.chend2fef3f2023-02-13 10:50:35 +08003018 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3019 break;
3020 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003021 else
you.chen6d247052023-06-01 16:39:54 +08003022 {
you.chend2fef3f2023-02-13 10:50:35 +08003023 ssid_end_flag = strstr(ssid, "\n");
3024 if (ssid_end_flag != NULL)
3025 {
3026 ssid_len = (ssid_end_flag - ssid) / 2;
3027 for(i=0; i<ssid_len; i++)
3028 {
3029 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3030 }
3031 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3032 break;
3033 }
3034 }
you.chen35020192022-05-06 11:30:57 +08003035 }
you.chend2fef3f2023-02-13 10:50:35 +08003036
you.chen35020192022-05-06 11:30:57 +08003037 }
3038
qs.xiong9fbf74e2023-03-28 13:38:22 +08003039 if (index >= len || NULL == p || network_len <= 0)
3040 {
you.chen6d247052023-06-01 16:39:54 +08003041 if (buff != NULL)
3042 {
3043 free(buff);
3044 }
you.chen35020192022-05-06 11:30:57 +08003045 return -1;
3046 }
3047
you.chen755332b2022-08-06 16:59:10 +08003048 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08003049
3050 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003051 for(index=0; index < count; index++)
3052 {
you.chen35020192022-05-06 11:30:57 +08003053 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003054 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08003055 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003056 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003057 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08003058 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003059 p++;
3060 }
you.chen35020192022-05-06 11:30:57 +08003061 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003062 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08003063 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003064 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003065 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08003066 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003067 p++;
3068 }
3069 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003070 else
you.chen6d247052023-06-01 16:39:54 +08003071 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003072 continue;
you.chen35020192022-05-06 11:30:57 +08003073 }
3074
qs.xiong13673462023-02-21 19:12:54 +08003075 if (*p == '\"')
3076 p++;
3077 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08003078
qs.xiong13673462023-02-21 19:12:54 +08003079 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003080 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08003081 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003082 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08003083 {
you.chen35020192022-05-06 11:30:57 +08003084 *password = '\0';
3085 break;
3086 }
3087 password++;
3088 }
3089 ret = 0;
3090 break;
3091 } //end for(index=0; index < count; index++)
3092
you.chen6d247052023-06-01 16:39:54 +08003093 if (buff != NULL)
3094 {
3095 free(buff);
3096 }
3097
you.chen35020192022-05-06 11:30:57 +08003098 return ret;
3099}
3100
3101static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3102{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003103 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003104
qs.xiong9fbf74e2023-03-28 13:38:22 +08003105 if (sta_ssid == NULL)
3106 {
3107 RLOGE("sta_ssid is null\n");
3108 return -1;
you.chen35020192022-05-06 11:30:57 +08003109 }
3110
qs.xiong9fbf74e2023-03-28 13:38:22 +08003111 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003112
3113 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3114
3115 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3116// DO_OK_FAIL_REQUEST(cmd_save_config);
3117
qs.xiong9fbf74e2023-03-28 13:38:22 +08003118 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003119
3120}
3121
you.chen35020192022-05-06 11:30:57 +08003122static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003123{
you.chen35020192022-05-06 11:30:57 +08003124 char lynq_disable_cmd[128]={0};
3125 char lynq_select_cmd[128]={0};
3126
3127 CHECK_WPA_CTRL(CTRL_STA);
3128
qs.xiong9fbf74e2023-03-28 13:38:22 +08003129 if (save != 0)
3130 {
you.chenc29444e2022-06-07 18:01:16 +08003131 if (start_flag != 0)
3132 {
3133 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3134 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3135 }
3136 else
3137 {
3138 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3139 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3140 }
you.chen35020192022-05-06 11:30:57 +08003141 DO_OK_FAIL_REQUEST(cmd_save_config);
3142 }
3143
qs.xiong9fbf74e2023-03-28 13:38:22 +08003144 if (start_flag == 0)
3145 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003146 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003147 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3148 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003149 else
3150 {
you.chen35020192022-05-06 11:30:57 +08003151 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3152 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3153 }
3154
3155 return 0;
3156}
3157
3158int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3159{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003160 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003161 CHECK_IDX(idx, CTRL_STA);
3162
you.chen6c2dd9c2022-05-16 17:55:28 +08003163 curr_status_info curr_state;
3164 ap_info_s ap_info;
3165 curr_state.ap = &ap_info;
3166 curr_state.state = NULL;
3167
qs.xiong9fbf74e2023-03-28 13:38:22 +08003168 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3169 {
you.chend2fef3f2023-02-13 10:50:35 +08003170 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003171 return 0;
3172 }
3173
3174 return -1;
you.chen35020192022-05-06 11:30:57 +08003175}
3176
3177int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3178{
qs.xiong5d716d22023-09-20 20:08:39 +08003179 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003180 scan_info_s *scan_list = NULL;
3181 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003182 int scan_len=0;
3183 int save_len=0;
3184 int best_index = -1;
3185 int best_scan_index = -1;
3186 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003187 int i, j, ret;
3188
3189 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003190
3191 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003192 if (info == NULL)
3193 {
you.chen35020192022-05-06 11:30:57 +08003194 return -1;
3195 }
3196
3197 curr_status_info curr_state;
3198 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003199 char status[64];
you.chen35020192022-05-06 11:30:57 +08003200
you.chen9ac66392022-08-06 17:01:16 +08003201 memset(&ap_info, 0, sizeof (ap_info));
3202 memset(status, 0, sizeof (status));
3203
3204 curr_state.ap = &ap_info;
3205 curr_state.state = status;
3206
qs.xiong9fbf74e2023-03-28 13:38:22 +08003207 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3208 {
you.chen35020192022-05-06 11:30:57 +08003209 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003210 if (strcmp(status, STATE_COMPLETED) == 0)
3211 {
3212 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003213 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003214 }
3215 else
3216 {
3217 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003218 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003219 }
you.chen593621d2023-04-27 17:52:44 +08003220 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003221 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003222 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003223 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003224 return 0;
3225 }
3226
you.chen9ac66392022-08-06 17:01:16 +08003227 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003228 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003229 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3230 {
you.chen9ac66392022-08-06 17:01:16 +08003231 if (NULL != scan_list)
3232 {
3233 free(scan_list);
3234 }
you.chen35020192022-05-06 11:30:57 +08003235 return -1;
3236 }
3237
qs.xiong9fbf74e2023-03-28 13:38:22 +08003238 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3239 {
you.chen9ac66392022-08-06 17:01:16 +08003240 if (NULL != scan_list)
3241 {
3242 free(scan_list);
3243 }
3244 if (NULL != save_list)
3245 {
3246 free(save_list);
3247 }
you.chen35020192022-05-06 11:30:57 +08003248 return -1;
3249 }
3250
qs.xiong9fbf74e2023-03-28 13:38:22 +08003251 for (i=0; i < save_len; i++)
3252 {
3253 for (j=0; j < scan_len; j++)
3254 {
you.chen35020192022-05-06 11:30:57 +08003255 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003256 && save_list[i].base_info.auth == scan_list[j].auth)
3257 {
3258 if (best_rssi == 0)
3259 {
you.chen9ac66392022-08-06 17:01:16 +08003260 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003261 best_rssi = scan_list[j].rssi;
3262 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003263 else if (best_rssi > scan_list[j].rssi)
3264 {
you.chen35020192022-05-06 11:30:57 +08003265 best_index = i;
3266 best_scan_index = j;
3267 best_rssi = scan_list[j].rssi;
3268 }
you.chend2fef3f2023-02-13 10:50:35 +08003269 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 +08003270 break;
3271 }
3272 }
3273 }
3274
qs.xiong9fbf74e2023-03-28 13:38:22 +08003275 if (best_index >= 0)
3276 {
you.chen35020192022-05-06 11:30:57 +08003277 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003278 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 +08003279 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003280 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003281 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003282 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003283 }
3284
you.chen9ac66392022-08-06 17:01:16 +08003285 if (NULL != scan_list)
3286 {
3287 free(scan_list);
3288 }
3289 if (NULL != save_list)
3290 {
3291 free(save_list);
3292 }
3293
qs.xiong5d716d22023-09-20 20:08:39 +08003294 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003295 return ret;
you.chen35020192022-05-06 11:30:57 +08003296}
3297
3298static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3299{
qs.xiongc8d92a62023-03-29 17:36:14 +08003300 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003301 char lynq_ket_mgmt_cmd[64]={0};
3302 char lynq_pairwise_cmd[64]={0};
3303 char lynq_psk_cmd[64]={0};
3304
3305 CHECK_WPA_CTRL(CTRL_STA);
3306
qs.xiong9fbf74e2023-03-28 13:38:22 +08003307 switch(auth)
3308 {
3309 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003310 {
3311 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003312
you.chen35020192022-05-06 11:30:57 +08003313 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003314// DO_OK_FAIL_REQUEST(cmd_save_config);
3315 break;
3316 }
3317 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003318 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003319 {
3320 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3321 {
you.chen35020192022-05-06 11:30:57 +08003322 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3323 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003324 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3325 {
you.chena6cd55a2022-05-08 12:20:18 +08003326 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003327 }
3328 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3329 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003330
you.chen35020192022-05-06 11:30:57 +08003331 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3332 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3333 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003334
qs.xiong9fbf74e2023-03-28 13:38:22 +08003335 if (password != NULL)
3336 {
you.chen35020192022-05-06 11:30:57 +08003337 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3338 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003339 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003340 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003341
you.chen35020192022-05-06 11:30:57 +08003342// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003343 break;
3344 }
3345 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3346 {
qs.xiong3e506812023-04-06 11:08:48 +08003347 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003348 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003349 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3350 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3351
qs.xiong3e506812023-04-06 11:08:48 +08003352 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003353 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3354 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3355 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3356
3357 break;
3358 }
3359 case LYNQ_WIFI_AUTH_WPA3_PSK:
3360 {
qs.xiong3e506812023-04-06 11:08:48 +08003361 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003362 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003363 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3364 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3365
qs.xiongb37f8c42023-09-13 21:21:58 +08003366 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003367 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3368 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3369 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3370
3371 break;
3372 }
3373 default:
3374 return -1;
you.chen35020192022-05-06 11:30:57 +08003375 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003376
qs.xiong9fbf74e2023-03-28 13:38:22 +08003377 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003378}
qs.xiong7a105ce2022-03-02 09:43:11 -05003379
you.chen35020192022-05-06 11:30:57 +08003380static int inner_get_curr_net_no(int interface) {
3381 curr_status_info curr_state;
3382 curr_state.ap = NULL;
3383 curr_state.state = NULL;
3384
qs.xiong9fbf74e2023-03-28 13:38:22 +08003385 if (0 != inner_get_status_info(interface, &curr_state))
3386 {
you.chen35020192022-05-06 11:30:57 +08003387 return -1;
3388 }
3389
3390 return curr_state.net_no;
3391}
3392
3393int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003394{
you.chen35020192022-05-06 11:30:57 +08003395 int net_no;
3396 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003397
you.chen35020192022-05-06 11:30:57 +08003398 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003399
qs.xiong9fbf74e2023-03-28 13:38:22 +08003400 if (net_no < 0)
3401 {
you.chen35020192022-05-06 11:30:57 +08003402 return -1;
3403 }
3404
3405 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003406}
3407
you.chenb95401e2023-05-12 19:39:06 +08003408int 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 -05003409{
you.chen35020192022-05-06 11:30:57 +08003410 int count, net_no, index;
3411 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003412 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003413 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003414 curr_status_info curr_state;
3415 ap_info_s ap_info;
3416 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003417
qs.xiong9fbf74e2023-03-28 13:38:22 +08003418 if (ssid == NULL || *ssid == '\0')
3419 {
3420 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003421 return -1;
3422 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003423
qs.xiong9fbf74e2023-03-28 13:38:22 +08003424 if (LYNQ_WIFI_AUTH_OPEN != auth)
3425 {
3426 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003427 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003428 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003429 return -1;
3430 }
3431 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003432
you.chen70f377f2023-04-14 18:17:09 +08003433
3434 pthread_mutex_lock(&s_global_check_mutex);
3435 if (s_sta_status != INNER_STA_STATUS_INIT)
3436 {
3437 s_sta_status = INNER_STA_STATUS_CANCEL;
3438 pthread_cond_signal(&s_global_check_cond);
3439 }
3440 pthread_mutex_unlock(&s_global_check_mutex);
3441
you.chen35020192022-05-06 11:30:57 +08003442 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003443 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003444
3445 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003446 memset(&ap_info, 0, sizeof (ap_info));
3447 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003448
you.chen70f377f2023-04-14 18:17:09 +08003449 curr_state.ap = &ap_info;
3450 curr_state.state = status;
3451
3452 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003453 {
you.chen70f377f2023-04-14 18:17:09 +08003454 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3455 {
3456 net_no = curr_state.net_no;
3457 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3458 && strcmp(ap_info.psw, psw) == 0)
3459 {
3460 RLOGD("already connected\n");
3461
3462 pthread_mutex_lock(&s_global_check_mutex);
3463 s_sta_status = INNER_STA_STATUS_CONNECTED;
3464 pthread_cond_signal(&s_global_check_cond);
3465 pthread_mutex_unlock(&s_global_check_mutex);
3466 return 0;
3467 }
you.chen35020192022-05-06 11:30:57 +08003468 }
3469 }
3470
you.chen70f377f2023-04-14 18:17:09 +08003471 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003472 {
you.chen70f377f2023-04-14 18:17:09 +08003473 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3474
3475 for (index=0; index < count; index++)
3476 {
3477 net_auth = -1;
3478 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3479 {
3480 net_no = net_no_list[index];
3481 break;
3482 }
you.chen35020192022-05-06 11:30:57 +08003483 }
3484
you.chen70f377f2023-04-14 18:17:09 +08003485 if (net_no < 0)
3486 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003487 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3488 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3489 {
3490 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3491 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3492 DO_OK_FAIL_REQUEST(rm_net_cmd);
3493 }
you.chen70f377f2023-04-14 18:17:09 +08003494 net_no = lynq_add_network(CTRL_STA);
3495 if (net_no == -1)
3496 {
3497 return -1;
3498 }
3499
3500 RLOGD("net no is %d\n", net_no);
3501 if (0 != inner_set_sta_ssid(net_no, ssid))
3502 {
3503 return -1;
3504 }
you.chen35020192022-05-06 11:30:57 +08003505 }
3506 }
3507
qs.xiong9fbf74e2023-03-28 13:38:22 +08003508 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3509 {
you.chen35020192022-05-06 11:30:57 +08003510 return -1;
3511 }
3512
you.chen70f377f2023-04-14 18:17:09 +08003513
3514 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003515 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003516 usleep(200*1000);
3517
3518 ret = inner_sta_start_stop(net_no, 1, 1);
3519
3520 pthread_mutex_lock(&s_global_check_mutex);
3521 s_sta_status = INNER_STA_STATUS_CONNECTING;
3522 strcpy(s_sta_current_connecting_ssid, ssid);
3523 struct timeval now;
3524 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003525 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003526 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3527 pthread_cond_signal(&s_global_check_cond);
3528 pthread_mutex_unlock(&s_global_check_mutex);
3529 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003530}
3531
you.chenb95401e2023-05-12 19:39:06 +08003532int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3533{
3534 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3535}
3536
you.chen35020192022-05-06 11:30:57 +08003537int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003538{
you.chen35020192022-05-06 11:30:57 +08003539 ap_info_s ap;
3540 curr_status_info curr_state;
3541 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003542
qs.xiong9fbf74e2023-03-28 13:38:22 +08003543 if (ssid == NULL || *ssid == '\0')
3544 {
3545 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003546 return -1;
3547 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003548
you.chen35020192022-05-06 11:30:57 +08003549 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003550
you.chen35020192022-05-06 11:30:57 +08003551 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003552 curr_state.state = NULL;
3553
qs.xiong9fbf74e2023-03-28 13:38:22 +08003554 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3555 {
you.chen35020192022-05-06 11:30:57 +08003556 return 0;
3557 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003558
qs.xiong9fbf74e2023-03-28 13:38:22 +08003559 if (strcmp(ap.ap_ssid, ssid) != 0)
3560 {
you.chen35020192022-05-06 11:30:57 +08003561 return 0;
3562 }
3563
you.chen70f377f2023-04-14 18:17:09 +08003564 pthread_mutex_lock(&s_global_check_mutex);
3565 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3566 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003567 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003568}
qs.xiong97fa59b2022-04-07 05:41:29 -04003569
qs.xiongc93bf2b2023-08-25 10:22:08 +08003570int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3571{
3572 ap_info_s ap;
3573 curr_status_info curr_state;
3574 ap.ap_ssid[0] = '\0';
3575
3576 if (ssid == NULL || *ssid == '\0')
3577 {
3578 RLOGE("input ssid is NULL\n");
3579 return -1;
3580 }
3581
3582 CHECK_IDX(idx, CTRL_STA);
3583
3584
3585 curr_state.ap = &ap;
3586 curr_state.state = NULL;
3587
3588 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3589 {
3590 return 0;
3591 }
3592
3593 if (strcmp(ap.ap_ssid, ssid) != 0)
3594 {
3595 return 0;
3596 }
3597
3598 pthread_mutex_lock(&s_global_check_mutex);
3599 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003600 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3601 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003602 pthread_mutex_unlock(&s_global_check_mutex);
3603 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3604
3605}
3606
3607
you.chena6cd55a2022-05-08 12:20:18 +08003608int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3609{
qs.xiongb37f8c42023-09-13 21:21:58 +08003610
qs.xiongad2f89d2023-01-18 13:17:41 +08003611 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3612 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003613
you.chen35020192022-05-06 11:30:57 +08003614 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003615 CHECK_WPA_CTRL(CTRL_STA);
3616
you.chenc9928582023-04-24 15:39:37 +08003617 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3618 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003619 {
you.chenc9928582023-04-24 15:39:37 +08003620 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003621 return -1;
3622 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003623
qs.xiongad2f89d2023-01-18 13:17:41 +08003624 system(lynq_enable_sta_cmd);
3625 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003626 pthread_mutex_lock(&s_global_check_mutex);
3627 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3628 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003629 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003630}
3631
you.chen6d247052023-06-01 16:39:54 +08003632static int inner_get_status_info_state (int interface, char *state) {
3633 curr_status_info curr_state;
3634 curr_state.ap = NULL;
3635 curr_state.state = state;
3636 return inner_get_status_info(interface, &curr_state);
3637}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003638
3639int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3640{
3641
qs.xiongb37f8c42023-09-13 21:21:58 +08003642 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3643 int tmp_open_idx[128];
3644 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003645
qs.xiongb37f8c42023-09-13 21:21:58 +08003646 pthread_mutex_lock(&s_global_check_mutex);
3647 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3648 pthread_mutex_unlock(&s_global_check_mutex);
3649 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003650 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003651 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003652 }
3653
qs.xiongb37f8c42023-09-13 21:21:58 +08003654 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003655 return 0;
3656}
3657
3658
you.chen35020192022-05-06 11:30:57 +08003659int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003660{
qs.xiongad2f89d2023-01-18 13:17:41 +08003661// char lynq_disable_network_cmd[MAX_CMD];
3662// curr_status_info curr_state;
3663// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003664 int i=0;
3665 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003666
you.chen6d247052023-06-01 16:39:54 +08003667// 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 +08003668 CHECK_IDX(idx, CTRL_STA);
3669 CHECK_WPA_CTRL(CTRL_STA);
3670
you.chen6d247052023-06-01 16:39:54 +08003671// system(lynq_disable_sta_cmd);
3672 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003673 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003674
3675 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3676 if (ret != 0)
3677 {
3678 RLOGE("lynq_wifi_ap_start excute script fail");
3679 return -1;
3680 }
3681
you.chen6d247052023-06-01 16:39:54 +08003682 for (i=0; i < 30; i++) // to check if sta is realy stoped
3683 {
3684 if (inner_get_status_info_state(idx, state) != 0)
3685 {
3686 break;
3687 }
3688
3689 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3690 {
3691 break;
3692 }
3693 RLOGD("lynq_wifi_ap_start curr state %s", state);
3694 usleep(SLEEP_TIME_ON_IDLE);
3695 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003696 pthread_mutex_lock(&s_global_check_mutex);
3697 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3698 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003699 return 0;
3700// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003701}
qs.xiongfcc914b2023-07-06 21:16:20 +08003702int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3703{
3704 char LYNQ_DISABLE_CMD[128]={0};
3705 CHECK_IDX(idx, CTRL_STA);
3706 CHECK_WPA_CTRL(CTRL_STA);
3707 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3708 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3709 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3710 return 0;
3711}
qs.xiong7a105ce2022-03-02 09:43:11 -05003712
you.chen35020192022-05-06 11:30:57 +08003713//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3714// int i, count;
3715// char *p;
3716// const char * FLAG_SSID = "ssid=";
3717// const char * FLAG_SBSID = "bssid=";
3718// const char * FLAG_KEY_MGMT = "key_mgmt=";
3719// const char * FLAG_FREQ = "freq=";
3720// char lynq_sta_cmd[MAX_CMD];
3721// char *split_lines[128] = {0};
3722
3723// CHECK_WPA_CTRL(CTRL_AP);
3724
3725// sprintf(lynq_sta_cmd, "STA %s", bssid);
3726
3727// DO_REQUEST(lynq_sta_cmd);
3728
3729// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3730
3731// for(i=0; i < count; i++) {
3732// p = strstr(split_lines[i], FLAG_SSID);
3733// if (p != NULL) {
3734// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3735// continue;
3736// }
3737// }
3738
3739// lynq_get_interface_ip(idx, ap->ap_ip);
3740// lynq_ap_password_set(idx, ap->psw);
3741
3742// return 0;
3743//}
3744
3745static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3746 curr_status_info curr_state;
3747 curr_state.ap = ap;
3748 curr_state.state = NULL;
3749 return inner_get_status_info(interface, &curr_state);
3750}
3751
3752int 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 -04003753{
qs.xiong5071c802023-09-06 14:04:15 +08003754 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003755 int index, line_count;
3756 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003757 const char *lynq_first_sta_cmd = "STA-FIRST";
3758 char lynq_next_sta_cmd[MAX_CMD];
3759 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003760 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003761
you.chen35020192022-05-06 11:30:57 +08003762 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003763
you.chen35020192022-05-06 11:30:57 +08003764 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003765
you.chenb95401e2023-05-12 19:39:06 +08003766 // ap_info_s * tmp_ap;
3767 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003768 if (ap == NULL || list == NULL || len == NULL)
3769 {
3770 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003771 return -1;
3772 }
3773
you.chenb95401e2023-05-12 19:39:06 +08003774 // ap = &tmp_ap;
3775 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003776 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003777 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003778
you.chenb95401e2023-05-12 19:39:06 +08003779 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003780 {
you.chenb95401e2023-05-12 19:39:06 +08003781 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003782 return -1;
3783 }
3784
3785 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3786 lynq_ap_password_get(idx, (*ap)->psw);
3787
you.chen35020192022-05-06 11:30:57 +08003788 DO_REQUEST(lynq_first_sta_cmd);
3789
3790 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003791 while (reply_len > 0)
3792 {
3793 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003794 {
you.chen35020192022-05-06 11:30:57 +08003795 break;
3796 }
3797 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3798 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3799 strcpy(bssid[index], split_lines[0]);
3800 index++;
3801 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3802 reply_len = MAX_RET;
3803 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003804 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 +08003805 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003806 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003807 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003808 break;
3809 }
3810 }
3811
3812 *len = index;
3813
3814 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003815 for (index=0; index < *len; index++)
3816 {
you.chend2fef3f2023-02-13 10:50:35 +08003817 dev_info = &(*list)[index];
3818 memset(dev_info, 0, sizeof(device_info_s));
3819 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3820 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3821 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3822 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003823 free(bssid[index]);
3824 }
qs.xiong5071c802023-09-06 14:04:15 +08003825 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003826 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003827}
3828
you.chen35020192022-05-06 11:30:57 +08003829int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003830{
you.chen35020192022-05-06 11:30:57 +08003831 int i, count, index, count_words;
3832 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3833 char *split_lines[128] = {0};
3834 char *split_words[128] = {0};
3835 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003836
qs.xiong9fbf74e2023-03-28 13:38:22 +08003837 if (list == NULL || len == NULL)
3838 {
you.chen35020192022-05-06 11:30:57 +08003839 return -1;
3840 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003841
you.chen9ac66392022-08-06 17:01:16 +08003842 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3843 {
3844 usleep(100 * 1000);
3845 }
3846
you.chen35020192022-05-06 11:30:57 +08003847 CHECK_IDX(idx, CTRL_STA);
3848
3849 CHECK_WPA_CTRL(CTRL_STA);
3850
3851 DO_REQUEST(lynq_scan_result_cmd);
3852
3853 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3854 *len = count - 1;
3855 *list = malloc(sizeof (scan_info_s) * *len);
3856
3857 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003858 for (index=0; index <count_words; index++)
3859 {
3860 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003861 }
3862
qs.xiong9fbf74e2023-03-28 13:38:22 +08003863 for(index = 1;index < count; index++)
3864 {
3865 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003866 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003867 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3868 if (count_words < 4)
3869 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003870 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003871 //bssid / frequency / signal level / flags / ssid
3872 p = (*list) + index - 1;
3873 strcpy(p->mac, split_words[0]);
3874 p->band = convert_band_from_freq(atoi(split_words[1]));
3875 p->rssi = -1 * atoi( split_words[2]);
3876 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003877 if (count_words == 4) // ssid hided
3878 {
3879 p->ssid[0] = '\0';
3880 }
3881 else
3882 {
3883 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3884 }
you.chen35020192022-05-06 11:30:57 +08003885 }
3886
3887 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003888}
qs.xiong97fa59b2022-04-07 05:41:29 -04003889
you.chen35020192022-05-06 11:30:57 +08003890int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3891{
3892 int count, net_no, index;
3893 int net_no_list[128];
3894 lynq_wifi_auth_s net_auth;
3895 char lynq_remove_cmd[MAX_CMD];
3896
qs.xiong9fbf74e2023-03-28 13:38:22 +08003897 if (ssid == NULL || *ssid == '\0')
3898 {
3899 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003900 return -1;
3901 }
3902
3903 CHECK_IDX(idx, CTRL_STA);
3904
3905 CHECK_WPA_CTRL(CTRL_STA);
3906
3907 net_no = -1;
3908 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3909
qs.xiong9fbf74e2023-03-28 13:38:22 +08003910 for (index=0; index < count; index++)
3911 {
you.chen35020192022-05-06 11:30:57 +08003912 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003913 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3914 {
you.chen35020192022-05-06 11:30:57 +08003915 net_no = net_no_list[index];
3916 break;
3917 }
3918 }
3919
qs.xiong9fbf74e2023-03-28 13:38:22 +08003920 if (net_no < 0)
3921 {
you.chen35020192022-05-06 11:30:57 +08003922 return 0;
3923 }
3924
3925 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3926
3927 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3928 DO_OK_FAIL_REQUEST(cmd_save_config);
3929
3930 return 0;
3931}
3932
3933int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003934{
you.chend2fef3f2023-02-13 10:50:35 +08003935 int count, index;
you.chen35020192022-05-06 11:30:57 +08003936 int net_no_list[128];
3937 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003938 RLOGD("enter lynq_get_sta_saved_ap api\n");
3939 if (list == NULL || len == NULL)
3940 {
3941 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003942 return -1;
3943 }
3944
3945 CHECK_IDX(idx, CTRL_STA);
3946
3947// CHECK_WPA_CTRL(CTRL_STA);
3948
3949 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003950 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003951
you.chen057aac42023-04-13 14:06:58 +08003952 if (count < 0)
3953 {
3954 RLOGE("list network fail");
3955 return count;
3956 }
3957 else if (count == 0)
3958 {
3959 *list = NULL;
3960 *len = 0;
3961 return 0;
3962 }
3963
you.chen35020192022-05-06 11:30:57 +08003964 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003965 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003966 *len = count;
3967
qs.xiong9fbf74e2023-03-28 13:38:22 +08003968 for (index=0; index < count; index++)
3969 {
you.chen35020192022-05-06 11:30:57 +08003970 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003971 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003972 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003973 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003974 {
you.chen35020192022-05-06 11:30:57 +08003975 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3976 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003977 else
you.chen057aac42023-04-13 14:06:58 +08003978 {
you.chen35020192022-05-06 11:30:57 +08003979 (*list)[index].base_info.band = -1;
3980 }
you.chen057aac42023-04-13 14:06:58 +08003981 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003982 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003983 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003984 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003985 return 0;
3986}
3987
3988int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3989{
qs.xiong20202422023-09-06 18:01:18 +08003990 if ( g_sta_conncet_status_flag != 0 )
3991 {
3992 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08003993 return 1;
qs.xiong20202422023-09-06 18:01:18 +08003994 }
qs.xiongc8d92a62023-03-29 17:36:14 +08003995 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003996 const char *lynq_scan_cmd = "SCAN";
3997
3998 CHECK_IDX(idx, CTRL_STA);
3999
4000 CHECK_WPA_CTRL(CTRL_STA);
4001
you.chen0df3e7e2023-05-10 15:56:26 +08004002 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4003 {
4004 RLOGD("tmp clear scanlist");
4005 system(clean_last_re);
4006 }
you.chen9ac66392022-08-06 17:01:16 +08004007 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004008 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004009 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4010 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004011 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004012 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4013 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004014 g_sta_scan_finish_flag = 1;
4015 return -1;
4016 }
you.chen35020192022-05-06 11:30:57 +08004017
4018 return 0;
4019}
4020
4021int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004022 if (cb == NULL)
4023 {
4024 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004025 return -1;
4026 }
4027
you.chen6d247052023-06-01 16:39:54 +08004028 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004029 g_ap_callback_priv = priv;
4030 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004031 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004032
you.chen6d247052023-06-01 16:39:54 +08004033 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004034 if (g_ap_watcher_pid == 0 )
4035 {
4036 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4037 {
4038 g_ap_watcher_pid = 0;
4039 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4040 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4041 return -1;
4042 }
4043 }
4044
4045 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4046 RLOGD("creat APWatcherTheradProc susccs");
4047
you.chen35020192022-05-06 11:30:57 +08004048 return 0;
4049}
4050
4051int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004052 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004053 if (g_ap_callback_priv == priv)
4054 {
you.chen35020192022-05-06 11:30:57 +08004055 g_ap_callback_func = NULL;
4056 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004057 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004058 return 0;
4059 }
you.chen6d247052023-06-01 16:39:54 +08004060 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004061 return -1;
4062}
4063
4064int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004065 if (cb == NULL)
4066 {
4067 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004068 return -1;
4069 }
4070
you.chen6d247052023-06-01 16:39:54 +08004071 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004072 g_sta_callback_priv = priv;
4073 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004074 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004075
you.chen6d247052023-06-01 16:39:54 +08004076 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004077 if (g_sta_watcher_pid == 0 ) {
4078 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4079 {
4080 g_sta_watcher_pid = 0;
4081 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4082 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4083 return -1;
4084 }
4085 }
4086
4087 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4088 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004089 return 0;
4090}
4091
4092int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004093 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004094 if (g_sta_callback_priv == priv)
4095 {
you.chen35020192022-05-06 11:30:57 +08004096 g_sta_callback_func = NULL;
4097 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004098 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004099 return 0;
4100 }
you.chen6d247052023-06-01 16:39:54 +08004101 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004102 return -1;
4103}
4104
qs.xiongfcc914b2023-07-06 21:16:20 +08004105int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4106 if (cb == NULL)
4107 {
4108 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4109 return -1;
4110 }
4111 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4112 g_sta_auto_callback_priv = priv;
4113 g_sta_auto_callback_func = cb;
4114 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4115 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4116 if (g_sta_auto_watcher_pid == 0 ) {
4117 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4118 {
4119 g_sta_auto_watcher_pid = 0;
4120 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4121 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4122 return -1;
4123 }
4124 }
4125 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4126 RLOGD("creat STAWatcherTheradProc susccs");
4127 return 0;
4128}
4129int lynq_unreg_sta_auto_event_callback(void * priv) {
4130 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4131 if (g_sta_auto_callback_priv == priv)
4132 {
4133 g_sta_auto_watcher_stop_flag = 1;
4134 if (g_sta_auto_watcher_pid != 0)
4135 {
4136 pthread_join(g_sta_auto_watcher_pid, NULL);
4137 }
4138 g_sta_auto_watcher_pid = 0;
4139 g_sta_auto_callback_func = NULL;
4140 g_sta_auto_callback_priv = NULL;
4141 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4142 return 0;
4143 }
4144 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4145 return -1;
4146}
you.chen35020192022-05-06 11:30:57 +08004147int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4148{
4149 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004150 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004151 CHECK_IDX(idx, CTRL_AP);
4152
qs.xiong9fbf74e2023-03-28 13:38:22 +08004153 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4154 {
you.chen35020192022-05-06 11:30:57 +08004155 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4156 return 0;
4157 }
4158
qs.xiong9fbf74e2023-03-28 13:38:22 +08004159 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4160 {
you.chen35020192022-05-06 11:30:57 +08004161 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4162 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004163 else
4164 {
you.chen35020192022-05-06 11:30:57 +08004165 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4166 }
4167
4168 return 0;
4169}
4170
4171int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4172 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004173 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004174 CHECK_IDX(idx, CTRL_STA);
4175
qs.xiong9fbf74e2023-03-28 13:38:22 +08004176 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4177 {
you.chen35020192022-05-06 11:30:57 +08004178 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4179 return 0;
4180 }
4181
qs.xiong9fbf74e2023-03-28 13:38:22 +08004182 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4183 {
you.chen35020192022-05-06 11:30:57 +08004184 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4185 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004186 else
4187 {
you.chen35020192022-05-06 11:30:57 +08004188 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4189 }
4190
4191 return 0;
4192}
4193
4194int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4195// CHECK_IDX(idx, CTRL_AP);
4196// int ret = 0;
4197// size_t reply_len = MAX_RET;
4198// char cmd_reply[MAX_RET]={0};
4199// const char * cmd_str = "GET country";
4200// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4201// do{
4202// if (NULL == s_lynq_wpa_ctrl) {
4203// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4204// if (NULL == s_lynq_wpa_ctrl ) {
4205// printf("wpa_ctrl_open fail\n");
4206// return -1;
4207// }
4208// }
4209// }while(0);
4210
4211// do {
4212// reply_len = MAX_RET;
4213// cmd_reply[0] = '\0';
4214// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004215// 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 +08004216// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004217// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004218// return ret;
4219// }
4220// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004221// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004222// }while(0);
4223
4224 FILE *fp;
4225 size_t i = 0;
4226 char lynq_cmd_ret[MAX_RET]={0};
4227
4228// CHECK_IDX(idx, CTRL_AP);
4229
4230 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004231 {
4232 perror("popen error!");
4233 return -1;
4234 }
you.chen35020192022-05-06 11:30:57 +08004235 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4236 {
4237 perror("fread fail!");
4238 return -1;
4239 }
4240
qs.xiong9fbf74e2023-03-28 13:38:22 +08004241 for(i=0; i < strlen(lynq_cmd_ret); i++)
4242 {
4243 if (lynq_cmd_ret[i] == ' ')
4244 {
you.chen35020192022-05-06 11:30:57 +08004245 lynq_cmd_ret[i] = '\0';
4246 break;
4247 }
4248 }
4249
4250 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004251 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004252
4253 int ret=pclose(fp);
4254 if(ret==-1)
4255 {
4256 perror("close file faild");
4257 }
4258
4259 return 0;
4260}
4261
qs.xiong44fac672023-08-29 16:15:55 +08004262
you.chen705a7ef2023-06-01 22:06:45 +08004263static int check_and_init_uci_config(char * country_code)
4264{
4265 FILE * fp;
4266 int is_different = 0;
4267 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4268 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4269 const char * commit_uci_cmd ="uci commit";
4270 char set_country_cmd[MAX_CMD];
4271 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004272
you.chen705a7ef2023-06-01 22:06:45 +08004273 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004274
you.chen705a7ef2023-06-01 22:06:45 +08004275 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004276 {
you.chen705a7ef2023-06-01 22:06:45 +08004277 if (0 != system(create_uci_cmd))
4278 {
4279 RLOGE("creat_uci_cmd fail");
4280 return -1;
4281 }
4282 is_different = 1;
4283 }
4284
4285 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4286 {
4287 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004288 return -1;
4289 }
4290
you.chen705a7ef2023-06-01 22:06:45 +08004291 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004292 {
you.chen705a7ef2023-06-01 22:06:45 +08004293 RLOGE("fread fail!");
4294 fclose(fp);
4295 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004296 }
4297
you.chen705a7ef2023-06-01 22:06:45 +08004298 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4299 {
qs.xiong44fac672023-08-29 16:15:55 +08004300 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 +08004301 is_different = 1;
4302 }
4303
4304 fclose(fp);
4305
4306 if (is_different)
4307 {
4308 if ( 0 != system(set_country_cmd))
4309 {
4310 RLOGE("set_country_cmd fail");
4311 return -1;
4312 }
4313 if (0 != system(commit_uci_cmd))
4314 {
4315 RLOGE("commmit fail");
4316 }
4317 }
4318
4319 return is_different;
4320}
4321
4322int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4323 char check_current_code[10];
4324 const char * support_country[] = {"CN", "EU"};
4325
4326 int ret,is_different, i, cc_count;
4327
4328 if (country_code == NULL || country_code[0] == '\0')
4329 {
4330 RLOGE("bad country code\n");
4331 return -1;
4332 }
4333
4334 cc_count = sizeof (support_country) / sizeof (char*);
4335 for(i=0; i < cc_count; i++)
4336 {
4337 if (strcmp(support_country[i], country_code) == 0)
4338 {
4339 break;
4340 }
4341 }
4342
4343 if (i >= cc_count)
4344 {
4345 RLOGE("unspported country code %s\n", country_code);
4346 return -1;
4347 }
4348
4349 is_different = check_and_init_uci_config(country_code);
4350 if( is_different < 0 )
4351 {
4352 RLOGE("init set uci fail\n");
4353 return -1;
4354 }
4355
4356 ret = lynq_get_country_code(idx,check_current_code);
4357 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4358 {
4359 ret = lynq_wifi_disable();
4360 if(ret != 0 )
4361 {
4362 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4363 return -1;
4364 }
4365 }
4366
4367 return 0;
you.chen35020192022-05-06 11:30:57 +08004368}
4369
4370int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4371{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004372 RLOGD("enter lynq_get_connect_ap_mac\n");
4373 if (mac == NULL)
4374 {
4375 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004376 return -1;
4377 }
4378
4379 CHECK_IDX(idx, CTRL_STA);
4380 ap_info_s ap;
4381 ap.ap_mac[0] = '\0';
4382
qs.xiong9fbf74e2023-03-28 13:38:22 +08004383 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4384 {
you.chen35020192022-05-06 11:30:57 +08004385 return -1;
4386 }
4387 strcpy(mac, ap.ap_mac);
4388
4389 return 0;
4390}
4391
4392int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4393{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004394 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004395 struct ifaddrs *ifaddr_header, *ifaddr;
4396 struct in_addr * ifa;
4397 const char * ifaName = "wlan0";
4398 if (ip == NULL)
4399 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004400 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004401 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004402 }
you.chenf58b3c92022-06-21 16:53:48 +08004403
qs.xiong9fbf74e2023-03-28 13:38:22 +08004404 if (idx == 1)
4405 {
you.chen0df3e7e2023-05-10 15:56:26 +08004406 ifaName = inner_get_ap_interface_name();
4407 if (ifaName == NULL)
4408 {
4409 RLOGE("[lynq_get_interface_ip] ap name get fail");
4410 return -1;
4411 }
you.chen9ac66392022-08-06 17:01:16 +08004412 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004413 else if (idx != 0)
4414 {
you.chen35020192022-05-06 11:30:57 +08004415 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004416 }
you.chen35020192022-05-06 11:30:57 +08004417
you.chen9ac66392022-08-06 17:01:16 +08004418 if (getifaddrs(&ifaddr_header) == -1)
4419 {
you.chen35020192022-05-06 11:30:57 +08004420 perror("getifaddrs");
4421 return -1;
4422 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004423 }
you.chen35020192022-05-06 11:30:57 +08004424
4425
you.chen9ac66392022-08-06 17:01:16 +08004426 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4427 {
4428 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004429 continue;
you.chen9ac66392022-08-06 17:01:16 +08004430 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4431 {
4432 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4433 {
4434 // is a valid IP4 Address
4435 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4436 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004437 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004438 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004439 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004440 return 0;
4441 }
4442 }
4443 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004444 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004445 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004446 return -1;
you.chen35020192022-05-06 11:30:57 +08004447}
4448
4449int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4450{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004451 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004452 int count;
4453 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004454 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004455 char *split_words[128] = {0};
4456 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4457
4458 CHECK_WPA_CTRL(idx);
4459
4460 DO_REQUEST(lynq_get_mac_cmd);
4461
qs.xiong9fbf74e2023-03-28 13:38:22 +08004462 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4463 {
4464 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004465 return -1;
4466 }
4467
4468 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4469
qs.xiong9fbf74e2023-03-28 13:38:22 +08004470 if (count < 2)
4471 {
you.chen35020192022-05-06 11:30:57 +08004472 return -1;
4473 }
4474
qs.xiong9fbf74e2023-03-28 13:38:22 +08004475 for (i=0; i < strlen(split_words[1]); i++ )
4476 {
4477 if (split_words[1][i] != ' ')
4478 {
you.chen35020192022-05-06 11:30:57 +08004479 break;
4480 }
4481 }
4482
qs.xiongdd6e44c2023-08-08 15:02:53 +08004483 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004484
4485 return 0;
4486}
4487
4488int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4489{
4490// int count;
4491// char *split_words[128] = {0};
4492// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4493
4494// if (rssi == NULL) {
4495// return -1;
4496// }
4497
4498// CHECK_IDX(idx, CTRL_STA);
4499
4500// CHECK_WPA_CTRL(CTRL_STA);
4501
4502// DO_REQUEST(lynq_get_rssi_cmd);
4503
4504// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4505// return -1;
4506// }
4507
4508// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4509
4510// if (count < 2) {
4511// return -1;
4512// }
4513
4514// *rssi = atoi(split_words[1]) * -1;
4515
you.chen35020192022-05-06 11:30:57 +08004516 char lynq_cmd_ret[MAX_RET]={0};
4517
qs.xiongff0ae0f2022-10-11 15:47:14 +08004518/*******change other cmd to get rssi*******
4519 *
4520 *wl rssi ---> wl -i wlan0 rssi
4521 *
4522 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004523 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004524 {
you.chen23c4a5f2023-04-12 16:46:00 +08004525 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004526 return -1;
4527 }
you.chen9f17e4d2022-06-06 17:18:18 +08004528 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004529/****** if got rssi is 0,means sta didn't connected any device****/
4530 if(*rssi == 0)
4531 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004532 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004533 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004534 }
you.chen35020192022-05-06 11:30:57 +08004535
4536 return 0;
4537}
4538
4539int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4540{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004541 RLOGD("enter lynq_get_connect_ap_band\n");
4542 if (band == NULL)
4543 {
you.chen35020192022-05-06 11:30:57 +08004544 return -1;
4545 }
4546
4547 CHECK_IDX(idx, CTRL_STA);
4548 ap_info_s ap;
4549 ap.band = -1;
4550
qs.xiong9fbf74e2023-03-28 13:38:22 +08004551 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4552 {
you.chen35020192022-05-06 11:30:57 +08004553 return -1;
4554 }
4555 *band = ap.band;
4556
4557 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004558}
you.chenf58b3c92022-06-21 16:53:48 +08004559
4560int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4561{
you.chenb95401e2023-05-12 19:39:06 +08004562 int ret;
4563 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004564 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004565
4566 if (ip == NULL)
4567 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004568 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004569 return -1;
4570 }
4571
4572 CHECK_IDX(idx, CTRL_STA);
4573
qs.xionge4cbf1c2023-02-28 18:22:49 +08004574 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004575 {
4576 return -1;
4577 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004578
you.chenb95401e2023-05-12 19:39:06 +08004579 ip[0] = '\0';
4580 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4581 if (ret != 0)
4582 {
4583 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4584 return -1;
4585 }
4586
4587 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4588 {
4589 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004590 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004591 if (ret != 0)
4592 {
4593 ip[0] = '\0';
4594 return 0;
4595 }
4596 else
4597 {
4598 p = strchr(ip, '\n');
4599 if (p != NULL)
4600 {
4601 *p = '\0';
4602 }
4603 }
4604 }
4605 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004606}
4607
qs.xionge02a5252023-09-20 14:00:21 +08004608int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4609{
4610 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4611 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4612}
4613
qs.xiong026c5c72022-10-17 11:15:45 +08004614int lynq_ap_connect_num(int sta_number)
4615{
4616 char lynq_limit_cmd[32]={0};
4617 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004618 if((sta_number < 1 ) && (sta_number > 15))
4619 {
4620 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004621 return -1;
4622 }
4623 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4624 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004625 if(ret != 0)
4626 {
4627 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004628 }
4629 return 0;
4630}
you.chenf58b3c92022-06-21 16:53:48 +08004631
qs.xiong77905552022-10-17 11:19:57 +08004632int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4633{
4634
4635 char lynq_wifi_acs_cmd[128]={0};
4636 char lynq_cmd_mode[128]={0};
4637 char lynq_cmd_slect[128]={0};
4638
qs.xiong9fbf74e2023-03-28 13:38:22 +08004639 if((acs_mode != 2) && (acs_mode != 5))
4640 {
qs.xiong77905552022-10-17 11:19:57 +08004641 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4642 }
4643
qs.xiong9fbf74e2023-03-28 13:38:22 +08004644 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4645 {
qs.xiong77905552022-10-17 11:19:57 +08004646 return -1;
4647 }
4648
4649 CHECK_IDX(idx, CTRL_AP);
4650
4651 CHECK_WPA_CTRL(CTRL_AP);
4652
4653 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4654 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4655 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4656
4657 DO_OK_FAIL_REQUEST(cmd_disconnect);
4658 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4659 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4660 DO_OK_FAIL_REQUEST(cmd_save_config);
4661 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4662
4663 return 0;
4664}
you.chen0f5c6432022-11-07 18:31:14 +08004665//you.chen add for tv-box start
4666static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4667 FILE *fp;
4668 //printf("to exec cmd:%s\n", str_cmd);
4669 if((fp=popen(str_cmd,"r"))==NULL)
4670 {
4671 perror("popen error!");
4672 return -1;
4673 }
4674 if((fread(str_cmd_ret,max_len,1,fp))<0)
4675 {
4676 perror("fread fail!");
4677 fclose(fp);
4678 return -1;
4679 }
4680 fclose(fp);
4681 return 0;
4682}
4683
4684static int get_netmask_length(const char* mask)
4685{
4686 int masklen=0, i=0;
4687 int netmask=0;
4688
4689 if(mask == NULL)
4690 {
4691 return 0;
4692 }
4693
4694 struct in_addr ip_addr;
4695 if( inet_aton(mask, &ip_addr) )
4696 {
4697 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004698 }else
4699 {
you.chen0f5c6432022-11-07 18:31:14 +08004700 netmask = 0;
4701 return 0;
4702 }
4703
4704 while(0 == (netmask & 0x01) && i<32)
4705 {
4706 i++;
4707 netmask = netmask>>1;
4708 }
4709 masklen = 32-i;
4710 return masklen;
4711}
4712
4713static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4714 int mask_len;
4715 char *p;
4716 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004717 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4718 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004719 return -1;
4720 p = strstr(str_cmd_ret, "Mask:");
4721 if (p == NULL)
4722 return -1;
4723 mask_len = get_netmask_length(p + 5);
4724 if (mask_len == 0)
4725 return -1;
4726 p = strstr(str_cmd_ret, "inet addr:");
4727 if (p == NULL)
4728 return -1;
4729 strcpy(tmp, p + 10);
4730 p = strstr(tmp, " ");
4731 if (p != NULL)
4732 *p = '\0';
4733 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4734 return 0;
4735}
4736
4737static void GBWWatchThreadProc() {
4738 int i,n, nloop, nmax, ncheckcount, nidlecount;
4739 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4740 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4741 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4742 char *results[16] = {0};
4743 char str_cmd[256] = {0};
4744 char str_cmd_ret[128] = {0};
4745 char dest_ip[32] = {0};
4746 lastAP1Bytes = lastAP2Bytes = 0;
4747 lastAP1Drop = lastAP2Drop = 0;
4748 lastAP1Speed = lastAP2Speed = 0;
4749 setAP1Speed = 50;
4750 setAP2Speed = 80;
4751 nloop = 0;
4752 nmax = 6;
4753 ncheckcount = nidlecount = 0;
4754
you.chen0df3e7e2023-05-10 15:56:26 +08004755 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004756 {
4757 RLOGE("------gbw thread run\n");
4758 return;
4759 }
4760
qs.xiong9fbf74e2023-03-28 13:38:22 +08004761 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004762 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4763 while (dest_ip[0] == '\0') {
4764 sleep(1);
4765 str_cmd_ret[0] = '\0';
4766 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4767 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4768 if (str_cmd_ret[n] == '\n'){
4769 str_cmd_ret[n] = '\0';
4770 break;
4771 }
4772 }
4773 if (str_cmd_ret[0] != '\0')
4774 {
4775 strcpy(dest_ip, str_cmd_ret);
4776 }
4777 }
4778
you.chenc9928582023-04-24 15:39:37 +08004779 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4780 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4781 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 +08004782 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4783 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004784 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004785 return;
4786 }
you.chenc9928582023-04-24 15:39:37 +08004787 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);
4788 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);
4789 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 +08004790
4791 while (1) {
4792 sleep(1);
4793 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004794 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4795 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4796 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004797 continue;
4798 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004799 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004800 if (n > 9) {
4801 if (strcmp(results[1], "Sent") == 0) {
4802 currAP1Bytes = atoll(results[2]);
4803 }
4804 if (strcmp(results[6], "(dropped") == 0) {
4805 currAP1Drop = atoi(results[7]);
4806 }
4807 }
4808
4809 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004810 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4811 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4812 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004813 continue;
4814 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004815 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004816 if (n > 9) {
4817 if (strcmp(results[1], "Sent") == 0) {
4818 currAP2Bytes = atoll(results[2]);
4819 }
4820 if (strcmp(results[6], "(dropped") == 0) {
4821 currAP2Drop = atoi(results[7]);
4822 }
4823 }
4824
4825 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4826 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4827 lastAP1Bytes = currAP1Bytes;
4828 lastAP2Bytes = currAP2Bytes;
4829 continue;
4830 }
4831
4832 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4833 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4834 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4835 lastAP1Speed = currAP1Speed;
4836 lastAP2Speed = currAP2Speed;
4837 lastAP1Bytes = currAP1Bytes;
4838 lastAP2Bytes = currAP2Bytes;
4839
4840 currSetAP1Speed = setAP1Speed;
4841 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4842 ncheckcount++;
4843 if (ncheckcount > 3) {
4844 ncheckcount = 0;
4845 currSetAP1Speed = 5;
4846 }
4847 }
4848 else {
4849 ncheckcount = 0;
4850 if (currAP1Speed < 5)
4851 nidlecount++;
4852 else
4853 nidlecount = 0;
4854
4855 }
4856
4857 if (nidlecount > 60 ){
4858 currSetAP1Speed = 50;
4859 }
4860
4861 if (currSetAP1Speed != setAP1Speed) {
4862 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004863 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4864 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004865 }
4866 }
4867}
4868
4869int enableGBW(const char* mac) {
4870 int i,len;
4871 char get_ipaddr_cmd[128]={0};
4872 ap_info_s *ap;
4873 device_info_s * list;
4874
4875 if (mac == NULL || g_gbw_enabled == 1)
4876 return -1;
4877 len = strlen(mac);
4878 g_gbw_mac = malloc(len + 1);
4879 for(i=0;i<len;i++) {
4880 if (mac[i] >= 'A' && mac[i] <= 'Z')
4881 {
4882 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4883 }
4884 else
4885 g_gbw_mac[i] = mac[i];
4886 }
4887 g_gbw_mac[i] = '\0';
4888 g_gbw_enabled = 1;
4889
4890 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4891 if (system(get_ipaddr_cmd) == 0) {
4892 //startGBW();
4893 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4894 for (i=0;i<len;i++) {
4895 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4896 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4897 startGBW();
4898 }
4899 free(ap);
4900 free(list);
4901 }
4902 }
4903 return 0;
4904}
4905
4906int disableGBW() {
4907 stopGBW();
4908 free(g_gbw_mac);
4909 g_gbw_mac = NULL;
4910 g_gbw_enabled = 1;
4911 return 0;
4912}
4913
4914static int startGBW() {
4915 if (g_gbw_watcher_pid != 0) {
4916 stopGBW();
4917 }
4918 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4919}
4920
4921static int stopGBW() {
4922 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004923 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004924 pthread_cancel(g_gbw_watcher_pid);
4925 pthread_join(g_gbw_watcher_pid, &retval);
4926 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004927 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4928 if (s_ap_iterface_name[0] != '\0')
4929 {
4930 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4931 system(cmd);
4932 }
you.chen0f5c6432022-11-07 18:31:14 +08004933}
4934//you.chen add for tv-box end