blob: 680243222456bd45ff3c366861ae5e87804aefd1 [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);
qs.xiongb5dab082023-10-13 14:43:41 +0800779 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800780 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);
qs.xiongb5dab082023-10-13 14:43:41 +0800795 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800796 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
qs.xiongb5dab082023-10-13 14:43:41 +08002946/**
2947* buff data
2948* buff_len size of buff
2949* idx sta
2950* *ap ap info for find ssid && password
2951* password return password
2952*
2953*/
2954static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
2955
2956 int ret, network_len, i, ssid_len,curr_auth;
2957 int count, index,org_index;
2958 char *split_lines[128] = {0};
2959 char *p, *ssid, *ssid_end_flag,*ptr;
2960 char tmp_ssid[128]={0};
2961 char tmp_auth[24]={0};
2962
2963 org_index = 0;
2964 network_len = 0;
2965 p = NULL;
2966
2967 CHECK_IDX(idx, CTRL_STA);
2968
2969 while(1){
2970 network_len = 0;
2971 p == NULL;
2972 for(; org_index < buff_len; org_index ++)
2973 {
2974 for(; org_index < buff_len; org_index ++)
2975 {
2976 if (memcmp(buff + org_index, "network={", 9) != 0)
2977 {
2978 continue;
2979 }
2980 p = buff + org_index + 9;
2981
2982 for (; org_index < buff_len; org_index ++ )
2983 {
2984 if (buff[org_index] != '}')
2985 {
2986 continue;
2987 }
2988 buff[org_index] = '\0';
2989 break;
2990 }
2991 network_len = buff + org_index - p;
2992 break;
2993 }
2994
2995 if (p == NULL)
2996 {
2997 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
2998 return -1;
2999 }
3000
3001 ssid = strstr(p, "ssid=");
3002 if (ssid != NULL) {
3003 ssid += strlen("ssid=");
3004 if (ssid[0] == '\"')
3005 {
3006 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3007 {
3008 RLOGD("-----curr_get ssid form config is %s",ssid);
3009 break;
3010 }
3011 RLOGD("-----countine to find dest ssid %s ---curr_get ssid from config is %s",ap->ap_ssid,ssid);
3012 }
3013 else
3014 {
3015 ssid_end_flag = strstr(ssid, "\n");
3016 if (ssid_end_flag != NULL)
3017 {
3018 ssid_len = (ssid_end_flag - ssid) / 2;
3019 for(i=0; i<ssid_len; i++)
3020 {
3021 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3022 }
3023 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3024 {
3025 RLOGD("curr_ssid is(from config) ---- %s ap_info ssid --->",tmp_ssid,ap->ap_ssid);
3026 break;
3027 }
3028 }
3029 }
3030 }
3031
3032 }
3033
3034 if (org_index >= buff_len || NULL == p || network_len <= 0)
3035 {
3036
3037 if (buff != NULL)
3038 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3039 return -1;
3040 }
3041
3042 count = lynq_split(p, network_len, '\n', split_lines);
3043 ret = -1;
3044 for( index=0; index < count; index++ )
3045 {
3046 p = strstr(split_lines[index], "key_mgmt=");
3047 RLOGD("current p str ------- %s",p);
3048 if(p != NULL)
3049 {
3050 p += 9;
3051 if(memcmp(p,"SAE",3) == 0)
3052 {
3053 curr_auth = 5;
3054 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3055 {
3056 curr_auth = 4;
3057 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3058 {
3059 curr_auth = 3;
3060 }else if(memcmp(p,"NONE",4) == 0 )
3061 {
3062 curr_auth = 0;
3063 }else{
3064 curr_auth = 1;
3065 }
3066 RLOGD("************curret_get_auth is %d ssid is %s",curr_auth,ap->ap_ssid);
3067 if( curr_auth < 1 || curr_auth > 6)
3068 {
3069 ret = -1;
3070 }
3071 break;
3072 }
3073 }
3074 if( curr_auth == 0)
3075 {
3076 return 0;
3077 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3078 {
3079 for(index=0; index < count; index++)
3080 {
3081 /*get psw info*/
3082
3083 p = strstr(split_lines[index], "psk=");
3084 if (p != NULL)
3085 {
3086 p += 4;
3087 if (*p == '\"')
3088 {
3089 p++;
3090 }
3091 }
3092 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3093 {
3094 p += 9;
3095 if (*p == '\"')
3096 {
3097 p++;
3098 }
3099 }
3100 else
3101 {
3102 continue;
3103 }
3104
3105 if (*p == '\"')
3106 p++;
3107 strncpy(password, p, 64);
3108 p = password;
3109 while(password - p < 64 && *password != '\0')
3110 {
3111 if (*password == '\"')
3112 {
3113 *password = '\0';
3114 RLOGD("---------password------- p:: %s",p);
3115 ret = 0;
3116 break;
3117 }
3118 password++;
3119 }
3120 break;
3121 }
3122 break;
3123 }
3124 }
3125
3126 return ret;
3127}
3128
3129
3130
you.chen35020192022-05-06 11:30:57 +08003131int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3132
3133 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003134 int len, ret;
3135 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003136 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003137
qs.xiongb5dab082023-10-13 14:43:41 +08003138 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003139 CHECK_IDX(idx, CTRL_STA);
3140
qs.xiong9fbf74e2023-03-28 13:38:22 +08003141 if (NULL == password)
3142 {
3143 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003144 return -1;
3145 }
3146
you.chen35020192022-05-06 11:30:57 +08003147 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003148 if (NULL == fp)
3149 {
3150 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003151 return -1;
3152 }
3153
you.chen6d247052023-06-01 16:39:54 +08003154 fseek(fp, 0, SEEK_END);
3155 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003156 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003157
qs.xiongb5dab082023-10-13 14:43:41 +08003158 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003159 {
3160 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3161 return -1;
3162 }
3163
you.chen35020192022-05-06 11:30:57 +08003164 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003165 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003166 fclose(fp);
3167
qs.xiongb5dab082023-10-13 14:43:41 +08003168
3169 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3170
3171 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003172 {
qs.xiongb5dab082023-10-13 14:43:41 +08003173 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3174 free(info_buff);
3175 return 0;
you.chen35020192022-05-06 11:30:57 +08003176 }
qs.xiongb5dab082023-10-13 14:43:41 +08003177 else{
3178 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003179 return -1;
3180 }
3181
you.chen35020192022-05-06 11:30:57 +08003182}
3183
qs.xiongb5dab082023-10-13 14:43:41 +08003184
you.chen35020192022-05-06 11:30:57 +08003185static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3186{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003187 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003188
qs.xiong9fbf74e2023-03-28 13:38:22 +08003189 if (sta_ssid == NULL)
3190 {
3191 RLOGE("sta_ssid is null\n");
3192 return -1;
you.chen35020192022-05-06 11:30:57 +08003193 }
3194
qs.xiong9fbf74e2023-03-28 13:38:22 +08003195 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003196
3197 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3198
3199 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3200// DO_OK_FAIL_REQUEST(cmd_save_config);
3201
qs.xiong9fbf74e2023-03-28 13:38:22 +08003202 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003203
3204}
3205
you.chen35020192022-05-06 11:30:57 +08003206static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003207{
you.chen35020192022-05-06 11:30:57 +08003208 char lynq_disable_cmd[128]={0};
3209 char lynq_select_cmd[128]={0};
3210
3211 CHECK_WPA_CTRL(CTRL_STA);
3212
qs.xiong9fbf74e2023-03-28 13:38:22 +08003213 if (save != 0)
3214 {
you.chenc29444e2022-06-07 18:01:16 +08003215 if (start_flag != 0)
3216 {
3217 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3218 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3219 }
3220 else
3221 {
3222 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3223 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3224 }
you.chen35020192022-05-06 11:30:57 +08003225 DO_OK_FAIL_REQUEST(cmd_save_config);
3226 }
3227
qs.xiong9fbf74e2023-03-28 13:38:22 +08003228 if (start_flag == 0)
3229 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003230 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003231 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3232 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003233 else
3234 {
you.chen35020192022-05-06 11:30:57 +08003235 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3236 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3237 }
3238
3239 return 0;
3240}
3241
3242int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3243{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003244 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003245 CHECK_IDX(idx, CTRL_STA);
3246
you.chen6c2dd9c2022-05-16 17:55:28 +08003247 curr_status_info curr_state;
3248 ap_info_s ap_info;
3249 curr_state.ap = &ap_info;
3250 curr_state.state = NULL;
3251
qs.xiong9fbf74e2023-03-28 13:38:22 +08003252 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3253 {
you.chend2fef3f2023-02-13 10:50:35 +08003254 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003255 return 0;
3256 }
3257
3258 return -1;
you.chen35020192022-05-06 11:30:57 +08003259}
3260
3261int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3262{
qs.xiong5d716d22023-09-20 20:08:39 +08003263 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003264 scan_info_s *scan_list = NULL;
3265 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003266 int scan_len=0;
3267 int save_len=0;
3268 int best_index = -1;
3269 int best_scan_index = -1;
3270 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003271 int i, j, ret;
3272
3273 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003274
3275 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003276 if (info == NULL)
3277 {
you.chen35020192022-05-06 11:30:57 +08003278 return -1;
3279 }
3280
3281 curr_status_info curr_state;
3282 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003283 char status[64];
you.chen35020192022-05-06 11:30:57 +08003284
you.chen9ac66392022-08-06 17:01:16 +08003285 memset(&ap_info, 0, sizeof (ap_info));
3286 memset(status, 0, sizeof (status));
3287
3288 curr_state.ap = &ap_info;
3289 curr_state.state = status;
3290
qs.xiong9fbf74e2023-03-28 13:38:22 +08003291 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3292 {
you.chen35020192022-05-06 11:30:57 +08003293 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003294 if (strcmp(status, STATE_COMPLETED) == 0)
3295 {
3296 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003297 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003298 }
3299 else
3300 {
3301 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003302 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003303 }
you.chen593621d2023-04-27 17:52:44 +08003304 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003305 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003306 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003307 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003308 return 0;
3309 }
3310
you.chen9ac66392022-08-06 17:01:16 +08003311 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003312 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003313 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3314 {
you.chen9ac66392022-08-06 17:01:16 +08003315 if (NULL != scan_list)
3316 {
3317 free(scan_list);
3318 }
you.chen35020192022-05-06 11:30:57 +08003319 return -1;
3320 }
3321
qs.xiong9fbf74e2023-03-28 13:38:22 +08003322 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3323 {
you.chen9ac66392022-08-06 17:01:16 +08003324 if (NULL != scan_list)
3325 {
3326 free(scan_list);
3327 }
3328 if (NULL != save_list)
3329 {
3330 free(save_list);
3331 }
you.chen35020192022-05-06 11:30:57 +08003332 return -1;
3333 }
3334
qs.xiong9fbf74e2023-03-28 13:38:22 +08003335 for (i=0; i < save_len; i++)
3336 {
3337 for (j=0; j < scan_len; j++)
3338 {
you.chen35020192022-05-06 11:30:57 +08003339 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003340 && save_list[i].base_info.auth == scan_list[j].auth)
3341 {
3342 if (best_rssi == 0)
3343 {
you.chen9ac66392022-08-06 17:01:16 +08003344 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003345 best_rssi = scan_list[j].rssi;
3346 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003347 else if (best_rssi > scan_list[j].rssi)
3348 {
you.chen35020192022-05-06 11:30:57 +08003349 best_index = i;
3350 best_scan_index = j;
3351 best_rssi = scan_list[j].rssi;
3352 }
you.chend2fef3f2023-02-13 10:50:35 +08003353 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 +08003354 break;
3355 }
3356 }
3357 }
3358
qs.xiong9fbf74e2023-03-28 13:38:22 +08003359 if (best_index >= 0)
3360 {
you.chen35020192022-05-06 11:30:57 +08003361 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003362 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 +08003363 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003364 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003365 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003366 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003367 }
3368
you.chen9ac66392022-08-06 17:01:16 +08003369 if (NULL != scan_list)
3370 {
3371 free(scan_list);
3372 }
3373 if (NULL != save_list)
3374 {
3375 free(save_list);
3376 }
3377
qs.xiong5d716d22023-09-20 20:08:39 +08003378 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003379 return ret;
you.chen35020192022-05-06 11:30:57 +08003380}
3381
3382static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3383{
qs.xiongc8d92a62023-03-29 17:36:14 +08003384 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003385 char lynq_ket_mgmt_cmd[64]={0};
3386 char lynq_pairwise_cmd[64]={0};
3387 char lynq_psk_cmd[64]={0};
3388
3389 CHECK_WPA_CTRL(CTRL_STA);
3390
qs.xiong9fbf74e2023-03-28 13:38:22 +08003391 switch(auth)
3392 {
3393 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003394 {
3395 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003396
you.chen35020192022-05-06 11:30:57 +08003397 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003398// DO_OK_FAIL_REQUEST(cmd_save_config);
3399 break;
3400 }
3401 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003402 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003403 {
3404 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3405 {
you.chen35020192022-05-06 11:30:57 +08003406 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3407 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003408 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3409 {
you.chena6cd55a2022-05-08 12:20:18 +08003410 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003411 }
3412 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3413 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003414
you.chen35020192022-05-06 11:30:57 +08003415 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3416 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3417 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003418
qs.xiong9fbf74e2023-03-28 13:38:22 +08003419 if (password != NULL)
3420 {
you.chen35020192022-05-06 11:30:57 +08003421 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3422 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003423 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003424 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003425
you.chen35020192022-05-06 11:30:57 +08003426// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003427 break;
3428 }
3429 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3430 {
qs.xiong3e506812023-04-06 11:08:48 +08003431 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003432 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003433 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3434 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3435
qs.xiong3e506812023-04-06 11:08:48 +08003436 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003437 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3438 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3439 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3440
3441 break;
3442 }
3443 case LYNQ_WIFI_AUTH_WPA3_PSK:
3444 {
qs.xiong3e506812023-04-06 11:08:48 +08003445 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003446 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003447 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3448 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3449
qs.xiongb37f8c42023-09-13 21:21:58 +08003450 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003451 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3452 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3453 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3454
3455 break;
3456 }
3457 default:
3458 return -1;
you.chen35020192022-05-06 11:30:57 +08003459 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003460
qs.xiong9fbf74e2023-03-28 13:38:22 +08003461 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003462}
qs.xiong7a105ce2022-03-02 09:43:11 -05003463
you.chen35020192022-05-06 11:30:57 +08003464static int inner_get_curr_net_no(int interface) {
3465 curr_status_info curr_state;
3466 curr_state.ap = NULL;
3467 curr_state.state = NULL;
3468
qs.xiong9fbf74e2023-03-28 13:38:22 +08003469 if (0 != inner_get_status_info(interface, &curr_state))
3470 {
you.chen35020192022-05-06 11:30:57 +08003471 return -1;
3472 }
3473
3474 return curr_state.net_no;
3475}
3476
3477int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003478{
you.chen35020192022-05-06 11:30:57 +08003479 int net_no;
3480 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003481
you.chen35020192022-05-06 11:30:57 +08003482 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003483
qs.xiong9fbf74e2023-03-28 13:38:22 +08003484 if (net_no < 0)
3485 {
you.chen35020192022-05-06 11:30:57 +08003486 return -1;
3487 }
3488
3489 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003490}
3491
you.chenb95401e2023-05-12 19:39:06 +08003492int 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 -05003493{
you.chen35020192022-05-06 11:30:57 +08003494 int count, net_no, index;
3495 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003496 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003497 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003498 curr_status_info curr_state;
3499 ap_info_s ap_info;
3500 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003501
qs.xiong9fbf74e2023-03-28 13:38:22 +08003502 if (ssid == NULL || *ssid == '\0')
3503 {
3504 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003505 return -1;
3506 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003507
qs.xiong9fbf74e2023-03-28 13:38:22 +08003508 if (LYNQ_WIFI_AUTH_OPEN != auth)
3509 {
3510 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003511 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003512 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003513 return -1;
3514 }
3515 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003516
you.chen70f377f2023-04-14 18:17:09 +08003517
3518 pthread_mutex_lock(&s_global_check_mutex);
3519 if (s_sta_status != INNER_STA_STATUS_INIT)
3520 {
3521 s_sta_status = INNER_STA_STATUS_CANCEL;
3522 pthread_cond_signal(&s_global_check_cond);
3523 }
3524 pthread_mutex_unlock(&s_global_check_mutex);
3525
you.chen35020192022-05-06 11:30:57 +08003526 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003527 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003528
3529 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003530 memset(&ap_info, 0, sizeof (ap_info));
3531 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003532
you.chen70f377f2023-04-14 18:17:09 +08003533 curr_state.ap = &ap_info;
3534 curr_state.state = status;
3535
3536 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003537 {
you.chen70f377f2023-04-14 18:17:09 +08003538 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3539 {
3540 net_no = curr_state.net_no;
3541 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3542 && strcmp(ap_info.psw, psw) == 0)
3543 {
3544 RLOGD("already connected\n");
3545
3546 pthread_mutex_lock(&s_global_check_mutex);
3547 s_sta_status = INNER_STA_STATUS_CONNECTED;
3548 pthread_cond_signal(&s_global_check_cond);
3549 pthread_mutex_unlock(&s_global_check_mutex);
3550 return 0;
3551 }
you.chen35020192022-05-06 11:30:57 +08003552 }
3553 }
3554
you.chen70f377f2023-04-14 18:17:09 +08003555 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003556 {
you.chen70f377f2023-04-14 18:17:09 +08003557 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3558
3559 for (index=0; index < count; index++)
3560 {
3561 net_auth = -1;
3562 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3563 {
3564 net_no = net_no_list[index];
3565 break;
3566 }
you.chen35020192022-05-06 11:30:57 +08003567 }
3568
you.chen70f377f2023-04-14 18:17:09 +08003569 if (net_no < 0)
3570 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003571 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3572 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3573 {
3574 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3575 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3576 DO_OK_FAIL_REQUEST(rm_net_cmd);
3577 }
you.chen70f377f2023-04-14 18:17:09 +08003578 net_no = lynq_add_network(CTRL_STA);
3579 if (net_no == -1)
3580 {
3581 return -1;
3582 }
3583
3584 RLOGD("net no is %d\n", net_no);
3585 if (0 != inner_set_sta_ssid(net_no, ssid))
3586 {
3587 return -1;
3588 }
you.chen35020192022-05-06 11:30:57 +08003589 }
3590 }
3591
qs.xiong9fbf74e2023-03-28 13:38:22 +08003592 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3593 {
you.chen35020192022-05-06 11:30:57 +08003594 return -1;
3595 }
3596
you.chen70f377f2023-04-14 18:17:09 +08003597
3598 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003599 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003600 usleep(200*1000);
3601
3602 ret = inner_sta_start_stop(net_no, 1, 1);
3603
3604 pthread_mutex_lock(&s_global_check_mutex);
3605 s_sta_status = INNER_STA_STATUS_CONNECTING;
3606 strcpy(s_sta_current_connecting_ssid, ssid);
3607 struct timeval now;
3608 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003609 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003610 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3611 pthread_cond_signal(&s_global_check_cond);
3612 pthread_mutex_unlock(&s_global_check_mutex);
3613 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003614}
3615
you.chenb95401e2023-05-12 19:39:06 +08003616int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3617{
3618 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3619}
3620
you.chen35020192022-05-06 11:30:57 +08003621int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003622{
you.chen35020192022-05-06 11:30:57 +08003623 ap_info_s ap;
3624 curr_status_info curr_state;
3625 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003626
qs.xiong9fbf74e2023-03-28 13:38:22 +08003627 if (ssid == NULL || *ssid == '\0')
3628 {
3629 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003630 return -1;
3631 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003632
you.chen35020192022-05-06 11:30:57 +08003633 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003634
you.chen35020192022-05-06 11:30:57 +08003635 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003636 curr_state.state = NULL;
3637
qs.xiong9fbf74e2023-03-28 13:38:22 +08003638 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3639 {
you.chen35020192022-05-06 11:30:57 +08003640 return 0;
3641 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003642
qs.xiong9fbf74e2023-03-28 13:38:22 +08003643 if (strcmp(ap.ap_ssid, ssid) != 0)
3644 {
you.chen35020192022-05-06 11:30:57 +08003645 return 0;
3646 }
3647
you.chen70f377f2023-04-14 18:17:09 +08003648 pthread_mutex_lock(&s_global_check_mutex);
3649 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3650 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003651 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003652}
qs.xiong97fa59b2022-04-07 05:41:29 -04003653
qs.xiongc93bf2b2023-08-25 10:22:08 +08003654int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3655{
3656 ap_info_s ap;
3657 curr_status_info curr_state;
3658 ap.ap_ssid[0] = '\0';
3659
3660 if (ssid == NULL || *ssid == '\0')
3661 {
3662 RLOGE("input ssid is NULL\n");
3663 return -1;
3664 }
3665
3666 CHECK_IDX(idx, CTRL_STA);
3667
3668
3669 curr_state.ap = &ap;
3670 curr_state.state = NULL;
3671
3672 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3673 {
3674 return 0;
3675 }
3676
3677 if (strcmp(ap.ap_ssid, ssid) != 0)
3678 {
3679 return 0;
3680 }
3681
3682 pthread_mutex_lock(&s_global_check_mutex);
3683 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003684 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3685 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003686 pthread_mutex_unlock(&s_global_check_mutex);
3687 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3688
3689}
3690
3691
you.chena6cd55a2022-05-08 12:20:18 +08003692int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3693{
qs.xiongb37f8c42023-09-13 21:21:58 +08003694
qs.xiongad2f89d2023-01-18 13:17:41 +08003695 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3696 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003697
you.chen35020192022-05-06 11:30:57 +08003698 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003699 CHECK_WPA_CTRL(CTRL_STA);
3700
you.chenc9928582023-04-24 15:39:37 +08003701 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3702 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003703 {
you.chenc9928582023-04-24 15:39:37 +08003704 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003705 return -1;
3706 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003707
qs.xiongad2f89d2023-01-18 13:17:41 +08003708 system(lynq_enable_sta_cmd);
3709 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003710 pthread_mutex_lock(&s_global_check_mutex);
3711 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3712 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003713 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003714}
3715
you.chen6d247052023-06-01 16:39:54 +08003716static int inner_get_status_info_state (int interface, char *state) {
3717 curr_status_info curr_state;
3718 curr_state.ap = NULL;
3719 curr_state.state = state;
3720 return inner_get_status_info(interface, &curr_state);
3721}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003722
3723int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3724{
3725
qs.xiongb37f8c42023-09-13 21:21:58 +08003726 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3727 int tmp_open_idx[128];
3728 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003729
qs.xiongb37f8c42023-09-13 21:21:58 +08003730 pthread_mutex_lock(&s_global_check_mutex);
3731 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3732 pthread_mutex_unlock(&s_global_check_mutex);
3733 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003734 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003735 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003736 }
3737
qs.xiongb37f8c42023-09-13 21:21:58 +08003738 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003739 return 0;
3740}
3741
3742
you.chen35020192022-05-06 11:30:57 +08003743int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003744{
qs.xiongad2f89d2023-01-18 13:17:41 +08003745// char lynq_disable_network_cmd[MAX_CMD];
3746// curr_status_info curr_state;
3747// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003748 int i=0;
3749 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003750
you.chen6d247052023-06-01 16:39:54 +08003751// 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 +08003752 CHECK_IDX(idx, CTRL_STA);
3753 CHECK_WPA_CTRL(CTRL_STA);
3754
you.chen6d247052023-06-01 16:39:54 +08003755// system(lynq_disable_sta_cmd);
3756 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003757 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003758
3759 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3760 if (ret != 0)
3761 {
3762 RLOGE("lynq_wifi_ap_start excute script fail");
3763 return -1;
3764 }
3765
you.chen6d247052023-06-01 16:39:54 +08003766 for (i=0; i < 30; i++) // to check if sta is realy stoped
3767 {
3768 if (inner_get_status_info_state(idx, state) != 0)
3769 {
3770 break;
3771 }
3772
3773 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3774 {
3775 break;
3776 }
3777 RLOGD("lynq_wifi_ap_start curr state %s", state);
3778 usleep(SLEEP_TIME_ON_IDLE);
3779 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003780 pthread_mutex_lock(&s_global_check_mutex);
3781 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3782 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003783 return 0;
3784// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003785}
qs.xiongfcc914b2023-07-06 21:16:20 +08003786int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3787{
3788 char LYNQ_DISABLE_CMD[128]={0};
3789 CHECK_IDX(idx, CTRL_STA);
3790 CHECK_WPA_CTRL(CTRL_STA);
3791 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3792 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3793 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3794 return 0;
3795}
qs.xiong7a105ce2022-03-02 09:43:11 -05003796
you.chen35020192022-05-06 11:30:57 +08003797//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3798// int i, count;
3799// char *p;
3800// const char * FLAG_SSID = "ssid=";
3801// const char * FLAG_SBSID = "bssid=";
3802// const char * FLAG_KEY_MGMT = "key_mgmt=";
3803// const char * FLAG_FREQ = "freq=";
3804// char lynq_sta_cmd[MAX_CMD];
3805// char *split_lines[128] = {0};
3806
3807// CHECK_WPA_CTRL(CTRL_AP);
3808
3809// sprintf(lynq_sta_cmd, "STA %s", bssid);
3810
3811// DO_REQUEST(lynq_sta_cmd);
3812
3813// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3814
3815// for(i=0; i < count; i++) {
3816// p = strstr(split_lines[i], FLAG_SSID);
3817// if (p != NULL) {
3818// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3819// continue;
3820// }
3821// }
3822
3823// lynq_get_interface_ip(idx, ap->ap_ip);
3824// lynq_ap_password_set(idx, ap->psw);
3825
3826// return 0;
3827//}
3828
3829static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3830 curr_status_info curr_state;
3831 curr_state.ap = ap;
3832 curr_state.state = NULL;
3833 return inner_get_status_info(interface, &curr_state);
3834}
3835
3836int 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 -04003837{
qs.xiong5071c802023-09-06 14:04:15 +08003838 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003839 int index, line_count;
3840 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003841 const char *lynq_first_sta_cmd = "STA-FIRST";
3842 char lynq_next_sta_cmd[MAX_CMD];
3843 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003844 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003845
you.chen35020192022-05-06 11:30:57 +08003846 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003847
you.chen35020192022-05-06 11:30:57 +08003848 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003849
you.chenb95401e2023-05-12 19:39:06 +08003850 // ap_info_s * tmp_ap;
3851 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003852 if (ap == NULL || list == NULL || len == NULL)
3853 {
3854 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003855 return -1;
3856 }
3857
you.chenb95401e2023-05-12 19:39:06 +08003858 // ap = &tmp_ap;
3859 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003860 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003861 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003862
you.chenb95401e2023-05-12 19:39:06 +08003863 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003864 {
you.chenb95401e2023-05-12 19:39:06 +08003865 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003866 return -1;
3867 }
3868
3869 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3870 lynq_ap_password_get(idx, (*ap)->psw);
3871
you.chen35020192022-05-06 11:30:57 +08003872 DO_REQUEST(lynq_first_sta_cmd);
3873
3874 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003875 while (reply_len > 0)
3876 {
3877 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003878 {
you.chen35020192022-05-06 11:30:57 +08003879 break;
3880 }
3881 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3882 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3883 strcpy(bssid[index], split_lines[0]);
3884 index++;
3885 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3886 reply_len = MAX_RET;
3887 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003888 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 +08003889 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003890 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003891 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003892 break;
3893 }
3894 }
3895
3896 *len = index;
3897
3898 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003899 for (index=0; index < *len; index++)
3900 {
you.chend2fef3f2023-02-13 10:50:35 +08003901 dev_info = &(*list)[index];
3902 memset(dev_info, 0, sizeof(device_info_s));
3903 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3904 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3905 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3906 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003907 free(bssid[index]);
3908 }
qs.xiong5071c802023-09-06 14:04:15 +08003909 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003910 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003911}
3912
you.chen35020192022-05-06 11:30:57 +08003913int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003914{
you.chen35020192022-05-06 11:30:57 +08003915 int i, count, index, count_words;
3916 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3917 char *split_lines[128] = {0};
3918 char *split_words[128] = {0};
3919 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003920
qs.xiong9fbf74e2023-03-28 13:38:22 +08003921 if (list == NULL || len == NULL)
3922 {
you.chen35020192022-05-06 11:30:57 +08003923 return -1;
3924 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003925
you.chen9ac66392022-08-06 17:01:16 +08003926 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3927 {
3928 usleep(100 * 1000);
3929 }
3930
you.chen35020192022-05-06 11:30:57 +08003931 CHECK_IDX(idx, CTRL_STA);
3932
3933 CHECK_WPA_CTRL(CTRL_STA);
3934
3935 DO_REQUEST(lynq_scan_result_cmd);
3936
3937 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3938 *len = count - 1;
3939 *list = malloc(sizeof (scan_info_s) * *len);
3940
3941 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003942 for (index=0; index <count_words; index++)
3943 {
3944 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003945 }
3946
qs.xiong9fbf74e2023-03-28 13:38:22 +08003947 for(index = 1;index < count; index++)
3948 {
3949 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003950 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003951 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3952 if (count_words < 4)
3953 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003954 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003955 //bssid / frequency / signal level / flags / ssid
3956 p = (*list) + index - 1;
3957 strcpy(p->mac, split_words[0]);
3958 p->band = convert_band_from_freq(atoi(split_words[1]));
3959 p->rssi = -1 * atoi( split_words[2]);
3960 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003961 if (count_words == 4) // ssid hided
3962 {
3963 p->ssid[0] = '\0';
3964 }
3965 else
3966 {
3967 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3968 }
you.chen35020192022-05-06 11:30:57 +08003969 }
3970
3971 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003972}
qs.xiong97fa59b2022-04-07 05:41:29 -04003973
you.chen35020192022-05-06 11:30:57 +08003974int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3975{
3976 int count, net_no, index;
3977 int net_no_list[128];
3978 lynq_wifi_auth_s net_auth;
3979 char lynq_remove_cmd[MAX_CMD];
3980
qs.xiong9fbf74e2023-03-28 13:38:22 +08003981 if (ssid == NULL || *ssid == '\0')
3982 {
3983 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003984 return -1;
3985 }
3986
3987 CHECK_IDX(idx, CTRL_STA);
3988
3989 CHECK_WPA_CTRL(CTRL_STA);
3990
3991 net_no = -1;
3992 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3993
qs.xiong9fbf74e2023-03-28 13:38:22 +08003994 for (index=0; index < count; index++)
3995 {
you.chen35020192022-05-06 11:30:57 +08003996 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003997 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3998 {
you.chen35020192022-05-06 11:30:57 +08003999 net_no = net_no_list[index];
4000 break;
4001 }
4002 }
4003
qs.xiong9fbf74e2023-03-28 13:38:22 +08004004 if (net_no < 0)
4005 {
you.chen35020192022-05-06 11:30:57 +08004006 return 0;
4007 }
4008
4009 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4010
4011 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
4012 DO_OK_FAIL_REQUEST(cmd_save_config);
4013
4014 return 0;
4015}
4016
4017int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004018{
you.chend2fef3f2023-02-13 10:50:35 +08004019 int count, index;
you.chen35020192022-05-06 11:30:57 +08004020 int net_no_list[128];
4021 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004022 RLOGD("enter lynq_get_sta_saved_ap api\n");
4023 if (list == NULL || len == NULL)
4024 {
4025 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004026 return -1;
4027 }
4028
4029 CHECK_IDX(idx, CTRL_STA);
4030
4031// CHECK_WPA_CTRL(CTRL_STA);
4032
4033 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004034 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004035
you.chen057aac42023-04-13 14:06:58 +08004036 if (count < 0)
4037 {
4038 RLOGE("list network fail");
4039 return count;
4040 }
4041 else if (count == 0)
4042 {
4043 *list = NULL;
4044 *len = 0;
4045 return 0;
4046 }
4047
you.chen35020192022-05-06 11:30:57 +08004048 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004049 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004050 *len = count;
4051
qs.xiong9fbf74e2023-03-28 13:38:22 +08004052 for (index=0; index < count; index++)
4053 {
you.chen35020192022-05-06 11:30:57 +08004054 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004055 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004056 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004057 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004058 {
you.chen35020192022-05-06 11:30:57 +08004059 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4060 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 else
you.chen057aac42023-04-13 14:06:58 +08004062 {
you.chen35020192022-05-06 11:30:57 +08004063 (*list)[index].base_info.band = -1;
4064 }
you.chen057aac42023-04-13 14:06:58 +08004065 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004066 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004067 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004068 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004069 return 0;
4070}
4071
4072int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4073{
qs.xiong20202422023-09-06 18:01:18 +08004074 if ( g_sta_conncet_status_flag != 0 )
4075 {
4076 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08004077 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004078 }
qs.xiongc8d92a62023-03-29 17:36:14 +08004079 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004080 const char *lynq_scan_cmd = "SCAN";
4081
4082 CHECK_IDX(idx, CTRL_STA);
4083
4084 CHECK_WPA_CTRL(CTRL_STA);
4085
you.chen0df3e7e2023-05-10 15:56:26 +08004086 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4087 {
4088 RLOGD("tmp clear scanlist");
4089 system(clean_last_re);
4090 }
you.chen9ac66392022-08-06 17:01:16 +08004091 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004092 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004093 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4094 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004095 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004096 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4097 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004098 g_sta_scan_finish_flag = 1;
4099 return -1;
4100 }
you.chen35020192022-05-06 11:30:57 +08004101
4102 return 0;
4103}
4104
4105int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004106 if (cb == NULL)
4107 {
4108 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004109 return -1;
4110 }
4111
you.chen6d247052023-06-01 16:39:54 +08004112 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004113 g_ap_callback_priv = priv;
4114 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004115 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004116
you.chen6d247052023-06-01 16:39:54 +08004117 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004118 if (g_ap_watcher_pid == 0 )
4119 {
4120 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4121 {
4122 g_ap_watcher_pid = 0;
4123 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4124 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4125 return -1;
4126 }
4127 }
4128
4129 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4130 RLOGD("creat APWatcherTheradProc susccs");
4131
you.chen35020192022-05-06 11:30:57 +08004132 return 0;
4133}
4134
4135int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004136 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004137 if (g_ap_callback_priv == priv)
4138 {
you.chen35020192022-05-06 11:30:57 +08004139 g_ap_callback_func = NULL;
4140 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004141 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004142 return 0;
4143 }
you.chen6d247052023-06-01 16:39:54 +08004144 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004145 return -1;
4146}
4147
4148int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004149 if (cb == NULL)
4150 {
4151 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004152 return -1;
4153 }
4154
you.chen6d247052023-06-01 16:39:54 +08004155 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004156 g_sta_callback_priv = priv;
4157 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004158 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004159
you.chen6d247052023-06-01 16:39:54 +08004160 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004161 if (g_sta_watcher_pid == 0 ) {
4162 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4163 {
4164 g_sta_watcher_pid = 0;
4165 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4166 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4167 return -1;
4168 }
4169 }
4170
4171 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4172 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004173 return 0;
4174}
4175
4176int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004177 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004178 if (g_sta_callback_priv == priv)
4179 {
you.chen35020192022-05-06 11:30:57 +08004180 g_sta_callback_func = NULL;
4181 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004182 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004183 return 0;
4184 }
you.chen6d247052023-06-01 16:39:54 +08004185 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004186 return -1;
4187}
4188
qs.xiongfcc914b2023-07-06 21:16:20 +08004189int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4190 if (cb == NULL)
4191 {
4192 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4193 return -1;
4194 }
4195 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4196 g_sta_auto_callback_priv = priv;
4197 g_sta_auto_callback_func = cb;
4198 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4199 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4200 if (g_sta_auto_watcher_pid == 0 ) {
4201 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4202 {
4203 g_sta_auto_watcher_pid = 0;
4204 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4205 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4206 return -1;
4207 }
4208 }
4209 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4210 RLOGD("creat STAWatcherTheradProc susccs");
4211 return 0;
4212}
4213int lynq_unreg_sta_auto_event_callback(void * priv) {
4214 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4215 if (g_sta_auto_callback_priv == priv)
4216 {
4217 g_sta_auto_watcher_stop_flag = 1;
4218 if (g_sta_auto_watcher_pid != 0)
4219 {
4220 pthread_join(g_sta_auto_watcher_pid, NULL);
4221 }
4222 g_sta_auto_watcher_pid = 0;
4223 g_sta_auto_callback_func = NULL;
4224 g_sta_auto_callback_priv = NULL;
4225 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4226 return 0;
4227 }
4228 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4229 return -1;
4230}
you.chen35020192022-05-06 11:30:57 +08004231int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4232{
4233 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004234 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004235 CHECK_IDX(idx, CTRL_AP);
4236
qs.xiong9fbf74e2023-03-28 13:38:22 +08004237 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4238 {
you.chen35020192022-05-06 11:30:57 +08004239 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4240 return 0;
4241 }
4242
qs.xiong9fbf74e2023-03-28 13:38:22 +08004243 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4244 {
you.chen35020192022-05-06 11:30:57 +08004245 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4246 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004247 else
4248 {
you.chen35020192022-05-06 11:30:57 +08004249 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4250 }
4251
4252 return 0;
4253}
4254
4255int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4256 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004257 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004258 CHECK_IDX(idx, CTRL_STA);
4259
qs.xiong9fbf74e2023-03-28 13:38:22 +08004260 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4261 {
you.chen35020192022-05-06 11:30:57 +08004262 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4263 return 0;
4264 }
4265
qs.xiong9fbf74e2023-03-28 13:38:22 +08004266 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4267 {
you.chen35020192022-05-06 11:30:57 +08004268 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4269 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004270 else
4271 {
you.chen35020192022-05-06 11:30:57 +08004272 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4273 }
4274
4275 return 0;
4276}
4277
4278int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4279// CHECK_IDX(idx, CTRL_AP);
4280// int ret = 0;
4281// size_t reply_len = MAX_RET;
4282// char cmd_reply[MAX_RET]={0};
4283// const char * cmd_str = "GET country";
4284// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4285// do{
4286// if (NULL == s_lynq_wpa_ctrl) {
4287// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4288// if (NULL == s_lynq_wpa_ctrl ) {
4289// printf("wpa_ctrl_open fail\n");
4290// return -1;
4291// }
4292// }
4293// }while(0);
4294
4295// do {
4296// reply_len = MAX_RET;
4297// cmd_reply[0] = '\0';
4298// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004299// 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 +08004300// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004301// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004302// return ret;
4303// }
4304// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004305// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004306// }while(0);
4307
4308 FILE *fp;
4309 size_t i = 0;
4310 char lynq_cmd_ret[MAX_RET]={0};
4311
4312// CHECK_IDX(idx, CTRL_AP);
4313
4314 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004315 {
4316 perror("popen error!");
4317 return -1;
4318 }
you.chen35020192022-05-06 11:30:57 +08004319 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4320 {
4321 perror("fread fail!");
4322 return -1;
4323 }
4324
qs.xiong9fbf74e2023-03-28 13:38:22 +08004325 for(i=0; i < strlen(lynq_cmd_ret); i++)
4326 {
4327 if (lynq_cmd_ret[i] == ' ')
4328 {
you.chen35020192022-05-06 11:30:57 +08004329 lynq_cmd_ret[i] = '\0';
4330 break;
4331 }
4332 }
4333
4334 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004335 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004336
4337 int ret=pclose(fp);
4338 if(ret==-1)
4339 {
4340 perror("close file faild");
4341 }
4342
4343 return 0;
4344}
4345
qs.xiong44fac672023-08-29 16:15:55 +08004346
you.chen705a7ef2023-06-01 22:06:45 +08004347static int check_and_init_uci_config(char * country_code)
4348{
4349 FILE * fp;
4350 int is_different = 0;
4351 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4352 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4353 const char * commit_uci_cmd ="uci commit";
4354 char set_country_cmd[MAX_CMD];
4355 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004356
you.chen705a7ef2023-06-01 22:06:45 +08004357 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004358
you.chen705a7ef2023-06-01 22:06:45 +08004359 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004360 {
you.chen705a7ef2023-06-01 22:06:45 +08004361 if (0 != system(create_uci_cmd))
4362 {
4363 RLOGE("creat_uci_cmd fail");
4364 return -1;
4365 }
4366 is_different = 1;
4367 }
4368
4369 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4370 {
4371 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004372 return -1;
4373 }
4374
you.chen705a7ef2023-06-01 22:06:45 +08004375 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004376 {
you.chen705a7ef2023-06-01 22:06:45 +08004377 RLOGE("fread fail!");
4378 fclose(fp);
4379 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004380 }
4381
you.chen705a7ef2023-06-01 22:06:45 +08004382 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4383 {
qs.xiong44fac672023-08-29 16:15:55 +08004384 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 +08004385 is_different = 1;
4386 }
4387
4388 fclose(fp);
4389
4390 if (is_different)
4391 {
4392 if ( 0 != system(set_country_cmd))
4393 {
4394 RLOGE("set_country_cmd fail");
4395 return -1;
4396 }
4397 if (0 != system(commit_uci_cmd))
4398 {
4399 RLOGE("commmit fail");
4400 }
4401 }
4402
4403 return is_different;
4404}
4405
4406int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4407 char check_current_code[10];
4408 const char * support_country[] = {"CN", "EU"};
4409
4410 int ret,is_different, i, cc_count;
4411
4412 if (country_code == NULL || country_code[0] == '\0')
4413 {
4414 RLOGE("bad country code\n");
4415 return -1;
4416 }
4417
4418 cc_count = sizeof (support_country) / sizeof (char*);
4419 for(i=0; i < cc_count; i++)
4420 {
4421 if (strcmp(support_country[i], country_code) == 0)
4422 {
4423 break;
4424 }
4425 }
4426
4427 if (i >= cc_count)
4428 {
4429 RLOGE("unspported country code %s\n", country_code);
4430 return -1;
4431 }
4432
4433 is_different = check_and_init_uci_config(country_code);
4434 if( is_different < 0 )
4435 {
4436 RLOGE("init set uci fail\n");
4437 return -1;
4438 }
4439
4440 ret = lynq_get_country_code(idx,check_current_code);
4441 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4442 {
4443 ret = lynq_wifi_disable();
4444 if(ret != 0 )
4445 {
4446 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4447 return -1;
4448 }
4449 }
4450
4451 return 0;
you.chen35020192022-05-06 11:30:57 +08004452}
4453
4454int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4455{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004456 RLOGD("enter lynq_get_connect_ap_mac\n");
4457 if (mac == NULL)
4458 {
4459 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004460 return -1;
4461 }
4462
4463 CHECK_IDX(idx, CTRL_STA);
4464 ap_info_s ap;
4465 ap.ap_mac[0] = '\0';
4466
qs.xiong9fbf74e2023-03-28 13:38:22 +08004467 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4468 {
you.chen35020192022-05-06 11:30:57 +08004469 return -1;
4470 }
4471 strcpy(mac, ap.ap_mac);
4472
4473 return 0;
4474}
4475
4476int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4477{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004478 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004479 struct ifaddrs *ifaddr_header, *ifaddr;
4480 struct in_addr * ifa;
4481 const char * ifaName = "wlan0";
4482 if (ip == NULL)
4483 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004484 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004485 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004486 }
you.chenf58b3c92022-06-21 16:53:48 +08004487
qs.xiong9fbf74e2023-03-28 13:38:22 +08004488 if (idx == 1)
4489 {
you.chen0df3e7e2023-05-10 15:56:26 +08004490 ifaName = inner_get_ap_interface_name();
4491 if (ifaName == NULL)
4492 {
4493 RLOGE("[lynq_get_interface_ip] ap name get fail");
4494 return -1;
4495 }
you.chen9ac66392022-08-06 17:01:16 +08004496 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004497 else if (idx != 0)
4498 {
you.chen35020192022-05-06 11:30:57 +08004499 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004500 }
you.chen35020192022-05-06 11:30:57 +08004501
you.chen9ac66392022-08-06 17:01:16 +08004502 if (getifaddrs(&ifaddr_header) == -1)
4503 {
you.chen35020192022-05-06 11:30:57 +08004504 perror("getifaddrs");
4505 return -1;
4506 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004507 }
you.chen35020192022-05-06 11:30:57 +08004508
4509
you.chen9ac66392022-08-06 17:01:16 +08004510 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4511 {
4512 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004513 continue;
you.chen9ac66392022-08-06 17:01:16 +08004514 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4515 {
4516 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4517 {
4518 // is a valid IP4 Address
4519 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4520 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004521 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004522 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004523 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004524 return 0;
4525 }
4526 }
4527 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004528 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004529 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004530 return -1;
you.chen35020192022-05-06 11:30:57 +08004531}
4532
4533int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4534{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004535 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004536 int count;
4537 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004538 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004539 char *split_words[128] = {0};
4540 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4541
4542 CHECK_WPA_CTRL(idx);
4543
4544 DO_REQUEST(lynq_get_mac_cmd);
4545
qs.xiong9fbf74e2023-03-28 13:38:22 +08004546 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4547 {
4548 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004549 return -1;
4550 }
4551
4552 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4553
qs.xiong9fbf74e2023-03-28 13:38:22 +08004554 if (count < 2)
4555 {
you.chen35020192022-05-06 11:30:57 +08004556 return -1;
4557 }
4558
qs.xiong9fbf74e2023-03-28 13:38:22 +08004559 for (i=0; i < strlen(split_words[1]); i++ )
4560 {
4561 if (split_words[1][i] != ' ')
4562 {
you.chen35020192022-05-06 11:30:57 +08004563 break;
4564 }
4565 }
4566
qs.xiongdd6e44c2023-08-08 15:02:53 +08004567 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004568
4569 return 0;
4570}
4571
4572int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4573{
4574// int count;
4575// char *split_words[128] = {0};
4576// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4577
4578// if (rssi == NULL) {
4579// return -1;
4580// }
4581
4582// CHECK_IDX(idx, CTRL_STA);
4583
4584// CHECK_WPA_CTRL(CTRL_STA);
4585
4586// DO_REQUEST(lynq_get_rssi_cmd);
4587
4588// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4589// return -1;
4590// }
4591
4592// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4593
4594// if (count < 2) {
4595// return -1;
4596// }
4597
4598// *rssi = atoi(split_words[1]) * -1;
4599
you.chen35020192022-05-06 11:30:57 +08004600 char lynq_cmd_ret[MAX_RET]={0};
4601
qs.xiongff0ae0f2022-10-11 15:47:14 +08004602/*******change other cmd to get rssi*******
4603 *
4604 *wl rssi ---> wl -i wlan0 rssi
4605 *
4606 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004607 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004608 {
you.chen23c4a5f2023-04-12 16:46:00 +08004609 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004610 return -1;
4611 }
you.chen9f17e4d2022-06-06 17:18:18 +08004612 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004613/****** if got rssi is 0,means sta didn't connected any device****/
4614 if(*rssi == 0)
4615 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004616 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004617 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004618 }
you.chen35020192022-05-06 11:30:57 +08004619
4620 return 0;
4621}
4622
4623int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4624{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004625 RLOGD("enter lynq_get_connect_ap_band\n");
4626 if (band == NULL)
4627 {
you.chen35020192022-05-06 11:30:57 +08004628 return -1;
4629 }
4630
4631 CHECK_IDX(idx, CTRL_STA);
4632 ap_info_s ap;
4633 ap.band = -1;
4634
qs.xiong9fbf74e2023-03-28 13:38:22 +08004635 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4636 {
you.chen35020192022-05-06 11:30:57 +08004637 return -1;
4638 }
4639 *band = ap.band;
4640
4641 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004642}
you.chenf58b3c92022-06-21 16:53:48 +08004643
4644int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4645{
you.chenb95401e2023-05-12 19:39:06 +08004646 int ret;
4647 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004648 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004649
4650 if (ip == NULL)
4651 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004652 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004653 return -1;
4654 }
4655
4656 CHECK_IDX(idx, CTRL_STA);
4657
qs.xionge4cbf1c2023-02-28 18:22:49 +08004658 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004659 {
4660 return -1;
4661 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004662
you.chenb95401e2023-05-12 19:39:06 +08004663 ip[0] = '\0';
4664 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4665 if (ret != 0)
4666 {
4667 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4668 return -1;
4669 }
4670
4671 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4672 {
4673 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004674 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004675 if (ret != 0)
4676 {
4677 ip[0] = '\0';
4678 return 0;
4679 }
4680 else
4681 {
4682 p = strchr(ip, '\n');
4683 if (p != NULL)
4684 {
4685 *p = '\0';
4686 }
4687 }
4688 }
4689 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004690}
4691
qs.xionge02a5252023-09-20 14:00:21 +08004692int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4693{
4694 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4695 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4696}
4697
qs.xiong026c5c72022-10-17 11:15:45 +08004698int lynq_ap_connect_num(int sta_number)
4699{
4700 char lynq_limit_cmd[32]={0};
4701 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004702 if((sta_number < 1 ) && (sta_number > 15))
4703 {
4704 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004705 return -1;
4706 }
4707 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4708 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004709 if(ret != 0)
4710 {
4711 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004712 }
4713 return 0;
4714}
you.chenf58b3c92022-06-21 16:53:48 +08004715
qs.xiong77905552022-10-17 11:19:57 +08004716int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4717{
4718
4719 char lynq_wifi_acs_cmd[128]={0};
4720 char lynq_cmd_mode[128]={0};
4721 char lynq_cmd_slect[128]={0};
4722
qs.xiong9fbf74e2023-03-28 13:38:22 +08004723 if((acs_mode != 2) && (acs_mode != 5))
4724 {
qs.xiong77905552022-10-17 11:19:57 +08004725 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4726 }
4727
qs.xiong9fbf74e2023-03-28 13:38:22 +08004728 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4729 {
qs.xiong77905552022-10-17 11:19:57 +08004730 return -1;
4731 }
4732
4733 CHECK_IDX(idx, CTRL_AP);
4734
4735 CHECK_WPA_CTRL(CTRL_AP);
4736
4737 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4738 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4739 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4740
4741 DO_OK_FAIL_REQUEST(cmd_disconnect);
4742 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4743 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4744 DO_OK_FAIL_REQUEST(cmd_save_config);
4745 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4746
4747 return 0;
4748}
you.chen0f5c6432022-11-07 18:31:14 +08004749//you.chen add for tv-box start
4750static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4751 FILE *fp;
4752 //printf("to exec cmd:%s\n", str_cmd);
4753 if((fp=popen(str_cmd,"r"))==NULL)
4754 {
4755 perror("popen error!");
4756 return -1;
4757 }
4758 if((fread(str_cmd_ret,max_len,1,fp))<0)
4759 {
4760 perror("fread fail!");
4761 fclose(fp);
4762 return -1;
4763 }
4764 fclose(fp);
4765 return 0;
4766}
4767
4768static int get_netmask_length(const char* mask)
4769{
4770 int masklen=0, i=0;
4771 int netmask=0;
4772
4773 if(mask == NULL)
4774 {
4775 return 0;
4776 }
4777
4778 struct in_addr ip_addr;
4779 if( inet_aton(mask, &ip_addr) )
4780 {
4781 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004782 }else
4783 {
you.chen0f5c6432022-11-07 18:31:14 +08004784 netmask = 0;
4785 return 0;
4786 }
4787
4788 while(0 == (netmask & 0x01) && i<32)
4789 {
4790 i++;
4791 netmask = netmask>>1;
4792 }
4793 masklen = 32-i;
4794 return masklen;
4795}
4796
4797static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4798 int mask_len;
4799 char *p;
4800 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004801 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4802 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004803 return -1;
4804 p = strstr(str_cmd_ret, "Mask:");
4805 if (p == NULL)
4806 return -1;
4807 mask_len = get_netmask_length(p + 5);
4808 if (mask_len == 0)
4809 return -1;
4810 p = strstr(str_cmd_ret, "inet addr:");
4811 if (p == NULL)
4812 return -1;
4813 strcpy(tmp, p + 10);
4814 p = strstr(tmp, " ");
4815 if (p != NULL)
4816 *p = '\0';
4817 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4818 return 0;
4819}
4820
4821static void GBWWatchThreadProc() {
4822 int i,n, nloop, nmax, ncheckcount, nidlecount;
4823 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4824 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4825 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4826 char *results[16] = {0};
4827 char str_cmd[256] = {0};
4828 char str_cmd_ret[128] = {0};
4829 char dest_ip[32] = {0};
4830 lastAP1Bytes = lastAP2Bytes = 0;
4831 lastAP1Drop = lastAP2Drop = 0;
4832 lastAP1Speed = lastAP2Speed = 0;
4833 setAP1Speed = 50;
4834 setAP2Speed = 80;
4835 nloop = 0;
4836 nmax = 6;
4837 ncheckcount = nidlecount = 0;
4838
you.chen0df3e7e2023-05-10 15:56:26 +08004839 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004840 {
4841 RLOGE("------gbw thread run\n");
4842 return;
4843 }
4844
qs.xiong9fbf74e2023-03-28 13:38:22 +08004845 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004846 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4847 while (dest_ip[0] == '\0') {
4848 sleep(1);
4849 str_cmd_ret[0] = '\0';
4850 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4851 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4852 if (str_cmd_ret[n] == '\n'){
4853 str_cmd_ret[n] = '\0';
4854 break;
4855 }
4856 }
4857 if (str_cmd_ret[0] != '\0')
4858 {
4859 strcpy(dest_ip, str_cmd_ret);
4860 }
4861 }
4862
you.chenc9928582023-04-24 15:39:37 +08004863 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4864 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4865 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 +08004866 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4867 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004868 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004869 return;
4870 }
you.chenc9928582023-04-24 15:39:37 +08004871 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);
4872 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);
4873 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 +08004874
4875 while (1) {
4876 sleep(1);
4877 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004878 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4879 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4880 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004881 continue;
4882 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004883 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004884 if (n > 9) {
4885 if (strcmp(results[1], "Sent") == 0) {
4886 currAP1Bytes = atoll(results[2]);
4887 }
4888 if (strcmp(results[6], "(dropped") == 0) {
4889 currAP1Drop = atoi(results[7]);
4890 }
4891 }
4892
4893 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004894 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4895 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4896 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004897 continue;
4898 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004899 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004900 if (n > 9) {
4901 if (strcmp(results[1], "Sent") == 0) {
4902 currAP2Bytes = atoll(results[2]);
4903 }
4904 if (strcmp(results[6], "(dropped") == 0) {
4905 currAP2Drop = atoi(results[7]);
4906 }
4907 }
4908
4909 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4910 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4911 lastAP1Bytes = currAP1Bytes;
4912 lastAP2Bytes = currAP2Bytes;
4913 continue;
4914 }
4915
4916 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4917 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4918 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4919 lastAP1Speed = currAP1Speed;
4920 lastAP2Speed = currAP2Speed;
4921 lastAP1Bytes = currAP1Bytes;
4922 lastAP2Bytes = currAP2Bytes;
4923
4924 currSetAP1Speed = setAP1Speed;
4925 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4926 ncheckcount++;
4927 if (ncheckcount > 3) {
4928 ncheckcount = 0;
4929 currSetAP1Speed = 5;
4930 }
4931 }
4932 else {
4933 ncheckcount = 0;
4934 if (currAP1Speed < 5)
4935 nidlecount++;
4936 else
4937 nidlecount = 0;
4938
4939 }
4940
4941 if (nidlecount > 60 ){
4942 currSetAP1Speed = 50;
4943 }
4944
4945 if (currSetAP1Speed != setAP1Speed) {
4946 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004947 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4948 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004949 }
4950 }
4951}
4952
4953int enableGBW(const char* mac) {
4954 int i,len;
4955 char get_ipaddr_cmd[128]={0};
4956 ap_info_s *ap;
4957 device_info_s * list;
4958
4959 if (mac == NULL || g_gbw_enabled == 1)
4960 return -1;
4961 len = strlen(mac);
4962 g_gbw_mac = malloc(len + 1);
4963 for(i=0;i<len;i++) {
4964 if (mac[i] >= 'A' && mac[i] <= 'Z')
4965 {
4966 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4967 }
4968 else
4969 g_gbw_mac[i] = mac[i];
4970 }
4971 g_gbw_mac[i] = '\0';
4972 g_gbw_enabled = 1;
4973
4974 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4975 if (system(get_ipaddr_cmd) == 0) {
4976 //startGBW();
4977 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4978 for (i=0;i<len;i++) {
4979 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4980 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4981 startGBW();
4982 }
4983 free(ap);
4984 free(list);
4985 }
4986 }
4987 return 0;
4988}
4989
4990int disableGBW() {
4991 stopGBW();
4992 free(g_gbw_mac);
4993 g_gbw_mac = NULL;
4994 g_gbw_enabled = 1;
4995 return 0;
4996}
4997
4998static int startGBW() {
4999 if (g_gbw_watcher_pid != 0) {
5000 stopGBW();
5001 }
5002 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5003}
5004
5005static int stopGBW() {
5006 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005007 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005008 pthread_cancel(g_gbw_watcher_pid);
5009 pthread_join(g_gbw_watcher_pid, &retval);
5010 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005011 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5012 if (s_ap_iterface_name[0] != '\0')
5013 {
5014 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5015 system(cmd);
5016 }
you.chen0f5c6432022-11-07 18:31:14 +08005017}
5018//you.chen add for tv-box end