blob: 5f02cff8e55b1916d8f9ca6569371ad50fb35f23 [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
qs.xiong1e81dfa2023-09-27 15:52:37 +0800885 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
886 {
887 *error = LYNQ_PSW_ERROR;
888 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
889 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
890 g_sta_conncet_status_flag = 0;
891 return;
892 }
qs.xiong455c30b2023-04-12 11:40:02 +0800893 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
894 {
qs.xiongf0128b12023-06-29 17:29:39 +0800895 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
896 wpanetid = strstr(modify,"id=");
897 if ( wpanetid != NULL )
898 {
899 wpanetid +=strlen("id=");
900 memcpy(destid,wpanetid,2);
901 tmpdisid = atoi(destid);
902
903 }
qs.xiong455c30b2023-04-12 11:40:02 +0800904 pReason = strstr(modify, "reason=");
905 if (pReason != NULL)
906 {
907 pReason += strlen("reason=");
908 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
909 {
910 *error = LYNQ_TIME_OUT;
911 }
912 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
913 {
914 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +0800915 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +0800916 // tmp fix sta autoconnect connect and disconnect
917 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
918 {
919 RLOGE("stop wlan0 network %d fail",tmpdisid);
920 }
qs.xiong455c30b2023-04-12 11:40:02 +0800921 }
922 else
923 {
924 *error = LYNQ_UNSPECIFIED_REASON;
925 }
qs.xiong455c30b2023-04-12 11:40:02 +0800926 }
927 else
928 {
929 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800930 }
qs.xiongf0128b12023-06-29 17:29:39 +0800931 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800932 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800933 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800934
935 }
936
937 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
938 {
939 *error = LYNQ_NOT_FIND_AP;
940 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
941 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800942 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800943 return;
944 }
945
946
947 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
948 {
949 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
950 pReason = strstr(modify, "status_code=");
951 if (pReason != NULL)
952 {
953 pReason += strlen("status_code=");
954 if (memcmp(pReason, "17", 2) == 0)
955 {
956 *error = LYNQ_AP_UNABLE_HANDLE;
957 }
958 else if (memcmp(pReason, "1",1) == 0)
959 {
960 *error = LYNQ_UNSPECIFIED_REASON;
961 }
962 else
963 {
964 *error = LYNQ_UNSPECIFIED_REASON;
965 }
966
967 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
968 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 +0800969 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800970 return;
971 }
972 else
973 {
974 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
975 *error = LYNQ_UNSPECIFIED_REASON;
976 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
977 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
978 return;
979 }
980 }
981
982 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
983 {
984 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
985 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
986 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
987 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
988 return;
989 }
990
991 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
992 {
993 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
994 *error = LYNQ_WAIT_CONNECT_ACTIVE;
995 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
996 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
997 return;
998 }
999
you.chen32cb31e2023-04-13 14:05:45 +08001000 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001001 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001002 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001003 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1004 return;
1005
1006}
1007
qs.xiongfcc914b2023-07-06 21:16:20 +08001008void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1009{
1010 char *pReason;
1011 char *wpanetid;
1012 char destid[3];
1013 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1014 *networkid = -1;
1015 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1016 {
1017 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1018 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1019 return;
1020 }
1021 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1022 {
1023 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1024 wpanetid = strstr(modify,"id=");
1025 if ( wpanetid != NULL )
1026 {
1027 wpanetid +=strlen("id=");
1028 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1029 if (memcpy(destid,wpanetid,2) != NULL)
1030 {
1031 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1032 *networkid = atoi(destid);
1033 RLOGD("get networkid is %d\n",*networkid);
1034 }
1035 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1036 }
1037 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1038 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1039 return;
1040 }
1041 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1042 {
1043 wpanetid = strstr(modify,"id=");
1044 if ( wpanetid != NULL )
1045 {
1046 wpanetid +=strlen("id=");
1047 if (memcpy(destid,wpanetid,2) != NULL)
1048 {
1049 *networkid = atoi(destid);
1050 RLOGD("get networkid is %d\n",*networkid);
1051 }
1052 }
1053 pReason = strstr(modify, "reason=");
1054 if (pReason != NULL)
1055 {
1056 pReason += strlen("reason=");
1057 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1058 {
1059 *error = LYNQ_TIME_OUT;
1060 }
1061 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1062 {
1063 *error = LYNQ_PSW_ERROR;
1064 }
1065 else
1066 {
1067 *error = LYNQ_UNSPECIFIED_REASON;
1068 }
1069 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1070 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1071 return;
1072 }
1073 else
1074 {
1075 *error = LYNQ_UNSPECIFIED_REASON;
1076 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1077 return;
1078 }
1079 }
1080 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1081 {
1082 wpanetid = strstr(modify,"id=");
1083 if ( wpanetid != NULL )
1084 {
1085 wpanetid +=strlen("id=");
1086 if (memcpy(destid,wpanetid,2) != NULL)
1087 {
1088 *networkid = atoi(destid);
1089 RLOGD("get networkid is %d\n",*networkid);
1090 }
1091 }
1092 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1093 pReason = strstr(modify, "status_code=");
1094 if (pReason != NULL)
1095 {
1096 pReason += strlen("status_code=");
1097 if (memcmp(pReason, "17", 2) == 0)
1098 {
1099 *error = LYNQ_AP_UNABLE_HANDLE;
1100 }
1101 else if (memcmp(pReason, "1",1) == 0)
1102 {
1103 *error = LYNQ_UNSPECIFIED_REASON;
1104 }
1105 else
1106 {
1107 *error = LYNQ_UNSPECIFIED_REASON;
1108 }
1109 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1110 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1111 return;
1112 }
1113 else
1114 {
1115 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1116 *error = LYNQ_UNSPECIFIED_REASON;
1117 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001118 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001119 return;
1120 }
1121 }
1122 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1123 {
1124 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1125 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1126 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1127 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1128 return;
1129 }
1130 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1131 {
1132 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1133 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1134 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1135 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1136 return;
1137 }
1138 RLOGD("EVENT : %s\n", modify);
1139 *error = LYNQ_UNSPECIFIED_REASON;
1140 *state = LYNQ_WIFI_STATUS_EGNORE;
1141 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1142 return;
1143}
you.chen70f377f2023-04-14 18:17:09 +08001144static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1145{
you.chen6d247052023-06-01 16:39:54 +08001146 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001147 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1148 {
1149 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1150 g_sta_callback_func(g_sta_callback_priv, state, error);
1151 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1152 }
you.chen6d247052023-06-01 16:39:54 +08001153 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001154}
qs.xiongfcc914b2023-07-06 21:16:20 +08001155static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1156{
1157 pthread_mutex_lock(&s_sta_callback_mutex);
1158 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1159 {
1160 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1161 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1162 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1163 }
1164 pthread_mutex_unlock(&s_sta_callback_mutex);
1165}
you.chen70f377f2023-04-14 18:17:09 +08001166
you.chen35020192022-05-06 11:30:57 +08001167static void STAWatcherThreadProc() {
1168 size_t len = MAX_RET;
1169 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001170 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001171 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001172 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001173
you.chen6c2dd9c2022-05-16 17:55:28 +08001174 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001175 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001176
you.chen70f377f2023-04-14 18:17:09 +08001177 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001178 while (g_sta_watcher_stop_flag == 0)
1179 {
you.chenf711c8a2023-04-13 13:49:45 +08001180 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001181 {
you.chen35020192022-05-06 11:30:57 +08001182 continue;
1183 }
you.chenf711c8a2023-04-13 13:49:45 +08001184
you.chen6c2dd9c2022-05-16 17:55:28 +08001185 memset(msg_notify, 0, MAX_RET);
1186 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001187 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001188 {
you.chen35020192022-05-06 11:30:57 +08001189 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001190 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1191 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001192 {
you.chen35020192022-05-06 11:30:57 +08001193 g_sta_scan_finish_flag = 1;
1194 }
1195
qs.xiong9fbf74e2023-03-28 13:38:22 +08001196 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001197 {
you.chen35020192022-05-06 11:30:57 +08001198 continue;
1199 }
qs.xiong455c30b2023-04-12 11:40:02 +08001200 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001201 notify_connect_status(state, error);
1202
1203 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001204 {
you.chen70f377f2023-04-14 18:17:09 +08001205 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001206 if (last_state != state)
1207 {
1208 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1209 {
1210 system_call_v("%s %s", sta_status_change_script, "connect");
1211 }
1212 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1213 {
1214 system_call_v("%s %s", sta_status_change_script, "disconnect");
1215 }
1216 }
1217
1218 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001219 }
you.chen35020192022-05-06 11:30:57 +08001220 }
1221 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001222 if (lynq_wpa_ctrl != NULL)
1223 {
you.chen92fd5d32022-05-25 10:09:47 +08001224 wpa_ctrl_detach(lynq_wpa_ctrl);
1225 wpa_ctrl_close(lynq_wpa_ctrl);
1226 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001227}
qs.xiongfcc914b2023-07-06 21:16:20 +08001228static void STAAutoWatcherThreadProc() {
1229 size_t len = MAX_RET;
1230 char msg_notify[MAX_RET];
1231 error_number_s error;
1232 lynq_wifi_sta_status_s state, last_state = -1;
1233 int idle_count = 0;
1234 int networkid;
1235 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1236 g_sta_auto_watcher_stop_flag = 0;
1237 RLOGD("STAAutoWatcherThreadProc thread started ------");
1238 while (g_sta_auto_watcher_stop_flag == 0)
1239 {
1240 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1241 {
1242 continue;
1243 }
1244 memset(msg_notify, 0, MAX_RET);
1245 len = MAX_RET;
1246 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1247 {
1248 msg_notify[len+1] = '\0';
1249 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1250 if (strstr(msg_notify, state_scan_result) != NULL)
1251 {
1252 g_sta_auto_scan_finish_flag = 1;
1253 }
1254 if (g_sta_auto_callback_func == NULL)
1255 {
1256 continue;
1257 }
1258 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1259 notify_auto_connect_status(state, error,networkid);
1260 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1261 {
1262 inner_check_connect_error(msg_notify, state, error);
1263 if (last_state != state)
1264 {
1265 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1266 {
1267 system_call_v("%s %s", sta_status_change_script, "connect");
1268 }
1269 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1270 {
1271 system_call_v("%s %s", sta_status_change_script, "disconnect");
1272 }
1273 }
1274 last_state = state;
1275 }
1276 }
1277 }
1278 if (lynq_wpa_ctrl != NULL)
1279 {
1280 wpa_ctrl_detach(lynq_wpa_ctrl);
1281 wpa_ctrl_close(lynq_wpa_ctrl);
1282 }
1283}
qs.xiongf1b525b2022-03-31 00:58:23 -04001284
you.chen70f377f2023-04-14 18:17:09 +08001285// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1286static void GlobalWatcherThreadProc()
1287{
1288 int ret, connect_timeout, service_abnormal;
1289 error_number_s error_num = -1;
1290 inner_sta_status_s sta_status;
1291 scan_info_s *scan_list = NULL;
1292 int i, scan_len=0;
1293 char connecting_ssid[64];
1294 struct timeval now;
1295
1296 RLOGD("GlobalWatcherThreadProc start to run");
1297
1298 while (1)
1299 {
1300 pthread_mutex_lock(&s_global_check_mutex);
1301 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1302 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1303 {
1304 pthread_mutex_unlock(&s_global_check_mutex);
1305 usleep(50*1000);
1306 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1307 continue;
1308 }
1309
1310 connect_timeout = 0;
1311 service_abnormal = 0;
1312 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1313 {
1314 while (1)
1315 {
1316 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1317 if (ret == ETIME)
1318 {
1319 connect_timeout = 1;
1320 }
1321 else if (ret != 0)
1322 {
1323 gettimeofday(&now,NULL);
1324 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1325 {
1326 usleep(SLEEP_TIME_ON_IDLE);
1327 continue;
1328 }
1329 connect_timeout = 1;
1330 }
1331 sta_status = s_sta_status;
1332 error_num = s_sta_error_number;
1333 s_sta_status = INNER_STA_STATUS_INIT;
1334 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1335 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1336 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1337 break;
1338 }
1339 }
1340 if (s_service_invoke_timeout_cnt > 10)
1341 {
1342 service_abnormal = 1;
1343 s_service_invoke_timeout_cnt = 0;
1344 }
1345 pthread_mutex_unlock(&s_global_check_mutex);
1346
1347 if (service_abnormal == 1)
1348 {
1349 sleep(1);
1350 RLOGE("wpa service is abnormal info app to exit");
1351 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001352
1353 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1354
you.chen70f377f2023-04-14 18:17:09 +08001355 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1356 }
1357
1358 if (sta_status == INNER_STA_STATUS_CANCEL)
1359 {
1360 continue;
1361 }
1362 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1363 {
1364 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1365 }
1366 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1367 {
1368 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1369 {
1370 for(i=0; i < scan_len;i++)
1371 {
1372 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1373 {
1374 error_num = LYNQ_AUTH_ERROR;
1375 break;
1376 }
1377 }
1378 free(scan_list);
1379 }
1380 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1381 }
1382 else if (connect_timeout == 0)
1383 {
1384 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1385 }
1386 else // wait timeout
1387 {
1388 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1389 {
1390 ; // wpa service abnormal
1391 }
1392 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1393 {
1394 RLOGD("GlobalWatcherThreadProc notify connected");
1395 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1396 }
1397 else
1398 {
1399 RLOGD("GlobalWatcherThreadProc notify timeout");
1400 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1401 }
1402 }
1403 } // while (1)
1404}
1405
qs.xiong1af5daf2022-03-14 09:12:12 -04001406int lynq_wifi_enable(void)
1407{
you.chen35020192022-05-06 11:30:57 +08001408 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001409 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001410 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001411 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1412
qs.xiong9fbf74e2023-03-28 13:38:22 +08001413 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1414 {
you.chend2fef3f2023-02-13 10:50:35 +08001415 goto out_enable;
1416 }
1417
you.chenc9928582023-04-24 15:39:37 +08001418 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001419 if (ret != 0)
1420 {
1421 //printf("service state %d\n", ret);
1422 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001423 ret = -1;
1424 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001425 }
lhfe8da902022-10-11 18:55:36 +08001426
you.chen70f377f2023-04-14 18:17:09 +08001427 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1428 {
1429 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1430 if(ret<0)
1431 {
1432 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1433 ret = -1;
1434 goto out_enable;
1435 }
1436 }
1437
you.chend2fef3f2023-02-13 10:50:35 +08001438 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1439 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1440 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1441 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1442out_enable:
1443 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001444 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001445}
1446
qs.xiong1af5daf2022-03-14 09:12:12 -04001447int lynq_wifi_disable(void)
1448{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001449 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001450 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001451 g_ap_watcher_stop_flag = 1;
1452 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001453 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001454 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001455 if (g_ap_watcher_pid != 0)
1456 pthread_join(g_ap_watcher_pid, NULL);
1457 if (g_sta_watcher_pid != 0)
1458 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001459 if (g_sta_auto_watcher_pid != 0)
1460 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001461 if (g_lynq_wpa_ctrl[0] != NULL)
1462 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1463 if (g_lynq_wpa_ctrl[1] != NULL)
1464 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001465 if (g_ap_tmp_watcher_pid != 0)
1466 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001467 g_ap_watcher_pid = 0;
1468 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001469 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001470 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001471 g_lynq_wpa_ctrl[0] = NULL;
1472 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001473 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001474 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001475 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1476 return 0;
1477}
1478
1479static inline char inner_convert_char(char in)
1480{
1481 if (in >= '0' && in <= '9')
1482 {
1483 return in - '0';
1484 }
1485 else if (in >= 'a' && in <= 'f')
1486 {
1487 return in - 'a' + 10;
1488 }
1489 else if (in >= 'A' && in <= 'F')
1490 {
1491 return in - 'A' + 10;
1492 }
1493 else
1494 {
1495 return '\xff';
1496 }
1497}
1498
1499static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1500{
1501 char *p;
1502 size_t pos = 0;
1503 if (NULL == out_ssid)
1504 return;
1505 //printf("input ssid=[%s]\n", ssid);
1506 memset(out_ssid, 0, out_ssid_len);
1507 if (NULL == ssid)
1508 return;
1509 p = strchr(ssid, '\\');
1510 if (NULL == p)
1511 {
1512 strncpy(out_ssid, ssid, out_ssid_len);
1513 //printf(" first %s\n", out_ssid);
1514 }
1515 else
1516 {
1517 pos = p - ssid;
1518 memcpy(out_ssid, ssid, pos);
1519 //printf("pos %lu -- %s\n", pos, out_ssid);
1520 for(; pos < out_ssid_len; pos ++)
1521 {
1522 if (p[0] == '\0')
1523 {
1524 //printf(" out %s\n", out_ssid);
1525 return;
1526 }
1527 else if (p[0] != '\\')
1528 {
1529 out_ssid[pos] = p[0];
1530 p += 1;
1531 }
1532 else if (p[1] == 'x' || p[1] == 'X')
1533 {
1534 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1535 p += 4;
1536 }
1537 else if (p[1] == '\\')
1538 {
1539 out_ssid[pos] = '\\';
1540 p += 2;
1541 }
1542 else if (p[1] == 't')
1543 {
1544 out_ssid[pos] = '\t';
1545 p += 2;
1546 }
1547 else if (p[1] == 'r')
1548 {
1549 out_ssid[pos] = '\r';
1550 p += 2;
1551 }
1552 else if (p[1] == 'n')
1553 {
1554 out_ssid[pos] = '\n';
1555 p += 2;
1556 }//todo find a better way to convert?
1557 }
1558 }
1559 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001560}
qs.xiong1af5daf2022-03-14 09:12:12 -04001561
you.chen35020192022-05-06 11:30:57 +08001562static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001563 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001564 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001565 RLOGD("enter inner_get_param");
1566 if (out_put == NULL)
1567 {
1568 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001569 return -1;
1570 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001571 if (param_name == NULL)
1572 {
1573 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001574 return -1;
1575 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001576 if (param_name[0] == '\0')
1577 {
1578 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001579 return -1;
1580 }
1581
1582 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1583
1584 CHECK_WPA_CTRL(interface);
1585
1586 DO_REQUEST(lynq_cmd_get);
1587
qs.xiong9fbf74e2023-03-28 13:38:22 +08001588 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1589 {
1590 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001591 return -1;
1592 }
1593
you.chena6fa5b22022-05-18 10:28:19 +08001594// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001595 if (strcmp(param_name, "ssid") == 0)
1596 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001597 if (cmd_reply[0] == '\"')
1598 {
you.chend2fef3f2023-02-13 10:50:35 +08001599 ssid_len = reply_len - 1;
1600 memcpy(out_put, cmd_reply + 1, ssid_len);
1601 if (out_put[ssid_len-1] == '\"')
1602 {
1603 out_put[ssid_len-1] = '\0';
1604 }
1605 else
1606 {
1607 out_put[ssid_len] = '\0';
1608 }
1609 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001610 else
1611 {
you.chend2fef3f2023-02-13 10:50:35 +08001612 ssid_len = reply_len / 2;
1613 for(i=0; i<ssid_len; i++)
1614 {
1615 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1616 }
1617 out_put[ssid_len] = '\0';
1618 }
1619 }
1620 else
1621 {
1622 memcpy(out_put, cmd_reply, reply_len + 1);
1623 }
you.chen35020192022-05-06 11:30:57 +08001624 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001625}
qs.xiong1af5daf2022-03-14 09:12:12 -04001626
you.chen35020192022-05-06 11:30:57 +08001627static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1628 int ret = 0;
1629 char * end = str + len - 1;
1630 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001631 while(str < end)
1632 {
1633 if (*str == delimiter)
1634 {
you.chen35020192022-05-06 11:30:57 +08001635 *str++ = '\0';
1636 results[ret++] = str;
1637 continue;
1638 }
1639 str++;
1640 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001641 if (*str == delimiter)
1642 {
you.chen35020192022-05-06 11:30:57 +08001643 *str = '\0';
1644 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001645
you.chen6ed36a62023-04-27 17:51:56 +08001646 results[ret] = NULL;
1647
you.chen35020192022-05-06 11:30:57 +08001648 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001649}
1650
you.chend2fef3f2023-02-13 10:50:35 +08001651static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1652{
1653 char * p;
1654 int ret = 0;
1655 char cmd[256]={0};
1656 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001657 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001658 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001659 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001660 ret = exec_cmd(cmd, ip, ip_len);
1661 p = strchr(ip, '\n');
1662 if (NULL != p)
1663 {
1664 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001665 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001666 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001667 return ret;
1668}
1669
you.chend2fef3f2023-02-13 10:50:35 +08001670static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001671 struct in_addr addr ={0};
1672 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001673 char cmd[64] = {0};
1674 char * p;
1675 int ret;
you.chen35020192022-05-06 11:30:57 +08001676
qs.xiong9fbf74e2023-03-28 13:38:22 +08001677 if (ip == NULL || *ip == '\0' || hostname == NULL)
1678 {
1679 RLOGE("ip == NULL or hostname == NULL");
1680 return -1;
you.chen35020192022-05-06 11:30:57 +08001681 }
1682
you.chend2fef3f2023-02-13 10:50:35 +08001683 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001684 if (inet_aton(ip, &addr) == 0)
1685 {
you.chen35020192022-05-06 11:30:57 +08001686 printf("---inet_aton fail\n");
1687 return -1;
1688 }
1689
1690 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1691
qs.xiong9fbf74e2023-03-28 13:38:22 +08001692 if (ht == NULL)
1693 {
you.chen186d3c32023-05-18 14:19:46 +08001694 hostname[0] = '\0';
1695 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1696 ret = exec_cmd(cmd, hostname, 32);
1697 if (ret == 0)
1698 {
1699 p = strchr(hostname, '\n');
1700 if (p != NULL)
1701 {
1702 *p = '\0';
1703 }
1704 return 0;
1705 }
1706 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001707 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001708 herror(NULL);
1709 return -1;
1710 }
1711
1712 strcpy(hostname, ht->h_name);
1713
1714 return 0;
1715}
1716
1717static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1718{
1719 int count, index, words_count;
1720 char * split_lines[128]= {0};
1721 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001722 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001723 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001724 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001725
1726 CHECK_WPA_CTRL(ap_sta);
1727
1728 DO_REQUEST(lynq_wifi_list_networks);
1729
1730 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1731
1732 //@todo check ssid field to compatible
1733
1734 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001735 for(index=1; index < count; index++)
1736 {
you.chen35020192022-05-06 11:30:57 +08001737 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001738 if (words_count > 2)
1739 {
you.chend2fef3f2023-02-13 10:50:35 +08001740 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001741 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1742 {
you.chen35020192022-05-06 11:30:57 +08001743 net_no_list[ret++] = atoi(split_words[0]);
1744 }
1745 }
1746 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001747 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001748 return ret;
1749}
1750
1751static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001752 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001753 CHECK_WPA_CTRL(ap_sta);
1754 const char *lynq_wifi_add_network = "ADD_NETWORK";
1755
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001757 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001758 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1759 {
1760 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001761 return -1;
1762 }
1763
qs.xiong9fbf74e2023-03-28 13:38:22 +08001764 for(i=0;i<reply_len;i++)
1765 {
1766 if(cmd_reply[i] == '\n')
1767 {
you.chen35020192022-05-06 11:30:57 +08001768 cmd_reply[i] = '\0';
1769 break;
1770 }
1771 }
1772 return atoi(cmd_reply);
1773}
you.chena6cd55a2022-05-08 12:20:18 +08001774
you.chen35020192022-05-06 11:30:57 +08001775static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1776{
1777 int count, index;
1778 int net_no_list[128];
1779
qs.xiong9fbf74e2023-03-28 13:38:22 +08001780 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001781 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001782 for (index=0; index < count; index++)
1783 {
1784 if (net_no_list[index] == net_no)
1785 {
you.chen35020192022-05-06 11:30:57 +08001786 return 0;
1787 }
1788 }
1789
1790 if (count >= 1)
1791 index = net_no_list[count - 1];
1792 else
1793 index = -1;
1794
qs.xiong9fbf74e2023-03-28 13:38:22 +08001795 while (index < net_no )
1796 {
you.chen35020192022-05-06 11:30:57 +08001797 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001798 if (index >= net_no)
1799 { // required network no created
1800 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001801 return 0;
1802 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001803 else if( index < 0)
1804 {
1805 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001806 return -1;
1807 }
you.chen35020192022-05-06 11:30:57 +08001808 }
1809
1810 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001811 {
1812 RLOGE("[lynq_check_network_number] network index < 0");
1813 return -1;
1814 }
1815 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001816 return 0;
1817}
1818
1819static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001820 if (freq > 5000 && freq < 6000)
1821 {
you.chen35020192022-05-06 11:30:57 +08001822 return LYNQ_WIFI_5G_band;
1823 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001824 else if (freq > 2000 && freq < 3000)
1825 {
you.chen35020192022-05-06 11:30:57 +08001826 return LYNQ_WIFI_2G_band;
1827 }
1828 return LYNQ_WIFI_2_and_5G_band;
1829}
1830
1831static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001832 if (key_mgmt != NULL)
1833 {
1834 if (memcmp( key_mgmt, "NONE", 4) == 0)
1835 {
you.chen35020192022-05-06 11:30:57 +08001836 return LYNQ_WIFI_AUTH_OPEN;
1837 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001838 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1839 {
you.chen35020192022-05-06 11:30:57 +08001840 return LYNQ_WIFI_AUTH_WEP;
1841 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001842 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1843 {
you.chen35020192022-05-06 11:30:57 +08001844 return LYNQ_WIFI_AUTH_WPA_PSK;
1845 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001846 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1847 {
you.chen35020192022-05-06 11:30:57 +08001848 return LYNQ_WIFI_AUTH_WPA2_PSK;
1849 }
1850 }
1851
1852 return -1;
1853}
1854
1855static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 if (flag != NULL)
1857 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001858 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 +08001859 {
1860 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001861 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001862 {
1863 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1864 }else if (strstr( flag, "WPA2-PSK") != NULL)
1865 {
you.chen35020192022-05-06 11:30:57 +08001866 return LYNQ_WIFI_AUTH_WPA2_PSK;
1867 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001868 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001869 {
you.chen35020192022-05-06 11:30:57 +08001870 return LYNQ_WIFI_AUTH_WPA_PSK;
1871 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001872 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001873 {
you.chen35020192022-05-06 11:30:57 +08001874 return LYNQ_WIFI_AUTH_WEP;
1875 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001876 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001877 {
you.chen35020192022-05-06 11:30:57 +08001878 return LYNQ_WIFI_AUTH_OPEN;
1879 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001880 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001881 {
you.chend2fef3f2023-02-13 10:50:35 +08001882 return LYNQ_WIFI_AUTH_OPEN;
1883 }
qs.xiong46f41562023-07-11 21:06:47 +08001884 else
1885 {
1886 RLOGD("convert_max_auth_from_flag not-found auth mode");
1887 }
you.chen35020192022-05-06 11:30:57 +08001888 }
1889
1890 return -1;
1891}
1892
1893static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1894 switch (bw) {
1895 case 10:
1896 return LYNQ_WIFI_BANDWIDTH_HT10;
1897 break;
1898 case 20:
1899 return LYNQ_WIFI_BANDWIDTH_HT20;
1900 break;
1901 case 40:
1902 return LYNQ_WIFI_BANDWIDTH_HT40;
1903 break;
1904 case 80:
1905 return LYNQ_WIFI_BANDWIDTH_HT80;
1906 break;
1907 default:
1908 break;
1909 }
1910
1911 return -1;
1912}
1913
you.chen70f377f2023-04-14 18:17:09 +08001914static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001915static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1916 int i, count;
1917 char *p;
1918 const char *lynq_status_cmd = "STATUS";
1919 const char * FLAG_SSID = "ssid=";
1920 const char * FLAG_SBSID = "bssid=";
1921 const char * FLAG_KEY_MGMT = "key_mgmt=";
1922 const char * FLAG_FREQ = "freq=";
1923 const char * FLAG_STATE = "wpa_state=";
1924 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001925 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001926 char *split_lines[128] = {0};
1927
1928 CHECK_WPA_CTRL(interface);
1929
qs.xiong9fbf74e2023-03-28 13:38:22 +08001930 if (curr_state == NULL)
1931 {
1932 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001933 return -1;
1934 }
1935
1936 DO_REQUEST(lynq_status_cmd);
1937
1938 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1939
1940 curr_state->net_no = -1;
1941 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001942 for(i=0; i < count; i++)
1943 {
1944 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001945 {
you.chen35020192022-05-06 11:30:57 +08001946 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001947 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001948 {
you.chend2fef3f2023-02-13 10:50:35 +08001949 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001950 ret = 0;
1951 continue;
1952 }
you.chenf58b3c92022-06-21 16:53:48 +08001953 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001954 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001955 {
you.chend2fef3f2023-02-13 10:50:35 +08001956 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 +08001957 ret = 0;
1958 continue;
1959 }
you.chen35020192022-05-06 11:30:57 +08001960 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001961 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001962 {
you.chen450d0172022-07-15 17:56:48 +08001963 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001964 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001965 ret = 0;
1966 continue;
1967 }
1968 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001969 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001970 {
you.chen35020192022-05-06 11:30:57 +08001971 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1972 ret = 0;
1973 continue;
1974 }
you.chend2fef3f2023-02-13 10:50:35 +08001975 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001976 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001977 {
you.chend2fef3f2023-02-13 10:50:35 +08001978 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1979 ret = 0;
1980 continue;
1981 }
you.chen35020192022-05-06 11:30:57 +08001982 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001983 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001984 {
you.chen35020192022-05-06 11:30:57 +08001985 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001986 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001987 {
you.chen35020192022-05-06 11:30:57 +08001988 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1989 ret = 0;
1990 continue;
1991 }
1992
1993 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001994 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001995 {
you.chen35020192022-05-06 11:30:57 +08001996 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001997 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001998 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001999 }
2000 }
2001
you.chen70f377f2023-04-14 18:17:09 +08002002 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2003 {
2004 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2005 }
2006
you.chen35020192022-05-06 11:30:57 +08002007 return ret;
2008}
2009
qs.xiongf1b525b2022-03-31 00:58:23 -04002010int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002011{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002012 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002013 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002014
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 if (ap_ssid == NULL)
2016 {
2017 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002018 return -1;
2019 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002020 else
2021 {
2022 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002023 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002024
qs.xiong9fbf74e2023-03-28 13:38:22 +08002025 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2026 {
2027 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002028 return -1;
2029 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002030
you.chen35020192022-05-06 11:30:57 +08002031 CHECK_IDX(idx, CTRL_AP);
2032
2033 CHECK_WPA_CTRL(CTRL_AP);
2034
2035 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2036
2037 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2038 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2040 return 0;
you.chen35020192022-05-06 11:30:57 +08002041
qs.xiong7a105ce2022-03-02 09:43:11 -05002042}
2043
you.chen35020192022-05-06 11:30:57 +08002044int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002045{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002046 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002047 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002048 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002049}
2050
qs.xiongc9c79f72022-10-17 15:27:18 +08002051/*****
2052 *frequency <------>channel
2053 *
2054 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2055 *
2056 *
2057 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2058 *
2059 *
2060 * */
2061static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002062 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};
2063 int i;
2064 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2065
qs.xiong69a332b2022-12-02 09:58:57 +08002066 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002067 {
2068 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002069 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002070 }
qs.xiongc00b6032022-11-29 16:28:03 +08002071
2072 if(i == arr_len)
2073 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002074 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002075 return -1;
2076 }
qs.xiongc00b6032022-11-29 16:28:03 +08002077
qs.xiongc9c79f72022-10-17 15:27:18 +08002078 return 0;
2079}
qs.xiong13673462023-02-21 19:12:54 +08002080
2081static int lynq_check_frequencyby_country_code(int input_frequency)
2082{
2083 char str_cnc[]="CN";
2084 char str_dest[20]="";
2085
2086 if( lynq_get_country_code(1,str_dest) != 0 )
2087 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002088 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002089 return -1;
2090 }
2091 if( strncmp(str_dest,str_cnc,2) != 0 )
2092 {
2093 return 0;
2094 }else if( 2473 < input_frequency && input_frequency < 5744)
2095 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002096 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002097 return -1;
2098 }
2099 return 0;
2100}
qs.xiongf1b525b2022-03-31 00:58:23 -04002101int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002102{
qs.xiongc00b6032022-11-29 16:28:03 +08002103 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002104 char lynq_wifi_frequency_cmd[128]={0};
2105 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002106 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002107 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002108 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002109 check = lynq_check_set_frequency(lynq_wifi_frequency);
2110 if(check != 0)
2111 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002112 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002113 return -1;
you.chen35020192022-05-06 11:30:57 +08002114 }
qs.xiong13673462023-02-21 19:12:54 +08002115 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2116 if(check != 0)
2117 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002119 return -1;
2120 }
2121
qs.xiongc00b6032022-11-29 16:28:03 +08002122 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2123 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002124 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002125 return -1;
2126 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002127
you.chen35020192022-05-06 11:30:57 +08002128 CHECK_IDX(idx, CTRL_AP);
2129
2130 CHECK_WPA_CTRL(CTRL_AP);
2131
2132 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2133 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2134 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2135
you.chen6c2dd9c2022-05-16 17:55:28 +08002136 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002137 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2138 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2139 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002140
qs.xiong9fbf74e2023-03-28 13:38:22 +08002141 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002142}
2143
qs.xiongf1b525b2022-03-31 00:58:23 -04002144int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002145{
you.chen35020192022-05-06 11:30:57 +08002146 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002147 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002148 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002149
qs.xiong9fbf74e2023-03-28 13:38:22 +08002150 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2151 {
2152 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002153 return -1;
2154 }
2155 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002156
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002158}
2159
qs.xiongf1b525b2022-03-31 00:58:23 -04002160int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2161{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002162 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002163 CHECK_IDX(idx, CTRL_AP);
2164 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002165 case LYNQ_WIFI_BANDWIDTH_HT10:
2166 {
2167 RLOGE("bandwith [%d] not support now\n", bandwidth);
2168 return -1;
2169 }
2170 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002171 {
2172 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2173 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002174 if (system(lynq_cmd_bandwith) != 0 )
2175 {
2176 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002177 return -1;
2178 }
2179 system("wl up");
2180 break;
2181 }
2182 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002183 {
qs.xiong10379192023-02-21 13:19:42 +08002184 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002185 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2186 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002187 if (system(lynq_cmd_bandwith) != 0 )
2188 {
2189 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002190 return -1;
2191 }
2192 system("wl up");
2193 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002194 }
you.chen35020192022-05-06 11:30:57 +08002195 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002196 {
qs.xiong10379192023-02-21 13:19:42 +08002197 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002198 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002199 if (system(lynq_cmd_bandwith) != 0 )
2200 {
2201 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002202 return -1;
2203 }
2204 system("wl up");
2205 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002206 }
2207 default:
you.chen35020192022-05-06 11:30:57 +08002208 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002209 RLOGE("auth type [%d] not support now\n", bandwidth);
2210 return -1;
you.chen35020192022-05-06 11:30:57 +08002211 }
2212 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002213
2214
you.chen35020192022-05-06 11:30:57 +08002215 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002216}
you.chen35020192022-05-06 11:30:57 +08002217
qs.xiongf1b525b2022-03-31 00:58:23 -04002218int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2219{
you.chen35020192022-05-06 11:30:57 +08002220 int count = 0;
2221 int index = 0;
2222 char *split_words[128] = {0};
2223 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002224 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002225 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002226
you.chen35020192022-05-06 11:30:57 +08002227 CHECK_WPA_CTRL(CTRL_AP);
2228
2229 DO_REQUEST(lynq_chanspec_cmd);
2230
2231 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2232 for(;index < count; index++) {
2233 if (strncmp(split_words[index], "bw", 2) != 0) {
2234 continue;
2235 }
2236
2237 index++;
2238 if (index >= count) {
2239 return -1;
2240 }
2241
qs.xiong9fbf74e2023-03-28 13:38:22 +08002242 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002243 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2244 return 0;
2245 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002246 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002247 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002248}
qs.xiong0fb469a2022-04-14 03:50:45 -04002249
qs.xiongf1b525b2022-03-31 00:58:23 -04002250int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002251{
you.chen35020192022-05-06 11:30:57 +08002252 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002253 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002254 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002255
you.chen35020192022-05-06 11:30:57 +08002256 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002257
qs.xiong9fbf74e2023-03-28 13:38:22 +08002258 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2259 {
you.chen35020192022-05-06 11:30:57 +08002260 return -1;
2261 }
2262
2263 system("wl down");
2264 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002265 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002266 return -1;
2267 }
2268 system("wl up");
2269 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002270}
qs.xiong0fb469a2022-04-14 03:50:45 -04002271
qs.xiongf1b525b2022-03-31 00:58:23 -04002272int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002273{
you.chen35020192022-05-06 11:30:57 +08002274 int count = 0;
2275 int index = 0;
2276 char *split_words[128] = {0};
2277 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002278 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002279 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002280
you.chen35020192022-05-06 11:30:57 +08002281 CHECK_WPA_CTRL(CTRL_AP);
2282
2283 DO_REQUEST(lynq_chanspec_cmd);
2284
2285 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002286 for(;index < count; index++)
2287 {
2288 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002289 if (strncmp(split_words[index], "channel", 2) != 0) {
2290 continue;
2291 }
2292
2293 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002294 if (index >= count)
2295 {
you.chen35020192022-05-06 11:30:57 +08002296 return -1;
2297 }
2298
2299 *channel = atoi(split_words[index]);
2300 return 0;
2301 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002302 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002303 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002304}
2305
2306
you.chen35020192022-05-06 11:30:57 +08002307int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002308{
you.chen6c2dd9c2022-05-16 17:55:28 +08002309 char ssid[MAX_CMD] = {0};
2310 int freq = 0;
2311 char lynq_auth_cmd[64]={0};
2312 char lynq_auth_alg_cmd[64]={0};
2313 char lynq_psk_cmd[64]={0};
2314 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 char lynq_ieee80211_cmd[64]={0};
2316 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002317 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002318 CHECK_IDX(idx, CTRL_AP);
2319
you.chen6c2dd9c2022-05-16 17:55:28 +08002320 CHECK_WPA_CTRL(CTRL_AP);
2321
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2323 {
2324 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002325 return -1;
2326 }
2327
you.chen92fd5d32022-05-25 10:09:47 +08002328 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002329 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002330 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002331 return 0;
2332 }
2333 else {
2334 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2335 ssid[0] = '\0';
2336 }
2337 lynq_wifi_ap_frequency_get(idx, &freq);
2338
2339 DO_OK_FAIL_REQUEST(cmd_disconnect);
2340 DO_OK_FAIL_REQUEST(cmd_remove_all);
2341 if (ssid[0] != '\0') {
2342 lynq_wifi_ap_ssid_set(idx, ssid);
2343 }
2344 if (freq != 0) {
2345 lynq_wifi_ap_frequency_set(idx, freq);
2346 }
2347 }
2348 }
you.chen35020192022-05-06 11:30:57 +08002349
qs.xiong9fbf74e2023-03-28 13:38:22 +08002350 switch(auth){
2351 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002352 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002353 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +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.chen35020192022-05-06 11:30:57 +08002356 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 break;
2358 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002359 case LYNQ_WIFI_AUTH_WEP:
2360 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002361 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002362 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002363 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002364 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2365
2366 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2367 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2368 break;
2369 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002370 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002371 {
2372 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2373 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2374 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2375
2376 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2377 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2378 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2379 break;
2380
2381 }
you.chen35020192022-05-06 11:30:57 +08002382 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002383 {
2384 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2385 {
you.chen35020192022-05-06 11:30:57 +08002386 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2387 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2388 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002389 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2390 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002391 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002392 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002393 }
2394// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2395// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2396 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002397
you.chen35020192022-05-06 11:30:57 +08002398 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2399 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2400 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002401 break;
2402 }
2403 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2404 {
2405 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2406 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2407 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2408 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2409
2410 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2411 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2412 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2413 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2414 break;
2415 }
2416 case LYNQ_WIFI_AUTH_WPA3_PSK:
2417 {
2418 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2419 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002420 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2422
2423 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2424 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2425 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2426 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2427 break;
2428 }
2429 default:
you.chen35020192022-05-06 11:30:57 +08002430 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002431 RLOGE("auth type [%d] not support now\n", auth);
2432 return -1;
you.chen35020192022-05-06 11:30:57 +08002433 }
2434 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002435 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002436
qs.xiong9fbf74e2023-03-28 13:38:22 +08002437 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002438}
2439
you.chen35020192022-05-06 11:30:57 +08002440int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002441{
you.chen35020192022-05-06 11:30:57 +08002442 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002443 char lynq_auth_alg_str[MAX_RET] = {0};
2444 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002445 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002446 CHECK_IDX(idx, CTRL_AP);
2447
qs.xiong9fbf74e2023-03-28 13:38:22 +08002448 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2449 {
2450 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002451 return -1;
2452 }
2453
qs.xiong9fbf74e2023-03-28 13:38:22 +08002454 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2455 {
2456 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2457 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002458 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002459 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002460 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002461 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002462 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2463 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002464 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002465 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002466 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002467 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002468 else
2469 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002470 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002471 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002472 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002473 }
you.chen35020192022-05-06 11:30:57 +08002474 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002475 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2476 {
2477 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2478 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002479 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002480 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002481 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002482 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2483 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002484 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002485 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002486 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002487 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002488 else
2489 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002490 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002491 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002492 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002493 }
you.chen35020192022-05-06 11:30:57 +08002494 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002495
2496 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2497 {
2498 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2499 return -1;
2500 }
2501
2502 if (memcmp(lynq_auth_str,"1",1) == 0 )
2503 {
2504 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2505 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002506 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002507 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2508 {
2509 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2510 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002511 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002512 }
2513 else
2514 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002515 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002516 *auth = -1;
2517 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002518
you.chen6c2dd9c2022-05-16 17:55:28 +08002519 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002520}
qs.xiong1af5daf2022-03-14 09:12:12 -04002521
you.chenb95401e2023-05-12 19:39:06 +08002522static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2523{
2524 char status[64];
you.chencba13492023-05-19 13:53:43 +08002525 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002526 curr_status_info curr_state;
2527
2528 CHECK_WPA_CTRL(CTRL_AP);
2529
2530 memset(status, 0, sizeof (status));
2531
2532 curr_state.ap = NULL;
2533 curr_state.state = status;
2534
2535 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002536 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002537 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002538 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002539 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002540 {
2541 return 0;
2542 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002543 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002544 {
2545 DO_REQUEST("RECONNECT");
2546 return inner_check_ap_connected(idx, retry_count+1);
2547 }
you.chencba13492023-05-19 13:53:43 +08002548 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002549 {
2550 printf("retry 10 time\n");
2551 return -1;
2552 }
2553 else
2554 {
you.chen6d247052023-06-01 16:39:54 +08002555 if (strcmp(status, STATE_DISCONNECTED) == 0)
2556 {
2557 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2558 DO_REQUEST(LYNQ_WIFI_CMD);
2559 }
you.chenb95401e2023-05-12 19:39:06 +08002560 return inner_check_ap_connected(idx, retry_count+1);
2561 }
2562 }
2563 return -1;
2564}
qs.xiong1af5daf2022-03-14 09:12:12 -04002565
qs.xiongf1b525b2022-03-31 00:58:23 -04002566int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002567{
qs.xiong5a2ba932023-09-13 16:30:21 +08002568 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002569 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002570 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2571 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2572 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002573 CHECK_IDX(idx, CTRL_AP);
2574
2575 CHECK_WPA_CTRL(CTRL_AP);
2576
you.chen0df3e7e2023-05-10 15:56:26 +08002577 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002578 {
you.chen0df3e7e2023-05-10 15:56:26 +08002579 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002580 return -1;
2581 }
you.chen35020192022-05-06 11:30:57 +08002582
qs.xiongb37f8c42023-09-13 21:21:58 +08002583 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2584 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2585
you.chen35020192022-05-06 11:30:57 +08002586 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2587 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2588
you.chenc9928582023-04-24 15:39:37 +08002589 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2590 if (ret != 0)
2591 {
2592 RLOGE("lynq_wifi_ap_start excute script fail");
2593 return -1;
2594 }
2595
you.chenb95401e2023-05-12 19:39:06 +08002596 if (inner_check_ap_connected(idx, 0) != 0)
2597 {
2598 return -1;
2599 }
2600
you.chen0df3e7e2023-05-10 15:56:26 +08002601 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002602 if (g_ap_tmp_watcher_pid == 0)
2603 {
2604 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2605 {
2606 g_ap_tmp_watcher_pid = 0;
2607 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2608 return -1;
2609 }
2610 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2611 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002612 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002613
qs.xiong9fbf74e2023-03-28 13:38:22 +08002614 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002615}
2616
qs.xiongf1b525b2022-03-31 00:58:23 -04002617int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002618{
you.chen35020192022-05-06 11:30:57 +08002619 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002620}
2621
qs.xiongf1b525b2022-03-31 00:58:23 -04002622int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002623{
you.chen35020192022-05-06 11:30:57 +08002624 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002625
you.chen35020192022-05-06 11:30:57 +08002626 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002627
you.chen35020192022-05-06 11:30:57 +08002628 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002629
you.chen35020192022-05-06 11:30:57 +08002630 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2631
2632 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2633
you.chenb4b121c2022-05-06 17:50:16 +08002634// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002635
2636 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2637 if (ret != 0)
2638 {
2639 RLOGE("lynq_wifi_ap_start excute script fail");
2640 return -1;
2641 }
qs.xiong44fac672023-08-29 16:15:55 +08002642 g_ap_tmp_watcher_stop_flag = 1;
2643 if (g_ap_tmp_watcher_pid != 0)
2644 pthread_join(g_ap_tmp_watcher_pid, NULL);
2645 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002646
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002648}
qs.xiong1af5daf2022-03-14 09:12:12 -04002649
qs.xiongf1b525b2022-03-31 00:58:23 -04002650int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002651{
you.chen35020192022-05-06 11:30:57 +08002652 char lynq_disable_cmd[128] = {0};
2653 char lynq_select_cmd[128] = {0};
2654 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002655 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002656 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002657
you.chen35020192022-05-06 11:30:57 +08002658 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002659 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2660 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2661
2662 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2663 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2664 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002665
qs.xiong9fbf74e2023-03-28 13:38:22 +08002666 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002667}
2668
qs.xiongf1b525b2022-03-31 00:58:23 -04002669int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002670{
you.chen35020192022-05-06 11:30:57 +08002671 char lynq_disable_cmd[128] = {0};
2672 char lynq_select_cmd[128] = {0};
2673 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002674 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002675 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002676
you.chen35020192022-05-06 11:30:57 +08002677 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002678
you.chen35020192022-05-06 11:30:57 +08002679 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2680 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2681
2682 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2683 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2684 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002685
qs.xiong9fbf74e2023-03-28 13:38:22 +08002686 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002687}
qs.xiongf1b525b2022-03-31 00:58:23 -04002688
you.chen35020192022-05-06 11:30:57 +08002689int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002690{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002691 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002692 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002693 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002694 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002695 RLOGD("enter lynq_ap_password_set");
2696 if( password == NULL )
2697 {
2698 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002699 return -1;
2700 }
2701 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002702 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002703 if(pass_len < 8 || pass_len >= 64)
2704 {
2705 RLOGE("[lynq_ap_password_set]input password len not in rage");
2706 return -1;
you.chen35020192022-05-06 11:30:57 +08002707 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002708
you.chen35020192022-05-06 11:30:57 +08002709 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002710
qs.xiong9fbf74e2023-03-28 13:38:22 +08002711 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2712 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002713 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002714 return -1;
2715 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002716 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2717 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002718 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2719 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002720 }
2721
you.chen35020192022-05-06 11:30:57 +08002722 CHECK_WPA_CTRL(CTRL_AP);
2723
qs.xiong9fbf74e2023-03-28 13:38:22 +08002724 if (auth == LYNQ_WIFI_AUTH_WEP)
2725 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002726 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002727 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2728 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2729 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2730 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2731 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002732 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2733 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002734 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 +08002735 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2736 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2737 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002738 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2739 {
2740
qs.xiongc8d92a62023-03-29 17:36:14 +08002741 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 +08002742 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002743 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002744 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2745 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2746
2747 }
2748 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002749 {
2750 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002751 return -1;
2752 }
you.chen35020192022-05-06 11:30:57 +08002753
you.chen35020192022-05-06 11:30:57 +08002754 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002755
qs.xiong9fbf74e2023-03-28 13:38:22 +08002756 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002757}
2758
you.chen35020192022-05-06 11:30:57 +08002759int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002760{
you.chen35020192022-05-06 11:30:57 +08002761 FILE * fp;
2762 int len, ret;
2763 int count, index;
2764 char *split_lines[128] = {0};
2765 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002766 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002767
you.chen35020192022-05-06 11:30:57 +08002768 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002769
you.chen35020192022-05-06 11:30:57 +08002770 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2771// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002772 if (NULL == fp)
2773 {
2774 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002775 return -1;
2776 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002777
you.chen35020192022-05-06 11:30:57 +08002778 buff = alloca(MAX_RET);
2779 fseek(fp, 0, SEEK_SET);
2780 len = fread(buff, 1, MAX_RET, fp);
2781 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002782
qs.xiong9fbf74e2023-03-28 13:38:22 +08002783 for(index=0; index < len; index ++)
2784 {
2785 if (memcmp(buff + index, "network={", 9) != 0)
2786 {
you.chen35020192022-05-06 11:30:57 +08002787 continue;
2788 }
2789 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002790 for (; index < len; index ++ )
2791 {
2792 if (buff[index] != '}')
2793 {
you.chen35020192022-05-06 11:30:57 +08002794 continue;
2795 }
2796 buff[index] = '\0';
2797 break;
2798 }
2799 len = buff + index - p;
2800 }
2801
2802 count = lynq_split(p, len, '\n', split_lines);
2803
2804 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002805 for(index=0; index < count; index++)
2806 {
you.chen35020192022-05-06 11:30:57 +08002807 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002808 if (p != NULL)
2809 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002810 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002811 if (*p == '\"')
2812 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002813 p++;
2814 }
you.chen35020192022-05-06 11:30:57 +08002815 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002816 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2817 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002818 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002819 if (*p == '\"')
2820 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002821 p++;
2822 }
2823 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002824 else
2825 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002826 continue;
you.chen35020192022-05-06 11:30:57 +08002827 }
2828
2829 strcpy(password, p);
2830
qs.xiong9fbf74e2023-03-28 13:38:22 +08002831 while(*password != '\0')
2832 {
2833 if (*password == '\"')
2834 {
you.chen35020192022-05-06 11:30:57 +08002835 *password = '\0';
2836 break;
2837 }
2838 password++;
2839 }
2840 ret = 0;
2841 break;
2842 } //end for(index=0; index < count; index++)
2843
2844 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002845}
2846
you.chen35020192022-05-06 11:30:57 +08002847static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2848 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002849 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002850
qs.xiong9fbf74e2023-03-28 13:38:22 +08002851 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2852 {
you.chen35020192022-05-06 11:30:57 +08002853 return -1;
2854 }
2855
2856 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002857
qs.xiong9fbf74e2023-03-28 13:38:22 +08002858 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2859 {
2860 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002861 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002862 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002863 {
you.chena6cd55a2022-05-08 12:20:18 +08002864 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002865 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002866 }
you.chen70f377f2023-04-14 18:17:09 +08002867 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2868 {
2869 return 0;
2870 }
you.chena6cd55a2022-05-08 12:20:18 +08002871 }
2872 }
you.chen70f377f2023-04-14 18:17:09 +08002873 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2874 {
2875 return 0;
2876 }
2877
qs.xiong9fbf74e2023-03-28 13:38:22 +08002878 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2879 {
you.chen70f377f2023-04-14 18:17:09 +08002880 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002881 return -1;
2882 }
2883 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2884 {
2885
you.chen70f377f2023-04-14 18:17:09 +08002886 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2887 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2889 {
2890
2891 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002892 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002893 }else
2894 {
you.chen70f377f2023-04-14 18:17:09 +08002895 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002896 *auth = -1;
2897 return -1;
2898 }
you.chen35020192022-05-06 11:30:57 +08002899 return 0;
2900}
2901
2902int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002903{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002904 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002905 int pass_len, net_no, count, index;
2906 char lynq_tmp_cmd[300]={0};
2907 int net_no_list[128];
2908 lynq_wifi_auth_s net_auth;
2909 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002910 if(pass_len < 8 || pass_len >= 64)
2911 {
2912 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002913 return -1;
2914 }
2915
2916 CHECK_IDX(idx, CTRL_STA);
2917
2918 net_no = -1;
2919 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2920
qs.xiong9fbf74e2023-03-28 13:38:22 +08002921 for (index=0; index < count; index++)
2922 {
you.chen35020192022-05-06 11:30:57 +08002923 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002924 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2925 {
you.chen35020192022-05-06 11:30:57 +08002926 net_no = net_no_list[index];
2927 break;
2928 }
2929 }
2930
qs.xiong9fbf74e2023-03-28 13:38:22 +08002931 if (net_no < 0)
2932 {
you.chen35020192022-05-06 11:30:57 +08002933 return -1;
2934 }
2935
2936 CHECK_WPA_CTRL(CTRL_STA);
2937
2938 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2939
2940 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2941 DO_OK_FAIL_REQUEST(cmd_save_config);
2942
2943 return 0;
2944}
2945
2946int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2947
2948 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002949 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002950 int count, index;
2951 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002952 char *buff, *p, *ssid, *ssid_end_flag;
2953 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002954 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002955
you.chen755332b2022-08-06 16:59:10 +08002956 network_len = 0;
2957 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002958 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002959
you.chen35020192022-05-06 11:30:57 +08002960 CHECK_IDX(idx, CTRL_STA);
2961
qs.xiong9fbf74e2023-03-28 13:38:22 +08002962 if (NULL == password)
2963 {
2964 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002965 return -1;
2966 }
2967
you.chen35020192022-05-06 11:30:57 +08002968 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002969 if (NULL == fp)
2970 {
2971 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002972 return -1;
2973 }
2974
you.chen6d247052023-06-01 16:39:54 +08002975 fseek(fp, 0, SEEK_END);
2976 len = ftell(fp);
2977 buff = malloc(len + 1);
2978
2979 if (buff == NULL)
2980 {
2981 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2982 return -1;
2983 }
2984
you.chen35020192022-05-06 11:30:57 +08002985 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002986 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002987 fclose(fp);
2988
qs.xiong9fbf74e2023-03-28 13:38:22 +08002989 for(index=0; index < len; index ++)
2990 {
2991 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002992 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002993 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002994 {
you.chen35020192022-05-06 11:30:57 +08002995 continue;
2996 }
you.chen6d247052023-06-01 16:39:54 +08002997 p = buff + index + 9;
2998 for (; index < len; index ++ )
2999 {
3000 if (buff[index] != '}')
3001 {
3002 continue;
3003 }
3004 buff[index] = '\0';
3005 break;
3006 }
3007 network_len = buff + index - p;
3008 break;
you.chen35020192022-05-06 11:30:57 +08003009 }
3010
qs.xiongb3f26af2023-02-17 18:41:07 +08003011 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08003012 {
3013 if (buff != NULL)
3014 {
3015 free(buff);
3016 }
3017
qs.xiongb3f26af2023-02-17 18:41:07 +08003018 return -1;
you.chen6d247052023-06-01 16:39:54 +08003019 }
qs.xiongb3f26af2023-02-17 18:41:07 +08003020
you.chend2fef3f2023-02-13 10:50:35 +08003021 ssid = strstr(p, "ssid=");
3022 if (ssid != NULL) {
3023 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003024 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08003025 {
you.chend2fef3f2023-02-13 10:50:35 +08003026 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3027 break;
3028 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003029 else
you.chen6d247052023-06-01 16:39:54 +08003030 {
you.chend2fef3f2023-02-13 10:50:35 +08003031 ssid_end_flag = strstr(ssid, "\n");
3032 if (ssid_end_flag != NULL)
3033 {
3034 ssid_len = (ssid_end_flag - ssid) / 2;
3035 for(i=0; i<ssid_len; i++)
3036 {
3037 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3038 }
3039 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3040 break;
3041 }
3042 }
you.chen35020192022-05-06 11:30:57 +08003043 }
you.chend2fef3f2023-02-13 10:50:35 +08003044
you.chen35020192022-05-06 11:30:57 +08003045 }
3046
qs.xiong9fbf74e2023-03-28 13:38:22 +08003047 if (index >= len || NULL == p || network_len <= 0)
3048 {
you.chen6d247052023-06-01 16:39:54 +08003049 if (buff != NULL)
3050 {
3051 free(buff);
3052 }
you.chen35020192022-05-06 11:30:57 +08003053 return -1;
3054 }
3055
you.chen755332b2022-08-06 16:59:10 +08003056 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08003057
3058 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003059 for(index=0; index < count; index++)
3060 {
you.chen35020192022-05-06 11:30:57 +08003061 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003062 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08003063 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003064 p += 4;
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 }
you.chen35020192022-05-06 11:30:57 +08003069 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003070 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08003071 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003072 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003073 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08003074 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003075 p++;
3076 }
3077 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003078 else
you.chen6d247052023-06-01 16:39:54 +08003079 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003080 continue;
you.chen35020192022-05-06 11:30:57 +08003081 }
3082
qs.xiong13673462023-02-21 19:12:54 +08003083 if (*p == '\"')
3084 p++;
3085 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08003086
qs.xiong13673462023-02-21 19:12:54 +08003087 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003088 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08003089 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003090 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08003091 {
you.chen35020192022-05-06 11:30:57 +08003092 *password = '\0';
3093 break;
3094 }
3095 password++;
3096 }
3097 ret = 0;
3098 break;
3099 } //end for(index=0; index < count; index++)
3100
you.chen6d247052023-06-01 16:39:54 +08003101 if (buff != NULL)
3102 {
3103 free(buff);
3104 }
3105
you.chen35020192022-05-06 11:30:57 +08003106 return ret;
3107}
3108
3109static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3110{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003111 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003112
qs.xiong9fbf74e2023-03-28 13:38:22 +08003113 if (sta_ssid == NULL)
3114 {
3115 RLOGE("sta_ssid is null\n");
3116 return -1;
you.chen35020192022-05-06 11:30:57 +08003117 }
3118
qs.xiong9fbf74e2023-03-28 13:38:22 +08003119 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003120
3121 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3122
3123 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3124// DO_OK_FAIL_REQUEST(cmd_save_config);
3125
qs.xiong9fbf74e2023-03-28 13:38:22 +08003126 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003127
3128}
3129
you.chen35020192022-05-06 11:30:57 +08003130static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003131{
you.chen35020192022-05-06 11:30:57 +08003132 char lynq_disable_cmd[128]={0};
3133 char lynq_select_cmd[128]={0};
3134
3135 CHECK_WPA_CTRL(CTRL_STA);
3136
qs.xiong9fbf74e2023-03-28 13:38:22 +08003137 if (save != 0)
3138 {
you.chenc29444e2022-06-07 18:01:16 +08003139 if (start_flag != 0)
3140 {
3141 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3142 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3143 }
3144 else
3145 {
3146 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3147 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3148 }
you.chen35020192022-05-06 11:30:57 +08003149 DO_OK_FAIL_REQUEST(cmd_save_config);
3150 }
3151
qs.xiong9fbf74e2023-03-28 13:38:22 +08003152 if (start_flag == 0)
3153 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003154 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003155 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3156 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003157 else
3158 {
you.chen35020192022-05-06 11:30:57 +08003159 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3160 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3161 }
3162
3163 return 0;
3164}
3165
3166int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3167{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003168 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003169 CHECK_IDX(idx, CTRL_STA);
3170
you.chen6c2dd9c2022-05-16 17:55:28 +08003171 curr_status_info curr_state;
3172 ap_info_s ap_info;
3173 curr_state.ap = &ap_info;
3174 curr_state.state = NULL;
3175
qs.xiong9fbf74e2023-03-28 13:38:22 +08003176 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3177 {
you.chend2fef3f2023-02-13 10:50:35 +08003178 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003179 return 0;
3180 }
3181
3182 return -1;
you.chen35020192022-05-06 11:30:57 +08003183}
3184
3185int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3186{
qs.xiong5d716d22023-09-20 20:08:39 +08003187 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003188 scan_info_s *scan_list = NULL;
3189 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003190 int scan_len=0;
3191 int save_len=0;
3192 int best_index = -1;
3193 int best_scan_index = -1;
3194 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003195 int i, j, ret;
3196
3197 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003198
3199 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003200 if (info == NULL)
3201 {
you.chen35020192022-05-06 11:30:57 +08003202 return -1;
3203 }
3204
3205 curr_status_info curr_state;
3206 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003207 char status[64];
you.chen35020192022-05-06 11:30:57 +08003208
you.chen9ac66392022-08-06 17:01:16 +08003209 memset(&ap_info, 0, sizeof (ap_info));
3210 memset(status, 0, sizeof (status));
3211
3212 curr_state.ap = &ap_info;
3213 curr_state.state = status;
3214
qs.xiong9fbf74e2023-03-28 13:38:22 +08003215 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3216 {
you.chen35020192022-05-06 11:30:57 +08003217 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003218 if (strcmp(status, STATE_COMPLETED) == 0)
3219 {
3220 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003221 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003222 }
3223 else
3224 {
3225 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003226 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003227 }
you.chen593621d2023-04-27 17:52:44 +08003228 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003229 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003230 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003231 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003232 return 0;
3233 }
3234
you.chen9ac66392022-08-06 17:01:16 +08003235 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003236 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003237 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3238 {
you.chen9ac66392022-08-06 17:01:16 +08003239 if (NULL != scan_list)
3240 {
3241 free(scan_list);
3242 }
you.chen35020192022-05-06 11:30:57 +08003243 return -1;
3244 }
3245
qs.xiong9fbf74e2023-03-28 13:38:22 +08003246 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3247 {
you.chen9ac66392022-08-06 17:01:16 +08003248 if (NULL != scan_list)
3249 {
3250 free(scan_list);
3251 }
3252 if (NULL != save_list)
3253 {
3254 free(save_list);
3255 }
you.chen35020192022-05-06 11:30:57 +08003256 return -1;
3257 }
3258
qs.xiong9fbf74e2023-03-28 13:38:22 +08003259 for (i=0; i < save_len; i++)
3260 {
3261 for (j=0; j < scan_len; j++)
3262 {
you.chen35020192022-05-06 11:30:57 +08003263 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003264 && save_list[i].base_info.auth == scan_list[j].auth)
3265 {
3266 if (best_rssi == 0)
3267 {
you.chen9ac66392022-08-06 17:01:16 +08003268 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003269 best_rssi = scan_list[j].rssi;
3270 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003271 else if (best_rssi > scan_list[j].rssi)
3272 {
you.chen35020192022-05-06 11:30:57 +08003273 best_index = i;
3274 best_scan_index = j;
3275 best_rssi = scan_list[j].rssi;
3276 }
you.chend2fef3f2023-02-13 10:50:35 +08003277 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 +08003278 break;
3279 }
3280 }
3281 }
3282
qs.xiong9fbf74e2023-03-28 13:38:22 +08003283 if (best_index >= 0)
3284 {
you.chen35020192022-05-06 11:30:57 +08003285 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003286 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 +08003287 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003288 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003289 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003290 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003291 }
3292
you.chen9ac66392022-08-06 17:01:16 +08003293 if (NULL != scan_list)
3294 {
3295 free(scan_list);
3296 }
3297 if (NULL != save_list)
3298 {
3299 free(save_list);
3300 }
3301
qs.xiong5d716d22023-09-20 20:08:39 +08003302 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003303 return ret;
you.chen35020192022-05-06 11:30:57 +08003304}
3305
3306static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3307{
qs.xiongc8d92a62023-03-29 17:36:14 +08003308 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003309 char lynq_ket_mgmt_cmd[64]={0};
3310 char lynq_pairwise_cmd[64]={0};
3311 char lynq_psk_cmd[64]={0};
3312
3313 CHECK_WPA_CTRL(CTRL_STA);
3314
qs.xiong9fbf74e2023-03-28 13:38:22 +08003315 switch(auth)
3316 {
3317 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003318 {
3319 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003320
you.chen35020192022-05-06 11:30:57 +08003321 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003322// DO_OK_FAIL_REQUEST(cmd_save_config);
3323 break;
3324 }
3325 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003326 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003327 {
3328 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3329 {
you.chen35020192022-05-06 11:30:57 +08003330 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3331 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003332 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3333 {
you.chena6cd55a2022-05-08 12:20:18 +08003334 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003335 }
3336 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3337 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003338
you.chen35020192022-05-06 11:30:57 +08003339 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3340 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3341 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003342
qs.xiong9fbf74e2023-03-28 13:38:22 +08003343 if (password != NULL)
3344 {
you.chen35020192022-05-06 11:30:57 +08003345 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3346 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003347 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003348 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003349
you.chen35020192022-05-06 11:30:57 +08003350// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003351 break;
3352 }
3353 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3354 {
qs.xiong3e506812023-04-06 11:08:48 +08003355 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003356 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003357 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3358 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3359
qs.xiong3e506812023-04-06 11:08:48 +08003360 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003361 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3362 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3363 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3364
3365 break;
3366 }
3367 case LYNQ_WIFI_AUTH_WPA3_PSK:
3368 {
qs.xiong3e506812023-04-06 11:08:48 +08003369 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003370 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003371 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3372 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3373
qs.xiongb37f8c42023-09-13 21:21:58 +08003374 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003375 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3376 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3377 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3378
3379 break;
3380 }
3381 default:
3382 return -1;
you.chen35020192022-05-06 11:30:57 +08003383 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003384
qs.xiong9fbf74e2023-03-28 13:38:22 +08003385 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003386}
qs.xiong7a105ce2022-03-02 09:43:11 -05003387
you.chen35020192022-05-06 11:30:57 +08003388static int inner_get_curr_net_no(int interface) {
3389 curr_status_info curr_state;
3390 curr_state.ap = NULL;
3391 curr_state.state = NULL;
3392
qs.xiong9fbf74e2023-03-28 13:38:22 +08003393 if (0 != inner_get_status_info(interface, &curr_state))
3394 {
you.chen35020192022-05-06 11:30:57 +08003395 return -1;
3396 }
3397
3398 return curr_state.net_no;
3399}
3400
3401int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003402{
you.chen35020192022-05-06 11:30:57 +08003403 int net_no;
3404 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003405
you.chen35020192022-05-06 11:30:57 +08003406 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003407
qs.xiong9fbf74e2023-03-28 13:38:22 +08003408 if (net_no < 0)
3409 {
you.chen35020192022-05-06 11:30:57 +08003410 return -1;
3411 }
3412
3413 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003414}
3415
you.chenb95401e2023-05-12 19:39:06 +08003416int 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 -05003417{
you.chen35020192022-05-06 11:30:57 +08003418 int count, net_no, index;
3419 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003420 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003421 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003422 curr_status_info curr_state;
3423 ap_info_s ap_info;
3424 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003425
qs.xiong9fbf74e2023-03-28 13:38:22 +08003426 if (ssid == NULL || *ssid == '\0')
3427 {
3428 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003429 return -1;
3430 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003431
qs.xiong9fbf74e2023-03-28 13:38:22 +08003432 if (LYNQ_WIFI_AUTH_OPEN != auth)
3433 {
3434 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003435 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003436 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003437 return -1;
3438 }
3439 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003440
you.chen70f377f2023-04-14 18:17:09 +08003441
3442 pthread_mutex_lock(&s_global_check_mutex);
3443 if (s_sta_status != INNER_STA_STATUS_INIT)
3444 {
3445 s_sta_status = INNER_STA_STATUS_CANCEL;
3446 pthread_cond_signal(&s_global_check_cond);
3447 }
3448 pthread_mutex_unlock(&s_global_check_mutex);
3449
you.chen35020192022-05-06 11:30:57 +08003450 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003451 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003452
3453 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003454 memset(&ap_info, 0, sizeof (ap_info));
3455 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003456
you.chen70f377f2023-04-14 18:17:09 +08003457 curr_state.ap = &ap_info;
3458 curr_state.state = status;
3459
3460 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003461 {
you.chen70f377f2023-04-14 18:17:09 +08003462 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3463 {
3464 net_no = curr_state.net_no;
3465 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3466 && strcmp(ap_info.psw, psw) == 0)
3467 {
3468 RLOGD("already connected\n");
3469
3470 pthread_mutex_lock(&s_global_check_mutex);
3471 s_sta_status = INNER_STA_STATUS_CONNECTED;
3472 pthread_cond_signal(&s_global_check_cond);
3473 pthread_mutex_unlock(&s_global_check_mutex);
3474 return 0;
3475 }
you.chen35020192022-05-06 11:30:57 +08003476 }
3477 }
3478
you.chen70f377f2023-04-14 18:17:09 +08003479 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003480 {
you.chen70f377f2023-04-14 18:17:09 +08003481 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3482
3483 for (index=0; index < count; index++)
3484 {
3485 net_auth = -1;
3486 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3487 {
3488 net_no = net_no_list[index];
3489 break;
3490 }
you.chen35020192022-05-06 11:30:57 +08003491 }
3492
you.chen70f377f2023-04-14 18:17:09 +08003493 if (net_no < 0)
3494 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003495 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3496 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3497 {
3498 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3499 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3500 DO_OK_FAIL_REQUEST(rm_net_cmd);
3501 }
you.chen70f377f2023-04-14 18:17:09 +08003502 net_no = lynq_add_network(CTRL_STA);
3503 if (net_no == -1)
3504 {
3505 return -1;
3506 }
3507
3508 RLOGD("net no is %d\n", net_no);
3509 if (0 != inner_set_sta_ssid(net_no, ssid))
3510 {
3511 return -1;
3512 }
you.chen35020192022-05-06 11:30:57 +08003513 }
3514 }
3515
qs.xiong9fbf74e2023-03-28 13:38:22 +08003516 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3517 {
you.chen35020192022-05-06 11:30:57 +08003518 return -1;
3519 }
3520
you.chen70f377f2023-04-14 18:17:09 +08003521
3522 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003523 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003524 usleep(200*1000);
3525
3526 ret = inner_sta_start_stop(net_no, 1, 1);
3527
3528 pthread_mutex_lock(&s_global_check_mutex);
3529 s_sta_status = INNER_STA_STATUS_CONNECTING;
3530 strcpy(s_sta_current_connecting_ssid, ssid);
3531 struct timeval now;
3532 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003533 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003534 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3535 pthread_cond_signal(&s_global_check_cond);
3536 pthread_mutex_unlock(&s_global_check_mutex);
3537 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003538}
3539
you.chenb95401e2023-05-12 19:39:06 +08003540int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3541{
3542 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3543}
3544
you.chen35020192022-05-06 11:30:57 +08003545int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003546{
you.chen35020192022-05-06 11:30:57 +08003547 ap_info_s ap;
3548 curr_status_info curr_state;
3549 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003550
qs.xiong9fbf74e2023-03-28 13:38:22 +08003551 if (ssid == NULL || *ssid == '\0')
3552 {
3553 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003554 return -1;
3555 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003556
you.chen35020192022-05-06 11:30:57 +08003557 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003558
you.chen35020192022-05-06 11:30:57 +08003559 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003560 curr_state.state = NULL;
3561
qs.xiong9fbf74e2023-03-28 13:38:22 +08003562 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3563 {
you.chen35020192022-05-06 11:30:57 +08003564 return 0;
3565 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003566
qs.xiong9fbf74e2023-03-28 13:38:22 +08003567 if (strcmp(ap.ap_ssid, ssid) != 0)
3568 {
you.chen35020192022-05-06 11:30:57 +08003569 return 0;
3570 }
3571
you.chen70f377f2023-04-14 18:17:09 +08003572 pthread_mutex_lock(&s_global_check_mutex);
3573 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3574 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003575 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003576}
qs.xiong97fa59b2022-04-07 05:41:29 -04003577
qs.xiongc93bf2b2023-08-25 10:22:08 +08003578int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3579{
3580 ap_info_s ap;
3581 curr_status_info curr_state;
3582 ap.ap_ssid[0] = '\0';
3583
3584 if (ssid == NULL || *ssid == '\0')
3585 {
3586 RLOGE("input ssid is NULL\n");
3587 return -1;
3588 }
3589
3590 CHECK_IDX(idx, CTRL_STA);
3591
3592
3593 curr_state.ap = &ap;
3594 curr_state.state = NULL;
3595
3596 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3597 {
3598 return 0;
3599 }
3600
3601 if (strcmp(ap.ap_ssid, ssid) != 0)
3602 {
3603 return 0;
3604 }
3605
3606 pthread_mutex_lock(&s_global_check_mutex);
3607 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003608 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3609 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003610 pthread_mutex_unlock(&s_global_check_mutex);
3611 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3612
3613}
3614
3615
you.chena6cd55a2022-05-08 12:20:18 +08003616int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3617{
qs.xiongb37f8c42023-09-13 21:21:58 +08003618
qs.xiongad2f89d2023-01-18 13:17:41 +08003619 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3620 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003621
you.chen35020192022-05-06 11:30:57 +08003622 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003623 CHECK_WPA_CTRL(CTRL_STA);
3624
you.chenc9928582023-04-24 15:39:37 +08003625 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3626 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003627 {
you.chenc9928582023-04-24 15:39:37 +08003628 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003629 return -1;
3630 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003631
qs.xiongad2f89d2023-01-18 13:17:41 +08003632 system(lynq_enable_sta_cmd);
3633 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003634 pthread_mutex_lock(&s_global_check_mutex);
3635 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3636 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003637 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003638}
3639
you.chen6d247052023-06-01 16:39:54 +08003640static int inner_get_status_info_state (int interface, char *state) {
3641 curr_status_info curr_state;
3642 curr_state.ap = NULL;
3643 curr_state.state = state;
3644 return inner_get_status_info(interface, &curr_state);
3645}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003646
3647int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3648{
3649
qs.xiongb37f8c42023-09-13 21:21:58 +08003650 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3651 int tmp_open_idx[128];
3652 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003653
qs.xiongb37f8c42023-09-13 21:21:58 +08003654 pthread_mutex_lock(&s_global_check_mutex);
3655 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3656 pthread_mutex_unlock(&s_global_check_mutex);
3657 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003658 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003659 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003660 }
3661
qs.xiongb37f8c42023-09-13 21:21:58 +08003662 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003663 return 0;
3664}
3665
3666
you.chen35020192022-05-06 11:30:57 +08003667int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003668{
qs.xiongad2f89d2023-01-18 13:17:41 +08003669// char lynq_disable_network_cmd[MAX_CMD];
3670// curr_status_info curr_state;
3671// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003672 int i=0;
3673 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003674
you.chen6d247052023-06-01 16:39:54 +08003675// 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 +08003676 CHECK_IDX(idx, CTRL_STA);
3677 CHECK_WPA_CTRL(CTRL_STA);
3678
you.chen6d247052023-06-01 16:39:54 +08003679// system(lynq_disable_sta_cmd);
3680 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003681 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003682
3683 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3684 if (ret != 0)
3685 {
3686 RLOGE("lynq_wifi_ap_start excute script fail");
3687 return -1;
3688 }
3689
you.chen6d247052023-06-01 16:39:54 +08003690 for (i=0; i < 30; i++) // to check if sta is realy stoped
3691 {
3692 if (inner_get_status_info_state(idx, state) != 0)
3693 {
3694 break;
3695 }
3696
3697 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3698 {
3699 break;
3700 }
3701 RLOGD("lynq_wifi_ap_start curr state %s", state);
3702 usleep(SLEEP_TIME_ON_IDLE);
3703 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003704 pthread_mutex_lock(&s_global_check_mutex);
3705 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3706 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003707 return 0;
3708// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003709}
qs.xiongfcc914b2023-07-06 21:16:20 +08003710int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3711{
3712 char LYNQ_DISABLE_CMD[128]={0};
3713 CHECK_IDX(idx, CTRL_STA);
3714 CHECK_WPA_CTRL(CTRL_STA);
3715 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3716 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3717 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3718 return 0;
3719}
qs.xiong7a105ce2022-03-02 09:43:11 -05003720
you.chen35020192022-05-06 11:30:57 +08003721//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3722// int i, count;
3723// char *p;
3724// const char * FLAG_SSID = "ssid=";
3725// const char * FLAG_SBSID = "bssid=";
3726// const char * FLAG_KEY_MGMT = "key_mgmt=";
3727// const char * FLAG_FREQ = "freq=";
3728// char lynq_sta_cmd[MAX_CMD];
3729// char *split_lines[128] = {0};
3730
3731// CHECK_WPA_CTRL(CTRL_AP);
3732
3733// sprintf(lynq_sta_cmd, "STA %s", bssid);
3734
3735// DO_REQUEST(lynq_sta_cmd);
3736
3737// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3738
3739// for(i=0; i < count; i++) {
3740// p = strstr(split_lines[i], FLAG_SSID);
3741// if (p != NULL) {
3742// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3743// continue;
3744// }
3745// }
3746
3747// lynq_get_interface_ip(idx, ap->ap_ip);
3748// lynq_ap_password_set(idx, ap->psw);
3749
3750// return 0;
3751//}
3752
3753static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3754 curr_status_info curr_state;
3755 curr_state.ap = ap;
3756 curr_state.state = NULL;
3757 return inner_get_status_info(interface, &curr_state);
3758}
3759
3760int 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 -04003761{
qs.xiong5071c802023-09-06 14:04:15 +08003762 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003763 int index, line_count;
3764 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003765 const char *lynq_first_sta_cmd = "STA-FIRST";
3766 char lynq_next_sta_cmd[MAX_CMD];
3767 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003768 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003769
you.chen35020192022-05-06 11:30:57 +08003770 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003771
you.chen35020192022-05-06 11:30:57 +08003772 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003773
you.chenb95401e2023-05-12 19:39:06 +08003774 // ap_info_s * tmp_ap;
3775 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003776 if (ap == NULL || list == NULL || len == NULL)
3777 {
3778 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003779 return -1;
3780 }
3781
you.chenb95401e2023-05-12 19:39:06 +08003782 // ap = &tmp_ap;
3783 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003784 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003785 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003786
you.chenb95401e2023-05-12 19:39:06 +08003787 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003788 {
you.chenb95401e2023-05-12 19:39:06 +08003789 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003790 return -1;
3791 }
3792
3793 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3794 lynq_ap_password_get(idx, (*ap)->psw);
3795
you.chen35020192022-05-06 11:30:57 +08003796 DO_REQUEST(lynq_first_sta_cmd);
3797
3798 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003799 while (reply_len > 0)
3800 {
3801 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003802 {
you.chen35020192022-05-06 11:30:57 +08003803 break;
3804 }
3805 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3806 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3807 strcpy(bssid[index], split_lines[0]);
3808 index++;
3809 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3810 reply_len = MAX_RET;
3811 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003812 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 +08003813 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003814 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003815 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003816 break;
3817 }
3818 }
3819
3820 *len = index;
3821
3822 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003823 for (index=0; index < *len; index++)
3824 {
you.chend2fef3f2023-02-13 10:50:35 +08003825 dev_info = &(*list)[index];
3826 memset(dev_info, 0, sizeof(device_info_s));
3827 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3828 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3829 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3830 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003831 free(bssid[index]);
3832 }
qs.xiong5071c802023-09-06 14:04:15 +08003833 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003834 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003835}
3836
you.chen35020192022-05-06 11:30:57 +08003837int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003838{
you.chen35020192022-05-06 11:30:57 +08003839 int i, count, index, count_words;
3840 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3841 char *split_lines[128] = {0};
3842 char *split_words[128] = {0};
3843 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003844
qs.xiong9fbf74e2023-03-28 13:38:22 +08003845 if (list == NULL || len == NULL)
3846 {
you.chen35020192022-05-06 11:30:57 +08003847 return -1;
3848 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003849
you.chen9ac66392022-08-06 17:01:16 +08003850 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3851 {
3852 usleep(100 * 1000);
3853 }
3854
you.chen35020192022-05-06 11:30:57 +08003855 CHECK_IDX(idx, CTRL_STA);
3856
3857 CHECK_WPA_CTRL(CTRL_STA);
3858
3859 DO_REQUEST(lynq_scan_result_cmd);
3860
3861 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3862 *len = count - 1;
3863 *list = malloc(sizeof (scan_info_s) * *len);
3864
3865 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003866 for (index=0; index <count_words; index++)
3867 {
3868 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003869 }
3870
qs.xiong9fbf74e2023-03-28 13:38:22 +08003871 for(index = 1;index < count; index++)
3872 {
3873 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003874 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003875 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3876 if (count_words < 4)
3877 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003878 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003879 //bssid / frequency / signal level / flags / ssid
3880 p = (*list) + index - 1;
3881 strcpy(p->mac, split_words[0]);
3882 p->band = convert_band_from_freq(atoi(split_words[1]));
3883 p->rssi = -1 * atoi( split_words[2]);
3884 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003885 if (count_words == 4) // ssid hided
3886 {
3887 p->ssid[0] = '\0';
3888 }
3889 else
3890 {
3891 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3892 }
you.chen35020192022-05-06 11:30:57 +08003893 }
3894
3895 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003896}
qs.xiong97fa59b2022-04-07 05:41:29 -04003897
you.chen35020192022-05-06 11:30:57 +08003898int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3899{
3900 int count, net_no, index;
3901 int net_no_list[128];
3902 lynq_wifi_auth_s net_auth;
3903 char lynq_remove_cmd[MAX_CMD];
3904
qs.xiong9fbf74e2023-03-28 13:38:22 +08003905 if (ssid == NULL || *ssid == '\0')
3906 {
3907 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003908 return -1;
3909 }
3910
3911 CHECK_IDX(idx, CTRL_STA);
3912
3913 CHECK_WPA_CTRL(CTRL_STA);
3914
3915 net_no = -1;
3916 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3917
qs.xiong9fbf74e2023-03-28 13:38:22 +08003918 for (index=0; index < count; index++)
3919 {
you.chen35020192022-05-06 11:30:57 +08003920 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003921 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3922 {
you.chen35020192022-05-06 11:30:57 +08003923 net_no = net_no_list[index];
3924 break;
3925 }
3926 }
3927
qs.xiong9fbf74e2023-03-28 13:38:22 +08003928 if (net_no < 0)
3929 {
you.chen35020192022-05-06 11:30:57 +08003930 return 0;
3931 }
3932
3933 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3934
3935 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3936 DO_OK_FAIL_REQUEST(cmd_save_config);
3937
3938 return 0;
3939}
3940
3941int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003942{
you.chend2fef3f2023-02-13 10:50:35 +08003943 int count, index;
you.chen35020192022-05-06 11:30:57 +08003944 int net_no_list[128];
3945 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003946 RLOGD("enter lynq_get_sta_saved_ap api\n");
3947 if (list == NULL || len == NULL)
3948 {
3949 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003950 return -1;
3951 }
3952
3953 CHECK_IDX(idx, CTRL_STA);
3954
3955// CHECK_WPA_CTRL(CTRL_STA);
3956
3957 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003958 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003959
you.chen057aac42023-04-13 14:06:58 +08003960 if (count < 0)
3961 {
3962 RLOGE("list network fail");
3963 return count;
3964 }
3965 else if (count == 0)
3966 {
3967 *list = NULL;
3968 *len = 0;
3969 return 0;
3970 }
3971
you.chen35020192022-05-06 11:30:57 +08003972 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003973 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003974 *len = count;
3975
qs.xiong9fbf74e2023-03-28 13:38:22 +08003976 for (index=0; index < count; index++)
3977 {
you.chen35020192022-05-06 11:30:57 +08003978 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003979 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003980 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003981 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003982 {
you.chen35020192022-05-06 11:30:57 +08003983 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3984 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003985 else
you.chen057aac42023-04-13 14:06:58 +08003986 {
you.chen35020192022-05-06 11:30:57 +08003987 (*list)[index].base_info.band = -1;
3988 }
you.chen057aac42023-04-13 14:06:58 +08003989 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003990 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003991 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003992 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003993 return 0;
3994}
3995
3996int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3997{
qs.xiong20202422023-09-06 18:01:18 +08003998 if ( g_sta_conncet_status_flag != 0 )
3999 {
4000 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08004001 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004002 }
qs.xiongc8d92a62023-03-29 17:36:14 +08004003 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004004 const char *lynq_scan_cmd = "SCAN";
4005
4006 CHECK_IDX(idx, CTRL_STA);
4007
4008 CHECK_WPA_CTRL(CTRL_STA);
4009
you.chen0df3e7e2023-05-10 15:56:26 +08004010 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4011 {
4012 RLOGD("tmp clear scanlist");
4013 system(clean_last_re);
4014 }
you.chen9ac66392022-08-06 17:01:16 +08004015 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004016 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004017 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4018 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004019 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004020 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4021 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004022 g_sta_scan_finish_flag = 1;
4023 return -1;
4024 }
you.chen35020192022-05-06 11:30:57 +08004025
4026 return 0;
4027}
4028
4029int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004030 if (cb == NULL)
4031 {
4032 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004033 return -1;
4034 }
4035
you.chen6d247052023-06-01 16:39:54 +08004036 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004037 g_ap_callback_priv = priv;
4038 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004039 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004040
you.chen6d247052023-06-01 16:39:54 +08004041 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004042 if (g_ap_watcher_pid == 0 )
4043 {
4044 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4045 {
4046 g_ap_watcher_pid = 0;
4047 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4048 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4049 return -1;
4050 }
4051 }
4052
4053 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4054 RLOGD("creat APWatcherTheradProc susccs");
4055
you.chen35020192022-05-06 11:30:57 +08004056 return 0;
4057}
4058
4059int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004060 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 if (g_ap_callback_priv == priv)
4062 {
you.chen35020192022-05-06 11:30:57 +08004063 g_ap_callback_func = NULL;
4064 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004065 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004066 return 0;
4067 }
you.chen6d247052023-06-01 16:39:54 +08004068 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004069 return -1;
4070}
4071
4072int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004073 if (cb == NULL)
4074 {
4075 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004076 return -1;
4077 }
4078
you.chen6d247052023-06-01 16:39:54 +08004079 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004080 g_sta_callback_priv = priv;
4081 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004082 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004083
you.chen6d247052023-06-01 16:39:54 +08004084 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004085 if (g_sta_watcher_pid == 0 ) {
4086 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4087 {
4088 g_sta_watcher_pid = 0;
4089 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4090 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4091 return -1;
4092 }
4093 }
4094
4095 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4096 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004097 return 0;
4098}
4099
4100int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004101 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004102 if (g_sta_callback_priv == priv)
4103 {
you.chen35020192022-05-06 11:30:57 +08004104 g_sta_callback_func = NULL;
4105 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004106 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004107 return 0;
4108 }
you.chen6d247052023-06-01 16:39:54 +08004109 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004110 return -1;
4111}
4112
qs.xiongfcc914b2023-07-06 21:16:20 +08004113int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4114 if (cb == NULL)
4115 {
4116 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4117 return -1;
4118 }
4119 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4120 g_sta_auto_callback_priv = priv;
4121 g_sta_auto_callback_func = cb;
4122 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4123 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4124 if (g_sta_auto_watcher_pid == 0 ) {
4125 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4126 {
4127 g_sta_auto_watcher_pid = 0;
4128 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4129 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4130 return -1;
4131 }
4132 }
4133 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4134 RLOGD("creat STAWatcherTheradProc susccs");
4135 return 0;
4136}
4137int lynq_unreg_sta_auto_event_callback(void * priv) {
4138 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4139 if (g_sta_auto_callback_priv == priv)
4140 {
4141 g_sta_auto_watcher_stop_flag = 1;
4142 if (g_sta_auto_watcher_pid != 0)
4143 {
4144 pthread_join(g_sta_auto_watcher_pid, NULL);
4145 }
4146 g_sta_auto_watcher_pid = 0;
4147 g_sta_auto_callback_func = NULL;
4148 g_sta_auto_callback_priv = NULL;
4149 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4150 return 0;
4151 }
4152 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4153 return -1;
4154}
you.chen35020192022-05-06 11:30:57 +08004155int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4156{
4157 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004158 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004159 CHECK_IDX(idx, CTRL_AP);
4160
qs.xiong9fbf74e2023-03-28 13:38:22 +08004161 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4162 {
you.chen35020192022-05-06 11:30:57 +08004163 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4164 return 0;
4165 }
4166
qs.xiong9fbf74e2023-03-28 13:38:22 +08004167 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4168 {
you.chen35020192022-05-06 11:30:57 +08004169 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4170 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004171 else
4172 {
you.chen35020192022-05-06 11:30:57 +08004173 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4174 }
4175
4176 return 0;
4177}
4178
4179int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4180 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004181 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004182 CHECK_IDX(idx, CTRL_STA);
4183
qs.xiong9fbf74e2023-03-28 13:38:22 +08004184 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4185 {
you.chen35020192022-05-06 11:30:57 +08004186 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4187 return 0;
4188 }
4189
qs.xiong9fbf74e2023-03-28 13:38:22 +08004190 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4191 {
you.chen35020192022-05-06 11:30:57 +08004192 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4193 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004194 else
4195 {
you.chen35020192022-05-06 11:30:57 +08004196 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4197 }
4198
4199 return 0;
4200}
4201
4202int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4203// CHECK_IDX(idx, CTRL_AP);
4204// int ret = 0;
4205// size_t reply_len = MAX_RET;
4206// char cmd_reply[MAX_RET]={0};
4207// const char * cmd_str = "GET country";
4208// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4209// do{
4210// if (NULL == s_lynq_wpa_ctrl) {
4211// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4212// if (NULL == s_lynq_wpa_ctrl ) {
4213// printf("wpa_ctrl_open fail\n");
4214// return -1;
4215// }
4216// }
4217// }while(0);
4218
4219// do {
4220// reply_len = MAX_RET;
4221// cmd_reply[0] = '\0';
4222// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004223// 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 +08004224// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004225// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004226// return ret;
4227// }
4228// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004229// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004230// }while(0);
4231
4232 FILE *fp;
4233 size_t i = 0;
4234 char lynq_cmd_ret[MAX_RET]={0};
4235
4236// CHECK_IDX(idx, CTRL_AP);
4237
4238 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004239 {
4240 perror("popen error!");
4241 return -1;
4242 }
you.chen35020192022-05-06 11:30:57 +08004243 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4244 {
4245 perror("fread fail!");
4246 return -1;
4247 }
4248
qs.xiong9fbf74e2023-03-28 13:38:22 +08004249 for(i=0; i < strlen(lynq_cmd_ret); i++)
4250 {
4251 if (lynq_cmd_ret[i] == ' ')
4252 {
you.chen35020192022-05-06 11:30:57 +08004253 lynq_cmd_ret[i] = '\0';
4254 break;
4255 }
4256 }
4257
4258 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004259 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004260
4261 int ret=pclose(fp);
4262 if(ret==-1)
4263 {
4264 perror("close file faild");
4265 }
4266
4267 return 0;
4268}
4269
qs.xiong44fac672023-08-29 16:15:55 +08004270
you.chen705a7ef2023-06-01 22:06:45 +08004271static int check_and_init_uci_config(char * country_code)
4272{
4273 FILE * fp;
4274 int is_different = 0;
4275 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4276 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4277 const char * commit_uci_cmd ="uci commit";
4278 char set_country_cmd[MAX_CMD];
4279 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004280
you.chen705a7ef2023-06-01 22:06:45 +08004281 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004282
you.chen705a7ef2023-06-01 22:06:45 +08004283 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004284 {
you.chen705a7ef2023-06-01 22:06:45 +08004285 if (0 != system(create_uci_cmd))
4286 {
4287 RLOGE("creat_uci_cmd fail");
4288 return -1;
4289 }
4290 is_different = 1;
4291 }
4292
4293 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4294 {
4295 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004296 return -1;
4297 }
4298
you.chen705a7ef2023-06-01 22:06:45 +08004299 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004300 {
you.chen705a7ef2023-06-01 22:06:45 +08004301 RLOGE("fread fail!");
4302 fclose(fp);
4303 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004304 }
4305
you.chen705a7ef2023-06-01 22:06:45 +08004306 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4307 {
qs.xiong44fac672023-08-29 16:15:55 +08004308 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 +08004309 is_different = 1;
4310 }
4311
4312 fclose(fp);
4313
4314 if (is_different)
4315 {
4316 if ( 0 != system(set_country_cmd))
4317 {
4318 RLOGE("set_country_cmd fail");
4319 return -1;
4320 }
4321 if (0 != system(commit_uci_cmd))
4322 {
4323 RLOGE("commmit fail");
4324 }
4325 }
4326
4327 return is_different;
4328}
4329
4330int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4331 char check_current_code[10];
4332 const char * support_country[] = {"CN", "EU"};
4333
4334 int ret,is_different, i, cc_count;
4335
4336 if (country_code == NULL || country_code[0] == '\0')
4337 {
4338 RLOGE("bad country code\n");
4339 return -1;
4340 }
4341
4342 cc_count = sizeof (support_country) / sizeof (char*);
4343 for(i=0; i < cc_count; i++)
4344 {
4345 if (strcmp(support_country[i], country_code) == 0)
4346 {
4347 break;
4348 }
4349 }
4350
4351 if (i >= cc_count)
4352 {
4353 RLOGE("unspported country code %s\n", country_code);
4354 return -1;
4355 }
4356
4357 is_different = check_and_init_uci_config(country_code);
4358 if( is_different < 0 )
4359 {
4360 RLOGE("init set uci fail\n");
4361 return -1;
4362 }
4363
4364 ret = lynq_get_country_code(idx,check_current_code);
4365 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4366 {
4367 ret = lynq_wifi_disable();
4368 if(ret != 0 )
4369 {
4370 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4371 return -1;
4372 }
4373 }
4374
4375 return 0;
you.chen35020192022-05-06 11:30:57 +08004376}
4377
4378int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4379{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004380 RLOGD("enter lynq_get_connect_ap_mac\n");
4381 if (mac == NULL)
4382 {
4383 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004384 return -1;
4385 }
4386
4387 CHECK_IDX(idx, CTRL_STA);
4388 ap_info_s ap;
4389 ap.ap_mac[0] = '\0';
4390
qs.xiong9fbf74e2023-03-28 13:38:22 +08004391 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4392 {
you.chen35020192022-05-06 11:30:57 +08004393 return -1;
4394 }
4395 strcpy(mac, ap.ap_mac);
4396
4397 return 0;
4398}
4399
4400int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4401{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004402 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004403 struct ifaddrs *ifaddr_header, *ifaddr;
4404 struct in_addr * ifa;
4405 const char * ifaName = "wlan0";
4406 if (ip == NULL)
4407 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004408 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004409 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004410 }
you.chenf58b3c92022-06-21 16:53:48 +08004411
qs.xiong9fbf74e2023-03-28 13:38:22 +08004412 if (idx == 1)
4413 {
you.chen0df3e7e2023-05-10 15:56:26 +08004414 ifaName = inner_get_ap_interface_name();
4415 if (ifaName == NULL)
4416 {
4417 RLOGE("[lynq_get_interface_ip] ap name get fail");
4418 return -1;
4419 }
you.chen9ac66392022-08-06 17:01:16 +08004420 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004421 else if (idx != 0)
4422 {
you.chen35020192022-05-06 11:30:57 +08004423 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004424 }
you.chen35020192022-05-06 11:30:57 +08004425
you.chen9ac66392022-08-06 17:01:16 +08004426 if (getifaddrs(&ifaddr_header) == -1)
4427 {
you.chen35020192022-05-06 11:30:57 +08004428 perror("getifaddrs");
4429 return -1;
4430 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004431 }
you.chen35020192022-05-06 11:30:57 +08004432
4433
you.chen9ac66392022-08-06 17:01:16 +08004434 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4435 {
4436 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004437 continue;
you.chen9ac66392022-08-06 17:01:16 +08004438 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4439 {
4440 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4441 {
4442 // is a valid IP4 Address
4443 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4444 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004445 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004446 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004447 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004448 return 0;
4449 }
4450 }
4451 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004452 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004453 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004454 return -1;
you.chen35020192022-05-06 11:30:57 +08004455}
4456
4457int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4458{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004459 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004460 int count;
4461 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004462 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004463 char *split_words[128] = {0};
4464 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4465
4466 CHECK_WPA_CTRL(idx);
4467
4468 DO_REQUEST(lynq_get_mac_cmd);
4469
qs.xiong9fbf74e2023-03-28 13:38:22 +08004470 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4471 {
4472 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004473 return -1;
4474 }
4475
4476 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4477
qs.xiong9fbf74e2023-03-28 13:38:22 +08004478 if (count < 2)
4479 {
you.chen35020192022-05-06 11:30:57 +08004480 return -1;
4481 }
4482
qs.xiong9fbf74e2023-03-28 13:38:22 +08004483 for (i=0; i < strlen(split_words[1]); i++ )
4484 {
4485 if (split_words[1][i] != ' ')
4486 {
you.chen35020192022-05-06 11:30:57 +08004487 break;
4488 }
4489 }
4490
qs.xiongdd6e44c2023-08-08 15:02:53 +08004491 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004492
4493 return 0;
4494}
4495
4496int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4497{
4498// int count;
4499// char *split_words[128] = {0};
4500// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4501
4502// if (rssi == NULL) {
4503// return -1;
4504// }
4505
4506// CHECK_IDX(idx, CTRL_STA);
4507
4508// CHECK_WPA_CTRL(CTRL_STA);
4509
4510// DO_REQUEST(lynq_get_rssi_cmd);
4511
4512// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4513// return -1;
4514// }
4515
4516// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4517
4518// if (count < 2) {
4519// return -1;
4520// }
4521
4522// *rssi = atoi(split_words[1]) * -1;
4523
you.chen35020192022-05-06 11:30:57 +08004524 char lynq_cmd_ret[MAX_RET]={0};
4525
qs.xiongff0ae0f2022-10-11 15:47:14 +08004526/*******change other cmd to get rssi*******
4527 *
4528 *wl rssi ---> wl -i wlan0 rssi
4529 *
4530 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004531 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004532 {
you.chen23c4a5f2023-04-12 16:46:00 +08004533 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004534 return -1;
4535 }
you.chen9f17e4d2022-06-06 17:18:18 +08004536 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004537/****** if got rssi is 0,means sta didn't connected any device****/
4538 if(*rssi == 0)
4539 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004540 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004541 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004542 }
you.chen35020192022-05-06 11:30:57 +08004543
4544 return 0;
4545}
4546
4547int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4548{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004549 RLOGD("enter lynq_get_connect_ap_band\n");
4550 if (band == NULL)
4551 {
you.chen35020192022-05-06 11:30:57 +08004552 return -1;
4553 }
4554
4555 CHECK_IDX(idx, CTRL_STA);
4556 ap_info_s ap;
4557 ap.band = -1;
4558
qs.xiong9fbf74e2023-03-28 13:38:22 +08004559 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4560 {
you.chen35020192022-05-06 11:30:57 +08004561 return -1;
4562 }
4563 *band = ap.band;
4564
4565 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004566}
you.chenf58b3c92022-06-21 16:53:48 +08004567
4568int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4569{
you.chenb95401e2023-05-12 19:39:06 +08004570 int ret;
4571 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004572 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004573
4574 if (ip == NULL)
4575 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004576 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004577 return -1;
4578 }
4579
4580 CHECK_IDX(idx, CTRL_STA);
4581
qs.xionge4cbf1c2023-02-28 18:22:49 +08004582 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004583 {
4584 return -1;
4585 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004586
you.chenb95401e2023-05-12 19:39:06 +08004587 ip[0] = '\0';
4588 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4589 if (ret != 0)
4590 {
4591 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4592 return -1;
4593 }
4594
4595 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4596 {
4597 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004598 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004599 if (ret != 0)
4600 {
4601 ip[0] = '\0';
4602 return 0;
4603 }
4604 else
4605 {
4606 p = strchr(ip, '\n');
4607 if (p != NULL)
4608 {
4609 *p = '\0';
4610 }
4611 }
4612 }
4613 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004614}
4615
qs.xionge02a5252023-09-20 14:00:21 +08004616int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4617{
4618 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4619 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4620}
4621
qs.xiong026c5c72022-10-17 11:15:45 +08004622int lynq_ap_connect_num(int sta_number)
4623{
4624 char lynq_limit_cmd[32]={0};
4625 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004626 if((sta_number < 1 ) && (sta_number > 15))
4627 {
4628 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004629 return -1;
4630 }
4631 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4632 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004633 if(ret != 0)
4634 {
4635 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004636 }
4637 return 0;
4638}
you.chenf58b3c92022-06-21 16:53:48 +08004639
qs.xiong77905552022-10-17 11:19:57 +08004640int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4641{
4642
4643 char lynq_wifi_acs_cmd[128]={0};
4644 char lynq_cmd_mode[128]={0};
4645 char lynq_cmd_slect[128]={0};
4646
qs.xiong9fbf74e2023-03-28 13:38:22 +08004647 if((acs_mode != 2) && (acs_mode != 5))
4648 {
qs.xiong77905552022-10-17 11:19:57 +08004649 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4650 }
4651
qs.xiong9fbf74e2023-03-28 13:38:22 +08004652 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4653 {
qs.xiong77905552022-10-17 11:19:57 +08004654 return -1;
4655 }
4656
4657 CHECK_IDX(idx, CTRL_AP);
4658
4659 CHECK_WPA_CTRL(CTRL_AP);
4660
4661 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4662 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4663 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4664
4665 DO_OK_FAIL_REQUEST(cmd_disconnect);
4666 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4667 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4668 DO_OK_FAIL_REQUEST(cmd_save_config);
4669 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4670
4671 return 0;
4672}
you.chen0f5c6432022-11-07 18:31:14 +08004673//you.chen add for tv-box start
4674static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4675 FILE *fp;
4676 //printf("to exec cmd:%s\n", str_cmd);
4677 if((fp=popen(str_cmd,"r"))==NULL)
4678 {
4679 perror("popen error!");
4680 return -1;
4681 }
4682 if((fread(str_cmd_ret,max_len,1,fp))<0)
4683 {
4684 perror("fread fail!");
4685 fclose(fp);
4686 return -1;
4687 }
4688 fclose(fp);
4689 return 0;
4690}
4691
4692static int get_netmask_length(const char* mask)
4693{
4694 int masklen=0, i=0;
4695 int netmask=0;
4696
4697 if(mask == NULL)
4698 {
4699 return 0;
4700 }
4701
4702 struct in_addr ip_addr;
4703 if( inet_aton(mask, &ip_addr) )
4704 {
4705 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004706 }else
4707 {
you.chen0f5c6432022-11-07 18:31:14 +08004708 netmask = 0;
4709 return 0;
4710 }
4711
4712 while(0 == (netmask & 0x01) && i<32)
4713 {
4714 i++;
4715 netmask = netmask>>1;
4716 }
4717 masklen = 32-i;
4718 return masklen;
4719}
4720
4721static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4722 int mask_len;
4723 char *p;
4724 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004725 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4726 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004727 return -1;
4728 p = strstr(str_cmd_ret, "Mask:");
4729 if (p == NULL)
4730 return -1;
4731 mask_len = get_netmask_length(p + 5);
4732 if (mask_len == 0)
4733 return -1;
4734 p = strstr(str_cmd_ret, "inet addr:");
4735 if (p == NULL)
4736 return -1;
4737 strcpy(tmp, p + 10);
4738 p = strstr(tmp, " ");
4739 if (p != NULL)
4740 *p = '\0';
4741 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4742 return 0;
4743}
4744
4745static void GBWWatchThreadProc() {
4746 int i,n, nloop, nmax, ncheckcount, nidlecount;
4747 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4748 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4749 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4750 char *results[16] = {0};
4751 char str_cmd[256] = {0};
4752 char str_cmd_ret[128] = {0};
4753 char dest_ip[32] = {0};
4754 lastAP1Bytes = lastAP2Bytes = 0;
4755 lastAP1Drop = lastAP2Drop = 0;
4756 lastAP1Speed = lastAP2Speed = 0;
4757 setAP1Speed = 50;
4758 setAP2Speed = 80;
4759 nloop = 0;
4760 nmax = 6;
4761 ncheckcount = nidlecount = 0;
4762
you.chen0df3e7e2023-05-10 15:56:26 +08004763 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004764 {
4765 RLOGE("------gbw thread run\n");
4766 return;
4767 }
4768
qs.xiong9fbf74e2023-03-28 13:38:22 +08004769 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004770 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4771 while (dest_ip[0] == '\0') {
4772 sleep(1);
4773 str_cmd_ret[0] = '\0';
4774 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4775 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4776 if (str_cmd_ret[n] == '\n'){
4777 str_cmd_ret[n] = '\0';
4778 break;
4779 }
4780 }
4781 if (str_cmd_ret[0] != '\0')
4782 {
4783 strcpy(dest_ip, str_cmd_ret);
4784 }
4785 }
4786
you.chenc9928582023-04-24 15:39:37 +08004787 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4788 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4789 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 +08004790 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4791 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004792 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004793 return;
4794 }
you.chenc9928582023-04-24 15:39:37 +08004795 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);
4796 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);
4797 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 +08004798
4799 while (1) {
4800 sleep(1);
4801 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004802 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4803 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4804 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004805 continue;
4806 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004807 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004808 if (n > 9) {
4809 if (strcmp(results[1], "Sent") == 0) {
4810 currAP1Bytes = atoll(results[2]);
4811 }
4812 if (strcmp(results[6], "(dropped") == 0) {
4813 currAP1Drop = atoi(results[7]);
4814 }
4815 }
4816
4817 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004818 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4819 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4820 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004821 continue;
4822 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004823 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004824 if (n > 9) {
4825 if (strcmp(results[1], "Sent") == 0) {
4826 currAP2Bytes = atoll(results[2]);
4827 }
4828 if (strcmp(results[6], "(dropped") == 0) {
4829 currAP2Drop = atoi(results[7]);
4830 }
4831 }
4832
4833 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4834 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4835 lastAP1Bytes = currAP1Bytes;
4836 lastAP2Bytes = currAP2Bytes;
4837 continue;
4838 }
4839
4840 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4841 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4842 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4843 lastAP1Speed = currAP1Speed;
4844 lastAP2Speed = currAP2Speed;
4845 lastAP1Bytes = currAP1Bytes;
4846 lastAP2Bytes = currAP2Bytes;
4847
4848 currSetAP1Speed = setAP1Speed;
4849 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4850 ncheckcount++;
4851 if (ncheckcount > 3) {
4852 ncheckcount = 0;
4853 currSetAP1Speed = 5;
4854 }
4855 }
4856 else {
4857 ncheckcount = 0;
4858 if (currAP1Speed < 5)
4859 nidlecount++;
4860 else
4861 nidlecount = 0;
4862
4863 }
4864
4865 if (nidlecount > 60 ){
4866 currSetAP1Speed = 50;
4867 }
4868
4869 if (currSetAP1Speed != setAP1Speed) {
4870 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004871 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4872 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004873 }
4874 }
4875}
4876
4877int enableGBW(const char* mac) {
4878 int i,len;
4879 char get_ipaddr_cmd[128]={0};
4880 ap_info_s *ap;
4881 device_info_s * list;
4882
4883 if (mac == NULL || g_gbw_enabled == 1)
4884 return -1;
4885 len = strlen(mac);
4886 g_gbw_mac = malloc(len + 1);
4887 for(i=0;i<len;i++) {
4888 if (mac[i] >= 'A' && mac[i] <= 'Z')
4889 {
4890 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4891 }
4892 else
4893 g_gbw_mac[i] = mac[i];
4894 }
4895 g_gbw_mac[i] = '\0';
4896 g_gbw_enabled = 1;
4897
4898 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4899 if (system(get_ipaddr_cmd) == 0) {
4900 //startGBW();
4901 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4902 for (i=0;i<len;i++) {
4903 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4904 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4905 startGBW();
4906 }
4907 free(ap);
4908 free(list);
4909 }
4910 }
4911 return 0;
4912}
4913
4914int disableGBW() {
4915 stopGBW();
4916 free(g_gbw_mac);
4917 g_gbw_mac = NULL;
4918 g_gbw_enabled = 1;
4919 return 0;
4920}
4921
4922static int startGBW() {
4923 if (g_gbw_watcher_pid != 0) {
4924 stopGBW();
4925 }
4926 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4927}
4928
4929static int stopGBW() {
4930 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004931 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004932 pthread_cancel(g_gbw_watcher_pid);
4933 pthread_join(g_gbw_watcher_pid, &retval);
4934 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004935 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4936 if (s_ap_iterface_name[0] != '\0')
4937 {
4938 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4939 system(cmd);
4940 }
you.chen0f5c6432022-11-07 18:31:14 +08004941}
4942//you.chen add for tv-box end