blob: 082f9bc2599a8bc6bc21748bf9981dec73769d62 [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
qs.xiong9fbf74e2023-03-28 13:38:22 +080034#undef LOG_TAG
35#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080036#define MAX_CMD 128
37#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040038#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080039#define CTRL_STA 0
40#define CTRL_AP 1
41#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080042#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080043#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
qs.xiong44fac672023-08-29 16:15:55 +080049pthread_t g_ap_tmp_watcher_pid = 0;
50volatile int g_ap_tmp_watcher_stop_flag = 0;
51
you.chen35020192022-05-06 11:30:57 +080052pthread_t g_sta_watcher_pid = 0;
53volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080054volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080055volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080056volatile int g_sta_conncet_status_flag = 0;
you.chen35020192022-05-06 11:30:57 +080057
qs.xiongfcc914b2023-07-06 21:16:20 +080058pthread_t g_sta_auto_watcher_pid = 0;
59volatile int g_sta_auto_watcher_stop_flag = 0;
60volatile int g_sta_auto_scan_finish_flag = 1;
61volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080062void * g_ap_callback_priv = NULL;
63AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
64void * g_sta_callback_priv = NULL;
65STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080066void * g_sta_auto_callback_priv = NULL;
67STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080068
69//const char * CTRL_PATH="/var/run/wpa_supplicant";
70const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
71//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
72const char * cmd_list_networks = "LIST_NETWORKS";
73const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080074const char * cmd_disconnect = "DISCONNECT";
75const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080076const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080077const char * STATE_COMPLETED = "COMPLETED";
qs.xiong5a2ba932023-09-13 16:30:21 +080078const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080079const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080080
you.chenf711c8a2023-04-13 13:49:45 +080081const char * cmd_ping = "PING";
82const char * rsp_pong = "PONG";
83const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
84const int MAX_IDLE_COUNT = 600; // 60s
85
you.chenc9928582023-04-24 15:39:37 +080086const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
87const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
88const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
89const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
90const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
91
92static char s_ap_iterface_name[64] = {0};
93
you.chend2fef3f2023-02-13 10:50:35 +080094struct local_wpa_ctrl{
95 struct wpa_ctrl *ctrl;
96 pthread_mutex_t mutex;
97};
98
qs.xiongb37f8c42023-09-13 21:21:58 +080099int g_history_disconnect_valid_num = 0;
100int g_history_disconnect_net[128];
you.chen70f377f2023-04-14 18:17:09 +0800101
you.chend2fef3f2023-02-13 10:50:35 +0800102static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800103static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
104static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800105// add for auto connect
106static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800107
108static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800109
you.chen0f5c6432022-11-07 18:31:14 +0800110//you.chen add for tv-box start
111volatile int g_gbw_enabled = 0;
112char * g_gbw_mac = NULL;
113pthread_t g_gbw_watcher_pid = 0;
114static int startGBW();
115static int stopGBW();
116//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800117
118typedef struct __curr_status_info {
119 ap_info_s *ap;
120 char * state;
121 int net_no;
122}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400123
you.chen70f377f2023-04-14 18:17:09 +0800124typedef enum {
125 INNER_STA_STATUS_INIT = 0,
126 INNER_STA_STATUS_CONNECTING,
127 INNER_STA_STATUS_ASSOCIATING,
128 INNER_STA_STATUS_ASSOCIATED,
129 INNER_STA_STATUS_CONNECTED,
130 INNER_STA_STATUS_DISCONNECTING,
131 INNER_STA_STATUS_DISCONNECTED,
132 INNER_STA_STATUS_CANCEL,
133}inner_sta_status_s;
134
135static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
136static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6e724162023-10-19 19:10:01 +0800137volatile inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
you.chen70f377f2023-04-14 18:17:09 +0800138static error_number_s s_sta_error_number = -1;
139static char s_sta_current_connecting_ssid[64] = {0};
140static struct timespec s_sta_connect_timeout;
141const int MAX_CONNNECT_TIME = 15; // second
142pthread_t g_global_watcher_pid = 0;
143static int s_service_invoke_timeout_cnt=0;
144const int FAKE_MAX_INT_VALUE = 99999;
145
146static void notify_service_invoke_fail(int error)
147{
148 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
149 pthread_mutex_lock(&s_global_check_mutex);
150 if (error == -2) //timeout
151 {
152 s_service_invoke_timeout_cnt++;
153 if (s_service_invoke_timeout_cnt > 10)
154 {
155 pthread_cond_signal(&s_global_check_cond);
156 }
157 }
158 else if (error == -1)
159 {
160 // check if can connect wpa service
161 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
162 if (lynq_wpa_ctrl == NULL)
163 {
164 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
165 pthread_cond_signal(&s_global_check_cond);
166 }
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
169 if (lynq_wpa_ctrl == NULL)
170 {
171 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
172 pthread_cond_signal(&s_global_check_cond);
173 }
174 wpa_ctrl_close(lynq_wpa_ctrl);
175 }
176
177 pthread_mutex_unlock(&s_global_check_mutex);
178}
179
you.chenc9928582023-04-24 15:39:37 +0800180static int system_call_v(const char * fmt, ...)
181{
182 char str_cmd[256] = {0};
183 va_list args;
184 va_start(args, fmt);
185 vsprintf(str_cmd, fmt, args);
186 va_end(args);
187 printf("system call----------%s\n", str_cmd);
188 return system(str_cmd);
189}
190
you.chen0df3e7e2023-05-10 15:56:26 +0800191static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
192
193static const char * inner_get_ap_interface_name()
194{
195 char * p;
196 char cmd[128]={0};
197
198 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
199 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
200 {
201 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
202 return NULL;
203 }
204 p = strchr(s_ap_iterface_name, ' ');
205 if (NULL != p)
206 {
207 *p = '\0';
208 }
209 p = strchr(s_ap_iterface_name, '\n');
210 if (NULL != p)
211 {
212 *p = '\0';
213 }
214 if (s_ap_iterface_name[0] == '\0')
215 {
216 return NULL;
217 }
218
219 return s_ap_iterface_name;
220}
221
you.chen70f377f2023-04-14 18:17:09 +0800222static void check_tether_and_notify()
223{
224 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800225 if (inner_get_ap_interface_name() == NULL || 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chen70f377f2023-04-14 18:17:09 +0800226 {
227 return;
228 }
229 pthread_mutex_lock(&s_global_check_mutex);
230 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
231 pthread_cond_signal(&s_global_check_cond);
232 pthread_mutex_unlock(&s_global_check_mutex);
233}
234
you.chend2fef3f2023-02-13 10:50:35 +0800235static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
236 char *reply, size_t *reply_len,
237 void (*msg_cb)(char *msg, size_t len))
238{
239 int ret;
240 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800241 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800242 return -1;
243 }
244 pthread_mutex_lock(&ctrl->mutex);
245 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
246 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800247 if (ret != 0)
248 {
249 notify_service_invoke_fail(ret);
250 }
you.chend2fef3f2023-02-13 10:50:35 +0800251 return ret;
252}
253
254static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
255 int repeat_cnt;
256 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
257 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800258 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800259 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
260 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
261// printf("wait enable finish\n");
262 usleep(500 * 1000);
263 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
264 }
265 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800266 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800267 goto out_addr;
268 }
269 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
270 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
271 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800272 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800273 goto out_addr;
274 }
275 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
276 }
277 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
278out_addr:
279 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
280 return lynq_wpa_ctrl;
281}
282
qs.xiong97fa59b2022-04-07 05:41:29 -0400283#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400284{\
you.chen35020192022-05-06 11:30:57 +0800285 perror((str));\
286 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400287}
288
you.chen35020192022-05-06 11:30:57 +0800289#define CHECK_IDX(idx, type) do { \
290 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
291 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800292 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800293 return -1; \
294 } \
295 }while (0)
296
297#define CHECK_WPA_CTRL(index) int ret = 0;\
298 size_t reply_len = MAX_RET; \
299 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800300 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800301 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800302 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
303 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800304 }while(0)
305
306#define DO_REQUEST(cmd_str) do { \
307 reply_len = MAX_RET;\
308 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800310 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800311 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800312 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800313 return ret; \
314 } \
315 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800316 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800317 }while(0)
318
319#define DO_OK_FAIL_REQUEST(cmd_str) do { \
320 DO_REQUEST(cmd_str); \
321 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800322 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800323 return -1; \
324 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800325 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800326 return -1; \
327 } \
328 }while (0)
329
330
you.chenf711c8a2023-04-13 13:49:45 +0800331static int check_connection(struct wpa_ctrl * wpa_ctrl)
332{
333 size_t reply_len = MAX_RET;
334 char cmd_reply[MAX_RET]={0};
335 int ret;
336
337 RLOGD("check_connection [%p]", wpa_ctrl);
338 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
339
340 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
341 {
342 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chen70f377f2023-04-14 18:17:09 +0800343 if (ret != 0)
344 {
345 notify_service_invoke_fail(ret);
346 }
you.chenf711c8a2023-04-13 13:49:45 +0800347 return -1;
348 }
349
350 return 0;
351}
352
353/**
354 * @brief check_pending_msg
355 * @param lynq_wpa_ctrl
356 * @return 1 has msg, 0 no msg, -1 error
357 */
358static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
359{
360 int ret;
361
362 if (*pp_lynq_wpa_ctrl == NULL) // need connect
363 {
364 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
365 if (*pp_lynq_wpa_ctrl == NULL)
366 {
367 usleep(SLEEP_TIME_ON_IDLE);
368 return -1;
369 }
370
371 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
372 if (ret == 0) // attach success
373 {
374 *started_flag = 1;
375 }
376 else
377 {
you.chen70f377f2023-04-14 18:17:09 +0800378 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800379 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
380 *pp_lynq_wpa_ctrl = NULL;
381 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800382 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800383 usleep(SLEEP_TIME_ON_IDLE);
384 return -1;
385 }
386 }
387
388 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
389 if ( ret == 0) // no pending messages
390 {
391 usleep(SLEEP_TIME_ON_IDLE);
392 *idle_count += 1;
393 if (*idle_count > MAX_IDLE_COUNT)
394 {
395 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
396 {
397 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
398 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
399 *pp_lynq_wpa_ctrl = NULL;
400 *idle_count = 0;
401 return -1;
402 }
403 *idle_count = 0;
404 }
405 return 0;
406 }
407 else if ( ret == -1) // on error
408 {
409 RLOGE("[wifi error]sta wpa_ctrl_pending");
410 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
411 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
412 *pp_lynq_wpa_ctrl = NULL;
413 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800414 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800415 return -1;
416 }
417
418 *idle_count = 0;
419 return 1;
420}
421
you.chen6d247052023-06-01 16:39:54 +0800422static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
423{
424 pthread_mutex_lock(&s_ap_callback_mutex);
425 if (g_ap_callback_func != NULL)
426 g_ap_callback_func(g_ap_callback_priv, status);
427 pthread_mutex_unlock(&s_ap_callback_mutex);
428}
429
you.chen35020192022-05-06 11:30:57 +0800430static void APWatcherThreadProc() {
431 size_t len = MAX_RET;
432 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800433 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800434
you.chen6c2dd9c2022-05-16 17:55:28 +0800435 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800436 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800437
qs.xiong9fbf74e2023-03-28 13:38:22 +0800438 while (g_ap_watcher_stop_flag == 0)
439 {
you.chenf711c8a2023-04-13 13:49:45 +0800440 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
441 {
you.chen70f377f2023-04-14 18:17:09 +0800442 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
443 {
444 check_tether_and_notify();
445 }
446
you.chen35020192022-05-06 11:30:57 +0800447 continue;
448 }
you.chenf711c8a2023-04-13 13:49:45 +0800449
you.chen6c2dd9c2022-05-16 17:55:28 +0800450 memset(msg_notify, 0, MAX_RET);
451 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800452 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800453 {
you.chen35020192022-05-06 11:30:57 +0800454 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800455 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800456 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800457 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800458 {
you.chen6d247052023-06-01 16:39:54 +0800459 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800460 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800461 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800462 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
463 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800464 {
you.chen0f5c6432022-11-07 18:31:14 +0800465 stopGBW();
466 }
467 }
you.chen35020192022-05-06 11:30:57 +0800468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800469 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800470 {
you.chen6d247052023-06-01 16:39:54 +0800471 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800472 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800473 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800474 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
475 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800476 {
you.chen0f5c6432022-11-07 18:31:14 +0800477 startGBW();
478 }
479 }
you.chen35020192022-05-06 11:30:57 +0800480 }
qs.xiongbaec30f2023-09-20 13:10:15 +0800481 else if ( strstr(msg_notify, "Failed to start AP functionality") != NULL )
qs.xiong31163d62023-07-11 18:54:40 +0800482 {
483 RLOGD("APWatcherThreadProc ap------> service error");
484 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
485 }
486 else
487 {
488 RLOGD("APWatcherThreadProc ap------> going on check next msg");
489 }
you.chenf711c8a2023-04-13 13:49:45 +0800490 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800491 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
492 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800493 if (lynq_wpa_ctrl != NULL)
494 {
you.chen92fd5d32022-05-25 10:09:47 +0800495 wpa_ctrl_detach(lynq_wpa_ctrl);
496 wpa_ctrl_close(lynq_wpa_ctrl);
497 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400498}
499
you.chen70f377f2023-04-14 18:17:09 +0800500static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
501{
502 char * p;
503 const char * try_associat_flag = "Trying to associate";
504 const char * associated_flag = "Associated with ";
505
506 pthread_mutex_lock(&s_global_check_mutex);
507 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
508 {
509 pthread_mutex_unlock(&s_global_check_mutex);
510 return;
511 }
512
you.chen6e724162023-10-19 19:10:01 +0800513 // youchen@2023-10-17 add for "not notify connect fail directly" begin
514 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
515 {
516 s_sta_error_number = error_num;
517 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
518 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
519 pthread_cond_signal(&s_global_check_cond);
520 pthread_mutex_unlock(&s_global_check_mutex);
521 return;
522 }
523 // youchen@2023-10-17 add for "not notify connect fail directly" end
524
you.chen70f377f2023-04-14 18:17:09 +0800525 if (state == LYNQ_WIFI_STATUS_EGNORE)
526 {
527 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
528 {
529 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
530 }
531 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
532 {
533 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
534 }
535 }
536 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
537 {
538 s_sta_error_number = error_num;
539 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
540 {
541 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800542 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800543 pthread_cond_signal(&s_global_check_cond);
544 }
545 }
546 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
547 {
548 s_sta_status = INNER_STA_STATUS_CONNECTED;
you.chen6e724162023-10-19 19:10:01 +0800549 RLOGD("inner_check_connect_error line: %d, curr state %d, %d %d ------",__LINE__, state, s_sta_status, s_sta_error_number);
you.chen70f377f2023-04-14 18:17:09 +0800550 pthread_cond_signal(&s_global_check_cond);
551 }
552 pthread_mutex_unlock(&s_global_check_mutex);
553}
554
qs.xiongb37f8c42023-09-13 21:21:58 +0800555static int lynq_split(char * str, int len, char delimiter, char * results[]);
556static inline char inner_convert_char(char in);
557static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len);
558static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid);
559
560
561
qs.xiong44fac672023-08-29 16:15:55 +0800562/*
563just tmp add for fix sta connect ap fail check ap connect info
564return 0 --->Current no sta device connect this AP
565*/
566static int lynq_connected_ap_sta_status() {
567
568 FILE *fp;
569 size_t i = 0;
570 int ret;
571 char lynq_cmd_ret[MAX_RET]={0};
572
573 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
574 {
575 perror("popen error!");
576 return -1;
577 }
578 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
579 {
580 perror("fread fail!");
581 ret=pclose(fp);
582 if(ret == -1)
583 perror("close file faild");
584 return -1;
585 }
586 if( strlen(lynq_cmd_ret) < MAC_LEN)
587 {
588 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
589 ret=pclose(fp);
590 if(ret==-1)
591 {
592 perror("close file faild");
593 }
594 return 0;
595 }else{
596 ret=pclose(fp);
597 if(ret==-1)
598 {
599 perror("close file faild");
600 }
601 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
602 return 1;
603 }
604}
605
606/*
607 just tmp add for fix sta connect ap fail; check fw status
608 return 1 ----> fw status error; need wl down/up
609*/
610static int check_current_fw_status() {
611
612 FILE *fp;
613 FILE *fp1;
614 size_t i = 0;
615 int ret;
616 char lynq_cmd_ret_2g[MAX_RET]={0};
617 char lynq_cmd_ret_5g[MAX_RET]={0};
618
619 const char * fw_status = "0x0096"; //0x0096 is normal fw status
620
621 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
622 {
623 perror("popen error!");
624 return -1;
625 }
626 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
627 {
628 perror("fread fail!");
629 if(pclose(fp) == -1)
630 perror("close fp file faild");
631 return -1;
632 }
633
634 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
635 {
636 perror("popen error!");
637 if(pclose(fp) == -1)
638 perror("clsoe fp file faild");
639 return -1;
640 }
641 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
642 {
643 perror("fread fail!");
644 if(pclose(fp1) == -1)
645 perror("clsoe fp1 file faild");
646 if(pclose(fp) == -1)
647 perror("clsoe fp file faild");
648 return -1;
649 }
650
651 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
652 {
653 ret=pclose(fp);
654 if(ret==-1)
655 {
656 perror("close fp file faild");
657 }
658 ret=pclose(fp1);
659 if(ret==-1)
660 {
661 perror("close fp1 file faild");
662 }
663 return 0;
664 }else
665 {
666 ret=pclose(fp);
667 if(ret==-1)
668 {
669 perror("close file faild");
670 }
671 if(pclose(fp1) == -1)
672 {
673 perror("clsoe file fp1 faild");
674 }
675 RLOGD("current fw status --error--");
676 return 1;
677 }
678}
679
qs.xiong1d4263a2023-09-06 10:46:23 +0800680/*
681eg: wl counters info
682sh-3.2# wl counters
683counters_version 30
684datalen 1648
685Slice_index: 0
686reinitreason_counts: 0(0) 1(0) 2(3) 3(0) 4(0) 5(0) 6(0) 7(0) 8(0) 9(0) 10(0) 11(0) 12(0) 13(0) 14(2) 15(0) 16(0) 17(0) 18(0) 19(0) 20(0) 21(0) 22(0) 23(0) 24(0) 25(0) 26(0) 27(0) 28(0) 29(0) 30(0) 31(0) 32(0) 33(0) 34(0) 35(0) 36(0) 37(0) 38(0) 39(0) 40(0) 41(0) 42(0) 43(0) 44(0) 45(0) 46(0) 47(0) 48(0) 49(0) 50(0) 51(0) 52(0) 53(0) 54(0)
687reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
688
689check reinit status
690return 0 ===> fw did wl reinit cmd
691*/
692static int check_current_reinit_info()
693{
694 FILE *fp;
695 int ret;
696 char lynq_cmd_ret[MAX_RET]={0};
697 char * dest;
698 char destid[3]={0};
699 if((fp=popen("wl counters","r"))==NULL)
700 {
701 perror("popen error!");
702 return -1;
703 }
704 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
705 {
706 perror("fread fail!");
707 if(pclose(fp) == -1)
708 {
709 perror("close fp file faild");
710 }
711 return -1;
712 }
713 dest = strstr(lynq_cmd_ret,"reinit ");
714 if(dest != NULL)
715 {
716 dest +=strlen("reinit ");
717 RLOGD("current get dest str is %s",dest);
718 memcpy(destid,dest,2);
719 ret = atoi(destid);
720 RLOGD("get current wl counters cmd return counts is %d",ret);
721 if( ret != 0 )
722 {
723 RLOGD("current fw did run cmd wl reinit");
724 if( pclose(fp) == -1 )
725 {
726 perror("close fp file faild");
727 }
728 return 0;
729 }
730 }
731 if( pclose(fp) == -1 )
732 {
733 perror("close fp file faild");
734 }
735 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
736 return -1;
737}
738
qs.xiong44fac672023-08-29 16:15:55 +0800739static void APTmpWatcherThreadProc() {
740
741 int i = 0;
742 int delytime = 300;
743 g_ap_tmp_watcher_stop_flag = 0;
744
745 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
746 while(1)
747 {
748 sleep(1);
749 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800750 if ( (i % 30) == 0 )
751 {
752 if ( check_current_reinit_info() == 0 )
753 {
754 system("wl reset_cnts");
755 system("wl down");
756 system("wl up");
757 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
758 }
759 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800760 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800761 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800762 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800763 {
764 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
765 {
766 system("wl down");
767 system("wl up");
768 }
qs.xiong44fac672023-08-29 16:15:55 +0800769 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800770 }
771 if ( i == delytime )
772 {
qs.xiong44fac672023-08-29 16:15:55 +0800773 i = 0;
774 }
775 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
776 {
777 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
778 return;
779 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800780
qs.xiong44fac672023-08-29 16:15:55 +0800781 }
782
783}
784
qs.xiongf0128b12023-06-29 17:29:39 +0800785static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
786{
787 char LYNQ_DISABLE_CMD[128]={0};
788
789 CHECK_IDX(idx, CTRL_STA);
790 CHECK_WPA_CTRL(CTRL_STA);
791
792 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800793 RLOGD("LYNQ_DISABLE_CMD is:%s\n",LYNQ_DISABLE_CMD);
qs.xiongf0128b12023-06-29 17:29:39 +0800794 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800795
qs.xiongf0128b12023-06-29 17:29:39 +0800796 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800797
qs.xiongf0128b12023-06-29 17:29:39 +0800798}
799
qs.xiongb37f8c42023-09-13 21:21:58 +0800800static int lynq_wifi_sta_enable_network(lynq_wifi_index_e idx,int networkid)
801{
802 char LYNQ_ENABLE_CMD[128]={0};
803
804 CHECK_IDX(idx, CTRL_STA);
805 CHECK_WPA_CTRL(CTRL_STA);
806
807
808 sprintf(LYNQ_ENABLE_CMD,"ENABLE_NETWORK %d",networkid);
qs.xiongb5dab082023-10-13 14:43:41 +0800809 RLOGD("LYNQ_ENABLE_CMD is:%s\n",LYNQ_ENABLE_CMD);
qs.xiongb37f8c42023-09-13 21:21:58 +0800810 DO_OK_FAIL_REQUEST(LYNQ_ENABLE_CMD);
811
812 return 0;
813
814}
815
816static int lynq_tmp_enable_network(lynq_wifi_index_e idx,int net_no_list[],int len)
817{
818
819 int index,networkid;
820
821 for ( index = 0; index < len ;index++)
822 {
823 networkid = net_no_list[index];
qs.xiongb8518cd2023-09-22 18:43:42 +0800824 if( lynq_wifi_sta_enable_network(idx,networkid) != 0 )
qs.xiongb37f8c42023-09-13 21:21:58 +0800825 {
826 RLOGE("[wifi]lynq_tmp_enable_network id is %d fail",networkid);
827 }
828 RLOGD("[wifi]lynq_tmp_enable_network id is %d",networkid);
829 }
830 return 0;
831
832}
833
834
835/*
836 dis_net_list user disconnect list
837*/
838static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
839{
840 int count,ncount,index;
841 int flag = 0;
842 int merge_index = 0;
843 int net_no_list[128];
844
845 index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
846 for( count = 0; count < index; count++)
847 {
848 for(ncount = 0; ncount < valid_num; ncount++)
849 {
850 if(net_no_list[count] == dis_net_list[ncount])
851 {
852 RLOGD("[wifi]this is history disconnect idx ----> ",net_no_list[count]);
853 flag = 1;
854 break;
855 }
856 }
857 if( flag != 1 )
858 {
859 out[merge_index] = net_no_list[count];
860 merge_index ++;
861 }
862 flag = 0;
863 }
864 * outlen =merge_index;
865 RLOGD("[wifi]lynq_two_arr_merge get len is -----> %d",* outlen);
866 return;
867}
qs.xiongf0128b12023-06-29 17:29:39 +0800868
qs.xiong455c30b2023-04-12 11:40:02 +0800869void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
870{
871 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800872 char *wpanetid;
873 char destid[3] = {0};
874 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800875 *error = LYNQ_WAIT_CONNECT_ACTIVE;
876 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
877 {
878 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
879 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
880 return;
881 }
882
qs.xiong20202422023-09-06 18:01:18 +0800883 if (strstr(modify, "Trying to associate") != NULL)
884 {
885 RLOGD("Current sta is Trying to associate");
886 *state = LYNQ_WIFI_STATUS_EGNORE;
887 g_sta_conncet_status_flag = 1;
888 return;
889 }
890
qs.xiong455c30b2023-04-12 11:40:02 +0800891 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
892 {
893 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
894 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800895 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800896 return;
897 }
898
qs.xiong1e81dfa2023-09-27 15:52:37 +0800899 if (strstr(modify,"CTRL-EVENT-AUTH-REJECT") != NULL)
900 {
901 *error = LYNQ_PSW_ERROR;
902 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
903 RLOGD("CTRL-EVENT-AUTH-REJECT state:%d,error:%d\n",*state,*error);
904 g_sta_conncet_status_flag = 0;
905 return;
906 }
qs.xiong455c30b2023-04-12 11:40:02 +0800907 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
908 {
qs.xiongf0128b12023-06-29 17:29:39 +0800909 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
910 wpanetid = strstr(modify,"id=");
911 if ( wpanetid != NULL )
912 {
913 wpanetid +=strlen("id=");
914 memcpy(destid,wpanetid,2);
915 tmpdisid = atoi(destid);
916
917 }
qs.xiong455c30b2023-04-12 11:40:02 +0800918 pReason = strstr(modify, "reason=");
919 if (pReason != NULL)
920 {
921 pReason += strlen("reason=");
922 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
923 {
924 *error = LYNQ_TIME_OUT;
925 }
926 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
927 {
928 *error = LYNQ_PSW_ERROR;
qs.xiong7d24bf52023-09-20 13:22:35 +0800929 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
qs.xiongf0128b12023-06-29 17:29:39 +0800930 // tmp fix sta autoconnect connect and disconnect
you.chen6e724162023-10-19 19:10:01 +0800931 // you.chen@2023-10-17 only disable network during autoconnect
932 if(tmpdisid != -1 && s_sta_status == INNER_STA_STATUS_INIT && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
qs.xiongf0128b12023-06-29 17:29:39 +0800933 {
934 RLOGE("stop wlan0 network %d fail",tmpdisid);
935 }
qs.xiong455c30b2023-04-12 11:40:02 +0800936 }
937 else
938 {
939 *error = LYNQ_UNSPECIFIED_REASON;
940 }
qs.xiong455c30b2023-04-12 11:40:02 +0800941 }
942 else
943 {
944 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800945 }
qs.xiongf0128b12023-06-29 17:29:39 +0800946 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800947 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800948 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800949
950 }
951
952 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
953 {
954 *error = LYNQ_NOT_FIND_AP;
955 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
956 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800957 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800958 return;
959 }
960
961
962 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
963 {
964 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
965 pReason = strstr(modify, "status_code=");
966 if (pReason != NULL)
967 {
968 pReason += strlen("status_code=");
969 if (memcmp(pReason, "17", 2) == 0)
970 {
971 *error = LYNQ_AP_UNABLE_HANDLE;
972 }
973 else if (memcmp(pReason, "1",1) == 0)
974 {
975 *error = LYNQ_UNSPECIFIED_REASON;
976 }
977 else
978 {
979 *error = LYNQ_UNSPECIFIED_REASON;
980 }
981
982 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
983 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800984 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800985 return;
986 }
987 else
988 {
989 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
990 *error = LYNQ_UNSPECIFIED_REASON;
991 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
992 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
993 return;
994 }
995 }
996
997 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
998 {
999 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1000 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1001 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1002 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
1003 return;
1004 }
1005
1006 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1007 {
1008 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1009 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1010 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1011 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
1012 return;
1013 }
1014
you.chen32cb31e2023-04-13 14:05:45 +08001015 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +08001016 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +08001017 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +08001018 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
1019 return;
1020
1021}
1022
qs.xiongfcc914b2023-07-06 21:16:20 +08001023void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
1024{
1025 char *pReason;
1026 char *wpanetid;
1027 char destid[3];
1028 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1029 *networkid = -1;
1030 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
1031 {
1032 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
1033 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
1034 return;
1035 }
1036 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
1037 {
1038 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
1039 wpanetid = strstr(modify,"id=");
1040 if ( wpanetid != NULL )
1041 {
1042 wpanetid +=strlen("id=");
1043 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
1044 if (memcpy(destid,wpanetid,2) != NULL)
1045 {
1046 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
1047 *networkid = atoi(destid);
1048 RLOGD("get networkid is %d\n",*networkid);
1049 }
1050 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
1051 }
1052 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
1053 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1054 return;
1055 }
1056 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
1057 {
1058 wpanetid = strstr(modify,"id=");
1059 if ( wpanetid != NULL )
1060 {
1061 wpanetid +=strlen("id=");
1062 if (memcpy(destid,wpanetid,2) != NULL)
1063 {
1064 *networkid = atoi(destid);
1065 RLOGD("get networkid is %d\n",*networkid);
1066 }
1067 }
1068 pReason = strstr(modify, "reason=");
1069 if (pReason != NULL)
1070 {
1071 pReason += strlen("reason=");
1072 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
1073 {
1074 *error = LYNQ_TIME_OUT;
1075 }
1076 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
1077 {
1078 *error = LYNQ_PSW_ERROR;
1079 }
1080 else
1081 {
1082 *error = LYNQ_UNSPECIFIED_REASON;
1083 }
1084 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1085 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1086 return;
1087 }
1088 else
1089 {
1090 *error = LYNQ_UNSPECIFIED_REASON;
1091 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
1092 return;
1093 }
1094 }
1095 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
1096 {
1097 wpanetid = strstr(modify,"id=");
1098 if ( wpanetid != NULL )
1099 {
1100 wpanetid +=strlen("id=");
1101 if (memcpy(destid,wpanetid,2) != NULL)
1102 {
1103 *networkid = atoi(destid);
1104 RLOGD("get networkid is %d\n",*networkid);
1105 }
1106 }
1107 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1108 pReason = strstr(modify, "status_code=");
1109 if (pReason != NULL)
1110 {
1111 pReason += strlen("status_code=");
1112 if (memcmp(pReason, "17", 2) == 0)
1113 {
1114 *error = LYNQ_AP_UNABLE_HANDLE;
1115 }
1116 else if (memcmp(pReason, "1",1) == 0)
1117 {
1118 *error = LYNQ_UNSPECIFIED_REASON;
1119 }
1120 else
1121 {
1122 *error = LYNQ_UNSPECIFIED_REASON;
1123 }
1124 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1125 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1126 return;
1127 }
1128 else
1129 {
1130 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1131 *error = LYNQ_UNSPECIFIED_REASON;
1132 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001133 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001134 return;
1135 }
1136 }
1137 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1138 {
1139 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1140 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1141 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1142 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1143 return;
1144 }
1145 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1146 {
1147 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1148 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1149 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1150 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1151 return;
1152 }
1153 RLOGD("EVENT : %s\n", modify);
1154 *error = LYNQ_UNSPECIFIED_REASON;
1155 *state = LYNQ_WIFI_STATUS_EGNORE;
1156 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1157 return;
1158}
you.chen70f377f2023-04-14 18:17:09 +08001159static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1160{
you.chen6d247052023-06-01 16:39:54 +08001161 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001162 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1163 {
1164 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1165 g_sta_callback_func(g_sta_callback_priv, state, error);
1166 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1167 }
you.chen6d247052023-06-01 16:39:54 +08001168 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001169}
qs.xiongfcc914b2023-07-06 21:16:20 +08001170static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1171{
1172 pthread_mutex_lock(&s_sta_callback_mutex);
1173 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1174 {
1175 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1176 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1177 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1178 }
1179 pthread_mutex_unlock(&s_sta_callback_mutex);
1180}
you.chen70f377f2023-04-14 18:17:09 +08001181
you.chen35020192022-05-06 11:30:57 +08001182static void STAWatcherThreadProc() {
1183 size_t len = MAX_RET;
1184 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001185 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001186 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001187 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001188
you.chen6c2dd9c2022-05-16 17:55:28 +08001189 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001190 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001191
you.chen70f377f2023-04-14 18:17:09 +08001192 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001193 while (g_sta_watcher_stop_flag == 0)
1194 {
you.chenf711c8a2023-04-13 13:49:45 +08001195 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001196 {
you.chen35020192022-05-06 11:30:57 +08001197 continue;
1198 }
you.chenf711c8a2023-04-13 13:49:45 +08001199
you.chen6c2dd9c2022-05-16 17:55:28 +08001200 memset(msg_notify, 0, MAX_RET);
1201 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001202 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001203 {
you.chen35020192022-05-06 11:30:57 +08001204 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001205 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1206 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001207 {
you.chen35020192022-05-06 11:30:57 +08001208 g_sta_scan_finish_flag = 1;
1209 }
1210
qs.xiong9fbf74e2023-03-28 13:38:22 +08001211 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001212 {
you.chen35020192022-05-06 11:30:57 +08001213 continue;
1214 }
qs.xiong455c30b2023-04-12 11:40:02 +08001215 get_state_error(msg_notify,&state,&error);
you.chen6e724162023-10-19 19:10:01 +08001216 // youchen@2023-10-17 add for "not notify connect fail directly" begin
1217 if (state == LYNQ_WIFI_STA_STATUS_CONNECT_FAIL)
1218 {
1219 notify_connect_status(LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
1220 }
1221 else
1222 {
1223 notify_connect_status(state, error);
1224 }
1225 // youchen@2023-10-17 add for "not notify connect fail directly" end
you.chen70f377f2023-04-14 18:17:09 +08001226
1227 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001228 {
you.chen70f377f2023-04-14 18:17:09 +08001229 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001230 if (last_state != state)
1231 {
1232 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1233 {
1234 system_call_v("%s %s", sta_status_change_script, "connect");
1235 }
1236 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1237 {
1238 system_call_v("%s %s", sta_status_change_script, "disconnect");
1239 }
1240 }
you.chenc9928582023-04-24 15:39:37 +08001241 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001242 }
you.chen35020192022-05-06 11:30:57 +08001243 }
1244 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001245 if (lynq_wpa_ctrl != NULL)
1246 {
you.chen92fd5d32022-05-25 10:09:47 +08001247 wpa_ctrl_detach(lynq_wpa_ctrl);
1248 wpa_ctrl_close(lynq_wpa_ctrl);
1249 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001250}
qs.xiongfcc914b2023-07-06 21:16:20 +08001251static void STAAutoWatcherThreadProc() {
1252 size_t len = MAX_RET;
1253 char msg_notify[MAX_RET];
1254 error_number_s error;
you.chen6e724162023-10-19 19:10:01 +08001255 lynq_wifi_sta_status_s state;
qs.xiongfcc914b2023-07-06 21:16:20 +08001256 int idle_count = 0;
1257 int networkid;
1258 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1259 g_sta_auto_watcher_stop_flag = 0;
1260 RLOGD("STAAutoWatcherThreadProc thread started ------");
1261 while (g_sta_auto_watcher_stop_flag == 0)
1262 {
1263 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1264 {
1265 continue;
1266 }
1267 memset(msg_notify, 0, MAX_RET);
1268 len = MAX_RET;
1269 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1270 {
1271 msg_notify[len+1] = '\0';
1272 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1273 if (strstr(msg_notify, state_scan_result) != NULL)
1274 {
1275 g_sta_auto_scan_finish_flag = 1;
1276 }
1277 if (g_sta_auto_callback_func == NULL)
1278 {
1279 continue;
1280 }
1281 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1282 notify_auto_connect_status(state, error,networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001283 }
1284 }
1285 if (lynq_wpa_ctrl != NULL)
1286 {
1287 wpa_ctrl_detach(lynq_wpa_ctrl);
1288 wpa_ctrl_close(lynq_wpa_ctrl);
1289 }
1290}
qs.xiongf1b525b2022-03-31 00:58:23 -04001291
you.chen70f377f2023-04-14 18:17:09 +08001292// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1293static void GlobalWatcherThreadProc()
1294{
1295 int ret, connect_timeout, service_abnormal;
1296 error_number_s error_num = -1;
1297 inner_sta_status_s sta_status;
1298 scan_info_s *scan_list = NULL;
1299 int i, scan_len=0;
1300 char connecting_ssid[64];
1301 struct timeval now;
1302
1303 RLOGD("GlobalWatcherThreadProc start to run");
1304
1305 while (1)
1306 {
1307 pthread_mutex_lock(&s_global_check_mutex);
1308 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1309 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1310 {
1311 pthread_mutex_unlock(&s_global_check_mutex);
1312 usleep(50*1000);
1313 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1314 continue;
1315 }
1316
1317 connect_timeout = 0;
1318 service_abnormal = 0;
1319 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1320 {
1321 while (1)
1322 {
1323 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1324 if (ret == ETIME)
1325 {
1326 connect_timeout = 1;
1327 }
1328 else if (ret != 0)
1329 {
1330 gettimeofday(&now,NULL);
1331 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1332 {
1333 usleep(SLEEP_TIME_ON_IDLE);
1334 continue;
1335 }
1336 connect_timeout = 1;
1337 }
1338 sta_status = s_sta_status;
1339 error_num = s_sta_error_number;
1340 s_sta_status = INNER_STA_STATUS_INIT;
1341 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1342 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1343 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1344 break;
1345 }
1346 }
1347 if (s_service_invoke_timeout_cnt > 10)
1348 {
1349 service_abnormal = 1;
1350 s_service_invoke_timeout_cnt = 0;
1351 }
1352 pthread_mutex_unlock(&s_global_check_mutex);
1353
1354 if (service_abnormal == 1)
1355 {
1356 sleep(1);
1357 RLOGE("wpa service is abnormal info app to exit");
1358 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001359
1360 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1361
you.chen70f377f2023-04-14 18:17:09 +08001362 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1363 }
1364
1365 if (sta_status == INNER_STA_STATUS_CANCEL)
1366 {
1367 continue;
1368 }
1369 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1370 {
1371 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1372 }
1373 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1374 {
1375 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1376 {
1377 for(i=0; i < scan_len;i++)
1378 {
1379 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1380 {
1381 error_num = LYNQ_AUTH_ERROR;
1382 break;
1383 }
1384 }
1385 free(scan_list);
1386 }
1387 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1388 }
1389 else if (connect_timeout == 0)
1390 {
1391 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1392 }
1393 else // wait timeout
1394 {
1395 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1396 {
1397 ; // wpa service abnormal
1398 }
1399 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1400 {
1401 RLOGD("GlobalWatcherThreadProc notify connected");
1402 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1403 }
1404 else
1405 {
1406 RLOGD("GlobalWatcherThreadProc notify timeout");
1407 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1408 }
1409 }
1410 } // while (1)
1411}
1412
qs.xiong1af5daf2022-03-14 09:12:12 -04001413int lynq_wifi_enable(void)
1414{
you.chen35020192022-05-06 11:30:57 +08001415 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001416 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001417 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001418 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1419
qs.xiong9fbf74e2023-03-28 13:38:22 +08001420 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1421 {
you.chend2fef3f2023-02-13 10:50:35 +08001422 goto out_enable;
1423 }
1424
you.chenc9928582023-04-24 15:39:37 +08001425 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001426 if (ret != 0)
1427 {
1428 //printf("service state %d\n", ret);
1429 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001430 ret = -1;
1431 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001432 }
lhfe8da902022-10-11 18:55:36 +08001433
you.chen70f377f2023-04-14 18:17:09 +08001434 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1435 {
1436 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1437 if(ret<0)
1438 {
1439 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1440 ret = -1;
1441 goto out_enable;
1442 }
1443 }
1444
you.chend2fef3f2023-02-13 10:50:35 +08001445 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1446 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1447 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1448 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1449out_enable:
1450 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001451 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001452}
1453
qs.xiong1af5daf2022-03-14 09:12:12 -04001454int lynq_wifi_disable(void)
1455{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001456 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001457 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001458 g_ap_watcher_stop_flag = 1;
1459 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001460 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001461 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001462 if (g_ap_watcher_pid != 0)
1463 pthread_join(g_ap_watcher_pid, NULL);
1464 if (g_sta_watcher_pid != 0)
1465 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001466 if (g_sta_auto_watcher_pid != 0)
1467 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001468 if (g_lynq_wpa_ctrl[0] != NULL)
1469 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1470 if (g_lynq_wpa_ctrl[1] != NULL)
1471 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001472 if (g_ap_tmp_watcher_pid != 0)
1473 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001474 g_ap_watcher_pid = 0;
1475 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001476 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001477 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001478 g_lynq_wpa_ctrl[0] = NULL;
1479 g_lynq_wpa_ctrl[1] = NULL;
qs.xiongb37f8c42023-09-13 21:21:58 +08001480 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen35020192022-05-06 11:30:57 +08001481 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001482 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1483 return 0;
1484}
1485
1486static inline char inner_convert_char(char in)
1487{
1488 if (in >= '0' && in <= '9')
1489 {
1490 return in - '0';
1491 }
1492 else if (in >= 'a' && in <= 'f')
1493 {
1494 return in - 'a' + 10;
1495 }
1496 else if (in >= 'A' && in <= 'F')
1497 {
1498 return in - 'A' + 10;
1499 }
1500 else
1501 {
1502 return '\xff';
1503 }
1504}
1505
1506static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1507{
1508 char *p;
1509 size_t pos = 0;
1510 if (NULL == out_ssid)
1511 return;
1512 //printf("input ssid=[%s]\n", ssid);
1513 memset(out_ssid, 0, out_ssid_len);
1514 if (NULL == ssid)
1515 return;
1516 p = strchr(ssid, '\\');
1517 if (NULL == p)
1518 {
1519 strncpy(out_ssid, ssid, out_ssid_len);
1520 //printf(" first %s\n", out_ssid);
1521 }
1522 else
1523 {
1524 pos = p - ssid;
1525 memcpy(out_ssid, ssid, pos);
1526 //printf("pos %lu -- %s\n", pos, out_ssid);
1527 for(; pos < out_ssid_len; pos ++)
1528 {
1529 if (p[0] == '\0')
1530 {
1531 //printf(" out %s\n", out_ssid);
1532 return;
1533 }
1534 else if (p[0] != '\\')
1535 {
1536 out_ssid[pos] = p[0];
1537 p += 1;
1538 }
1539 else if (p[1] == 'x' || p[1] == 'X')
1540 {
1541 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1542 p += 4;
1543 }
1544 else if (p[1] == '\\')
1545 {
1546 out_ssid[pos] = '\\';
1547 p += 2;
1548 }
1549 else if (p[1] == 't')
1550 {
1551 out_ssid[pos] = '\t';
1552 p += 2;
1553 }
1554 else if (p[1] == 'r')
1555 {
1556 out_ssid[pos] = '\r';
1557 p += 2;
1558 }
1559 else if (p[1] == 'n')
1560 {
1561 out_ssid[pos] = '\n';
1562 p += 2;
1563 }//todo find a better way to convert?
1564 }
1565 }
1566 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001567}
qs.xiong1af5daf2022-03-14 09:12:12 -04001568
you.chen35020192022-05-06 11:30:57 +08001569static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001570 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001571 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001572 RLOGD("enter inner_get_param");
1573 if (out_put == NULL)
1574 {
1575 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001576 return -1;
1577 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001578 if (param_name == NULL)
1579 {
1580 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001581 return -1;
1582 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001583 if (param_name[0] == '\0')
1584 {
1585 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001586 return -1;
1587 }
1588
1589 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1590
1591 CHECK_WPA_CTRL(interface);
1592
1593 DO_REQUEST(lynq_cmd_get);
1594
qs.xiong9fbf74e2023-03-28 13:38:22 +08001595 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1596 {
1597 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001598 return -1;
1599 }
1600
you.chena6fa5b22022-05-18 10:28:19 +08001601// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001602 if (strcmp(param_name, "ssid") == 0)
1603 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001604 if (cmd_reply[0] == '\"')
1605 {
you.chend2fef3f2023-02-13 10:50:35 +08001606 ssid_len = reply_len - 1;
1607 memcpy(out_put, cmd_reply + 1, ssid_len);
1608 if (out_put[ssid_len-1] == '\"')
1609 {
1610 out_put[ssid_len-1] = '\0';
1611 }
1612 else
1613 {
1614 out_put[ssid_len] = '\0';
1615 }
1616 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001617 else
1618 {
you.chend2fef3f2023-02-13 10:50:35 +08001619 ssid_len = reply_len / 2;
1620 for(i=0; i<ssid_len; i++)
1621 {
1622 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1623 }
1624 out_put[ssid_len] = '\0';
1625 }
1626 }
1627 else
1628 {
1629 memcpy(out_put, cmd_reply, reply_len + 1);
1630 }
you.chen35020192022-05-06 11:30:57 +08001631 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001632}
qs.xiong1af5daf2022-03-14 09:12:12 -04001633
you.chen35020192022-05-06 11:30:57 +08001634static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1635 int ret = 0;
1636 char * end = str + len - 1;
1637 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001638 while(str < end)
1639 {
1640 if (*str == delimiter)
1641 {
you.chen35020192022-05-06 11:30:57 +08001642 *str++ = '\0';
1643 results[ret++] = str;
1644 continue;
1645 }
1646 str++;
1647 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001648 if (*str == delimiter)
1649 {
you.chen35020192022-05-06 11:30:57 +08001650 *str = '\0';
1651 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001652
you.chen6ed36a62023-04-27 17:51:56 +08001653 results[ret] = NULL;
1654
you.chen35020192022-05-06 11:30:57 +08001655 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001656}
1657
you.chend2fef3f2023-02-13 10:50:35 +08001658static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1659{
1660 char * p;
1661 int ret = 0;
1662 char cmd[256]={0};
1663 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001664 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001665 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001666 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001667 ret = exec_cmd(cmd, ip, ip_len);
1668 p = strchr(ip, '\n');
1669 if (NULL != p)
1670 {
1671 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001672 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001673 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001674 return ret;
1675}
1676
you.chend2fef3f2023-02-13 10:50:35 +08001677static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001678 struct in_addr addr ={0};
1679 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001680 char cmd[64] = {0};
1681 char * p;
1682 int ret;
you.chen35020192022-05-06 11:30:57 +08001683
qs.xiong9fbf74e2023-03-28 13:38:22 +08001684 if (ip == NULL || *ip == '\0' || hostname == NULL)
1685 {
1686 RLOGE("ip == NULL or hostname == NULL");
1687 return -1;
you.chen35020192022-05-06 11:30:57 +08001688 }
1689
you.chend2fef3f2023-02-13 10:50:35 +08001690 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001691 if (inet_aton(ip, &addr) == 0)
1692 {
you.chen35020192022-05-06 11:30:57 +08001693 printf("---inet_aton fail\n");
1694 return -1;
1695 }
1696
1697 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1698
qs.xiong9fbf74e2023-03-28 13:38:22 +08001699 if (ht == NULL)
1700 {
you.chen186d3c32023-05-18 14:19:46 +08001701 hostname[0] = '\0';
1702 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1703 ret = exec_cmd(cmd, hostname, 32);
1704 if (ret == 0)
1705 {
1706 p = strchr(hostname, '\n');
1707 if (p != NULL)
1708 {
1709 *p = '\0';
1710 }
1711 return 0;
1712 }
1713 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001714 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001715 herror(NULL);
1716 return -1;
1717 }
1718
1719 strcpy(hostname, ht->h_name);
1720
1721 return 0;
1722}
1723
1724static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1725{
1726 int count, index, words_count;
1727 char * split_lines[128]= {0};
1728 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001729 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001730 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001731 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001732
1733 CHECK_WPA_CTRL(ap_sta);
1734
1735 DO_REQUEST(lynq_wifi_list_networks);
1736
1737 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1738
1739 //@todo check ssid field to compatible
1740
1741 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001742 for(index=1; index < count; index++)
1743 {
you.chen35020192022-05-06 11:30:57 +08001744 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001745 if (words_count > 2)
1746 {
you.chend2fef3f2023-02-13 10:50:35 +08001747 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001748 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1749 {
you.chen35020192022-05-06 11:30:57 +08001750 net_no_list[ret++] = atoi(split_words[0]);
1751 }
1752 }
1753 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001754 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001755 return ret;
1756}
1757
1758static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001759 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001760 CHECK_WPA_CTRL(ap_sta);
1761 const char *lynq_wifi_add_network = "ADD_NETWORK";
1762
qs.xiong9fbf74e2023-03-28 13:38:22 +08001763 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001764 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1766 {
1767 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001768 return -1;
1769 }
1770
qs.xiong9fbf74e2023-03-28 13:38:22 +08001771 for(i=0;i<reply_len;i++)
1772 {
1773 if(cmd_reply[i] == '\n')
1774 {
you.chen35020192022-05-06 11:30:57 +08001775 cmd_reply[i] = '\0';
1776 break;
1777 }
1778 }
1779 return atoi(cmd_reply);
1780}
you.chena6cd55a2022-05-08 12:20:18 +08001781
you.chen35020192022-05-06 11:30:57 +08001782static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1783{
1784 int count, index;
1785 int net_no_list[128];
1786
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001788 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001789 for (index=0; index < count; index++)
1790 {
1791 if (net_no_list[index] == net_no)
1792 {
you.chen35020192022-05-06 11:30:57 +08001793 return 0;
1794 }
1795 }
1796
1797 if (count >= 1)
1798 index = net_no_list[count - 1];
1799 else
1800 index = -1;
1801
qs.xiong9fbf74e2023-03-28 13:38:22 +08001802 while (index < net_no )
1803 {
you.chen35020192022-05-06 11:30:57 +08001804 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001805 if (index >= net_no)
1806 { // required network no created
1807 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001808 return 0;
1809 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001810 else if( index < 0)
1811 {
1812 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001813 return -1;
1814 }
you.chen35020192022-05-06 11:30:57 +08001815 }
1816
1817 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001818 {
1819 RLOGE("[lynq_check_network_number] network index < 0");
1820 return -1;
1821 }
1822 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001823 return 0;
1824}
1825
1826static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001827 if (freq > 5000 && freq < 6000)
1828 {
you.chen35020192022-05-06 11:30:57 +08001829 return LYNQ_WIFI_5G_band;
1830 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001831 else if (freq > 2000 && freq < 3000)
1832 {
you.chen35020192022-05-06 11:30:57 +08001833 return LYNQ_WIFI_2G_band;
1834 }
1835 return LYNQ_WIFI_2_and_5G_band;
1836}
1837
1838static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 if (key_mgmt != NULL)
1840 {
1841 if (memcmp( key_mgmt, "NONE", 4) == 0)
1842 {
you.chen35020192022-05-06 11:30:57 +08001843 return LYNQ_WIFI_AUTH_OPEN;
1844 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001845 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1846 {
you.chen35020192022-05-06 11:30:57 +08001847 return LYNQ_WIFI_AUTH_WEP;
1848 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001849 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1850 {
you.chen35020192022-05-06 11:30:57 +08001851 return LYNQ_WIFI_AUTH_WPA_PSK;
1852 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001853 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1854 {
you.chen35020192022-05-06 11:30:57 +08001855 return LYNQ_WIFI_AUTH_WPA2_PSK;
1856 }
1857 }
1858
1859 return -1;
1860}
1861
1862static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001863 if (flag != NULL)
1864 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001865 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 +08001866 {
1867 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001868 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001869 {
1870 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1871 }else if (strstr( flag, "WPA2-PSK") != NULL)
1872 {
you.chen35020192022-05-06 11:30:57 +08001873 return LYNQ_WIFI_AUTH_WPA2_PSK;
1874 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001875 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001876 {
you.chen35020192022-05-06 11:30:57 +08001877 return LYNQ_WIFI_AUTH_WPA_PSK;
1878 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001879 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001880 {
you.chen35020192022-05-06 11:30:57 +08001881 return LYNQ_WIFI_AUTH_WEP;
1882 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001883 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001884 {
you.chen35020192022-05-06 11:30:57 +08001885 return LYNQ_WIFI_AUTH_OPEN;
1886 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001887 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001888 {
you.chend2fef3f2023-02-13 10:50:35 +08001889 return LYNQ_WIFI_AUTH_OPEN;
1890 }
qs.xiong46f41562023-07-11 21:06:47 +08001891 else
1892 {
1893 RLOGD("convert_max_auth_from_flag not-found auth mode");
1894 }
you.chen35020192022-05-06 11:30:57 +08001895 }
1896
1897 return -1;
1898}
1899
1900static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1901 switch (bw) {
1902 case 10:
1903 return LYNQ_WIFI_BANDWIDTH_HT10;
1904 break;
1905 case 20:
1906 return LYNQ_WIFI_BANDWIDTH_HT20;
1907 break;
1908 case 40:
1909 return LYNQ_WIFI_BANDWIDTH_HT40;
1910 break;
1911 case 80:
1912 return LYNQ_WIFI_BANDWIDTH_HT80;
1913 break;
1914 default:
1915 break;
1916 }
1917
1918 return -1;
1919}
1920
you.chen70f377f2023-04-14 18:17:09 +08001921static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001922static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1923 int i, count;
1924 char *p;
1925 const char *lynq_status_cmd = "STATUS";
1926 const char * FLAG_SSID = "ssid=";
1927 const char * FLAG_SBSID = "bssid=";
1928 const char * FLAG_KEY_MGMT = "key_mgmt=";
1929 const char * FLAG_FREQ = "freq=";
1930 const char * FLAG_STATE = "wpa_state=";
1931 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001932 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001933 char *split_lines[128] = {0};
1934
1935 CHECK_WPA_CTRL(interface);
1936
qs.xiong9fbf74e2023-03-28 13:38:22 +08001937 if (curr_state == NULL)
1938 {
1939 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001940 return -1;
1941 }
1942
1943 DO_REQUEST(lynq_status_cmd);
1944
1945 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1946
1947 curr_state->net_no = -1;
1948 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001949 for(i=0; i < count; i++)
1950 {
1951 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001952 {
you.chen35020192022-05-06 11:30:57 +08001953 p = strstr(split_lines[i], FLAG_SBSID);
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 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001957 ret = 0;
1958 continue;
1959 }
you.chenf58b3c92022-06-21 16:53:48 +08001960 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001961 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001962 {
you.chend2fef3f2023-02-13 10:50:35 +08001963 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 +08001964 ret = 0;
1965 continue;
1966 }
you.chen35020192022-05-06 11:30:57 +08001967 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001969 {
you.chen450d0172022-07-15 17:56:48 +08001970 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001971 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001972 ret = 0;
1973 continue;
1974 }
1975 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001976 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001977 {
you.chen35020192022-05-06 11:30:57 +08001978 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1979 ret = 0;
1980 continue;
1981 }
you.chend2fef3f2023-02-13 10:50:35 +08001982 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001983 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001984 {
you.chend2fef3f2023-02-13 10:50:35 +08001985 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1986 ret = 0;
1987 continue;
1988 }
you.chen35020192022-05-06 11:30:57 +08001989 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001991 {
you.chen35020192022-05-06 11:30:57 +08001992 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001993 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001994 {
you.chen35020192022-05-06 11:30:57 +08001995 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1996 ret = 0;
1997 continue;
1998 }
1999
2000 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08002001 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08002002 {
you.chen35020192022-05-06 11:30:57 +08002003 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08002004 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08002005 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08002006 }
2007 }
2008
you.chen70f377f2023-04-14 18:17:09 +08002009 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
2010 {
2011 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
2012 }
2013
you.chen35020192022-05-06 11:30:57 +08002014 return ret;
2015}
2016
qs.xiongf1b525b2022-03-31 00:58:23 -04002017int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002018{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002019 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08002020 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05002021
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 if (ap_ssid == NULL)
2023 {
2024 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08002025 return -1;
2026 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002027 else
2028 {
2029 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002030 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002031
qs.xiong9fbf74e2023-03-28 13:38:22 +08002032 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2033 {
2034 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08002035 return -1;
2036 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002037
you.chen35020192022-05-06 11:30:57 +08002038 CHECK_IDX(idx, CTRL_AP);
2039
2040 CHECK_WPA_CTRL(CTRL_AP);
2041
2042 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
2043
2044 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2045 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002046 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
2047 return 0;
you.chen35020192022-05-06 11:30:57 +08002048
qs.xiong7a105ce2022-03-02 09:43:11 -05002049}
2050
you.chen35020192022-05-06 11:30:57 +08002051int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05002052{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002053 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08002054 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08002055 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05002056}
2057
qs.xiongc9c79f72022-10-17 15:27:18 +08002058/*****
2059 *frequency <------>channel
2060 *
2061 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
2062 *
2063 *
2064 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
2065 *
2066 *
2067 * */
2068static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08002069 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};
2070 int i;
2071 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
2072
qs.xiong69a332b2022-12-02 09:58:57 +08002073 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08002074 {
2075 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08002076 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08002077 }
qs.xiongc00b6032022-11-29 16:28:03 +08002078
2079 if(i == arr_len)
2080 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002081 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002082 return -1;
2083 }
qs.xiongc00b6032022-11-29 16:28:03 +08002084
qs.xiongc9c79f72022-10-17 15:27:18 +08002085 return 0;
2086}
qs.xiong13673462023-02-21 19:12:54 +08002087
2088static int lynq_check_frequencyby_country_code(int input_frequency)
2089{
2090 char str_cnc[]="CN";
2091 char str_dest[20]="";
2092
2093 if( lynq_get_country_code(1,str_dest) != 0 )
2094 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002095 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002096 return -1;
2097 }
2098 if( strncmp(str_dest,str_cnc,2) != 0 )
2099 {
2100 return 0;
2101 }else if( 2473 < input_frequency && input_frequency < 5744)
2102 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002103 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002104 return -1;
2105 }
2106 return 0;
2107}
qs.xiongf1b525b2022-03-31 00:58:23 -04002108int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002109{
qs.xiongc00b6032022-11-29 16:28:03 +08002110 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002111 char lynq_wifi_frequency_cmd[128]={0};
2112 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002113 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002114 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002115 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002116 check = lynq_check_set_frequency(lynq_wifi_frequency);
2117 if(check != 0)
2118 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002120 return -1;
you.chen35020192022-05-06 11:30:57 +08002121 }
qs.xiong13673462023-02-21 19:12:54 +08002122 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2123 if(check != 0)
2124 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002125 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002126 return -1;
2127 }
2128
qs.xiongc00b6032022-11-29 16:28:03 +08002129 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2130 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002131 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002132 return -1;
2133 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002134
you.chen35020192022-05-06 11:30:57 +08002135 CHECK_IDX(idx, CTRL_AP);
2136
2137 CHECK_WPA_CTRL(CTRL_AP);
2138
2139 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2140 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2141 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2142
you.chen6c2dd9c2022-05-16 17:55:28 +08002143 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002144 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2145 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2146 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002147
qs.xiong9fbf74e2023-03-28 13:38:22 +08002148 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002149}
2150
qs.xiongf1b525b2022-03-31 00:58:23 -04002151int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002152{
you.chen35020192022-05-06 11:30:57 +08002153 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002154 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002155 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002156
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2158 {
2159 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002160 return -1;
2161 }
2162 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002163
qs.xiong9fbf74e2023-03-28 13:38:22 +08002164 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002165}
2166
qs.xiongf1b525b2022-03-31 00:58:23 -04002167int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2168{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002169 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002170 CHECK_IDX(idx, CTRL_AP);
2171 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 case LYNQ_WIFI_BANDWIDTH_HT10:
2173 {
2174 RLOGE("bandwith [%d] not support now\n", bandwidth);
2175 return -1;
2176 }
2177 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002178 {
2179 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2180 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002181 if (system(lynq_cmd_bandwith) != 0 )
2182 {
2183 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002184 return -1;
2185 }
2186 system("wl up");
2187 break;
2188 }
2189 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002190 {
qs.xiong10379192023-02-21 13:19:42 +08002191 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002192 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2193 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002194 if (system(lynq_cmd_bandwith) != 0 )
2195 {
2196 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002197 return -1;
2198 }
2199 system("wl up");
2200 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002201 }
you.chen35020192022-05-06 11:30:57 +08002202 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002203 {
qs.xiong10379192023-02-21 13:19:42 +08002204 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002205 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002206 if (system(lynq_cmd_bandwith) != 0 )
2207 {
2208 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002209 return -1;
2210 }
2211 system("wl up");
2212 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002213 }
2214 default:
you.chen35020192022-05-06 11:30:57 +08002215 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002216 RLOGE("auth type [%d] not support now\n", bandwidth);
2217 return -1;
you.chen35020192022-05-06 11:30:57 +08002218 }
2219 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002220
2221
you.chen35020192022-05-06 11:30:57 +08002222 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002223}
you.chen35020192022-05-06 11:30:57 +08002224
qs.xiongf1b525b2022-03-31 00:58:23 -04002225int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2226{
you.chen35020192022-05-06 11:30:57 +08002227 int count = 0;
2228 int index = 0;
2229 char *split_words[128] = {0};
2230 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002231 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002232 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002233
you.chen35020192022-05-06 11:30:57 +08002234 CHECK_WPA_CTRL(CTRL_AP);
2235
2236 DO_REQUEST(lynq_chanspec_cmd);
2237
2238 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2239 for(;index < count; index++) {
2240 if (strncmp(split_words[index], "bw", 2) != 0) {
2241 continue;
2242 }
2243
2244 index++;
2245 if (index >= count) {
2246 return -1;
2247 }
2248
qs.xiong9fbf74e2023-03-28 13:38:22 +08002249 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002250 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2251 return 0;
2252 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002253 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002254 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002255}
qs.xiong0fb469a2022-04-14 03:50:45 -04002256
qs.xiongf1b525b2022-03-31 00:58:23 -04002257int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002258{
you.chen35020192022-05-06 11:30:57 +08002259 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002260 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002261 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002262
you.chen35020192022-05-06 11:30:57 +08002263 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002264
qs.xiong9fbf74e2023-03-28 13:38:22 +08002265 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2266 {
you.chen35020192022-05-06 11:30:57 +08002267 return -1;
2268 }
2269
2270 system("wl down");
2271 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002272 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002273 return -1;
2274 }
2275 system("wl up");
2276 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002277}
qs.xiong0fb469a2022-04-14 03:50:45 -04002278
qs.xiongf1b525b2022-03-31 00:58:23 -04002279int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002280{
you.chen35020192022-05-06 11:30:57 +08002281 int count = 0;
2282 int index = 0;
2283 char *split_words[128] = {0};
2284 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002286 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002287
you.chen35020192022-05-06 11:30:57 +08002288 CHECK_WPA_CTRL(CTRL_AP);
2289
2290 DO_REQUEST(lynq_chanspec_cmd);
2291
2292 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002293 for(;index < count; index++)
2294 {
2295 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002296 if (strncmp(split_words[index], "channel", 2) != 0) {
2297 continue;
2298 }
2299
2300 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002301 if (index >= count)
2302 {
you.chen35020192022-05-06 11:30:57 +08002303 return -1;
2304 }
2305
2306 *channel = atoi(split_words[index]);
2307 return 0;
2308 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002309 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002310 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002311}
2312
2313
you.chen35020192022-05-06 11:30:57 +08002314int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002315{
you.chen6c2dd9c2022-05-16 17:55:28 +08002316 char ssid[MAX_CMD] = {0};
2317 int freq = 0;
2318 char lynq_auth_cmd[64]={0};
2319 char lynq_auth_alg_cmd[64]={0};
2320 char lynq_psk_cmd[64]={0};
2321 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 char lynq_ieee80211_cmd[64]={0};
2323 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002324 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002325 CHECK_IDX(idx, CTRL_AP);
2326
you.chen6c2dd9c2022-05-16 17:55:28 +08002327 CHECK_WPA_CTRL(CTRL_AP);
2328
qs.xiong9fbf74e2023-03-28 13:38:22 +08002329 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2330 {
2331 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002332 return -1;
2333 }
2334
you.chen92fd5d32022-05-25 10:09:47 +08002335 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002336 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002337 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002338 return 0;
2339 }
2340 else {
2341 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2342 ssid[0] = '\0';
2343 }
2344 lynq_wifi_ap_frequency_get(idx, &freq);
2345
2346 DO_OK_FAIL_REQUEST(cmd_disconnect);
2347 DO_OK_FAIL_REQUEST(cmd_remove_all);
2348 if (ssid[0] != '\0') {
2349 lynq_wifi_ap_ssid_set(idx, ssid);
2350 }
2351 if (freq != 0) {
2352 lynq_wifi_ap_frequency_set(idx, freq);
2353 }
2354 }
2355 }
you.chen35020192022-05-06 11:30:57 +08002356
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 switch(auth){
2358 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002359 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002360 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002361 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002362 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002363 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002364 break;
2365 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002366 case LYNQ_WIFI_AUTH_WEP:
2367 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002368 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002369 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002370 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002371 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2372
2373 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2374 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2375 break;
2376 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002377 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002378 {
2379 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2380 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2381 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2382
2383 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2384 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2385 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2386 break;
2387
2388 }
you.chen35020192022-05-06 11:30:57 +08002389 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 {
2391 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2392 {
you.chen35020192022-05-06 11:30:57 +08002393 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2394 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2395 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002396 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2397 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002398 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002399 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002400 }
2401// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2402// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2403 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002404
you.chen35020192022-05-06 11:30:57 +08002405 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2406 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2407 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002408 break;
2409 }
2410 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2411 {
2412 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2413 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2414 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2415 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2416
2417 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2418 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2419 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2420 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2421 break;
2422 }
2423 case LYNQ_WIFI_AUTH_WPA3_PSK:
2424 {
2425 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2426 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002427 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002428 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2429
2430 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2431 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2432 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2433 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2434 break;
2435 }
2436 default:
you.chen35020192022-05-06 11:30:57 +08002437 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002438 RLOGE("auth type [%d] not support now\n", auth);
2439 return -1;
you.chen35020192022-05-06 11:30:57 +08002440 }
2441 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002442 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002443
qs.xiong9fbf74e2023-03-28 13:38:22 +08002444 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002445}
2446
you.chen35020192022-05-06 11:30:57 +08002447int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002448{
you.chen35020192022-05-06 11:30:57 +08002449 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002450 char lynq_auth_alg_str[MAX_RET] = {0};
2451 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002452 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002453 CHECK_IDX(idx, CTRL_AP);
2454
qs.xiong9fbf74e2023-03-28 13:38:22 +08002455 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2456 {
2457 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002458 return -1;
2459 }
2460
qs.xiong9fbf74e2023-03-28 13:38:22 +08002461 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2462 {
2463 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2464 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002465 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002466 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002467 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002469 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2470 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002471 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002472 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002473 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002474 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002475 else
2476 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002477 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002478 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002479 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002480 }
you.chen35020192022-05-06 11:30:57 +08002481 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002482 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2483 {
2484 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2485 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002486 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002487 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002488 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002489 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2490 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002491 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002492 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002493 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002494 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002495 else
2496 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002497 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002498 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002499 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002500 }
you.chen35020192022-05-06 11:30:57 +08002501 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002502
2503 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2504 {
2505 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2506 return -1;
2507 }
2508
2509 if (memcmp(lynq_auth_str,"1",1) == 0 )
2510 {
2511 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2512 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002513 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002514 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2515 {
2516 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2517 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002518 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002519 }
2520 else
2521 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002522 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002523 *auth = -1;
2524 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002525
you.chen6c2dd9c2022-05-16 17:55:28 +08002526 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002527}
qs.xiong1af5daf2022-03-14 09:12:12 -04002528
you.chenb95401e2023-05-12 19:39:06 +08002529static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2530{
2531 char status[64];
you.chencba13492023-05-19 13:53:43 +08002532 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002533 curr_status_info curr_state;
2534
2535 CHECK_WPA_CTRL(CTRL_AP);
2536
2537 memset(status, 0, sizeof (status));
2538
2539 curr_state.ap = NULL;
2540 curr_state.state = status;
2541
2542 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002543 usleep(250*1000);
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002544 if (0 == inner_get_status_info(idx, &curr_state))
you.chenb95401e2023-05-12 19:39:06 +08002545 {
qs.xiong2b2c8cd2023-09-14 15:58:51 +08002546 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002547 {
2548 return 0;
2549 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002550 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002551 {
2552 DO_REQUEST("RECONNECT");
2553 return inner_check_ap_connected(idx, retry_count+1);
2554 }
you.chencba13492023-05-19 13:53:43 +08002555 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002556 {
2557 printf("retry 10 time\n");
2558 return -1;
2559 }
2560 else
2561 {
you.chen6d247052023-06-01 16:39:54 +08002562 if (strcmp(status, STATE_DISCONNECTED) == 0)
2563 {
2564 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2565 DO_REQUEST(LYNQ_WIFI_CMD);
2566 }
you.chenb95401e2023-05-12 19:39:06 +08002567 return inner_check_ap_connected(idx, retry_count+1);
2568 }
2569 }
2570 return -1;
2571}
qs.xiong1af5daf2022-03-14 09:12:12 -04002572
qs.xiongf1b525b2022-03-31 00:58:23 -04002573int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002574{
qs.xiong5a2ba932023-09-13 16:30:21 +08002575 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002576 char LYNQ_WIFI_CMD[128]={0};
qs.xiongb37f8c42023-09-13 21:21:58 +08002577 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2578 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2579 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002580 CHECK_IDX(idx, CTRL_AP);
2581
2582 CHECK_WPA_CTRL(CTRL_AP);
2583
you.chen0df3e7e2023-05-10 15:56:26 +08002584 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002585 {
you.chen0df3e7e2023-05-10 15:56:26 +08002586 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002587 return -1;
2588 }
you.chen35020192022-05-06 11:30:57 +08002589
qs.xiongb37f8c42023-09-13 21:21:58 +08002590 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2591 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2592
you.chen35020192022-05-06 11:30:57 +08002593 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2594 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2595
you.chenc9928582023-04-24 15:39:37 +08002596 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2597 if (ret != 0)
2598 {
2599 RLOGE("lynq_wifi_ap_start excute script fail");
2600 return -1;
2601 }
2602
you.chenb95401e2023-05-12 19:39:06 +08002603 if (inner_check_ap_connected(idx, 0) != 0)
2604 {
2605 return -1;
2606 }
2607
you.chen0df3e7e2023-05-10 15:56:26 +08002608 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002609 if (g_ap_tmp_watcher_pid == 0)
2610 {
2611 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2612 {
2613 g_ap_tmp_watcher_pid = 0;
2614 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2615 return -1;
2616 }
2617 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2618 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002619 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiongb37f8c42023-09-13 21:21:58 +08002620
qs.xiong9fbf74e2023-03-28 13:38:22 +08002621 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002622}
2623
qs.xiongf1b525b2022-03-31 00:58:23 -04002624int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002625{
you.chen35020192022-05-06 11:30:57 +08002626 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002627}
2628
qs.xiongf1b525b2022-03-31 00:58:23 -04002629int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002630{
you.chen35020192022-05-06 11:30:57 +08002631 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002632
you.chen35020192022-05-06 11:30:57 +08002633 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002634
you.chen35020192022-05-06 11:30:57 +08002635 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002636
you.chen35020192022-05-06 11:30:57 +08002637 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2638
2639 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2640
you.chenb4b121c2022-05-06 17:50:16 +08002641// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002642
2643 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2644 if (ret != 0)
2645 {
2646 RLOGE("lynq_wifi_ap_start excute script fail");
2647 return -1;
2648 }
qs.xiong44fac672023-08-29 16:15:55 +08002649 g_ap_tmp_watcher_stop_flag = 1;
2650 if (g_ap_tmp_watcher_pid != 0)
2651 pthread_join(g_ap_tmp_watcher_pid, NULL);
2652 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002653
qs.xiong9fbf74e2023-03-28 13:38:22 +08002654 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002655}
qs.xiong1af5daf2022-03-14 09:12:12 -04002656
qs.xiongf1b525b2022-03-31 00:58:23 -04002657int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002658{
you.chen35020192022-05-06 11:30:57 +08002659 char lynq_disable_cmd[128] = {0};
2660 char lynq_select_cmd[128] = {0};
2661 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002662 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002663 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002664
you.chen35020192022-05-06 11:30:57 +08002665 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002666 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2667 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2668
2669 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2670 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2671 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002672
qs.xiong9fbf74e2023-03-28 13:38:22 +08002673 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002674}
2675
qs.xiongf1b525b2022-03-31 00:58:23 -04002676int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002677{
you.chen35020192022-05-06 11:30:57 +08002678 char lynq_disable_cmd[128] = {0};
2679 char lynq_select_cmd[128] = {0};
2680 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002681 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002682 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002683
you.chen35020192022-05-06 11:30:57 +08002684 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002685
you.chen35020192022-05-06 11:30:57 +08002686 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2687 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2688
2689 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2690 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2691 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002692
qs.xiong9fbf74e2023-03-28 13:38:22 +08002693 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002694}
qs.xiongf1b525b2022-03-31 00:58:23 -04002695
you.chen35020192022-05-06 11:30:57 +08002696int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002697{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002698 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002699 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002700 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002701 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002702 RLOGD("enter lynq_ap_password_set");
2703 if( password == NULL )
2704 {
2705 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002706 return -1;
2707 }
2708 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002709 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002710 if(pass_len < 8 || pass_len >= 64)
2711 {
2712 RLOGE("[lynq_ap_password_set]input password len not in rage");
2713 return -1;
you.chen35020192022-05-06 11:30:57 +08002714 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002715
you.chen35020192022-05-06 11:30:57 +08002716 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002717
qs.xiong9fbf74e2023-03-28 13:38:22 +08002718 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2719 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002720 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002721 return -1;
2722 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2724 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002725 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2726 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002727 }
2728
you.chen35020192022-05-06 11:30:57 +08002729 CHECK_WPA_CTRL(CTRL_AP);
2730
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 if (auth == LYNQ_WIFI_AUTH_WEP)
2732 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002733 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002734 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2735 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2736 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2737 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2738 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002739 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2740 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002741 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 +08002742 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2743 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2744 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002745 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2746 {
2747
qs.xiongc8d92a62023-03-29 17:36:14 +08002748 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 +08002749 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002750 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002751 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2752 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2753
2754 }
2755 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002756 {
2757 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002758 return -1;
2759 }
you.chen35020192022-05-06 11:30:57 +08002760
you.chen35020192022-05-06 11:30:57 +08002761 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002762
qs.xiong9fbf74e2023-03-28 13:38:22 +08002763 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002764}
2765
you.chen35020192022-05-06 11:30:57 +08002766int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002767{
you.chen35020192022-05-06 11:30:57 +08002768 FILE * fp;
2769 int len, ret;
2770 int count, index;
2771 char *split_lines[128] = {0};
2772 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002773 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002774
you.chen35020192022-05-06 11:30:57 +08002775 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002776
you.chen35020192022-05-06 11:30:57 +08002777 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2778// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002779 if (NULL == fp)
2780 {
2781 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002782 return -1;
2783 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002784
you.chen35020192022-05-06 11:30:57 +08002785 buff = alloca(MAX_RET);
2786 fseek(fp, 0, SEEK_SET);
2787 len = fread(buff, 1, MAX_RET, fp);
2788 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002789
qs.xiong9fbf74e2023-03-28 13:38:22 +08002790 for(index=0; index < len; index ++)
2791 {
2792 if (memcmp(buff + index, "network={", 9) != 0)
2793 {
you.chen35020192022-05-06 11:30:57 +08002794 continue;
2795 }
2796 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002797 for (; index < len; index ++ )
2798 {
2799 if (buff[index] != '}')
2800 {
you.chen35020192022-05-06 11:30:57 +08002801 continue;
2802 }
2803 buff[index] = '\0';
2804 break;
2805 }
2806 len = buff + index - p;
2807 }
2808
2809 count = lynq_split(p, len, '\n', split_lines);
2810
2811 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002812 for(index=0; index < count; index++)
2813 {
you.chen35020192022-05-06 11:30:57 +08002814 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002815 if (p != NULL)
2816 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002817 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002818 if (*p == '\"')
2819 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002820 p++;
2821 }
you.chen35020192022-05-06 11:30:57 +08002822 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002823 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2824 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002825 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002826 if (*p == '\"')
2827 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002828 p++;
2829 }
2830 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002831 else
2832 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002833 continue;
you.chen35020192022-05-06 11:30:57 +08002834 }
2835
2836 strcpy(password, p);
2837
qs.xiong9fbf74e2023-03-28 13:38:22 +08002838 while(*password != '\0')
2839 {
2840 if (*password == '\"')
2841 {
you.chen35020192022-05-06 11:30:57 +08002842 *password = '\0';
2843 break;
2844 }
2845 password++;
2846 }
2847 ret = 0;
2848 break;
2849 } //end for(index=0; index < count; index++)
2850
2851 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002852}
2853
you.chen35020192022-05-06 11:30:57 +08002854static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2855 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002856 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002857
qs.xiong9fbf74e2023-03-28 13:38:22 +08002858 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2859 {
you.chen35020192022-05-06 11:30:57 +08002860 return -1;
2861 }
2862
2863 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002864
qs.xiong9fbf74e2023-03-28 13:38:22 +08002865 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2866 {
2867 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002868 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002869 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002870 {
you.chena6cd55a2022-05-08 12:20:18 +08002871 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002872 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002873 }
you.chen70f377f2023-04-14 18:17:09 +08002874 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2875 {
2876 return 0;
2877 }
you.chena6cd55a2022-05-08 12:20:18 +08002878 }
2879 }
you.chen70f377f2023-04-14 18:17:09 +08002880 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2881 {
2882 return 0;
2883 }
2884
qs.xiong9fbf74e2023-03-28 13:38:22 +08002885 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2886 {
you.chen70f377f2023-04-14 18:17:09 +08002887 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 return -1;
2889 }
2890 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2891 {
2892
you.chen70f377f2023-04-14 18:17:09 +08002893 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2894 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2896 {
2897
2898 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002899 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 }else
2901 {
you.chen70f377f2023-04-14 18:17:09 +08002902 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 *auth = -1;
2904 return -1;
2905 }
you.chen35020192022-05-06 11:30:57 +08002906 return 0;
2907}
2908
2909int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002910{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002911 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002912 int pass_len, net_no, count, index;
2913 char lynq_tmp_cmd[300]={0};
2914 int net_no_list[128];
2915 lynq_wifi_auth_s net_auth;
2916 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002917 if(pass_len < 8 || pass_len >= 64)
2918 {
2919 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002920 return -1;
2921 }
2922
2923 CHECK_IDX(idx, CTRL_STA);
2924
2925 net_no = -1;
2926 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2927
qs.xiong9fbf74e2023-03-28 13:38:22 +08002928 for (index=0; index < count; index++)
2929 {
you.chen35020192022-05-06 11:30:57 +08002930 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002931 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2932 {
you.chen35020192022-05-06 11:30:57 +08002933 net_no = net_no_list[index];
2934 break;
2935 }
2936 }
2937
qs.xiong9fbf74e2023-03-28 13:38:22 +08002938 if (net_no < 0)
2939 {
you.chen35020192022-05-06 11:30:57 +08002940 return -1;
2941 }
2942
2943 CHECK_WPA_CTRL(CTRL_STA);
2944
2945 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2946
2947 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2948 DO_OK_FAIL_REQUEST(cmd_save_config);
2949
2950 return 0;
2951}
2952
qs.xiongb5dab082023-10-13 14:43:41 +08002953/**
2954* buff data
2955* buff_len size of buff
2956* idx sta
2957* *ap ap info for find ssid && password
2958* password return password
2959*
2960*/
2961static int lynq_sta_ssid_password_auth_get(char * buff,int buff_len,lynq_wifi_index_e idx, ap_info_s *ap,char *password) { // @todo
2962
2963 int ret, network_len, i, ssid_len,curr_auth;
2964 int count, index,org_index;
2965 char *split_lines[128] = {0};
2966 char *p, *ssid, *ssid_end_flag,*ptr;
2967 char tmp_ssid[128]={0};
2968 char tmp_auth[24]={0};
2969
2970 org_index = 0;
2971 network_len = 0;
2972 p = NULL;
2973
2974 CHECK_IDX(idx, CTRL_STA);
2975
2976 while(1){
2977 network_len = 0;
2978 p == NULL;
2979 for(; org_index < buff_len; org_index ++)
2980 {
2981 for(; org_index < buff_len; org_index ++)
2982 {
2983 if (memcmp(buff + org_index, "network={", 9) != 0)
2984 {
2985 continue;
2986 }
2987 p = buff + org_index + 9;
2988
2989 for (; org_index < buff_len; org_index ++ )
2990 {
2991 if (buff[org_index] != '}')
2992 {
2993 continue;
2994 }
2995 buff[org_index] = '\0';
2996 break;
2997 }
2998 network_len = buff + org_index - p;
2999 break;
3000 }
3001
3002 if (p == NULL)
3003 {
3004 RLOGD("not find dest info %s(),line %dERROR",__func__,__LINE__);
3005 return -1;
3006 }
3007
3008 ssid = strstr(p, "ssid=");
3009 if (ssid != NULL) {
3010 ssid += strlen("ssid=");
3011 if (ssid[0] == '\"')
3012 {
3013 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
3014 {
3015 RLOGD("-----curr_get ssid form config is %s",ssid);
3016 break;
3017 }
3018 RLOGD("-----countine to find dest ssid %s ---curr_get ssid from config is %s",ap->ap_ssid,ssid);
3019 }
3020 else
3021 {
3022 ssid_end_flag = strstr(ssid, "\n");
3023 if (ssid_end_flag != NULL)
3024 {
3025 ssid_len = (ssid_end_flag - ssid) / 2;
3026 for(i=0; i<ssid_len; i++)
3027 {
3028 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
3029 }
3030 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
3031 {
3032 RLOGD("curr_ssid is(from config) ---- %s ap_info ssid --->",tmp_ssid,ap->ap_ssid);
3033 break;
3034 }
3035 }
3036 }
3037 }
3038
3039 }
3040
3041 if (org_index >= buff_len || NULL == p || network_len <= 0)
3042 {
3043
3044 if (buff != NULL)
3045 RLOGD("not find dest ssid %s(),line %dERROR",__func__,__LINE__);
3046 return -1;
3047 }
3048
3049 count = lynq_split(p, network_len, '\n', split_lines);
3050 ret = -1;
3051 for( index=0; index < count; index++ )
3052 {
3053 p = strstr(split_lines[index], "key_mgmt=");
3054 RLOGD("current p str ------- %s",p);
3055 if(p != NULL)
3056 {
3057 p += 9;
3058 if(memcmp(p,"SAE",3) == 0)
3059 {
3060 curr_auth = 5;
3061 }else if(memcmp(p,"WPA-PSK SAE",11) == 0)
3062 {
3063 curr_auth = 4;
3064 }else if(memcmp(p,"WPA-PSK",7) == 0 )
3065 {
3066 curr_auth = 3;
3067 }else if(memcmp(p,"NONE",4) == 0 )
3068 {
3069 curr_auth = 0;
3070 }else{
3071 curr_auth = 1;
3072 }
3073 RLOGD("************curret_get_auth is %d ssid is %s",curr_auth,ap->ap_ssid);
3074 if( curr_auth < 1 || curr_auth > 6)
3075 {
3076 ret = -1;
3077 }
3078 break;
3079 }
3080 }
3081 if( curr_auth == 0)
3082 {
3083 return 0;
3084 }else if(curr_auth == ap->auth || ap->auth <= 3 && curr_auth <= 3 && curr_auth != -1 )
3085 {
3086 for(index=0; index < count; index++)
3087 {
3088 /*get psw info*/
3089
3090 p = strstr(split_lines[index], "psk=");
3091 if (p != NULL)
3092 {
3093 p += 4;
3094 if (*p == '\"')
3095 {
3096 p++;
3097 }
3098 }
3099 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
3100 {
3101 p += 9;
3102 if (*p == '\"')
3103 {
3104 p++;
3105 }
3106 }
3107 else
3108 {
3109 continue;
3110 }
3111
3112 if (*p == '\"')
3113 p++;
3114 strncpy(password, p, 64);
3115 p = password;
3116 while(password - p < 64 && *password != '\0')
3117 {
3118 if (*password == '\"')
3119 {
3120 *password = '\0';
3121 RLOGD("---------password------- p:: %s",p);
3122 ret = 0;
3123 break;
3124 }
3125 password++;
3126 }
3127 break;
3128 }
3129 break;
3130 }
3131 }
3132
3133 return ret;
3134}
3135
3136
3137
you.chen35020192022-05-06 11:30:57 +08003138int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
3139
3140 FILE * fp;
qs.xiongb5dab082023-10-13 14:43:41 +08003141 int len, ret;
3142 char *info_buff;
you.chen6d247052023-06-01 16:39:54 +08003143 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08003144
qs.xiongb5dab082023-10-13 14:43:41 +08003145 info_buff = NULL;
you.chen35020192022-05-06 11:30:57 +08003146 CHECK_IDX(idx, CTRL_STA);
3147
qs.xiong9fbf74e2023-03-28 13:38:22 +08003148 if (NULL == password)
3149 {
3150 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08003151 return -1;
3152 }
3153
you.chen35020192022-05-06 11:30:57 +08003154 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08003155 if (NULL == fp)
3156 {
3157 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08003158 return -1;
3159 }
3160
you.chen6d247052023-06-01 16:39:54 +08003161 fseek(fp, 0, SEEK_END);
3162 len = ftell(fp);
qs.xiongb5dab082023-10-13 14:43:41 +08003163 info_buff = malloc(len + 1);
you.chen6d247052023-06-01 16:39:54 +08003164
qs.xiongb5dab082023-10-13 14:43:41 +08003165 if (info_buff == NULL)
you.chen6d247052023-06-01 16:39:54 +08003166 {
3167 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
3168 return -1;
3169 }
3170
you.chen35020192022-05-06 11:30:57 +08003171 fseek(fp, 0, SEEK_SET);
qs.xiongb5dab082023-10-13 14:43:41 +08003172 len = fread(info_buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08003173 fclose(fp);
3174
qs.xiongb5dab082023-10-13 14:43:41 +08003175
3176 ret= lynq_sta_ssid_password_auth_get(info_buff,len,0, ap,password);
3177
3178 if(ret == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003179 {
qs.xiongb5dab082023-10-13 14:43:41 +08003180 RLOGD("lynq_sta_ssid_password_auth_get pass return ssid :%s psw is %s",ap->ap_ssid,password);
3181 free(info_buff);
3182 return 0;
you.chen35020192022-05-06 11:30:57 +08003183 }
qs.xiongb5dab082023-10-13 14:43:41 +08003184 else{
3185 free(info_buff);
you.chen35020192022-05-06 11:30:57 +08003186 return -1;
3187 }
3188
you.chen35020192022-05-06 11:30:57 +08003189}
3190
qs.xiongb5dab082023-10-13 14:43:41 +08003191
you.chen35020192022-05-06 11:30:57 +08003192static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3193{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003194 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003195
qs.xiong9fbf74e2023-03-28 13:38:22 +08003196 if (sta_ssid == NULL)
3197 {
3198 RLOGE("sta_ssid is null\n");
3199 return -1;
you.chen35020192022-05-06 11:30:57 +08003200 }
3201
qs.xiong9fbf74e2023-03-28 13:38:22 +08003202 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003203
3204 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3205
3206 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3207// DO_OK_FAIL_REQUEST(cmd_save_config);
3208
qs.xiong9fbf74e2023-03-28 13:38:22 +08003209 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003210
3211}
3212
you.chen35020192022-05-06 11:30:57 +08003213static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003214{
you.chen35020192022-05-06 11:30:57 +08003215 char lynq_disable_cmd[128]={0};
3216 char lynq_select_cmd[128]={0};
3217
3218 CHECK_WPA_CTRL(CTRL_STA);
3219
qs.xiong9fbf74e2023-03-28 13:38:22 +08003220 if (save != 0)
3221 {
you.chenc29444e2022-06-07 18:01:16 +08003222 if (start_flag != 0)
3223 {
3224 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3225 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3226 }
3227 else
3228 {
3229 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3230 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3231 }
you.chen35020192022-05-06 11:30:57 +08003232 DO_OK_FAIL_REQUEST(cmd_save_config);
3233 }
3234
qs.xiong9fbf74e2023-03-28 13:38:22 +08003235 if (start_flag == 0)
3236 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003237 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003238 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3239 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003240 else
3241 {
you.chen35020192022-05-06 11:30:57 +08003242 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3243 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3244 }
3245
3246 return 0;
3247}
3248
3249int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3250{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003251 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003252 CHECK_IDX(idx, CTRL_STA);
3253
you.chen6c2dd9c2022-05-16 17:55:28 +08003254 curr_status_info curr_state;
3255 ap_info_s ap_info;
3256 curr_state.ap = &ap_info;
3257 curr_state.state = NULL;
3258
qs.xiong9fbf74e2023-03-28 13:38:22 +08003259 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3260 {
you.chend2fef3f2023-02-13 10:50:35 +08003261 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003262 return 0;
3263 }
3264
3265 return -1;
you.chen35020192022-05-06 11:30:57 +08003266}
3267
3268int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3269{
qs.xiong5d716d22023-09-20 20:08:39 +08003270 RLOGD("[wifi] ---ernter lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003271 scan_info_s *scan_list = NULL;
3272 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003273 int scan_len=0;
3274 int save_len=0;
3275 int best_index = -1;
3276 int best_scan_index = -1;
3277 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003278 int i, j, ret;
3279
3280 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003281
3282 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003283 if (info == NULL)
3284 {
you.chen35020192022-05-06 11:30:57 +08003285 return -1;
3286 }
3287
3288 curr_status_info curr_state;
3289 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003290 char status[64];
you.chen35020192022-05-06 11:30:57 +08003291
you.chen9ac66392022-08-06 17:01:16 +08003292 memset(&ap_info, 0, sizeof (ap_info));
3293 memset(status, 0, sizeof (status));
3294
3295 curr_state.ap = &ap_info;
3296 curr_state.state = status;
3297
qs.xiong9fbf74e2023-03-28 13:38:22 +08003298 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3299 {
you.chen35020192022-05-06 11:30:57 +08003300 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003301 if (strcmp(status, STATE_COMPLETED) == 0)
3302 {
3303 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003304 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen9ac66392022-08-06 17:01:16 +08003305 }
3306 else
3307 {
3308 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003309 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status --> LYNQ_WIFI_AP_STATUS_DISABLE");
you.chen9ac66392022-08-06 17:01:16 +08003310 }
you.chen593621d2023-04-27 17:52:44 +08003311 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003312 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003313 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
qs.xiong5d716d22023-09-20 20:08:39 +08003314 RLOGD("[wifi] ---ent --lynq_wifi_get_sta_available_ap");
you.chen35020192022-05-06 11:30:57 +08003315 return 0;
3316 }
3317
you.chen9ac66392022-08-06 17:01:16 +08003318 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003319 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003320 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3321 {
you.chen9ac66392022-08-06 17:01:16 +08003322 if (NULL != scan_list)
3323 {
3324 free(scan_list);
3325 }
you.chen35020192022-05-06 11:30:57 +08003326 return -1;
3327 }
3328
qs.xiong9fbf74e2023-03-28 13:38:22 +08003329 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3330 {
you.chen9ac66392022-08-06 17:01:16 +08003331 if (NULL != scan_list)
3332 {
3333 free(scan_list);
3334 }
3335 if (NULL != save_list)
3336 {
3337 free(save_list);
3338 }
you.chen35020192022-05-06 11:30:57 +08003339 return -1;
3340 }
3341
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 for (i=0; i < save_len; i++)
3343 {
3344 for (j=0; j < scan_len; j++)
3345 {
you.chen35020192022-05-06 11:30:57 +08003346 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003347 && save_list[i].base_info.auth == scan_list[j].auth)
3348 {
3349 if (best_rssi == 0)
3350 {
you.chen9ac66392022-08-06 17:01:16 +08003351 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003352 best_rssi = scan_list[j].rssi;
3353 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003354 else if (best_rssi > scan_list[j].rssi)
3355 {
you.chen35020192022-05-06 11:30:57 +08003356 best_index = i;
3357 best_scan_index = j;
3358 best_rssi = scan_list[j].rssi;
3359 }
you.chend2fef3f2023-02-13 10:50:35 +08003360 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 +08003361 break;
3362 }
3363 }
3364 }
3365
qs.xiong9fbf74e2023-03-28 13:38:22 +08003366 if (best_index >= 0)
3367 {
you.chen35020192022-05-06 11:30:57 +08003368 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003369 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 +08003370 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
qs.xiong5d716d22023-09-20 20:08:39 +08003371 RLOGD("[wifi] ---lynq_wifi_get_sta_available_ap status ---> LYNQ_WIFI_AP_STATUS_ENABLE");
you.chen35020192022-05-06 11:30:57 +08003372 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003373 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003374 }
3375
you.chen9ac66392022-08-06 17:01:16 +08003376 if (NULL != scan_list)
3377 {
3378 free(scan_list);
3379 }
3380 if (NULL != save_list)
3381 {
3382 free(save_list);
3383 }
3384
qs.xiong5d716d22023-09-20 20:08:39 +08003385 RLOGD("[wifi] ---end -lynq_wifi_get_sta_available_ap");
you.chen9ac66392022-08-06 17:01:16 +08003386 return ret;
you.chen35020192022-05-06 11:30:57 +08003387}
3388
3389static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3390{
qs.xiongc8d92a62023-03-29 17:36:14 +08003391 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003392 char lynq_ket_mgmt_cmd[64]={0};
3393 char lynq_pairwise_cmd[64]={0};
3394 char lynq_psk_cmd[64]={0};
3395
3396 CHECK_WPA_CTRL(CTRL_STA);
3397
qs.xiong9fbf74e2023-03-28 13:38:22 +08003398 switch(auth)
3399 {
3400 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003401 {
3402 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003403
you.chen35020192022-05-06 11:30:57 +08003404 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003405// DO_OK_FAIL_REQUEST(cmd_save_config);
3406 break;
3407 }
3408 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003409 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003410 {
3411 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3412 {
you.chen35020192022-05-06 11:30:57 +08003413 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3414 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003415 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3416 {
you.chena6cd55a2022-05-08 12:20:18 +08003417 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003418 }
3419 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3420 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003421
you.chen35020192022-05-06 11:30:57 +08003422 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3423 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3424 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003425
qs.xiong9fbf74e2023-03-28 13:38:22 +08003426 if (password != NULL)
3427 {
you.chen35020192022-05-06 11:30:57 +08003428 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3429 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003430 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003431 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003432
you.chen35020192022-05-06 11:30:57 +08003433// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003434 break;
3435 }
3436 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3437 {
qs.xiong3e506812023-04-06 11:08:48 +08003438 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003439 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003440 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3441 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3442
qs.xiong3e506812023-04-06 11:08:48 +08003443 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003444 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3445 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3446 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3447
3448 break;
3449 }
3450 case LYNQ_WIFI_AUTH_WPA3_PSK:
3451 {
qs.xiong3e506812023-04-06 11:08:48 +08003452 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003453 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003454 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3455 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3456
qs.xiongb37f8c42023-09-13 21:21:58 +08003457 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003458 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3459 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3460 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3461
3462 break;
3463 }
3464 default:
3465 return -1;
you.chen35020192022-05-06 11:30:57 +08003466 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003467
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003469}
qs.xiong7a105ce2022-03-02 09:43:11 -05003470
you.chen35020192022-05-06 11:30:57 +08003471static int inner_get_curr_net_no(int interface) {
3472 curr_status_info curr_state;
3473 curr_state.ap = NULL;
3474 curr_state.state = NULL;
3475
qs.xiong9fbf74e2023-03-28 13:38:22 +08003476 if (0 != inner_get_status_info(interface, &curr_state))
3477 {
you.chen35020192022-05-06 11:30:57 +08003478 return -1;
3479 }
3480
3481 return curr_state.net_no;
3482}
3483
3484int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003485{
you.chen35020192022-05-06 11:30:57 +08003486 int net_no;
3487 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003488
you.chen35020192022-05-06 11:30:57 +08003489 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003490
qs.xiong9fbf74e2023-03-28 13:38:22 +08003491 if (net_no < 0)
3492 {
you.chen35020192022-05-06 11:30:57 +08003493 return -1;
3494 }
3495
3496 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003497}
3498
you.chenb95401e2023-05-12 19:39:06 +08003499int 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 -05003500{
you.chen35020192022-05-06 11:30:57 +08003501 int count, net_no, index;
3502 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003503 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003504 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003505 curr_status_info curr_state;
3506 ap_info_s ap_info;
3507 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003508
qs.xiong9fbf74e2023-03-28 13:38:22 +08003509 if (ssid == NULL || *ssid == '\0')
3510 {
3511 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003512 return -1;
3513 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003514
qs.xiong9fbf74e2023-03-28 13:38:22 +08003515 if (LYNQ_WIFI_AUTH_OPEN != auth)
3516 {
3517 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003518 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003519 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003520 return -1;
3521 }
3522 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003523
you.chen70f377f2023-04-14 18:17:09 +08003524
3525 pthread_mutex_lock(&s_global_check_mutex);
3526 if (s_sta_status != INNER_STA_STATUS_INIT)
3527 {
3528 s_sta_status = INNER_STA_STATUS_CANCEL;
3529 pthread_cond_signal(&s_global_check_cond);
3530 }
3531 pthread_mutex_unlock(&s_global_check_mutex);
3532
you.chen35020192022-05-06 11:30:57 +08003533 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003534 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003535
3536 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003537 memset(&ap_info, 0, sizeof (ap_info));
3538 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003539
you.chen70f377f2023-04-14 18:17:09 +08003540 curr_state.ap = &ap_info;
3541 curr_state.state = status;
3542
3543 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003544 {
you.chen70f377f2023-04-14 18:17:09 +08003545 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3546 {
3547 net_no = curr_state.net_no;
3548 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3549 && strcmp(ap_info.psw, psw) == 0)
3550 {
3551 RLOGD("already connected\n");
3552
3553 pthread_mutex_lock(&s_global_check_mutex);
3554 s_sta_status = INNER_STA_STATUS_CONNECTED;
3555 pthread_cond_signal(&s_global_check_cond);
3556 pthread_mutex_unlock(&s_global_check_mutex);
3557 return 0;
3558 }
you.chen35020192022-05-06 11:30:57 +08003559 }
3560 }
3561
you.chen70f377f2023-04-14 18:17:09 +08003562 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003563 {
you.chen70f377f2023-04-14 18:17:09 +08003564 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3565
3566 for (index=0; index < count; index++)
3567 {
3568 net_auth = -1;
3569 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3570 {
3571 net_no = net_no_list[index];
3572 break;
3573 }
you.chen35020192022-05-06 11:30:57 +08003574 }
3575
you.chen70f377f2023-04-14 18:17:09 +08003576 if (net_no < 0)
3577 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003578 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3579 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3580 {
3581 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3582 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3583 DO_OK_FAIL_REQUEST(rm_net_cmd);
3584 }
you.chen70f377f2023-04-14 18:17:09 +08003585 net_no = lynq_add_network(CTRL_STA);
3586 if (net_no == -1)
3587 {
3588 return -1;
3589 }
3590
3591 RLOGD("net no is %d\n", net_no);
3592 if (0 != inner_set_sta_ssid(net_no, ssid))
3593 {
3594 return -1;
3595 }
you.chen35020192022-05-06 11:30:57 +08003596 }
3597 }
3598
qs.xiong9fbf74e2023-03-28 13:38:22 +08003599 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3600 {
you.chen35020192022-05-06 11:30:57 +08003601 return -1;
3602 }
3603
you.chen70f377f2023-04-14 18:17:09 +08003604
3605 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003606 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003607 usleep(200*1000);
3608
3609 ret = inner_sta_start_stop(net_no, 1, 1);
3610
3611 pthread_mutex_lock(&s_global_check_mutex);
3612 s_sta_status = INNER_STA_STATUS_CONNECTING;
3613 strcpy(s_sta_current_connecting_ssid, ssid);
3614 struct timeval now;
3615 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003616 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003617 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3618 pthread_cond_signal(&s_global_check_cond);
3619 pthread_mutex_unlock(&s_global_check_mutex);
3620 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003621}
3622
you.chenb95401e2023-05-12 19:39:06 +08003623int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3624{
3625 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3626}
3627
you.chen35020192022-05-06 11:30:57 +08003628int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003629{
you.chen35020192022-05-06 11:30:57 +08003630 ap_info_s ap;
3631 curr_status_info curr_state;
3632 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003633
qs.xiong9fbf74e2023-03-28 13:38:22 +08003634 if (ssid == NULL || *ssid == '\0')
3635 {
3636 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003637 return -1;
3638 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003639
you.chen35020192022-05-06 11:30:57 +08003640 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003641
you.chen35020192022-05-06 11:30:57 +08003642 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003643 curr_state.state = NULL;
3644
qs.xiong9fbf74e2023-03-28 13:38:22 +08003645 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3646 {
you.chen35020192022-05-06 11:30:57 +08003647 return 0;
3648 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003649
qs.xiong9fbf74e2023-03-28 13:38:22 +08003650 if (strcmp(ap.ap_ssid, ssid) != 0)
3651 {
you.chen35020192022-05-06 11:30:57 +08003652 return 0;
3653 }
3654
you.chen70f377f2023-04-14 18:17:09 +08003655 pthread_mutex_lock(&s_global_check_mutex);
3656 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3657 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003658 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003659}
qs.xiong97fa59b2022-04-07 05:41:29 -04003660
qs.xiongc93bf2b2023-08-25 10:22:08 +08003661int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3662{
3663 ap_info_s ap;
3664 curr_status_info curr_state;
3665 ap.ap_ssid[0] = '\0';
3666
3667 if (ssid == NULL || *ssid == '\0')
3668 {
3669 RLOGE("input ssid is NULL\n");
3670 return -1;
3671 }
3672
3673 CHECK_IDX(idx, CTRL_STA);
3674
3675
3676 curr_state.ap = &ap;
3677 curr_state.state = NULL;
3678
3679 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3680 {
3681 return 0;
3682 }
3683
3684 if (strcmp(ap.ap_ssid, ssid) != 0)
3685 {
3686 return 0;
3687 }
3688
3689 pthread_mutex_lock(&s_global_check_mutex);
3690 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
qs.xiongb37f8c42023-09-13 21:21:58 +08003691 g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
3692 g_history_disconnect_valid_num++;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003693 pthread_mutex_unlock(&s_global_check_mutex);
3694 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3695
3696}
3697
3698
you.chena6cd55a2022-05-08 12:20:18 +08003699int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3700{
qs.xiongb37f8c42023-09-13 21:21:58 +08003701
qs.xiongad2f89d2023-01-18 13:17:41 +08003702 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3703 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003704
you.chen35020192022-05-06 11:30:57 +08003705 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003706 CHECK_WPA_CTRL(CTRL_STA);
3707
you.chenc9928582023-04-24 15:39:37 +08003708 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3709 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003710 {
you.chenc9928582023-04-24 15:39:37 +08003711 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003712 return -1;
3713 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003714
qs.xiongad2f89d2023-01-18 13:17:41 +08003715 system(lynq_enable_sta_cmd);
3716 system(lynq_reconnect_cmd);
qs.xiongb37f8c42023-09-13 21:21:58 +08003717 pthread_mutex_lock(&s_global_check_mutex);
3718 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
you.chen6e724162023-10-19 19:10:01 +08003719 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003720 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003721 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003722}
3723
you.chen6d247052023-06-01 16:39:54 +08003724static int inner_get_status_info_state (int interface, char *state) {
3725 curr_status_info curr_state;
3726 curr_state.ap = NULL;
3727 curr_state.state = state;
3728 return inner_get_status_info(interface, &curr_state);
3729}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003730
3731int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3732{
3733
qs.xiongb37f8c42023-09-13 21:21:58 +08003734 RLOGD("[wifi]enter lynq_wifi_sta_start_auto start");
3735 int tmp_open_idx[128];
3736 int len;
qs.xiongc93bf2b2023-08-25 10:22:08 +08003737
qs.xiongb37f8c42023-09-13 21:21:58 +08003738 pthread_mutex_lock(&s_global_check_mutex);
you.chen6e724162023-10-19 19:10:01 +08003739 s_sta_status = INNER_STA_STATUS_INIT;
qs.xiongb37f8c42023-09-13 21:21:58 +08003740 lynq_two_arr_merge(g_history_disconnect_net,g_history_disconnect_valid_num,tmp_open_idx,&len);
3741 pthread_mutex_unlock(&s_global_check_mutex);
3742 if(lynq_tmp_enable_network(idx,tmp_open_idx,len) != 0 )
qs.xiongc93bf2b2023-08-25 10:22:08 +08003743 {
qs.xiongb37f8c42023-09-13 21:21:58 +08003744 RLOGD("[wifi]lynq_tmp_enable_network error");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003745 }
3746
qs.xiongb37f8c42023-09-13 21:21:58 +08003747 RLOGD("[wifi]enter lynq_wifi_sta_start_auto end");
qs.xiongc93bf2b2023-08-25 10:22:08 +08003748 return 0;
3749}
3750
3751
you.chen35020192022-05-06 11:30:57 +08003752int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003753{
qs.xiongad2f89d2023-01-18 13:17:41 +08003754// char lynq_disable_network_cmd[MAX_CMD];
3755// curr_status_info curr_state;
3756// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003757 int i=0;
3758 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003759
you.chen6d247052023-06-01 16:39:54 +08003760// 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 +08003761 CHECK_IDX(idx, CTRL_STA);
3762 CHECK_WPA_CTRL(CTRL_STA);
3763
you.chen6d247052023-06-01 16:39:54 +08003764// system(lynq_disable_sta_cmd);
3765 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003766 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003767
3768 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3769 if (ret != 0)
3770 {
3771 RLOGE("lynq_wifi_ap_start excute script fail");
3772 return -1;
3773 }
3774
you.chen6d247052023-06-01 16:39:54 +08003775 for (i=0; i < 30; i++) // to check if sta is realy stoped
3776 {
3777 if (inner_get_status_info_state(idx, state) != 0)
3778 {
3779 break;
3780 }
3781
3782 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3783 {
3784 break;
3785 }
3786 RLOGD("lynq_wifi_ap_start curr state %s", state);
3787 usleep(SLEEP_TIME_ON_IDLE);
3788 }
qs.xiongb37f8c42023-09-13 21:21:58 +08003789 pthread_mutex_lock(&s_global_check_mutex);
3790 g_history_disconnect_valid_num = 0; //clean history_disconenct_list info
3791 pthread_mutex_unlock(&s_global_check_mutex);
you.chena6cd55a2022-05-08 12:20:18 +08003792 return 0;
3793// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003794}
qs.xiongfcc914b2023-07-06 21:16:20 +08003795int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3796{
3797 char LYNQ_DISABLE_CMD[128]={0};
3798 CHECK_IDX(idx, CTRL_STA);
3799 CHECK_WPA_CTRL(CTRL_STA);
3800 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3801 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3802 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3803 return 0;
3804}
qs.xiong7a105ce2022-03-02 09:43:11 -05003805
you.chen35020192022-05-06 11:30:57 +08003806//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3807// int i, count;
3808// char *p;
3809// const char * FLAG_SSID = "ssid=";
3810// const char * FLAG_SBSID = "bssid=";
3811// const char * FLAG_KEY_MGMT = "key_mgmt=";
3812// const char * FLAG_FREQ = "freq=";
3813// char lynq_sta_cmd[MAX_CMD];
3814// char *split_lines[128] = {0};
3815
3816// CHECK_WPA_CTRL(CTRL_AP);
3817
3818// sprintf(lynq_sta_cmd, "STA %s", bssid);
3819
3820// DO_REQUEST(lynq_sta_cmd);
3821
3822// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3823
3824// for(i=0; i < count; i++) {
3825// p = strstr(split_lines[i], FLAG_SSID);
3826// if (p != NULL) {
3827// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3828// continue;
3829// }
3830// }
3831
3832// lynq_get_interface_ip(idx, ap->ap_ip);
3833// lynq_ap_password_set(idx, ap->psw);
3834
3835// return 0;
3836//}
3837
3838static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3839 curr_status_info curr_state;
3840 curr_state.ap = ap;
3841 curr_state.state = NULL;
3842 return inner_get_status_info(interface, &curr_state);
3843}
3844
3845int 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 -04003846{
qs.xiong5071c802023-09-06 14:04:15 +08003847 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003848 int index, line_count;
3849 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003850 const char *lynq_first_sta_cmd = "STA-FIRST";
3851 char lynq_next_sta_cmd[MAX_CMD];
3852 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003853 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003854
you.chen35020192022-05-06 11:30:57 +08003855 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003856
you.chen35020192022-05-06 11:30:57 +08003857 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003858
you.chenb95401e2023-05-12 19:39:06 +08003859 // ap_info_s * tmp_ap;
3860 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003861 if (ap == NULL || list == NULL || len == NULL)
3862 {
3863 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003864 return -1;
3865 }
3866
you.chenb95401e2023-05-12 19:39:06 +08003867 // ap = &tmp_ap;
3868 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003869 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003870 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003871
you.chenb95401e2023-05-12 19:39:06 +08003872 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003873 {
you.chenb95401e2023-05-12 19:39:06 +08003874 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003875 return -1;
3876 }
3877
3878 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3879 lynq_ap_password_get(idx, (*ap)->psw);
3880
you.chen35020192022-05-06 11:30:57 +08003881 DO_REQUEST(lynq_first_sta_cmd);
3882
3883 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003884 while (reply_len > 0)
3885 {
3886 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003887 {
you.chen35020192022-05-06 11:30:57 +08003888 break;
3889 }
3890 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3891 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3892 strcpy(bssid[index], split_lines[0]);
3893 index++;
3894 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3895 reply_len = MAX_RET;
3896 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003897 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 +08003898 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003899 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003900 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003901 break;
3902 }
3903 }
3904
3905 *len = index;
3906
3907 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003908 for (index=0; index < *len; index++)
3909 {
you.chend2fef3f2023-02-13 10:50:35 +08003910 dev_info = &(*list)[index];
3911 memset(dev_info, 0, sizeof(device_info_s));
3912 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3913 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3914 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3915 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003916 free(bssid[index]);
3917 }
qs.xiong5071c802023-09-06 14:04:15 +08003918 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003919 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003920}
3921
you.chen35020192022-05-06 11:30:57 +08003922int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003923{
you.chen35020192022-05-06 11:30:57 +08003924 int i, count, index, count_words;
3925 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3926 char *split_lines[128] = {0};
3927 char *split_words[128] = {0};
3928 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003929
qs.xiong9fbf74e2023-03-28 13:38:22 +08003930 if (list == NULL || len == NULL)
3931 {
you.chen35020192022-05-06 11:30:57 +08003932 return -1;
3933 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003934
you.chen9ac66392022-08-06 17:01:16 +08003935 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3936 {
3937 usleep(100 * 1000);
3938 }
3939
you.chen35020192022-05-06 11:30:57 +08003940 CHECK_IDX(idx, CTRL_STA);
3941
3942 CHECK_WPA_CTRL(CTRL_STA);
3943
3944 DO_REQUEST(lynq_scan_result_cmd);
3945
3946 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3947 *len = count - 1;
3948 *list = malloc(sizeof (scan_info_s) * *len);
3949
3950 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003951 for (index=0; index <count_words; index++)
3952 {
3953 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003954 }
3955
qs.xiong9fbf74e2023-03-28 13:38:22 +08003956 for(index = 1;index < count; index++)
3957 {
3958 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003959 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003960 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3961 if (count_words < 4)
3962 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003963 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003964 //bssid / frequency / signal level / flags / ssid
3965 p = (*list) + index - 1;
3966 strcpy(p->mac, split_words[0]);
3967 p->band = convert_band_from_freq(atoi(split_words[1]));
3968 p->rssi = -1 * atoi( split_words[2]);
3969 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003970 if (count_words == 4) // ssid hided
3971 {
3972 p->ssid[0] = '\0';
3973 }
3974 else
3975 {
3976 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3977 }
you.chen35020192022-05-06 11:30:57 +08003978 }
3979
3980 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003981}
qs.xiong97fa59b2022-04-07 05:41:29 -04003982
you.chen35020192022-05-06 11:30:57 +08003983int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3984{
3985 int count, net_no, index;
3986 int net_no_list[128];
3987 lynq_wifi_auth_s net_auth;
3988 char lynq_remove_cmd[MAX_CMD];
3989
qs.xiong9fbf74e2023-03-28 13:38:22 +08003990 if (ssid == NULL || *ssid == '\0')
3991 {
3992 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003993 return -1;
3994 }
3995
3996 CHECK_IDX(idx, CTRL_STA);
3997
3998 CHECK_WPA_CTRL(CTRL_STA);
3999
4000 net_no = -1;
4001 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
4002
qs.xiong9fbf74e2023-03-28 13:38:22 +08004003 for (index=0; index < count; index++)
4004 {
you.chen35020192022-05-06 11:30:57 +08004005 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004006 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
4007 {
you.chen35020192022-05-06 11:30:57 +08004008 net_no = net_no_list[index];
4009 break;
4010 }
4011 }
4012
qs.xiong9fbf74e2023-03-28 13:38:22 +08004013 if (net_no < 0)
4014 {
you.chen35020192022-05-06 11:30:57 +08004015 return 0;
4016 }
4017
4018 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
4019
4020 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
4021 DO_OK_FAIL_REQUEST(cmd_save_config);
4022
4023 return 0;
4024}
4025
4026int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004027{
you.chend2fef3f2023-02-13 10:50:35 +08004028 int count, index;
you.chen35020192022-05-06 11:30:57 +08004029 int net_no_list[128];
4030 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004031 RLOGD("enter lynq_get_sta_saved_ap api\n");
4032 if (list == NULL || len == NULL)
4033 {
4034 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08004035 return -1;
4036 }
4037
4038 CHECK_IDX(idx, CTRL_STA);
4039
4040// CHECK_WPA_CTRL(CTRL_STA);
4041
4042 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004043 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08004044
you.chen057aac42023-04-13 14:06:58 +08004045 if (count < 0)
4046 {
4047 RLOGE("list network fail");
4048 return count;
4049 }
4050 else if (count == 0)
4051 {
4052 *list = NULL;
4053 *len = 0;
4054 return 0;
4055 }
4056
you.chen35020192022-05-06 11:30:57 +08004057 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08004058 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08004059 *len = count;
4060
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 for (index=0; index < count; index++)
4062 {
you.chen35020192022-05-06 11:30:57 +08004063 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08004064 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08004065 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004066 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08004067 {
you.chen35020192022-05-06 11:30:57 +08004068 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
4069 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004070 else
you.chen057aac42023-04-13 14:06:58 +08004071 {
you.chen35020192022-05-06 11:30:57 +08004072 (*list)[index].base_info.band = -1;
4073 }
you.chen057aac42023-04-13 14:06:58 +08004074 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08004075 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08004076 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004077 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08004078 return 0;
4079}
4080
4081int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
4082{
qs.xiong20202422023-09-06 18:01:18 +08004083 if ( g_sta_conncet_status_flag != 0 )
4084 {
4085 RLOGD("current sta is connecting dest ap");
qs.xiongba5b5f22023-09-19 14:55:34 +08004086 return 1;
qs.xiong20202422023-09-06 18:01:18 +08004087 }
qs.xiongc8d92a62023-03-29 17:36:14 +08004088 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08004089 const char *lynq_scan_cmd = "SCAN";
4090
4091 CHECK_IDX(idx, CTRL_STA);
4092
4093 CHECK_WPA_CTRL(CTRL_STA);
4094
you.chen0df3e7e2023-05-10 15:56:26 +08004095 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
4096 {
4097 RLOGD("tmp clear scanlist");
4098 system(clean_last_re);
4099 }
you.chen9ac66392022-08-06 17:01:16 +08004100 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08004101 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004102 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
4103 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004104 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004105 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
4106 {
qs.xiongb3f26af2023-02-17 18:41:07 +08004107 g_sta_scan_finish_flag = 1;
4108 return -1;
4109 }
you.chen35020192022-05-06 11:30:57 +08004110
4111 return 0;
4112}
4113
4114int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004115 if (cb == NULL)
4116 {
4117 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004118 return -1;
4119 }
4120
you.chen6d247052023-06-01 16:39:54 +08004121 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004122 g_ap_callback_priv = priv;
4123 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004124 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004125
you.chen6d247052023-06-01 16:39:54 +08004126 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004127 if (g_ap_watcher_pid == 0 )
4128 {
4129 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
4130 {
4131 g_ap_watcher_pid = 0;
4132 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4133 RLOGE("[wifi error]creat APWatcherThreadProc fail");
4134 return -1;
4135 }
4136 }
4137
4138 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4139 RLOGD("creat APWatcherTheradProc susccs");
4140
you.chen35020192022-05-06 11:30:57 +08004141 return 0;
4142}
4143
4144int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004145 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004146 if (g_ap_callback_priv == priv)
4147 {
you.chen35020192022-05-06 11:30:57 +08004148 g_ap_callback_func = NULL;
4149 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004150 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004151 return 0;
4152 }
you.chen6d247052023-06-01 16:39:54 +08004153 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004154 return -1;
4155}
4156
4157int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08004158 if (cb == NULL)
4159 {
4160 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08004161 return -1;
4162 }
4163
you.chen6d247052023-06-01 16:39:54 +08004164 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004165 g_sta_callback_priv = priv;
4166 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08004167 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004168
you.chen6d247052023-06-01 16:39:54 +08004169 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08004170 if (g_sta_watcher_pid == 0 ) {
4171 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
4172 {
4173 g_sta_watcher_pid = 0;
4174 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4175 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4176 return -1;
4177 }
4178 }
4179
4180 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4181 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004182 return 0;
4183}
4184
4185int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004186 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004187 if (g_sta_callback_priv == priv)
4188 {
you.chen35020192022-05-06 11:30:57 +08004189 g_sta_callback_func = NULL;
4190 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004191 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004192 return 0;
4193 }
you.chen6d247052023-06-01 16:39:54 +08004194 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004195 return -1;
4196}
4197
qs.xiongfcc914b2023-07-06 21:16:20 +08004198int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4199 if (cb == NULL)
4200 {
4201 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4202 return -1;
4203 }
4204 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4205 g_sta_auto_callback_priv = priv;
4206 g_sta_auto_callback_func = cb;
4207 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4208 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4209 if (g_sta_auto_watcher_pid == 0 ) {
4210 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4211 {
4212 g_sta_auto_watcher_pid = 0;
4213 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4214 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4215 return -1;
4216 }
4217 }
4218 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4219 RLOGD("creat STAWatcherTheradProc susccs");
4220 return 0;
4221}
4222int lynq_unreg_sta_auto_event_callback(void * priv) {
4223 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4224 if (g_sta_auto_callback_priv == priv)
4225 {
4226 g_sta_auto_watcher_stop_flag = 1;
4227 if (g_sta_auto_watcher_pid != 0)
4228 {
4229 pthread_join(g_sta_auto_watcher_pid, NULL);
4230 }
4231 g_sta_auto_watcher_pid = 0;
4232 g_sta_auto_callback_func = NULL;
4233 g_sta_auto_callback_priv = NULL;
4234 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4235 return 0;
4236 }
4237 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4238 return -1;
4239}
you.chen35020192022-05-06 11:30:57 +08004240int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4241{
4242 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004243 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004244 CHECK_IDX(idx, CTRL_AP);
4245
qs.xiong9fbf74e2023-03-28 13:38:22 +08004246 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4247 {
you.chen35020192022-05-06 11:30:57 +08004248 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4249 return 0;
4250 }
4251
qs.xiong9fbf74e2023-03-28 13:38:22 +08004252 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4253 {
you.chen35020192022-05-06 11:30:57 +08004254 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4255 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004256 else
4257 {
you.chen35020192022-05-06 11:30:57 +08004258 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4259 }
4260
4261 return 0;
4262}
4263
4264int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4265 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004266 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004267 CHECK_IDX(idx, CTRL_STA);
4268
qs.xiong9fbf74e2023-03-28 13:38:22 +08004269 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4270 {
you.chen35020192022-05-06 11:30:57 +08004271 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4272 return 0;
4273 }
4274
qs.xiong9fbf74e2023-03-28 13:38:22 +08004275 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4276 {
you.chen35020192022-05-06 11:30:57 +08004277 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4278 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004279 else
4280 {
you.chen35020192022-05-06 11:30:57 +08004281 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4282 }
4283
4284 return 0;
4285}
4286
4287int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4288// CHECK_IDX(idx, CTRL_AP);
4289// int ret = 0;
4290// size_t reply_len = MAX_RET;
4291// char cmd_reply[MAX_RET]={0};
4292// const char * cmd_str = "GET country";
4293// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4294// do{
4295// if (NULL == s_lynq_wpa_ctrl) {
4296// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4297// if (NULL == s_lynq_wpa_ctrl ) {
4298// printf("wpa_ctrl_open fail\n");
4299// return -1;
4300// }
4301// }
4302// }while(0);
4303
4304// do {
4305// reply_len = MAX_RET;
4306// cmd_reply[0] = '\0';
4307// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004308// 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 +08004309// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004310// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004311// return ret;
4312// }
4313// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004314// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004315// }while(0);
4316
4317 FILE *fp;
4318 size_t i = 0;
4319 char lynq_cmd_ret[MAX_RET]={0};
4320
4321// CHECK_IDX(idx, CTRL_AP);
4322
4323 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004324 {
4325 perror("popen error!");
4326 return -1;
4327 }
you.chen35020192022-05-06 11:30:57 +08004328 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4329 {
4330 perror("fread fail!");
4331 return -1;
4332 }
4333
qs.xiong9fbf74e2023-03-28 13:38:22 +08004334 for(i=0; i < strlen(lynq_cmd_ret); i++)
4335 {
4336 if (lynq_cmd_ret[i] == ' ')
4337 {
you.chen35020192022-05-06 11:30:57 +08004338 lynq_cmd_ret[i] = '\0';
4339 break;
4340 }
4341 }
4342
4343 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004344 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004345
4346 int ret=pclose(fp);
4347 if(ret==-1)
4348 {
4349 perror("close file faild");
4350 }
4351
4352 return 0;
4353}
4354
qs.xiong44fac672023-08-29 16:15:55 +08004355
you.chen705a7ef2023-06-01 22:06:45 +08004356static int check_and_init_uci_config(char * country_code)
4357{
4358 FILE * fp;
4359 int is_different = 0;
4360 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4361 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4362 const char * commit_uci_cmd ="uci commit";
4363 char set_country_cmd[MAX_CMD];
4364 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004365
you.chen705a7ef2023-06-01 22:06:45 +08004366 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004367
you.chen705a7ef2023-06-01 22:06:45 +08004368 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004369 {
you.chen705a7ef2023-06-01 22:06:45 +08004370 if (0 != system(create_uci_cmd))
4371 {
4372 RLOGE("creat_uci_cmd fail");
4373 return -1;
4374 }
4375 is_different = 1;
4376 }
4377
4378 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4379 {
4380 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004381 return -1;
4382 }
4383
you.chen705a7ef2023-06-01 22:06:45 +08004384 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004385 {
you.chen705a7ef2023-06-01 22:06:45 +08004386 RLOGE("fread fail!");
4387 fclose(fp);
4388 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004389 }
4390
you.chen705a7ef2023-06-01 22:06:45 +08004391 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4392 {
qs.xiong44fac672023-08-29 16:15:55 +08004393 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 +08004394 is_different = 1;
4395 }
4396
4397 fclose(fp);
4398
4399 if (is_different)
4400 {
4401 if ( 0 != system(set_country_cmd))
4402 {
4403 RLOGE("set_country_cmd fail");
4404 return -1;
4405 }
4406 if (0 != system(commit_uci_cmd))
4407 {
4408 RLOGE("commmit fail");
4409 }
4410 }
4411
4412 return is_different;
4413}
4414
4415int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4416 char check_current_code[10];
4417 const char * support_country[] = {"CN", "EU"};
4418
4419 int ret,is_different, i, cc_count;
4420
4421 if (country_code == NULL || country_code[0] == '\0')
4422 {
4423 RLOGE("bad country code\n");
4424 return -1;
4425 }
4426
4427 cc_count = sizeof (support_country) / sizeof (char*);
4428 for(i=0; i < cc_count; i++)
4429 {
4430 if (strcmp(support_country[i], country_code) == 0)
4431 {
4432 break;
4433 }
4434 }
4435
4436 if (i >= cc_count)
4437 {
4438 RLOGE("unspported country code %s\n", country_code);
4439 return -1;
4440 }
4441
4442 is_different = check_and_init_uci_config(country_code);
4443 if( is_different < 0 )
4444 {
4445 RLOGE("init set uci fail\n");
4446 return -1;
4447 }
4448
4449 ret = lynq_get_country_code(idx,check_current_code);
4450 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4451 {
4452 ret = lynq_wifi_disable();
4453 if(ret != 0 )
4454 {
4455 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4456 return -1;
4457 }
4458 }
4459
4460 return 0;
you.chen35020192022-05-06 11:30:57 +08004461}
4462
4463int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4464{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004465 RLOGD("enter lynq_get_connect_ap_mac\n");
4466 if (mac == NULL)
4467 {
4468 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004469 return -1;
4470 }
4471
4472 CHECK_IDX(idx, CTRL_STA);
4473 ap_info_s ap;
4474 ap.ap_mac[0] = '\0';
4475
qs.xiong9fbf74e2023-03-28 13:38:22 +08004476 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4477 {
you.chen35020192022-05-06 11:30:57 +08004478 return -1;
4479 }
4480 strcpy(mac, ap.ap_mac);
4481
4482 return 0;
4483}
4484
4485int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4486{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004487 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004488 struct ifaddrs *ifaddr_header, *ifaddr;
4489 struct in_addr * ifa;
4490 const char * ifaName = "wlan0";
4491 if (ip == NULL)
4492 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004493 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004494 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004495 }
you.chenf58b3c92022-06-21 16:53:48 +08004496
qs.xiong9fbf74e2023-03-28 13:38:22 +08004497 if (idx == 1)
4498 {
you.chen0df3e7e2023-05-10 15:56:26 +08004499 ifaName = inner_get_ap_interface_name();
4500 if (ifaName == NULL)
4501 {
4502 RLOGE("[lynq_get_interface_ip] ap name get fail");
4503 return -1;
4504 }
you.chen9ac66392022-08-06 17:01:16 +08004505 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004506 else if (idx != 0)
4507 {
you.chen35020192022-05-06 11:30:57 +08004508 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004509 }
you.chen35020192022-05-06 11:30:57 +08004510
you.chen9ac66392022-08-06 17:01:16 +08004511 if (getifaddrs(&ifaddr_header) == -1)
4512 {
you.chen35020192022-05-06 11:30:57 +08004513 perror("getifaddrs");
4514 return -1;
4515 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004516 }
you.chen35020192022-05-06 11:30:57 +08004517
4518
you.chen9ac66392022-08-06 17:01:16 +08004519 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4520 {
4521 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004522 continue;
you.chen9ac66392022-08-06 17:01:16 +08004523 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4524 {
4525 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4526 {
4527 // is a valid IP4 Address
4528 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4529 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004530 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004531 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004532 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004533 return 0;
4534 }
4535 }
4536 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004537 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004538 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004539 return -1;
you.chen35020192022-05-06 11:30:57 +08004540}
4541
4542int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4543{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004544 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004545 int count;
4546 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004547 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004548 char *split_words[128] = {0};
4549 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4550
4551 CHECK_WPA_CTRL(idx);
4552
4553 DO_REQUEST(lynq_get_mac_cmd);
4554
qs.xiong9fbf74e2023-03-28 13:38:22 +08004555 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4556 {
4557 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004558 return -1;
4559 }
4560
4561 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4562
qs.xiong9fbf74e2023-03-28 13:38:22 +08004563 if (count < 2)
4564 {
you.chen35020192022-05-06 11:30:57 +08004565 return -1;
4566 }
4567
qs.xiong9fbf74e2023-03-28 13:38:22 +08004568 for (i=0; i < strlen(split_words[1]); i++ )
4569 {
4570 if (split_words[1][i] != ' ')
4571 {
you.chen35020192022-05-06 11:30:57 +08004572 break;
4573 }
4574 }
4575
qs.xiongdd6e44c2023-08-08 15:02:53 +08004576 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004577
4578 return 0;
4579}
4580
4581int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4582{
4583// int count;
4584// char *split_words[128] = {0};
4585// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4586
4587// if (rssi == NULL) {
4588// return -1;
4589// }
4590
4591// CHECK_IDX(idx, CTRL_STA);
4592
4593// CHECK_WPA_CTRL(CTRL_STA);
4594
4595// DO_REQUEST(lynq_get_rssi_cmd);
4596
4597// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4598// return -1;
4599// }
4600
4601// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4602
4603// if (count < 2) {
4604// return -1;
4605// }
4606
4607// *rssi = atoi(split_words[1]) * -1;
4608
you.chen35020192022-05-06 11:30:57 +08004609 char lynq_cmd_ret[MAX_RET]={0};
4610
qs.xiongff0ae0f2022-10-11 15:47:14 +08004611/*******change other cmd to get rssi*******
4612 *
4613 *wl rssi ---> wl -i wlan0 rssi
4614 *
4615 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004616 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004617 {
you.chen23c4a5f2023-04-12 16:46:00 +08004618 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004619 return -1;
4620 }
you.chen9f17e4d2022-06-06 17:18:18 +08004621 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004622/****** if got rssi is 0,means sta didn't connected any device****/
4623 if(*rssi == 0)
4624 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004625 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004626 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004627 }
you.chen35020192022-05-06 11:30:57 +08004628
4629 return 0;
4630}
4631
4632int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4633{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004634 RLOGD("enter lynq_get_connect_ap_band\n");
4635 if (band == NULL)
4636 {
you.chen35020192022-05-06 11:30:57 +08004637 return -1;
4638 }
4639
4640 CHECK_IDX(idx, CTRL_STA);
4641 ap_info_s ap;
4642 ap.band = -1;
4643
qs.xiong9fbf74e2023-03-28 13:38:22 +08004644 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4645 {
you.chen35020192022-05-06 11:30:57 +08004646 return -1;
4647 }
4648 *band = ap.band;
4649
4650 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004651}
you.chenf58b3c92022-06-21 16:53:48 +08004652
4653int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4654{
you.chenb95401e2023-05-12 19:39:06 +08004655 int ret;
4656 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004657 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004658
4659 if (ip == NULL)
4660 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004661 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004662 return -1;
4663 }
4664
4665 CHECK_IDX(idx, CTRL_STA);
4666
qs.xionge4cbf1c2023-02-28 18:22:49 +08004667 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004668 {
4669 return -1;
4670 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004671
you.chenb95401e2023-05-12 19:39:06 +08004672 ip[0] = '\0';
4673 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4674 if (ret != 0)
4675 {
4676 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4677 return -1;
4678 }
4679
4680 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4681 {
4682 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004683 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004684 if (ret != 0)
4685 {
4686 ip[0] = '\0';
4687 return 0;
4688 }
4689 else
4690 {
4691 p = strchr(ip, '\n');
4692 if (p != NULL)
4693 {
4694 *p = '\0';
4695 }
4696 }
4697 }
4698 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004699}
4700
qs.xionge02a5252023-09-20 14:00:21 +08004701int lynq_get_sta_connected_dns(lynq_wifi_index_e idx, char *dns)
4702{
4703 RLOGD("[wifi]--enter--lynq_get_sta_connected_dns");
4704 return lynq_get_connect_ap_ip(idx,dns); //> not 100 % get dns info
4705}
4706
qs.xiong026c5c72022-10-17 11:15:45 +08004707int lynq_ap_connect_num(int sta_number)
4708{
4709 char lynq_limit_cmd[32]={0};
4710 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004711 if((sta_number < 1 ) && (sta_number > 15))
4712 {
4713 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004714 return -1;
4715 }
4716 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4717 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004718 if(ret != 0)
4719 {
4720 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004721 }
4722 return 0;
4723}
you.chenf58b3c92022-06-21 16:53:48 +08004724
qs.xiong77905552022-10-17 11:19:57 +08004725int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4726{
4727
4728 char lynq_wifi_acs_cmd[128]={0};
4729 char lynq_cmd_mode[128]={0};
4730 char lynq_cmd_slect[128]={0};
4731
qs.xiong9fbf74e2023-03-28 13:38:22 +08004732 if((acs_mode != 2) && (acs_mode != 5))
4733 {
qs.xiong77905552022-10-17 11:19:57 +08004734 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4735 }
4736
qs.xiong9fbf74e2023-03-28 13:38:22 +08004737 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4738 {
qs.xiong77905552022-10-17 11:19:57 +08004739 return -1;
4740 }
4741
4742 CHECK_IDX(idx, CTRL_AP);
4743
4744 CHECK_WPA_CTRL(CTRL_AP);
4745
4746 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4747 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4748 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4749
4750 DO_OK_FAIL_REQUEST(cmd_disconnect);
4751 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4752 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4753 DO_OK_FAIL_REQUEST(cmd_save_config);
4754 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4755
4756 return 0;
4757}
you.chen0f5c6432022-11-07 18:31:14 +08004758//you.chen add for tv-box start
4759static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4760 FILE *fp;
4761 //printf("to exec cmd:%s\n", str_cmd);
4762 if((fp=popen(str_cmd,"r"))==NULL)
4763 {
4764 perror("popen error!");
4765 return -1;
4766 }
4767 if((fread(str_cmd_ret,max_len,1,fp))<0)
4768 {
4769 perror("fread fail!");
4770 fclose(fp);
4771 return -1;
4772 }
4773 fclose(fp);
4774 return 0;
4775}
4776
4777static int get_netmask_length(const char* mask)
4778{
4779 int masklen=0, i=0;
4780 int netmask=0;
4781
4782 if(mask == NULL)
4783 {
4784 return 0;
4785 }
4786
4787 struct in_addr ip_addr;
4788 if( inet_aton(mask, &ip_addr) )
4789 {
4790 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004791 }else
4792 {
you.chen0f5c6432022-11-07 18:31:14 +08004793 netmask = 0;
4794 return 0;
4795 }
4796
4797 while(0 == (netmask & 0x01) && i<32)
4798 {
4799 i++;
4800 netmask = netmask>>1;
4801 }
4802 masklen = 32-i;
4803 return masklen;
4804}
4805
4806static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4807 int mask_len;
4808 char *p;
4809 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004810 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4811 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004812 return -1;
4813 p = strstr(str_cmd_ret, "Mask:");
4814 if (p == NULL)
4815 return -1;
4816 mask_len = get_netmask_length(p + 5);
4817 if (mask_len == 0)
4818 return -1;
4819 p = strstr(str_cmd_ret, "inet addr:");
4820 if (p == NULL)
4821 return -1;
4822 strcpy(tmp, p + 10);
4823 p = strstr(tmp, " ");
4824 if (p != NULL)
4825 *p = '\0';
4826 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4827 return 0;
4828}
4829
4830static void GBWWatchThreadProc() {
4831 int i,n, nloop, nmax, ncheckcount, nidlecount;
4832 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4833 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4834 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4835 char *results[16] = {0};
4836 char str_cmd[256] = {0};
4837 char str_cmd_ret[128] = {0};
4838 char dest_ip[32] = {0};
4839 lastAP1Bytes = lastAP2Bytes = 0;
4840 lastAP1Drop = lastAP2Drop = 0;
4841 lastAP1Speed = lastAP2Speed = 0;
4842 setAP1Speed = 50;
4843 setAP2Speed = 80;
4844 nloop = 0;
4845 nmax = 6;
4846 ncheckcount = nidlecount = 0;
4847
you.chen0df3e7e2023-05-10 15:56:26 +08004848 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004849 {
4850 RLOGE("------gbw thread run\n");
4851 return;
4852 }
4853
qs.xiong9fbf74e2023-03-28 13:38:22 +08004854 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004855 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4856 while (dest_ip[0] == '\0') {
4857 sleep(1);
4858 str_cmd_ret[0] = '\0';
4859 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4860 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4861 if (str_cmd_ret[n] == '\n'){
4862 str_cmd_ret[n] = '\0';
4863 break;
4864 }
4865 }
4866 if (str_cmd_ret[0] != '\0')
4867 {
4868 strcpy(dest_ip, str_cmd_ret);
4869 }
4870 }
4871
you.chenc9928582023-04-24 15:39:37 +08004872 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4873 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4874 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 +08004875 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4876 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004877 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004878 return;
4879 }
you.chenc9928582023-04-24 15:39:37 +08004880 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);
4881 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);
4882 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 +08004883
4884 while (1) {
4885 sleep(1);
4886 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004887 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4888 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4889 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004890 continue;
4891 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004892 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004893 if (n > 9) {
4894 if (strcmp(results[1], "Sent") == 0) {
4895 currAP1Bytes = atoll(results[2]);
4896 }
4897 if (strcmp(results[6], "(dropped") == 0) {
4898 currAP1Drop = atoi(results[7]);
4899 }
4900 }
4901
4902 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004903 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4904 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4905 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004906 continue;
4907 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004908 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004909 if (n > 9) {
4910 if (strcmp(results[1], "Sent") == 0) {
4911 currAP2Bytes = atoll(results[2]);
4912 }
4913 if (strcmp(results[6], "(dropped") == 0) {
4914 currAP2Drop = atoi(results[7]);
4915 }
4916 }
4917
4918 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4919 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4920 lastAP1Bytes = currAP1Bytes;
4921 lastAP2Bytes = currAP2Bytes;
4922 continue;
4923 }
4924
4925 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4926 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4927 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4928 lastAP1Speed = currAP1Speed;
4929 lastAP2Speed = currAP2Speed;
4930 lastAP1Bytes = currAP1Bytes;
4931 lastAP2Bytes = currAP2Bytes;
4932
4933 currSetAP1Speed = setAP1Speed;
4934 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4935 ncheckcount++;
4936 if (ncheckcount > 3) {
4937 ncheckcount = 0;
4938 currSetAP1Speed = 5;
4939 }
4940 }
4941 else {
4942 ncheckcount = 0;
4943 if (currAP1Speed < 5)
4944 nidlecount++;
4945 else
4946 nidlecount = 0;
4947
4948 }
4949
4950 if (nidlecount > 60 ){
4951 currSetAP1Speed = 50;
4952 }
4953
4954 if (currSetAP1Speed != setAP1Speed) {
4955 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004956 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4957 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004958 }
4959 }
4960}
4961
4962int enableGBW(const char* mac) {
4963 int i,len;
4964 char get_ipaddr_cmd[128]={0};
4965 ap_info_s *ap;
4966 device_info_s * list;
4967
4968 if (mac == NULL || g_gbw_enabled == 1)
4969 return -1;
4970 len = strlen(mac);
4971 g_gbw_mac = malloc(len + 1);
4972 for(i=0;i<len;i++) {
4973 if (mac[i] >= 'A' && mac[i] <= 'Z')
4974 {
4975 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4976 }
4977 else
4978 g_gbw_mac[i] = mac[i];
4979 }
4980 g_gbw_mac[i] = '\0';
4981 g_gbw_enabled = 1;
4982
4983 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4984 if (system(get_ipaddr_cmd) == 0) {
4985 //startGBW();
4986 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4987 for (i=0;i<len;i++) {
4988 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4989 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4990 startGBW();
4991 }
4992 free(ap);
4993 free(list);
4994 }
4995 }
4996 return 0;
4997}
4998
4999int disableGBW() {
5000 stopGBW();
5001 free(g_gbw_mac);
5002 g_gbw_mac = NULL;
5003 g_gbw_enabled = 1;
5004 return 0;
5005}
5006
5007static int startGBW() {
5008 if (g_gbw_watcher_pid != 0) {
5009 stopGBW();
5010 }
5011 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
5012}
5013
5014static int stopGBW() {
5015 void* retval;
you.chenc9928582023-04-24 15:39:37 +08005016 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08005017 pthread_cancel(g_gbw_watcher_pid);
5018 pthread_join(g_gbw_watcher_pid, &retval);
5019 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08005020 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
5021 if (s_ap_iterface_name[0] != '\0')
5022 {
5023 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
5024 system(cmd);
5025 }
you.chen0f5c6432022-11-07 18:31:14 +08005026}
5027//you.chen add for tv-box end