blob: feb33f9d171c0eca7f874e245433329b89b17ec0 [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
you.chen35020192022-05-06 11:30:57 +080034
qs.xiong9fbf74e2023-03-28 13:38:22 +080035#undef LOG_TAG
36#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080037#define MAX_CMD 128
38#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040039#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080040#define CTRL_STA 0
41#define CTRL_AP 1
42#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080043#define STA_MAX_SAVED_AP_NUM 50
qs.xiong44fac672023-08-29 16:15:55 +080044#define MAC_LEN 17
you.chen35020192022-05-06 11:30:57 +080045
46pthread_t g_ap_watcher_pid = 0;
47volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080048volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080049
qs.xiong44fac672023-08-29 16:15:55 +080050pthread_t g_ap_tmp_watcher_pid = 0;
51volatile int g_ap_tmp_watcher_stop_flag = 0;
52
you.chen35020192022-05-06 11:30:57 +080053pthread_t g_sta_watcher_pid = 0;
54volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080055volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080056volatile int g_sta_watcher_started_flag = 0;
qs.xiong20202422023-09-06 18:01:18 +080057volatile int g_sta_conncet_status_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058
qs.xiongfcc914b2023-07-06 21:16:20 +080059pthread_t g_sta_auto_watcher_pid = 0;
60volatile int g_sta_auto_watcher_stop_flag = 0;
61volatile int g_sta_auto_scan_finish_flag = 1;
62volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080063void * g_ap_callback_priv = NULL;
64AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
65void * g_sta_callback_priv = NULL;
66STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080067void * g_sta_auto_callback_priv = NULL;
68STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080069
70//const char * CTRL_PATH="/var/run/wpa_supplicant";
71const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
72//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
73const char * cmd_list_networks = "LIST_NETWORKS";
74const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080075const char * cmd_disconnect = "DISCONNECT";
76const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080077const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080078const char * STATE_COMPLETED = "COMPLETED";
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
you.chen70f377f2023-04-14 18:17:09 +080099
you.chend2fef3f2023-02-13 10:50:35 +0800100static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800101static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
102static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800103// add for auto connect
104static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800105
106static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800107
you.chen0f5c6432022-11-07 18:31:14 +0800108//you.chen add for tv-box start
109volatile int g_gbw_enabled = 0;
110char * g_gbw_mac = NULL;
111pthread_t g_gbw_watcher_pid = 0;
112static int startGBW();
113static int stopGBW();
114//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800115
116typedef struct __curr_status_info {
117 ap_info_s *ap;
118 char * state;
119 int net_no;
120}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400121
you.chen70f377f2023-04-14 18:17:09 +0800122typedef enum {
123 INNER_STA_STATUS_INIT = 0,
124 INNER_STA_STATUS_CONNECTING,
125 INNER_STA_STATUS_ASSOCIATING,
126 INNER_STA_STATUS_ASSOCIATED,
127 INNER_STA_STATUS_CONNECTED,
128 INNER_STA_STATUS_DISCONNECTING,
129 INNER_STA_STATUS_DISCONNECTED,
130 INNER_STA_STATUS_CANCEL,
131}inner_sta_status_s;
132
133static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
134static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
135static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
136static error_number_s s_sta_error_number = -1;
137static char s_sta_current_connecting_ssid[64] = {0};
138static struct timespec s_sta_connect_timeout;
139const int MAX_CONNNECT_TIME = 15; // second
140pthread_t g_global_watcher_pid = 0;
141static int s_service_invoke_timeout_cnt=0;
142const int FAKE_MAX_INT_VALUE = 99999;
143
144static void notify_service_invoke_fail(int error)
145{
146 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
147 pthread_mutex_lock(&s_global_check_mutex);
148 if (error == -2) //timeout
149 {
150 s_service_invoke_timeout_cnt++;
151 if (s_service_invoke_timeout_cnt > 10)
152 {
153 pthread_cond_signal(&s_global_check_cond);
154 }
155 }
156 else if (error == -1)
157 {
158 // check if can connect wpa service
159 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
160 if (lynq_wpa_ctrl == NULL)
161 {
162 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
163 pthread_cond_signal(&s_global_check_cond);
164 }
165 wpa_ctrl_close(lynq_wpa_ctrl);
166 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
167 if (lynq_wpa_ctrl == NULL)
168 {
169 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
170 pthread_cond_signal(&s_global_check_cond);
171 }
172 wpa_ctrl_close(lynq_wpa_ctrl);
173 }
174
175 pthread_mutex_unlock(&s_global_check_mutex);
176}
177
you.chenc9928582023-04-24 15:39:37 +0800178static int system_call_v(const char * fmt, ...)
179{
180 char str_cmd[256] = {0};
181 va_list args;
182 va_start(args, fmt);
183 vsprintf(str_cmd, fmt, args);
184 va_end(args);
185 printf("system call----------%s\n", str_cmd);
186 return system(str_cmd);
187}
188
you.chen0df3e7e2023-05-10 15:56:26 +0800189static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
190
191static const char * inner_get_ap_interface_name()
192{
193 char * p;
194 char cmd[128]={0};
195
196 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
197 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
198 {
199 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
200 return NULL;
201 }
202 p = strchr(s_ap_iterface_name, ' ');
203 if (NULL != p)
204 {
205 *p = '\0';
206 }
207 p = strchr(s_ap_iterface_name, '\n');
208 if (NULL != p)
209 {
210 *p = '\0';
211 }
212 if (s_ap_iterface_name[0] == '\0')
213 {
214 return NULL;
215 }
216
217 return s_ap_iterface_name;
218}
219
you.chen70f377f2023-04-14 18:17:09 +0800220static void check_tether_and_notify()
221{
222 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800223 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 +0800224 {
225 return;
226 }
227 pthread_mutex_lock(&s_global_check_mutex);
228 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
229 pthread_cond_signal(&s_global_check_cond);
230 pthread_mutex_unlock(&s_global_check_mutex);
231}
232
you.chend2fef3f2023-02-13 10:50:35 +0800233static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
234 char *reply, size_t *reply_len,
235 void (*msg_cb)(char *msg, size_t len))
236{
237 int ret;
238 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800239 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800240 return -1;
241 }
242 pthread_mutex_lock(&ctrl->mutex);
243 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
244 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800245 if (ret != 0)
246 {
247 notify_service_invoke_fail(ret);
248 }
you.chend2fef3f2023-02-13 10:50:35 +0800249 return ret;
250}
251
252static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
253 int repeat_cnt;
254 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
255 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800256 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800257 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
258 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
259// printf("wait enable finish\n");
260 usleep(500 * 1000);
261 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
262 }
263 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800264 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800265 goto out_addr;
266 }
267 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
268 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
269 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800270 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800271 goto out_addr;
272 }
273 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
274 }
275 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
276out_addr:
277 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
278 return lynq_wpa_ctrl;
279}
280
qs.xiong97fa59b2022-04-07 05:41:29 -0400281#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400282{\
you.chen35020192022-05-06 11:30:57 +0800283 perror((str));\
284 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400285}
286
you.chen35020192022-05-06 11:30:57 +0800287#define CHECK_IDX(idx, type) do { \
288 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
289 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800290 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800291 return -1; \
292 } \
293 }while (0)
294
295#define CHECK_WPA_CTRL(index) int ret = 0;\
296 size_t reply_len = MAX_RET; \
297 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800298 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800299 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800300 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
301 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800302 }while(0)
303
304#define DO_REQUEST(cmd_str) do { \
305 reply_len = MAX_RET;\
306 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800307 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800308 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 +0800309 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800310 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800311 return ret; \
312 } \
313 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800314 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800315 }while(0)
316
317#define DO_OK_FAIL_REQUEST(cmd_str) do { \
318 DO_REQUEST(cmd_str); \
319 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800320 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800321 return -1; \
322 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800323 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800324 return -1; \
325 } \
326 }while (0)
327
328
you.chenf711c8a2023-04-13 13:49:45 +0800329static int check_connection(struct wpa_ctrl * wpa_ctrl)
330{
331 size_t reply_len = MAX_RET;
332 char cmd_reply[MAX_RET]={0};
333 int ret;
334
335 RLOGD("check_connection [%p]", wpa_ctrl);
336 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
337
338 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
339 {
340 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 +0800341 if (ret != 0)
342 {
343 notify_service_invoke_fail(ret);
344 }
you.chenf711c8a2023-04-13 13:49:45 +0800345 return -1;
346 }
347
348 return 0;
349}
350
351/**
352 * @brief check_pending_msg
353 * @param lynq_wpa_ctrl
354 * @return 1 has msg, 0 no msg, -1 error
355 */
356static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
357{
358 int ret;
359
360 if (*pp_lynq_wpa_ctrl == NULL) // need connect
361 {
362 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
363 if (*pp_lynq_wpa_ctrl == NULL)
364 {
365 usleep(SLEEP_TIME_ON_IDLE);
366 return -1;
367 }
368
369 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
370 if (ret == 0) // attach success
371 {
372 *started_flag = 1;
373 }
374 else
375 {
you.chen70f377f2023-04-14 18:17:09 +0800376 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800377 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
378 *pp_lynq_wpa_ctrl = NULL;
379 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800380 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800381 usleep(SLEEP_TIME_ON_IDLE);
382 return -1;
383 }
384 }
385
386 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
387 if ( ret == 0) // no pending messages
388 {
389 usleep(SLEEP_TIME_ON_IDLE);
390 *idle_count += 1;
391 if (*idle_count > MAX_IDLE_COUNT)
392 {
393 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
394 {
395 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
396 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
397 *pp_lynq_wpa_ctrl = NULL;
398 *idle_count = 0;
399 return -1;
400 }
401 *idle_count = 0;
402 }
403 return 0;
404 }
405 else if ( ret == -1) // on error
406 {
407 RLOGE("[wifi error]sta wpa_ctrl_pending");
408 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
409 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
410 *pp_lynq_wpa_ctrl = NULL;
411 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800412 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800413 return -1;
414 }
415
416 *idle_count = 0;
417 return 1;
418}
419
you.chen6d247052023-06-01 16:39:54 +0800420static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
421{
422 pthread_mutex_lock(&s_ap_callback_mutex);
423 if (g_ap_callback_func != NULL)
424 g_ap_callback_func(g_ap_callback_priv, status);
425 pthread_mutex_unlock(&s_ap_callback_mutex);
426}
427
you.chen35020192022-05-06 11:30:57 +0800428static void APWatcherThreadProc() {
429 size_t len = MAX_RET;
430 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800431 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800432
you.chen6c2dd9c2022-05-16 17:55:28 +0800433 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800434 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800435
qs.xiong9fbf74e2023-03-28 13:38:22 +0800436 while (g_ap_watcher_stop_flag == 0)
437 {
you.chenf711c8a2023-04-13 13:49:45 +0800438 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
439 {
you.chen70f377f2023-04-14 18:17:09 +0800440 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
441 {
442 check_tether_and_notify();
443 }
444
you.chen35020192022-05-06 11:30:57 +0800445 continue;
446 }
you.chenf711c8a2023-04-13 13:49:45 +0800447
you.chen6c2dd9c2022-05-16 17:55:28 +0800448 memset(msg_notify, 0, MAX_RET);
449 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800450 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800451 {
you.chen35020192022-05-06 11:30:57 +0800452 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800453 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800454 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800455 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800456 {
you.chen6d247052023-06-01 16:39:54 +0800457 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800458 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800459 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800460 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
461 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800462 {
you.chen0f5c6432022-11-07 18:31:14 +0800463 stopGBW();
464 }
465 }
you.chen35020192022-05-06 11:30:57 +0800466 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800467 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800468 {
you.chen6d247052023-06-01 16:39:54 +0800469 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800470 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800471 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800472 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
473 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800474 {
you.chen0f5c6432022-11-07 18:31:14 +0800475 startGBW();
476 }
477 }
you.chen35020192022-05-06 11:30:57 +0800478 }
qs.xiong31163d62023-07-11 18:54:40 +0800479 else if (strstr(msg_notify, "WoWLAN is enabled") != NULL || strstr(msg_notify, "Failed to start AP functionality") != NULL || strstr(msg_notify, "Could not connect to kernel driver") != NULL )
480 {
481 RLOGD("APWatcherThreadProc ap------> service error");
482 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
483 }
484 else
485 {
486 RLOGD("APWatcherThreadProc ap------> going on check next msg");
487 }
you.chenf711c8a2023-04-13 13:49:45 +0800488 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800489 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
490 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800491 if (lynq_wpa_ctrl != NULL)
492 {
you.chen92fd5d32022-05-25 10:09:47 +0800493 wpa_ctrl_detach(lynq_wpa_ctrl);
494 wpa_ctrl_close(lynq_wpa_ctrl);
495 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400496}
497
you.chen70f377f2023-04-14 18:17:09 +0800498static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
499{
500 char * p;
501 const char * try_associat_flag = "Trying to associate";
502 const char * associated_flag = "Associated with ";
503
504 pthread_mutex_lock(&s_global_check_mutex);
505 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
506 {
507 pthread_mutex_unlock(&s_global_check_mutex);
508 return;
509 }
510
511 if (state == LYNQ_WIFI_STATUS_EGNORE)
512 {
513 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
514 {
515 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
516 }
517 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
518 {
519 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
520 }
521 }
522 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
523 {
524 s_sta_error_number = error_num;
525 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
526 {
527 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
528 pthread_cond_signal(&s_global_check_cond);
529 }
530 }
531 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
532 {
533 s_sta_status = INNER_STA_STATUS_CONNECTED;
534 pthread_cond_signal(&s_global_check_cond);
535 }
536 pthread_mutex_unlock(&s_global_check_mutex);
537}
538
qs.xiong44fac672023-08-29 16:15:55 +0800539/*
540just tmp add for fix sta connect ap fail check ap connect info
541return 0 --->Current no sta device connect this AP
542*/
543static int lynq_connected_ap_sta_status() {
544
545 FILE *fp;
546 size_t i = 0;
547 int ret;
548 char lynq_cmd_ret[MAX_RET]={0};
549
550 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
551 {
552 perror("popen error!");
553 return -1;
554 }
555 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
556 {
557 perror("fread fail!");
558 ret=pclose(fp);
559 if(ret == -1)
560 perror("close file faild");
561 return -1;
562 }
563 if( strlen(lynq_cmd_ret) < MAC_LEN)
564 {
565 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
566 ret=pclose(fp);
567 if(ret==-1)
568 {
569 perror("close file faild");
570 }
571 return 0;
572 }else{
573 ret=pclose(fp);
574 if(ret==-1)
575 {
576 perror("close file faild");
577 }
578 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
579 return 1;
580 }
581}
582
583/*
584 just tmp add for fix sta connect ap fail; check fw status
585 return 1 ----> fw status error; need wl down/up
586*/
587static int check_current_fw_status() {
588
589 FILE *fp;
590 FILE *fp1;
591 size_t i = 0;
592 int ret;
593 char lynq_cmd_ret_2g[MAX_RET]={0};
594 char lynq_cmd_ret_5g[MAX_RET]={0};
595
596 const char * fw_status = "0x0096"; //0x0096 is normal fw status
597
598 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
599 {
600 perror("popen error!");
601 return -1;
602 }
603 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
604 {
605 perror("fread fail!");
606 if(pclose(fp) == -1)
607 perror("close fp file faild");
608 return -1;
609 }
610
611 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
612 {
613 perror("popen error!");
614 if(pclose(fp) == -1)
615 perror("clsoe fp file faild");
616 return -1;
617 }
618 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
619 {
620 perror("fread fail!");
621 if(pclose(fp1) == -1)
622 perror("clsoe fp1 file faild");
623 if(pclose(fp) == -1)
624 perror("clsoe fp file faild");
625 return -1;
626 }
627
628 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
629 {
630 ret=pclose(fp);
631 if(ret==-1)
632 {
633 perror("close fp file faild");
634 }
635 ret=pclose(fp1);
636 if(ret==-1)
637 {
638 perror("close fp1 file faild");
639 }
640 return 0;
641 }else
642 {
643 ret=pclose(fp);
644 if(ret==-1)
645 {
646 perror("close file faild");
647 }
648 if(pclose(fp1) == -1)
649 {
650 perror("clsoe file fp1 faild");
651 }
652 RLOGD("current fw status --error--");
653 return 1;
654 }
655}
656
qs.xiong1d4263a2023-09-06 10:46:23 +0800657/*
658eg: wl counters info
659sh-3.2# wl counters
660counters_version 30
661datalen 1648
662Slice_index: 0
663reinitreason_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)
664reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
665
666check reinit status
667return 0 ===> fw did wl reinit cmd
668*/
669static int check_current_reinit_info()
670{
671 FILE *fp;
672 int ret;
673 char lynq_cmd_ret[MAX_RET]={0};
674 char * dest;
675 char destid[3]={0};
676 if((fp=popen("wl counters","r"))==NULL)
677 {
678 perror("popen error!");
679 return -1;
680 }
681 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
682 {
683 perror("fread fail!");
684 if(pclose(fp) == -1)
685 {
686 perror("close fp file faild");
687 }
688 return -1;
689 }
690 dest = strstr(lynq_cmd_ret,"reinit ");
691 if(dest != NULL)
692 {
693 dest +=strlen("reinit ");
694 RLOGD("current get dest str is %s",dest);
695 memcpy(destid,dest,2);
696 ret = atoi(destid);
697 RLOGD("get current wl counters cmd return counts is %d",ret);
698 if( ret != 0 )
699 {
700 RLOGD("current fw did run cmd wl reinit");
701 if( pclose(fp) == -1 )
702 {
703 perror("close fp file faild");
704 }
705 return 0;
706 }
707 }
708 if( pclose(fp) == -1 )
709 {
710 perror("close fp file faild");
711 }
712 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
713 return -1;
714}
715
qs.xiong44fac672023-08-29 16:15:55 +0800716static void APTmpWatcherThreadProc() {
717
718 int i = 0;
719 int delytime = 300;
720 g_ap_tmp_watcher_stop_flag = 0;
721
722 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
723 while(1)
724 {
725 sleep(1);
726 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800727 if ( (i % 30) == 0 )
728 {
729 if ( check_current_reinit_info() == 0 )
730 {
731 system("wl reset_cnts");
732 system("wl down");
733 system("wl up");
734 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
735 }
736 }
qs.xiong44fac672023-08-29 16:15:55 +0800737 if ( i == delytime )
738 {
739 if( lynq_connected_ap_sta_status() == 0 ) //o --->no sta device connect this ap
740 {
741 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
742 {
743 system("wl down");
744 system("wl up");
745 }
746
747 }
748 i = 0;
749 }
750 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
751 {
752 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
753 return;
754 }
755 }
756
757}
758
qs.xiongf0128b12023-06-29 17:29:39 +0800759static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
760{
761 char LYNQ_DISABLE_CMD[128]={0};
762
763 CHECK_IDX(idx, CTRL_STA);
764 CHECK_WPA_CTRL(CTRL_STA);
765
766 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
767 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
768 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800769 DO_OK_FAIL_REQUEST(cmd_save_config);
770
qs.xiongf0128b12023-06-29 17:29:39 +0800771 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800772
qs.xiongf0128b12023-06-29 17:29:39 +0800773}
774
775
qs.xiong455c30b2023-04-12 11:40:02 +0800776void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
777{
778 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800779 char *wpanetid;
780 char destid[3] = {0};
781 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800782 *error = LYNQ_WAIT_CONNECT_ACTIVE;
783 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
784 {
785 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
786 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
787 return;
788 }
789
qs.xiong20202422023-09-06 18:01:18 +0800790 if (strstr(modify, "Trying to associate") != NULL)
791 {
792 RLOGD("Current sta is Trying to associate");
793 *state = LYNQ_WIFI_STATUS_EGNORE;
794 g_sta_conncet_status_flag = 1;
795 return;
796 }
797
qs.xiong455c30b2023-04-12 11:40:02 +0800798 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
799 {
800 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
801 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800802 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800803 return;
804 }
805
806 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
807 {
qs.xiongf0128b12023-06-29 17:29:39 +0800808 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
809 wpanetid = strstr(modify,"id=");
810 if ( wpanetid != NULL )
811 {
812 wpanetid +=strlen("id=");
813 memcpy(destid,wpanetid,2);
814 tmpdisid = atoi(destid);
815
816 }
qs.xiong455c30b2023-04-12 11:40:02 +0800817 pReason = strstr(modify, "reason=");
818 if (pReason != NULL)
819 {
820 pReason += strlen("reason=");
821 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
822 {
823 *error = LYNQ_TIME_OUT;
824 }
825 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
826 {
827 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800828 // tmp fix sta autoconnect connect and disconnect
829 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
830 {
831 RLOGE("stop wlan0 network %d fail",tmpdisid);
832 }
qs.xiong455c30b2023-04-12 11:40:02 +0800833 }
834 else
835 {
836 *error = LYNQ_UNSPECIFIED_REASON;
837 }
qs.xiong455c30b2023-04-12 11:40:02 +0800838 }
839 else
840 {
841 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800842 }
qs.xiongf0128b12023-06-29 17:29:39 +0800843 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800844 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800845 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800846
847 }
848
849 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
850 {
851 *error = LYNQ_NOT_FIND_AP;
852 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
853 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800854 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800855 return;
856 }
857
858
859 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
860 {
861 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
862 pReason = strstr(modify, "status_code=");
863 if (pReason != NULL)
864 {
865 pReason += strlen("status_code=");
866 if (memcmp(pReason, "17", 2) == 0)
867 {
868 *error = LYNQ_AP_UNABLE_HANDLE;
869 }
870 else if (memcmp(pReason, "1",1) == 0)
871 {
872 *error = LYNQ_UNSPECIFIED_REASON;
873 }
874 else
875 {
876 *error = LYNQ_UNSPECIFIED_REASON;
877 }
878
879 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
880 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 +0800881 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800882 return;
883 }
884 else
885 {
886 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
887 *error = LYNQ_UNSPECIFIED_REASON;
888 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
889 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
890 return;
891 }
892 }
893
894 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
895 {
896 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
897 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
898 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
899 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
900 return;
901 }
902
903 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
904 {
905 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
906 *error = LYNQ_WAIT_CONNECT_ACTIVE;
907 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
908 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
909 return;
910 }
911
you.chen32cb31e2023-04-13 14:05:45 +0800912 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800913 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800914 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800915 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
916 return;
917
918}
919
qs.xiongfcc914b2023-07-06 21:16:20 +0800920void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
921{
922 char *pReason;
923 char *wpanetid;
924 char destid[3];
925 *error = LYNQ_WAIT_CONNECT_ACTIVE;
926 *networkid = -1;
927 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
928 {
929 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
930 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
931 return;
932 }
933 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
934 {
935 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
936 wpanetid = strstr(modify,"id=");
937 if ( wpanetid != NULL )
938 {
939 wpanetid +=strlen("id=");
940 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
941 if (memcpy(destid,wpanetid,2) != NULL)
942 {
943 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
944 *networkid = atoi(destid);
945 RLOGD("get networkid is %d\n",*networkid);
946 }
947 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
948 }
949 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
950 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
951 return;
952 }
953 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
954 {
955 wpanetid = strstr(modify,"id=");
956 if ( wpanetid != NULL )
957 {
958 wpanetid +=strlen("id=");
959 if (memcpy(destid,wpanetid,2) != NULL)
960 {
961 *networkid = atoi(destid);
962 RLOGD("get networkid is %d\n",*networkid);
963 }
964 }
965 pReason = strstr(modify, "reason=");
966 if (pReason != NULL)
967 {
968 pReason += strlen("reason=");
969 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
970 {
971 *error = LYNQ_TIME_OUT;
972 }
973 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
974 {
975 *error = LYNQ_PSW_ERROR;
976 }
977 else
978 {
979 *error = LYNQ_UNSPECIFIED_REASON;
980 }
981 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
982 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
983 return;
984 }
985 else
986 {
987 *error = LYNQ_UNSPECIFIED_REASON;
988 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
989 return;
990 }
991 }
992 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
993 {
994 wpanetid = strstr(modify,"id=");
995 if ( wpanetid != NULL )
996 {
997 wpanetid +=strlen("id=");
998 if (memcpy(destid,wpanetid,2) != NULL)
999 {
1000 *networkid = atoi(destid);
1001 RLOGD("get networkid is %d\n",*networkid);
1002 }
1003 }
1004 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1005 pReason = strstr(modify, "status_code=");
1006 if (pReason != NULL)
1007 {
1008 pReason += strlen("status_code=");
1009 if (memcmp(pReason, "17", 2) == 0)
1010 {
1011 *error = LYNQ_AP_UNABLE_HANDLE;
1012 }
1013 else if (memcmp(pReason, "1",1) == 0)
1014 {
1015 *error = LYNQ_UNSPECIFIED_REASON;
1016 }
1017 else
1018 {
1019 *error = LYNQ_UNSPECIFIED_REASON;
1020 }
1021 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1022 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1023 return;
1024 }
1025 else
1026 {
1027 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1028 *error = LYNQ_UNSPECIFIED_REASON;
1029 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001030 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001031 return;
1032 }
1033 }
1034 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1035 {
1036 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1037 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1038 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1039 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1040 return;
1041 }
1042 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1043 {
1044 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1045 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1046 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1047 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1048 return;
1049 }
1050 RLOGD("EVENT : %s\n", modify);
1051 *error = LYNQ_UNSPECIFIED_REASON;
1052 *state = LYNQ_WIFI_STATUS_EGNORE;
1053 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1054 return;
1055}
you.chen70f377f2023-04-14 18:17:09 +08001056static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1057{
you.chen6d247052023-06-01 16:39:54 +08001058 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001059 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1060 {
1061 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1062 g_sta_callback_func(g_sta_callback_priv, state, error);
1063 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1064 }
you.chen6d247052023-06-01 16:39:54 +08001065 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001066}
qs.xiongfcc914b2023-07-06 21:16:20 +08001067static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1068{
1069 pthread_mutex_lock(&s_sta_callback_mutex);
1070 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1071 {
1072 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1073 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1074 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1075 }
1076 pthread_mutex_unlock(&s_sta_callback_mutex);
1077}
you.chen70f377f2023-04-14 18:17:09 +08001078
you.chen35020192022-05-06 11:30:57 +08001079static void STAWatcherThreadProc() {
1080 size_t len = MAX_RET;
1081 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001082 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001083 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001084 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001085
you.chen6c2dd9c2022-05-16 17:55:28 +08001086 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001087 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001088
you.chen70f377f2023-04-14 18:17:09 +08001089 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001090 while (g_sta_watcher_stop_flag == 0)
1091 {
you.chenf711c8a2023-04-13 13:49:45 +08001092 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001093 {
you.chen35020192022-05-06 11:30:57 +08001094 continue;
1095 }
you.chenf711c8a2023-04-13 13:49:45 +08001096
you.chen6c2dd9c2022-05-16 17:55:28 +08001097 memset(msg_notify, 0, MAX_RET);
1098 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001099 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001100 {
you.chen35020192022-05-06 11:30:57 +08001101 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001102 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1103 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001104 {
you.chen35020192022-05-06 11:30:57 +08001105 g_sta_scan_finish_flag = 1;
1106 }
1107
qs.xiong9fbf74e2023-03-28 13:38:22 +08001108 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001109 {
you.chen35020192022-05-06 11:30:57 +08001110 continue;
1111 }
qs.xiong455c30b2023-04-12 11:40:02 +08001112 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001113 notify_connect_status(state, error);
1114
1115 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001116 {
you.chen70f377f2023-04-14 18:17:09 +08001117 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001118 if (last_state != state)
1119 {
1120 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1121 {
1122 system_call_v("%s %s", sta_status_change_script, "connect");
1123 }
1124 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1125 {
1126 system_call_v("%s %s", sta_status_change_script, "disconnect");
1127 }
1128 }
1129
1130 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001131 }
you.chen35020192022-05-06 11:30:57 +08001132 }
1133 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001134 if (lynq_wpa_ctrl != NULL)
1135 {
you.chen92fd5d32022-05-25 10:09:47 +08001136 wpa_ctrl_detach(lynq_wpa_ctrl);
1137 wpa_ctrl_close(lynq_wpa_ctrl);
1138 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001139}
qs.xiongfcc914b2023-07-06 21:16:20 +08001140static void STAAutoWatcherThreadProc() {
1141 size_t len = MAX_RET;
1142 char msg_notify[MAX_RET];
1143 error_number_s error;
1144 lynq_wifi_sta_status_s state, last_state = -1;
1145 int idle_count = 0;
1146 int networkid;
1147 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1148 g_sta_auto_watcher_stop_flag = 0;
1149 RLOGD("STAAutoWatcherThreadProc thread started ------");
1150 while (g_sta_auto_watcher_stop_flag == 0)
1151 {
1152 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1153 {
1154 continue;
1155 }
1156 memset(msg_notify, 0, MAX_RET);
1157 len = MAX_RET;
1158 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1159 {
1160 msg_notify[len+1] = '\0';
1161 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1162 if (strstr(msg_notify, state_scan_result) != NULL)
1163 {
1164 g_sta_auto_scan_finish_flag = 1;
1165 }
1166 if (g_sta_auto_callback_func == NULL)
1167 {
1168 continue;
1169 }
1170 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1171 notify_auto_connect_status(state, error,networkid);
1172 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1173 {
1174 inner_check_connect_error(msg_notify, state, error);
1175 if (last_state != state)
1176 {
1177 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1178 {
1179 system_call_v("%s %s", sta_status_change_script, "connect");
1180 }
1181 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1182 {
1183 system_call_v("%s %s", sta_status_change_script, "disconnect");
1184 }
1185 }
1186 last_state = state;
1187 }
1188 }
1189 }
1190 if (lynq_wpa_ctrl != NULL)
1191 {
1192 wpa_ctrl_detach(lynq_wpa_ctrl);
1193 wpa_ctrl_close(lynq_wpa_ctrl);
1194 }
1195}
qs.xiongf1b525b2022-03-31 00:58:23 -04001196
you.chen70f377f2023-04-14 18:17:09 +08001197// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1198static void GlobalWatcherThreadProc()
1199{
1200 int ret, connect_timeout, service_abnormal;
1201 error_number_s error_num = -1;
1202 inner_sta_status_s sta_status;
1203 scan_info_s *scan_list = NULL;
1204 int i, scan_len=0;
1205 char connecting_ssid[64];
1206 struct timeval now;
1207
1208 RLOGD("GlobalWatcherThreadProc start to run");
1209
1210 while (1)
1211 {
1212 pthread_mutex_lock(&s_global_check_mutex);
1213 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1214 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1215 {
1216 pthread_mutex_unlock(&s_global_check_mutex);
1217 usleep(50*1000);
1218 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1219 continue;
1220 }
1221
1222 connect_timeout = 0;
1223 service_abnormal = 0;
1224 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1225 {
1226 while (1)
1227 {
1228 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1229 if (ret == ETIME)
1230 {
1231 connect_timeout = 1;
1232 }
1233 else if (ret != 0)
1234 {
1235 gettimeofday(&now,NULL);
1236 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1237 {
1238 usleep(SLEEP_TIME_ON_IDLE);
1239 continue;
1240 }
1241 connect_timeout = 1;
1242 }
1243 sta_status = s_sta_status;
1244 error_num = s_sta_error_number;
1245 s_sta_status = INNER_STA_STATUS_INIT;
1246 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1247 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1248 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1249 break;
1250 }
1251 }
1252 if (s_service_invoke_timeout_cnt > 10)
1253 {
1254 service_abnormal = 1;
1255 s_service_invoke_timeout_cnt = 0;
1256 }
1257 pthread_mutex_unlock(&s_global_check_mutex);
1258
1259 if (service_abnormal == 1)
1260 {
1261 sleep(1);
1262 RLOGE("wpa service is abnormal info app to exit");
1263 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001264
1265 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1266
you.chen70f377f2023-04-14 18:17:09 +08001267 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1268 }
1269
1270 if (sta_status == INNER_STA_STATUS_CANCEL)
1271 {
1272 continue;
1273 }
1274 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1275 {
1276 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1277 }
1278 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1279 {
1280 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1281 {
1282 for(i=0; i < scan_len;i++)
1283 {
1284 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1285 {
1286 error_num = LYNQ_AUTH_ERROR;
1287 break;
1288 }
1289 }
1290 free(scan_list);
1291 }
1292 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1293 }
1294 else if (connect_timeout == 0)
1295 {
1296 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1297 }
1298 else // wait timeout
1299 {
1300 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1301 {
1302 ; // wpa service abnormal
1303 }
1304 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1305 {
1306 RLOGD("GlobalWatcherThreadProc notify connected");
1307 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1308 }
1309 else
1310 {
1311 RLOGD("GlobalWatcherThreadProc notify timeout");
1312 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1313 }
1314 }
1315 } // while (1)
1316}
1317
qs.xiong1af5daf2022-03-14 09:12:12 -04001318int lynq_wifi_enable(void)
1319{
you.chen35020192022-05-06 11:30:57 +08001320 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001321 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001322 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001323 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1324
qs.xiong9fbf74e2023-03-28 13:38:22 +08001325 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1326 {
you.chend2fef3f2023-02-13 10:50:35 +08001327 goto out_enable;
1328 }
1329
you.chenc9928582023-04-24 15:39:37 +08001330 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001331 if (ret != 0)
1332 {
1333 //printf("service state %d\n", ret);
1334 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001335 ret = -1;
1336 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001337 }
lhfe8da902022-10-11 18:55:36 +08001338
you.chen70f377f2023-04-14 18:17:09 +08001339 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1340 {
1341 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1342 if(ret<0)
1343 {
1344 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1345 ret = -1;
1346 goto out_enable;
1347 }
1348 }
1349
you.chend2fef3f2023-02-13 10:50:35 +08001350 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1351 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1352 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1353 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1354out_enable:
1355 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001356 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001357}
1358
qs.xiong1af5daf2022-03-14 09:12:12 -04001359int lynq_wifi_disable(void)
1360{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001361 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001362 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001363 g_ap_watcher_stop_flag = 1;
1364 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001365 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001366 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001367 if (g_ap_watcher_pid != 0)
1368 pthread_join(g_ap_watcher_pid, NULL);
1369 if (g_sta_watcher_pid != 0)
1370 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001371 if (g_sta_auto_watcher_pid != 0)
1372 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001373 if (g_lynq_wpa_ctrl[0] != NULL)
1374 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1375 if (g_lynq_wpa_ctrl[1] != NULL)
1376 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001377 if (g_ap_tmp_watcher_pid != 0)
1378 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001379 g_ap_watcher_pid = 0;
1380 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001381 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001382 g_lynq_wpa_ctrl[0] = NULL;
1383 g_lynq_wpa_ctrl[1] = NULL;
1384 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001385 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1386 return 0;
1387}
1388
1389static inline char inner_convert_char(char in)
1390{
1391 if (in >= '0' && in <= '9')
1392 {
1393 return in - '0';
1394 }
1395 else if (in >= 'a' && in <= 'f')
1396 {
1397 return in - 'a' + 10;
1398 }
1399 else if (in >= 'A' && in <= 'F')
1400 {
1401 return in - 'A' + 10;
1402 }
1403 else
1404 {
1405 return '\xff';
1406 }
1407}
1408
1409static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1410{
1411 char *p;
1412 size_t pos = 0;
1413 if (NULL == out_ssid)
1414 return;
1415 //printf("input ssid=[%s]\n", ssid);
1416 memset(out_ssid, 0, out_ssid_len);
1417 if (NULL == ssid)
1418 return;
1419 p = strchr(ssid, '\\');
1420 if (NULL == p)
1421 {
1422 strncpy(out_ssid, ssid, out_ssid_len);
1423 //printf(" first %s\n", out_ssid);
1424 }
1425 else
1426 {
1427 pos = p - ssid;
1428 memcpy(out_ssid, ssid, pos);
1429 //printf("pos %lu -- %s\n", pos, out_ssid);
1430 for(; pos < out_ssid_len; pos ++)
1431 {
1432 if (p[0] == '\0')
1433 {
1434 //printf(" out %s\n", out_ssid);
1435 return;
1436 }
1437 else if (p[0] != '\\')
1438 {
1439 out_ssid[pos] = p[0];
1440 p += 1;
1441 }
1442 else if (p[1] == 'x' || p[1] == 'X')
1443 {
1444 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1445 p += 4;
1446 }
1447 else if (p[1] == '\\')
1448 {
1449 out_ssid[pos] = '\\';
1450 p += 2;
1451 }
1452 else if (p[1] == 't')
1453 {
1454 out_ssid[pos] = '\t';
1455 p += 2;
1456 }
1457 else if (p[1] == 'r')
1458 {
1459 out_ssid[pos] = '\r';
1460 p += 2;
1461 }
1462 else if (p[1] == 'n')
1463 {
1464 out_ssid[pos] = '\n';
1465 p += 2;
1466 }//todo find a better way to convert?
1467 }
1468 }
1469 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001470}
qs.xiong1af5daf2022-03-14 09:12:12 -04001471
you.chen35020192022-05-06 11:30:57 +08001472static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001473 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001474 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001475 RLOGD("enter inner_get_param");
1476 if (out_put == NULL)
1477 {
1478 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001479 return -1;
1480 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001481 if (param_name == NULL)
1482 {
1483 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001484 return -1;
1485 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001486 if (param_name[0] == '\0')
1487 {
1488 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001489 return -1;
1490 }
1491
1492 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1493
1494 CHECK_WPA_CTRL(interface);
1495
1496 DO_REQUEST(lynq_cmd_get);
1497
qs.xiong9fbf74e2023-03-28 13:38:22 +08001498 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1499 {
1500 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001501 return -1;
1502 }
1503
you.chena6fa5b22022-05-18 10:28:19 +08001504// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001505 if (strcmp(param_name, "ssid") == 0)
1506 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001507 if (cmd_reply[0] == '\"')
1508 {
you.chend2fef3f2023-02-13 10:50:35 +08001509 ssid_len = reply_len - 1;
1510 memcpy(out_put, cmd_reply + 1, ssid_len);
1511 if (out_put[ssid_len-1] == '\"')
1512 {
1513 out_put[ssid_len-1] = '\0';
1514 }
1515 else
1516 {
1517 out_put[ssid_len] = '\0';
1518 }
1519 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001520 else
1521 {
you.chend2fef3f2023-02-13 10:50:35 +08001522 ssid_len = reply_len / 2;
1523 for(i=0; i<ssid_len; i++)
1524 {
1525 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1526 }
1527 out_put[ssid_len] = '\0';
1528 }
1529 }
1530 else
1531 {
1532 memcpy(out_put, cmd_reply, reply_len + 1);
1533 }
you.chen35020192022-05-06 11:30:57 +08001534 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001535}
qs.xiong1af5daf2022-03-14 09:12:12 -04001536
you.chen35020192022-05-06 11:30:57 +08001537static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1538 int ret = 0;
1539 char * end = str + len - 1;
1540 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001541 while(str < end)
1542 {
1543 if (*str == delimiter)
1544 {
you.chen35020192022-05-06 11:30:57 +08001545 *str++ = '\0';
1546 results[ret++] = str;
1547 continue;
1548 }
1549 str++;
1550 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001551 if (*str == delimiter)
1552 {
you.chen35020192022-05-06 11:30:57 +08001553 *str = '\0';
1554 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001555
you.chen6ed36a62023-04-27 17:51:56 +08001556 results[ret] = NULL;
1557
you.chen35020192022-05-06 11:30:57 +08001558 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001559}
1560
you.chend2fef3f2023-02-13 10:50:35 +08001561static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1562{
1563 char * p;
1564 int ret = 0;
1565 char cmd[256]={0};
1566 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001567 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001568 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001569 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001570 ret = exec_cmd(cmd, ip, ip_len);
1571 p = strchr(ip, '\n');
1572 if (NULL != p)
1573 {
1574 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001575 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001576 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001577 return ret;
1578}
1579
you.chend2fef3f2023-02-13 10:50:35 +08001580static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001581 struct in_addr addr ={0};
1582 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001583 char cmd[64] = {0};
1584 char * p;
1585 int ret;
you.chen35020192022-05-06 11:30:57 +08001586
qs.xiong9fbf74e2023-03-28 13:38:22 +08001587 if (ip == NULL || *ip == '\0' || hostname == NULL)
1588 {
1589 RLOGE("ip == NULL or hostname == NULL");
1590 return -1;
you.chen35020192022-05-06 11:30:57 +08001591 }
1592
you.chend2fef3f2023-02-13 10:50:35 +08001593 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001594 if (inet_aton(ip, &addr) == 0)
1595 {
you.chen35020192022-05-06 11:30:57 +08001596 printf("---inet_aton fail\n");
1597 return -1;
1598 }
1599
1600 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1601
qs.xiong9fbf74e2023-03-28 13:38:22 +08001602 if (ht == NULL)
1603 {
you.chen186d3c32023-05-18 14:19:46 +08001604 hostname[0] = '\0';
1605 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1606 ret = exec_cmd(cmd, hostname, 32);
1607 if (ret == 0)
1608 {
1609 p = strchr(hostname, '\n');
1610 if (p != NULL)
1611 {
1612 *p = '\0';
1613 }
1614 return 0;
1615 }
1616 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001617 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001618 herror(NULL);
1619 return -1;
1620 }
1621
1622 strcpy(hostname, ht->h_name);
1623
1624 return 0;
1625}
1626
1627static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1628{
1629 int count, index, words_count;
1630 char * split_lines[128]= {0};
1631 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001632 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001633 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001634 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001635
1636 CHECK_WPA_CTRL(ap_sta);
1637
1638 DO_REQUEST(lynq_wifi_list_networks);
1639
1640 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1641
1642 //@todo check ssid field to compatible
1643
1644 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001645 for(index=1; index < count; index++)
1646 {
you.chen35020192022-05-06 11:30:57 +08001647 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001648 if (words_count > 2)
1649 {
you.chend2fef3f2023-02-13 10:50:35 +08001650 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001651 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1652 {
you.chen35020192022-05-06 11:30:57 +08001653 net_no_list[ret++] = atoi(split_words[0]);
1654 }
1655 }
1656 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001657 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001658 return ret;
1659}
1660
1661static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001662 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001663 CHECK_WPA_CTRL(ap_sta);
1664 const char *lynq_wifi_add_network = "ADD_NETWORK";
1665
qs.xiong9fbf74e2023-03-28 13:38:22 +08001666 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001667 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001668 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1669 {
1670 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001671 return -1;
1672 }
1673
qs.xiong9fbf74e2023-03-28 13:38:22 +08001674 for(i=0;i<reply_len;i++)
1675 {
1676 if(cmd_reply[i] == '\n')
1677 {
you.chen35020192022-05-06 11:30:57 +08001678 cmd_reply[i] = '\0';
1679 break;
1680 }
1681 }
1682 return atoi(cmd_reply);
1683}
you.chena6cd55a2022-05-08 12:20:18 +08001684
you.chen35020192022-05-06 11:30:57 +08001685static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1686{
1687 int count, index;
1688 int net_no_list[128];
1689
qs.xiong9fbf74e2023-03-28 13:38:22 +08001690 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001691 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001692 for (index=0; index < count; index++)
1693 {
1694 if (net_no_list[index] == net_no)
1695 {
you.chen35020192022-05-06 11:30:57 +08001696 return 0;
1697 }
1698 }
1699
1700 if (count >= 1)
1701 index = net_no_list[count - 1];
1702 else
1703 index = -1;
1704
qs.xiong9fbf74e2023-03-28 13:38:22 +08001705 while (index < net_no )
1706 {
you.chen35020192022-05-06 11:30:57 +08001707 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001708 if (index >= net_no)
1709 { // required network no created
1710 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001711 return 0;
1712 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001713 else if( index < 0)
1714 {
1715 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001716 return -1;
1717 }
you.chen35020192022-05-06 11:30:57 +08001718 }
1719
1720 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001721 {
1722 RLOGE("[lynq_check_network_number] network index < 0");
1723 return -1;
1724 }
1725 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001726 return 0;
1727}
1728
1729static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001730 if (freq > 5000 && freq < 6000)
1731 {
you.chen35020192022-05-06 11:30:57 +08001732 return LYNQ_WIFI_5G_band;
1733 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001734 else if (freq > 2000 && freq < 3000)
1735 {
you.chen35020192022-05-06 11:30:57 +08001736 return LYNQ_WIFI_2G_band;
1737 }
1738 return LYNQ_WIFI_2_and_5G_band;
1739}
1740
1741static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001742 if (key_mgmt != NULL)
1743 {
1744 if (memcmp( key_mgmt, "NONE", 4) == 0)
1745 {
you.chen35020192022-05-06 11:30:57 +08001746 return LYNQ_WIFI_AUTH_OPEN;
1747 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001748 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1749 {
you.chen35020192022-05-06 11:30:57 +08001750 return LYNQ_WIFI_AUTH_WEP;
1751 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001752 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1753 {
you.chen35020192022-05-06 11:30:57 +08001754 return LYNQ_WIFI_AUTH_WPA_PSK;
1755 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1757 {
you.chen35020192022-05-06 11:30:57 +08001758 return LYNQ_WIFI_AUTH_WPA2_PSK;
1759 }
1760 }
1761
1762 return -1;
1763}
1764
1765static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001766 if (flag != NULL)
1767 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001768 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 +08001769 {
1770 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001771 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001772 {
1773 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1774 }else if (strstr( flag, "WPA2-PSK") != NULL)
1775 {
you.chen35020192022-05-06 11:30:57 +08001776 return LYNQ_WIFI_AUTH_WPA2_PSK;
1777 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001778 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001779 {
you.chen35020192022-05-06 11:30:57 +08001780 return LYNQ_WIFI_AUTH_WPA_PSK;
1781 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001782 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001783 {
you.chen35020192022-05-06 11:30:57 +08001784 return LYNQ_WIFI_AUTH_WEP;
1785 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001786 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001787 {
you.chen35020192022-05-06 11:30:57 +08001788 return LYNQ_WIFI_AUTH_OPEN;
1789 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001790 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001791 {
you.chend2fef3f2023-02-13 10:50:35 +08001792 return LYNQ_WIFI_AUTH_OPEN;
1793 }
qs.xiong46f41562023-07-11 21:06:47 +08001794 else
1795 {
1796 RLOGD("convert_max_auth_from_flag not-found auth mode");
1797 }
you.chen35020192022-05-06 11:30:57 +08001798 }
1799
1800 return -1;
1801}
1802
1803static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1804 switch (bw) {
1805 case 10:
1806 return LYNQ_WIFI_BANDWIDTH_HT10;
1807 break;
1808 case 20:
1809 return LYNQ_WIFI_BANDWIDTH_HT20;
1810 break;
1811 case 40:
1812 return LYNQ_WIFI_BANDWIDTH_HT40;
1813 break;
1814 case 80:
1815 return LYNQ_WIFI_BANDWIDTH_HT80;
1816 break;
1817 default:
1818 break;
1819 }
1820
1821 return -1;
1822}
1823
you.chen70f377f2023-04-14 18:17:09 +08001824static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001825static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1826 int i, count;
1827 char *p;
1828 const char *lynq_status_cmd = "STATUS";
1829 const char * FLAG_SSID = "ssid=";
1830 const char * FLAG_SBSID = "bssid=";
1831 const char * FLAG_KEY_MGMT = "key_mgmt=";
1832 const char * FLAG_FREQ = "freq=";
1833 const char * FLAG_STATE = "wpa_state=";
1834 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001835 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001836 char *split_lines[128] = {0};
1837
1838 CHECK_WPA_CTRL(interface);
1839
qs.xiong9fbf74e2023-03-28 13:38:22 +08001840 if (curr_state == NULL)
1841 {
1842 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001843 return -1;
1844 }
1845
1846 DO_REQUEST(lynq_status_cmd);
1847
1848 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1849
1850 curr_state->net_no = -1;
1851 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001852 for(i=0; i < count; i++)
1853 {
1854 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001855 {
you.chen35020192022-05-06 11:30:57 +08001856 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001857 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001858 {
you.chend2fef3f2023-02-13 10:50:35 +08001859 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001860 ret = 0;
1861 continue;
1862 }
you.chenf58b3c92022-06-21 16:53:48 +08001863 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001865 {
you.chend2fef3f2023-02-13 10:50:35 +08001866 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 +08001867 ret = 0;
1868 continue;
1869 }
you.chen35020192022-05-06 11:30:57 +08001870 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001871 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001872 {
you.chen450d0172022-07-15 17:56:48 +08001873 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001874 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001875 ret = 0;
1876 continue;
1877 }
1878 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001879 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001880 {
you.chen35020192022-05-06 11:30:57 +08001881 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1882 ret = 0;
1883 continue;
1884 }
you.chend2fef3f2023-02-13 10:50:35 +08001885 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001886 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001887 {
you.chend2fef3f2023-02-13 10:50:35 +08001888 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1889 ret = 0;
1890 continue;
1891 }
you.chen35020192022-05-06 11:30:57 +08001892 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001893 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001894 {
you.chen35020192022-05-06 11:30:57 +08001895 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001896 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001897 {
you.chen35020192022-05-06 11:30:57 +08001898 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1899 ret = 0;
1900 continue;
1901 }
1902
1903 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001904 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001905 {
you.chen35020192022-05-06 11:30:57 +08001906 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001907 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001908 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001909 }
1910 }
1911
you.chen70f377f2023-04-14 18:17:09 +08001912 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1913 {
1914 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1915 }
1916
you.chen35020192022-05-06 11:30:57 +08001917 return ret;
1918}
1919
qs.xiongf1b525b2022-03-31 00:58:23 -04001920int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001921{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001923 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001924
qs.xiong9fbf74e2023-03-28 13:38:22 +08001925 if (ap_ssid == NULL)
1926 {
1927 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001928 return -1;
1929 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001930 else
1931 {
1932 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001933 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001934
qs.xiong9fbf74e2023-03-28 13:38:22 +08001935 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1936 {
1937 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001938 return -1;
1939 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001940
you.chen35020192022-05-06 11:30:57 +08001941 CHECK_IDX(idx, CTRL_AP);
1942
1943 CHECK_WPA_CTRL(CTRL_AP);
1944
1945 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1946
1947 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1948 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001949 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1950 return 0;
you.chen35020192022-05-06 11:30:57 +08001951
qs.xiong7a105ce2022-03-02 09:43:11 -05001952}
1953
you.chen35020192022-05-06 11:30:57 +08001954int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001955{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001956 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001957 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001958 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001959}
1960
qs.xiongc9c79f72022-10-17 15:27:18 +08001961/*****
1962 *frequency <------>channel
1963 *
1964 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1965 *
1966 *
1967 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1968 *
1969 *
1970 * */
1971static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001972 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};
1973 int i;
1974 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1975
qs.xiong69a332b2022-12-02 09:58:57 +08001976 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001977 {
1978 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001979 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001980 }
qs.xiongc00b6032022-11-29 16:28:03 +08001981
1982 if(i == arr_len)
1983 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001984 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001985 return -1;
1986 }
qs.xiongc00b6032022-11-29 16:28:03 +08001987
qs.xiongc9c79f72022-10-17 15:27:18 +08001988 return 0;
1989}
qs.xiong13673462023-02-21 19:12:54 +08001990
1991static int lynq_check_frequencyby_country_code(int input_frequency)
1992{
1993 char str_cnc[]="CN";
1994 char str_dest[20]="";
1995
1996 if( lynq_get_country_code(1,str_dest) != 0 )
1997 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001998 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001999 return -1;
2000 }
2001 if( strncmp(str_dest,str_cnc,2) != 0 )
2002 {
2003 return 0;
2004 }else if( 2473 < input_frequency && input_frequency < 5744)
2005 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002006 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002007 return -1;
2008 }
2009 return 0;
2010}
qs.xiongf1b525b2022-03-31 00:58:23 -04002011int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002012{
qs.xiongc00b6032022-11-29 16:28:03 +08002013 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002014 char lynq_wifi_frequency_cmd[128]={0};
2015 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002016 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002017 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002018 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002019 check = lynq_check_set_frequency(lynq_wifi_frequency);
2020 if(check != 0)
2021 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002023 return -1;
you.chen35020192022-05-06 11:30:57 +08002024 }
qs.xiong13673462023-02-21 19:12:54 +08002025 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2026 if(check != 0)
2027 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002028 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002029 return -1;
2030 }
2031
qs.xiongc00b6032022-11-29 16:28:03 +08002032 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2033 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002034 RLOGE("[set ap frequecny][lynq_check_network_number] error");
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_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2043 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2044 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2045
you.chen6c2dd9c2022-05-16 17:55:28 +08002046 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002047 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2048 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2049 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002050
qs.xiong9fbf74e2023-03-28 13:38:22 +08002051 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002052}
2053
qs.xiongf1b525b2022-03-31 00:58:23 -04002054int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002055{
you.chen35020192022-05-06 11:30:57 +08002056 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002057 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002058 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002059
qs.xiong9fbf74e2023-03-28 13:38:22 +08002060 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2061 {
2062 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002063 return -1;
2064 }
2065 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002066
qs.xiong9fbf74e2023-03-28 13:38:22 +08002067 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002068}
2069
qs.xiongf1b525b2022-03-31 00:58:23 -04002070int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2071{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002072 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002073 CHECK_IDX(idx, CTRL_AP);
2074 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002075 case LYNQ_WIFI_BANDWIDTH_HT10:
2076 {
2077 RLOGE("bandwith [%d] not support now\n", bandwidth);
2078 return -1;
2079 }
2080 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002081 {
2082 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2083 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002084 if (system(lynq_cmd_bandwith) != 0 )
2085 {
2086 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002087 return -1;
2088 }
2089 system("wl up");
2090 break;
2091 }
2092 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002093 {
qs.xiong10379192023-02-21 13:19:42 +08002094 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002095 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2096 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002097 if (system(lynq_cmd_bandwith) != 0 )
2098 {
2099 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002100 return -1;
2101 }
2102 system("wl up");
2103 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002104 }
you.chen35020192022-05-06 11:30:57 +08002105 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002106 {
qs.xiong10379192023-02-21 13:19:42 +08002107 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002108 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002109 if (system(lynq_cmd_bandwith) != 0 )
2110 {
2111 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002112 return -1;
2113 }
2114 system("wl up");
2115 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002116 }
2117 default:
you.chen35020192022-05-06 11:30:57 +08002118 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 RLOGE("auth type [%d] not support now\n", bandwidth);
2120 return -1;
you.chen35020192022-05-06 11:30:57 +08002121 }
2122 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002123
2124
you.chen35020192022-05-06 11:30:57 +08002125 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002126}
you.chen35020192022-05-06 11:30:57 +08002127
qs.xiongf1b525b2022-03-31 00:58:23 -04002128int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2129{
you.chen35020192022-05-06 11:30:57 +08002130 int count = 0;
2131 int index = 0;
2132 char *split_words[128] = {0};
2133 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002134 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002135 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002136
you.chen35020192022-05-06 11:30:57 +08002137 CHECK_WPA_CTRL(CTRL_AP);
2138
2139 DO_REQUEST(lynq_chanspec_cmd);
2140
2141 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2142 for(;index < count; index++) {
2143 if (strncmp(split_words[index], "bw", 2) != 0) {
2144 continue;
2145 }
2146
2147 index++;
2148 if (index >= count) {
2149 return -1;
2150 }
2151
qs.xiong9fbf74e2023-03-28 13:38:22 +08002152 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002153 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2154 return 0;
2155 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002157 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002158}
qs.xiong0fb469a2022-04-14 03:50:45 -04002159
qs.xiongf1b525b2022-03-31 00:58:23 -04002160int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002161{
you.chen35020192022-05-06 11:30:57 +08002162 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002163 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002164 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002165
you.chen35020192022-05-06 11:30:57 +08002166 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002167
qs.xiong9fbf74e2023-03-28 13:38:22 +08002168 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2169 {
you.chen35020192022-05-06 11:30:57 +08002170 return -1;
2171 }
2172
2173 system("wl down");
2174 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002175 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002176 return -1;
2177 }
2178 system("wl up");
2179 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002180}
qs.xiong0fb469a2022-04-14 03:50:45 -04002181
qs.xiongf1b525b2022-03-31 00:58:23 -04002182int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002183{
you.chen35020192022-05-06 11:30:57 +08002184 int count = 0;
2185 int index = 0;
2186 char *split_words[128] = {0};
2187 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002188 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002189 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002190
you.chen35020192022-05-06 11:30:57 +08002191 CHECK_WPA_CTRL(CTRL_AP);
2192
2193 DO_REQUEST(lynq_chanspec_cmd);
2194
2195 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002196 for(;index < count; index++)
2197 {
2198 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002199 if (strncmp(split_words[index], "channel", 2) != 0) {
2200 continue;
2201 }
2202
2203 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002204 if (index >= count)
2205 {
you.chen35020192022-05-06 11:30:57 +08002206 return -1;
2207 }
2208
2209 *channel = atoi(split_words[index]);
2210 return 0;
2211 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002212 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002213 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002214}
2215
2216
you.chen35020192022-05-06 11:30:57 +08002217int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002218{
you.chen6c2dd9c2022-05-16 17:55:28 +08002219 char ssid[MAX_CMD] = {0};
2220 int freq = 0;
2221 char lynq_auth_cmd[64]={0};
2222 char lynq_auth_alg_cmd[64]={0};
2223 char lynq_psk_cmd[64]={0};
2224 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002225 char lynq_ieee80211_cmd[64]={0};
2226 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002227 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002228 CHECK_IDX(idx, CTRL_AP);
2229
you.chen6c2dd9c2022-05-16 17:55:28 +08002230 CHECK_WPA_CTRL(CTRL_AP);
2231
qs.xiong9fbf74e2023-03-28 13:38:22 +08002232 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2233 {
2234 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002235 return -1;
2236 }
2237
you.chen92fd5d32022-05-25 10:09:47 +08002238 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002239 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002240 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002241 return 0;
2242 }
2243 else {
2244 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2245 ssid[0] = '\0';
2246 }
2247 lynq_wifi_ap_frequency_get(idx, &freq);
2248
2249 DO_OK_FAIL_REQUEST(cmd_disconnect);
2250 DO_OK_FAIL_REQUEST(cmd_remove_all);
2251 if (ssid[0] != '\0') {
2252 lynq_wifi_ap_ssid_set(idx, ssid);
2253 }
2254 if (freq != 0) {
2255 lynq_wifi_ap_frequency_set(idx, freq);
2256 }
2257 }
2258 }
you.chen35020192022-05-06 11:30:57 +08002259
qs.xiong9fbf74e2023-03-28 13:38:22 +08002260 switch(auth){
2261 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002262 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002263 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002264 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002265 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002266 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002267 break;
2268 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002269 case LYNQ_WIFI_AUTH_WEP:
2270 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002271 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002272 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002273 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002274 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2275
2276 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2277 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2278 break;
2279 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002280 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002281 {
2282 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2283 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2284 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2285
2286 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2287 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2288 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2289 break;
2290
2291 }
you.chen35020192022-05-06 11:30:57 +08002292 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002293 {
2294 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2295 {
you.chen35020192022-05-06 11:30:57 +08002296 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2297 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2298 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002299 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2300 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002301 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002302 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002303 }
2304// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2305// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2306 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002307
you.chen35020192022-05-06 11:30:57 +08002308 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2309 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2310 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002311 break;
2312 }
2313 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2314 {
2315 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2316 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2317 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2318 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2319
2320 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2321 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2322 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2323 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2324 break;
2325 }
2326 case LYNQ_WIFI_AUTH_WPA3_PSK:
2327 {
2328 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2329 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002330 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002331 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2332
2333 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2334 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2335 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2336 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2337 break;
2338 }
2339 default:
you.chen35020192022-05-06 11:30:57 +08002340 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002341 RLOGE("auth type [%d] not support now\n", auth);
2342 return -1;
you.chen35020192022-05-06 11:30:57 +08002343 }
2344 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002345 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002346
qs.xiong9fbf74e2023-03-28 13:38:22 +08002347 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002348}
2349
you.chen35020192022-05-06 11:30:57 +08002350int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002351{
you.chen35020192022-05-06 11:30:57 +08002352 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002353 char lynq_auth_alg_str[MAX_RET] = {0};
2354 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002355 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002356 CHECK_IDX(idx, CTRL_AP);
2357
qs.xiong9fbf74e2023-03-28 13:38:22 +08002358 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2359 {
2360 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002361 return -1;
2362 }
2363
qs.xiong9fbf74e2023-03-28 13:38:22 +08002364 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2365 {
2366 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2367 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002368 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002369 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002370 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002371 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2373 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002374 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002375 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002376 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002377 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002378 else
2379 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002380 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002381 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002382 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002383 }
you.chen35020192022-05-06 11:30:57 +08002384 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002385 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2386 {
2387 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2388 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002389 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002390 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002391 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002392 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2393 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002394 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002395 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002396 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002397 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002398 else
2399 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002400 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002401 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002402 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002403 }
you.chen35020192022-05-06 11:30:57 +08002404 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405
2406 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2407 {
2408 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2409 return -1;
2410 }
2411
2412 if (memcmp(lynq_auth_str,"1",1) == 0 )
2413 {
2414 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2415 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002416 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002417 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2418 {
2419 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2420 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002421 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 }
2423 else
2424 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002425 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002426 *auth = -1;
2427 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002428
you.chen6c2dd9c2022-05-16 17:55:28 +08002429 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002430}
qs.xiong1af5daf2022-03-14 09:12:12 -04002431
you.chenb95401e2023-05-12 19:39:06 +08002432static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2433{
2434 char status[64];
you.chencba13492023-05-19 13:53:43 +08002435 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002436 curr_status_info curr_state;
2437
2438 CHECK_WPA_CTRL(CTRL_AP);
2439
2440 memset(status, 0, sizeof (status));
2441
2442 curr_state.ap = NULL;
2443 curr_state.state = status;
2444
2445 printf("inner_check_ap_connected %d\n", retry_count);
2446 usleep(500*1000);
2447 if (0 == inner_get_status_info(idx, &curr_state))
2448 {
2449 if (strcmp(status, STATE_COMPLETED) == 0)
2450 {
2451 return 0;
2452 }
2453 else if (retry_count == 4) //not ok in 2s, do reconnect
2454 {
2455 DO_REQUEST("RECONNECT");
2456 return inner_check_ap_connected(idx, retry_count+1);
2457 }
you.chencba13492023-05-19 13:53:43 +08002458 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002459 {
2460 printf("retry 10 time\n");
2461 return -1;
2462 }
2463 else
2464 {
you.chen6d247052023-06-01 16:39:54 +08002465 if (strcmp(status, STATE_DISCONNECTED) == 0)
2466 {
2467 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2468 DO_REQUEST(LYNQ_WIFI_CMD);
2469 }
you.chenb95401e2023-05-12 19:39:06 +08002470 return inner_check_ap_connected(idx, retry_count+1);
2471 }
2472 }
2473 return -1;
2474}
qs.xiong1af5daf2022-03-14 09:12:12 -04002475
qs.xiongf1b525b2022-03-31 00:58:23 -04002476int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002477{
you.chen35020192022-05-06 11:30:57 +08002478 char LYNQ_WIFI_CMD[128]={0};
2479 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2480 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002481 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002482 CHECK_IDX(idx, CTRL_AP);
2483
2484 CHECK_WPA_CTRL(CTRL_AP);
2485
you.chen0df3e7e2023-05-10 15:56:26 +08002486 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002487 {
you.chen0df3e7e2023-05-10 15:56:26 +08002488 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002489 return -1;
2490 }
you.chen35020192022-05-06 11:30:57 +08002491
2492 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2493 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2494
2495 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2496 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2497
you.chenc9928582023-04-24 15:39:37 +08002498 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2499 if (ret != 0)
2500 {
2501 RLOGE("lynq_wifi_ap_start excute script fail");
2502 return -1;
2503 }
2504
you.chenb95401e2023-05-12 19:39:06 +08002505 if (inner_check_ap_connected(idx, 0) != 0)
2506 {
2507 return -1;
2508 }
2509
you.chen0df3e7e2023-05-10 15:56:26 +08002510 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002511 if (g_ap_tmp_watcher_pid == 0)
2512 {
2513 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2514 {
2515 g_ap_tmp_watcher_pid = 0;
2516 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2517 return -1;
2518 }
2519 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2520 }
you.chen0df3e7e2023-05-10 15:56:26 +08002521
qs.xiong9fbf74e2023-03-28 13:38:22 +08002522 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002523}
2524
qs.xiongf1b525b2022-03-31 00:58:23 -04002525int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002526{
you.chen35020192022-05-06 11:30:57 +08002527 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002528}
2529
qs.xiongf1b525b2022-03-31 00:58:23 -04002530int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002531{
you.chen35020192022-05-06 11:30:57 +08002532 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002533
you.chen35020192022-05-06 11:30:57 +08002534 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002535
you.chen35020192022-05-06 11:30:57 +08002536 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002537
you.chen35020192022-05-06 11:30:57 +08002538 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2539
2540 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2541
you.chenb4b121c2022-05-06 17:50:16 +08002542// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002543
2544 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2545 if (ret != 0)
2546 {
2547 RLOGE("lynq_wifi_ap_start excute script fail");
2548 return -1;
2549 }
qs.xiong44fac672023-08-29 16:15:55 +08002550 g_ap_tmp_watcher_stop_flag = 1;
2551 if (g_ap_tmp_watcher_pid != 0)
2552 pthread_join(g_ap_tmp_watcher_pid, NULL);
2553 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002554
qs.xiong9fbf74e2023-03-28 13:38:22 +08002555 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002556}
qs.xiong1af5daf2022-03-14 09:12:12 -04002557
qs.xiongf1b525b2022-03-31 00:58:23 -04002558int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002559{
you.chen35020192022-05-06 11:30:57 +08002560 char lynq_disable_cmd[128] = {0};
2561 char lynq_select_cmd[128] = {0};
2562 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002563 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002564 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002565
you.chen35020192022-05-06 11:30:57 +08002566 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002567 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2568 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2569
2570 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2571 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2572 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002573
qs.xiong9fbf74e2023-03-28 13:38:22 +08002574 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002575}
2576
qs.xiongf1b525b2022-03-31 00:58:23 -04002577int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002578{
you.chen35020192022-05-06 11:30:57 +08002579 char lynq_disable_cmd[128] = {0};
2580 char lynq_select_cmd[128] = {0};
2581 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002582 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002583 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002584
you.chen35020192022-05-06 11:30:57 +08002585 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002586
you.chen35020192022-05-06 11:30:57 +08002587 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2588 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2589
2590 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2591 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2592 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002593
qs.xiong9fbf74e2023-03-28 13:38:22 +08002594 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002595}
qs.xiongf1b525b2022-03-31 00:58:23 -04002596
you.chen35020192022-05-06 11:30:57 +08002597int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002598{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002599 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002600 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002601 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002602 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002603 RLOGD("enter lynq_ap_password_set");
2604 if( password == NULL )
2605 {
2606 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002607 return -1;
2608 }
2609 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002610 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002611 if(pass_len < 8 || pass_len >= 64)
2612 {
2613 RLOGE("[lynq_ap_password_set]input password len not in rage");
2614 return -1;
you.chen35020192022-05-06 11:30:57 +08002615 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002616
you.chen35020192022-05-06 11:30:57 +08002617 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002618
qs.xiong9fbf74e2023-03-28 13:38:22 +08002619 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2620 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002621 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002622 return -1;
2623 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002624 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2625 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002626 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2627 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002628 }
2629
you.chen35020192022-05-06 11:30:57 +08002630 CHECK_WPA_CTRL(CTRL_AP);
2631
qs.xiong9fbf74e2023-03-28 13:38:22 +08002632 if (auth == LYNQ_WIFI_AUTH_WEP)
2633 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002634 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002635 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2636 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2637 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2638 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2639 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002640 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2641 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002642 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 +08002643 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2644 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2645 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002646 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2647 {
2648
qs.xiongc8d92a62023-03-29 17:36:14 +08002649 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 +08002650 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002651 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002652 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2653 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2654
2655 }
2656 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002657 {
2658 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002659 return -1;
2660 }
you.chen35020192022-05-06 11:30:57 +08002661
you.chen35020192022-05-06 11:30:57 +08002662 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002663
qs.xiong9fbf74e2023-03-28 13:38:22 +08002664 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002665}
2666
you.chen35020192022-05-06 11:30:57 +08002667int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002668{
you.chen35020192022-05-06 11:30:57 +08002669 FILE * fp;
2670 int len, ret;
2671 int count, index;
2672 char *split_lines[128] = {0};
2673 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002674 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002675
you.chen35020192022-05-06 11:30:57 +08002676 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002677
you.chen35020192022-05-06 11:30:57 +08002678 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2679// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002680 if (NULL == fp)
2681 {
2682 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002683 return -1;
2684 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002685
you.chen35020192022-05-06 11:30:57 +08002686 buff = alloca(MAX_RET);
2687 fseek(fp, 0, SEEK_SET);
2688 len = fread(buff, 1, MAX_RET, fp);
2689 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002690
qs.xiong9fbf74e2023-03-28 13:38:22 +08002691 for(index=0; index < len; index ++)
2692 {
2693 if (memcmp(buff + index, "network={", 9) != 0)
2694 {
you.chen35020192022-05-06 11:30:57 +08002695 continue;
2696 }
2697 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002698 for (; index < len; index ++ )
2699 {
2700 if (buff[index] != '}')
2701 {
you.chen35020192022-05-06 11:30:57 +08002702 continue;
2703 }
2704 buff[index] = '\0';
2705 break;
2706 }
2707 len = buff + index - p;
2708 }
2709
2710 count = lynq_split(p, len, '\n', split_lines);
2711
2712 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002713 for(index=0; index < count; index++)
2714 {
you.chen35020192022-05-06 11:30:57 +08002715 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002716 if (p != NULL)
2717 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002718 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002719 if (*p == '\"')
2720 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002721 p++;
2722 }
you.chen35020192022-05-06 11:30:57 +08002723 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002724 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2725 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002726 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002727 if (*p == '\"')
2728 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002729 p++;
2730 }
2731 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002732 else
2733 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002734 continue;
you.chen35020192022-05-06 11:30:57 +08002735 }
2736
2737 strcpy(password, p);
2738
qs.xiong9fbf74e2023-03-28 13:38:22 +08002739 while(*password != '\0')
2740 {
2741 if (*password == '\"')
2742 {
you.chen35020192022-05-06 11:30:57 +08002743 *password = '\0';
2744 break;
2745 }
2746 password++;
2747 }
2748 ret = 0;
2749 break;
2750 } //end for(index=0; index < count; index++)
2751
2752 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002753}
2754
you.chen35020192022-05-06 11:30:57 +08002755static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2756 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002757 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002758
qs.xiong9fbf74e2023-03-28 13:38:22 +08002759 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2760 {
you.chen35020192022-05-06 11:30:57 +08002761 return -1;
2762 }
2763
2764 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002765
qs.xiong9fbf74e2023-03-28 13:38:22 +08002766 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2767 {
2768 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002769 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002770 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002771 {
you.chena6cd55a2022-05-08 12:20:18 +08002772 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002773 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002774 }
you.chen70f377f2023-04-14 18:17:09 +08002775 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2776 {
2777 return 0;
2778 }
you.chena6cd55a2022-05-08 12:20:18 +08002779 }
2780 }
you.chen70f377f2023-04-14 18:17:09 +08002781 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2782 {
2783 return 0;
2784 }
2785
qs.xiong9fbf74e2023-03-28 13:38:22 +08002786 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2787 {
you.chen70f377f2023-04-14 18:17:09 +08002788 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002789 return -1;
2790 }
2791 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2792 {
2793
you.chen70f377f2023-04-14 18:17:09 +08002794 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2795 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002796 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2797 {
2798
2799 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002800 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002801 }else
2802 {
you.chen70f377f2023-04-14 18:17:09 +08002803 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002804 *auth = -1;
2805 return -1;
2806 }
you.chen35020192022-05-06 11:30:57 +08002807 return 0;
2808}
2809
2810int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002811{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002812 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002813 int pass_len, net_no, count, index;
2814 char lynq_tmp_cmd[300]={0};
2815 int net_no_list[128];
2816 lynq_wifi_auth_s net_auth;
2817 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002818 if(pass_len < 8 || pass_len >= 64)
2819 {
2820 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002821 return -1;
2822 }
2823
2824 CHECK_IDX(idx, CTRL_STA);
2825
2826 net_no = -1;
2827 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2828
qs.xiong9fbf74e2023-03-28 13:38:22 +08002829 for (index=0; index < count; index++)
2830 {
you.chen35020192022-05-06 11:30:57 +08002831 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002832 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2833 {
you.chen35020192022-05-06 11:30:57 +08002834 net_no = net_no_list[index];
2835 break;
2836 }
2837 }
2838
qs.xiong9fbf74e2023-03-28 13:38:22 +08002839 if (net_no < 0)
2840 {
you.chen35020192022-05-06 11:30:57 +08002841 return -1;
2842 }
2843
2844 CHECK_WPA_CTRL(CTRL_STA);
2845
2846 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2847
2848 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2849 DO_OK_FAIL_REQUEST(cmd_save_config);
2850
2851 return 0;
2852}
2853
2854int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2855
2856 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002857 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002858 int count, index;
2859 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002860 char *buff, *p, *ssid, *ssid_end_flag;
2861 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002862 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002863
you.chen755332b2022-08-06 16:59:10 +08002864 network_len = 0;
2865 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002866 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002867
you.chen35020192022-05-06 11:30:57 +08002868 CHECK_IDX(idx, CTRL_STA);
2869
qs.xiong9fbf74e2023-03-28 13:38:22 +08002870 if (NULL == password)
2871 {
2872 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002873 return -1;
2874 }
2875
you.chen35020192022-05-06 11:30:57 +08002876 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002877 if (NULL == fp)
2878 {
2879 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002880 return -1;
2881 }
2882
you.chen6d247052023-06-01 16:39:54 +08002883 fseek(fp, 0, SEEK_END);
2884 len = ftell(fp);
2885 buff = malloc(len + 1);
2886
2887 if (buff == NULL)
2888 {
2889 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2890 return -1;
2891 }
2892
you.chen35020192022-05-06 11:30:57 +08002893 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002894 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002895 fclose(fp);
2896
qs.xiong9fbf74e2023-03-28 13:38:22 +08002897 for(index=0; index < len; index ++)
2898 {
2899 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002900 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002901 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002902 {
you.chen35020192022-05-06 11:30:57 +08002903 continue;
2904 }
you.chen6d247052023-06-01 16:39:54 +08002905 p = buff + index + 9;
2906 for (; index < len; index ++ )
2907 {
2908 if (buff[index] != '}')
2909 {
2910 continue;
2911 }
2912 buff[index] = '\0';
2913 break;
2914 }
2915 network_len = buff + index - p;
2916 break;
you.chen35020192022-05-06 11:30:57 +08002917 }
2918
qs.xiongb3f26af2023-02-17 18:41:07 +08002919 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002920 {
2921 if (buff != NULL)
2922 {
2923 free(buff);
2924 }
2925
qs.xiongb3f26af2023-02-17 18:41:07 +08002926 return -1;
you.chen6d247052023-06-01 16:39:54 +08002927 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002928
you.chend2fef3f2023-02-13 10:50:35 +08002929 ssid = strstr(p, "ssid=");
2930 if (ssid != NULL) {
2931 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002932 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002933 {
you.chend2fef3f2023-02-13 10:50:35 +08002934 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2935 break;
2936 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002937 else
you.chen6d247052023-06-01 16:39:54 +08002938 {
you.chend2fef3f2023-02-13 10:50:35 +08002939 ssid_end_flag = strstr(ssid, "\n");
2940 if (ssid_end_flag != NULL)
2941 {
2942 ssid_len = (ssid_end_flag - ssid) / 2;
2943 for(i=0; i<ssid_len; i++)
2944 {
2945 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2946 }
2947 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2948 break;
2949 }
2950 }
you.chen35020192022-05-06 11:30:57 +08002951 }
you.chend2fef3f2023-02-13 10:50:35 +08002952
you.chen35020192022-05-06 11:30:57 +08002953 }
2954
qs.xiong9fbf74e2023-03-28 13:38:22 +08002955 if (index >= len || NULL == p || network_len <= 0)
2956 {
you.chen6d247052023-06-01 16:39:54 +08002957 if (buff != NULL)
2958 {
2959 free(buff);
2960 }
you.chen35020192022-05-06 11:30:57 +08002961 return -1;
2962 }
2963
you.chen755332b2022-08-06 16:59:10 +08002964 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002965
2966 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002967 for(index=0; index < count; index++)
2968 {
you.chen35020192022-05-06 11:30:57 +08002969 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002970 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002971 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002972 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002973 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002974 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002975 p++;
2976 }
you.chen35020192022-05-06 11:30:57 +08002977 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002978 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002979 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002980 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002982 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002983 p++;
2984 }
2985 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002986 else
you.chen6d247052023-06-01 16:39:54 +08002987 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002988 continue;
you.chen35020192022-05-06 11:30:57 +08002989 }
2990
qs.xiong13673462023-02-21 19:12:54 +08002991 if (*p == '\"')
2992 p++;
2993 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002994
qs.xiong13673462023-02-21 19:12:54 +08002995 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002996 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002997 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002998 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002999 {
you.chen35020192022-05-06 11:30:57 +08003000 *password = '\0';
3001 break;
3002 }
3003 password++;
3004 }
3005 ret = 0;
3006 break;
3007 } //end for(index=0; index < count; index++)
3008
you.chen6d247052023-06-01 16:39:54 +08003009 if (buff != NULL)
3010 {
3011 free(buff);
3012 }
3013
you.chen35020192022-05-06 11:30:57 +08003014 return ret;
3015}
3016
3017static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3018{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003020
qs.xiong9fbf74e2023-03-28 13:38:22 +08003021 if (sta_ssid == NULL)
3022 {
3023 RLOGE("sta_ssid is null\n");
3024 return -1;
you.chen35020192022-05-06 11:30:57 +08003025 }
3026
qs.xiong9fbf74e2023-03-28 13:38:22 +08003027 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003028
3029 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3030
3031 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3032// DO_OK_FAIL_REQUEST(cmd_save_config);
3033
qs.xiong9fbf74e2023-03-28 13:38:22 +08003034 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003035
3036}
3037
you.chen35020192022-05-06 11:30:57 +08003038static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003039{
you.chen35020192022-05-06 11:30:57 +08003040 char lynq_disable_cmd[128]={0};
3041 char lynq_select_cmd[128]={0};
3042
3043 CHECK_WPA_CTRL(CTRL_STA);
3044
qs.xiong9fbf74e2023-03-28 13:38:22 +08003045 if (save != 0)
3046 {
you.chenc29444e2022-06-07 18:01:16 +08003047 if (start_flag != 0)
3048 {
3049 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3050 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3051 }
3052 else
3053 {
3054 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3055 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3056 }
you.chen35020192022-05-06 11:30:57 +08003057 DO_OK_FAIL_REQUEST(cmd_save_config);
3058 }
3059
qs.xiong9fbf74e2023-03-28 13:38:22 +08003060 if (start_flag == 0)
3061 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003062 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003063 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3064 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003065 else
3066 {
you.chen35020192022-05-06 11:30:57 +08003067 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3068 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3069 }
3070
3071 return 0;
3072}
3073
3074int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3075{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003076 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003077 CHECK_IDX(idx, CTRL_STA);
3078
you.chen6c2dd9c2022-05-16 17:55:28 +08003079 curr_status_info curr_state;
3080 ap_info_s ap_info;
3081 curr_state.ap = &ap_info;
3082 curr_state.state = NULL;
3083
qs.xiong9fbf74e2023-03-28 13:38:22 +08003084 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3085 {
you.chend2fef3f2023-02-13 10:50:35 +08003086 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003087 return 0;
3088 }
3089
3090 return -1;
you.chen35020192022-05-06 11:30:57 +08003091}
3092
3093int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3094{
you.chen9ac66392022-08-06 17:01:16 +08003095 scan_info_s *scan_list = NULL;
3096 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003097 int scan_len=0;
3098 int save_len=0;
3099 int best_index = -1;
3100 int best_scan_index = -1;
3101 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003102 int i, j, ret;
3103
3104 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003105
3106 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003107 if (info == NULL)
3108 {
you.chen35020192022-05-06 11:30:57 +08003109 return -1;
3110 }
3111
3112 curr_status_info curr_state;
3113 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003114 char status[64];
you.chen35020192022-05-06 11:30:57 +08003115
you.chen9ac66392022-08-06 17:01:16 +08003116 memset(&ap_info, 0, sizeof (ap_info));
3117 memset(status, 0, sizeof (status));
3118
3119 curr_state.ap = &ap_info;
3120 curr_state.state = status;
3121
qs.xiong9fbf74e2023-03-28 13:38:22 +08003122 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3123 {
you.chen35020192022-05-06 11:30:57 +08003124 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003125 if (strcmp(status, STATE_COMPLETED) == 0)
3126 {
3127 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3128 }
3129 else
3130 {
3131 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3132 }
you.chen593621d2023-04-27 17:52:44 +08003133 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003134 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003135 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003136 return 0;
3137 }
3138
you.chen9ac66392022-08-06 17:01:16 +08003139 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003140 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003141 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3142 {
you.chen9ac66392022-08-06 17:01:16 +08003143 if (NULL != scan_list)
3144 {
3145 free(scan_list);
3146 }
you.chen35020192022-05-06 11:30:57 +08003147 return -1;
3148 }
3149
qs.xiong9fbf74e2023-03-28 13:38:22 +08003150 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3151 {
you.chen9ac66392022-08-06 17:01:16 +08003152 if (NULL != scan_list)
3153 {
3154 free(scan_list);
3155 }
3156 if (NULL != save_list)
3157 {
3158 free(save_list);
3159 }
you.chen35020192022-05-06 11:30:57 +08003160 return -1;
3161 }
3162
qs.xiong9fbf74e2023-03-28 13:38:22 +08003163 for (i=0; i < save_len; i++)
3164 {
3165 for (j=0; j < scan_len; j++)
3166 {
you.chen35020192022-05-06 11:30:57 +08003167 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003168 && save_list[i].base_info.auth == scan_list[j].auth)
3169 {
3170 if (best_rssi == 0)
3171 {
you.chen9ac66392022-08-06 17:01:16 +08003172 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003173 best_rssi = scan_list[j].rssi;
3174 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003175 else if (best_rssi > scan_list[j].rssi)
3176 {
you.chen35020192022-05-06 11:30:57 +08003177 best_index = i;
3178 best_scan_index = j;
3179 best_rssi = scan_list[j].rssi;
3180 }
you.chend2fef3f2023-02-13 10:50:35 +08003181 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 +08003182 break;
3183 }
3184 }
3185 }
3186
qs.xiong9fbf74e2023-03-28 13:38:22 +08003187 if (best_index >= 0)
3188 {
you.chen35020192022-05-06 11:30:57 +08003189 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003190 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 +08003191 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3192 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003193 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003194 }
3195
you.chen9ac66392022-08-06 17:01:16 +08003196 if (NULL != scan_list)
3197 {
3198 free(scan_list);
3199 }
3200 if (NULL != save_list)
3201 {
3202 free(save_list);
3203 }
3204
3205 return ret;
you.chen35020192022-05-06 11:30:57 +08003206}
3207
3208static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3209{
qs.xiongc8d92a62023-03-29 17:36:14 +08003210 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003211 char lynq_ket_mgmt_cmd[64]={0};
3212 char lynq_pairwise_cmd[64]={0};
3213 char lynq_psk_cmd[64]={0};
3214
3215 CHECK_WPA_CTRL(CTRL_STA);
3216
qs.xiong9fbf74e2023-03-28 13:38:22 +08003217 switch(auth)
3218 {
3219 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003220 {
3221 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003222
you.chen35020192022-05-06 11:30:57 +08003223 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003224// DO_OK_FAIL_REQUEST(cmd_save_config);
3225 break;
3226 }
3227 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003228 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003229 {
3230 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3231 {
you.chen35020192022-05-06 11:30:57 +08003232 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3233 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003234 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3235 {
you.chena6cd55a2022-05-08 12:20:18 +08003236 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003237 }
3238 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3239 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003240
you.chen35020192022-05-06 11:30:57 +08003241 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3242 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3243 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003244
qs.xiong9fbf74e2023-03-28 13:38:22 +08003245 if (password != NULL)
3246 {
you.chen35020192022-05-06 11:30:57 +08003247 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3248 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003249 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003250 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003251
you.chen35020192022-05-06 11:30:57 +08003252// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003253 break;
3254 }
3255 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3256 {
qs.xiong3e506812023-04-06 11:08:48 +08003257 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003258 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003259 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3260 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3261
qs.xiong3e506812023-04-06 11:08:48 +08003262 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003263 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3264 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3265 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3266
3267 break;
3268 }
3269 case LYNQ_WIFI_AUTH_WPA3_PSK:
3270 {
qs.xiong3e506812023-04-06 11:08:48 +08003271 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003272 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003273 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3274 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3275
qs.xiong3e506812023-04-06 11:08:48 +08003276 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003277 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3278 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3279 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3280
3281 break;
3282 }
3283 default:
3284 return -1;
you.chen35020192022-05-06 11:30:57 +08003285 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003286
qs.xiong9fbf74e2023-03-28 13:38:22 +08003287 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003288}
qs.xiong7a105ce2022-03-02 09:43:11 -05003289
you.chen35020192022-05-06 11:30:57 +08003290static int inner_get_curr_net_no(int interface) {
3291 curr_status_info curr_state;
3292 curr_state.ap = NULL;
3293 curr_state.state = NULL;
3294
qs.xiong9fbf74e2023-03-28 13:38:22 +08003295 if (0 != inner_get_status_info(interface, &curr_state))
3296 {
you.chen35020192022-05-06 11:30:57 +08003297 return -1;
3298 }
3299
3300 return curr_state.net_no;
3301}
3302
3303int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003304{
you.chen35020192022-05-06 11:30:57 +08003305 int net_no;
3306 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003307
you.chen35020192022-05-06 11:30:57 +08003308 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003309
qs.xiong9fbf74e2023-03-28 13:38:22 +08003310 if (net_no < 0)
3311 {
you.chen35020192022-05-06 11:30:57 +08003312 return -1;
3313 }
3314
3315 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003316}
3317
you.chenb95401e2023-05-12 19:39:06 +08003318int 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 -05003319{
you.chen35020192022-05-06 11:30:57 +08003320 int count, net_no, index;
3321 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003322 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003323 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003324 curr_status_info curr_state;
3325 ap_info_s ap_info;
3326 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003327
qs.xiong9fbf74e2023-03-28 13:38:22 +08003328 if (ssid == NULL || *ssid == '\0')
3329 {
3330 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003331 return -1;
3332 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003333
qs.xiong9fbf74e2023-03-28 13:38:22 +08003334 if (LYNQ_WIFI_AUTH_OPEN != auth)
3335 {
3336 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003337 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003338 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003339 return -1;
3340 }
3341 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003342
you.chen70f377f2023-04-14 18:17:09 +08003343
3344 pthread_mutex_lock(&s_global_check_mutex);
3345 if (s_sta_status != INNER_STA_STATUS_INIT)
3346 {
3347 s_sta_status = INNER_STA_STATUS_CANCEL;
3348 pthread_cond_signal(&s_global_check_cond);
3349 }
3350 pthread_mutex_unlock(&s_global_check_mutex);
3351
you.chen35020192022-05-06 11:30:57 +08003352 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003353 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003354
3355 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003356 memset(&ap_info, 0, sizeof (ap_info));
3357 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003358
you.chen70f377f2023-04-14 18:17:09 +08003359 curr_state.ap = &ap_info;
3360 curr_state.state = status;
3361
3362 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003363 {
you.chen70f377f2023-04-14 18:17:09 +08003364 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3365 {
3366 net_no = curr_state.net_no;
3367 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3368 && strcmp(ap_info.psw, psw) == 0)
3369 {
3370 RLOGD("already connected\n");
3371
3372 pthread_mutex_lock(&s_global_check_mutex);
3373 s_sta_status = INNER_STA_STATUS_CONNECTED;
3374 pthread_cond_signal(&s_global_check_cond);
3375 pthread_mutex_unlock(&s_global_check_mutex);
3376 return 0;
3377 }
you.chen35020192022-05-06 11:30:57 +08003378 }
3379 }
3380
you.chen70f377f2023-04-14 18:17:09 +08003381 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003382 {
you.chen70f377f2023-04-14 18:17:09 +08003383 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3384
3385 for (index=0; index < count; index++)
3386 {
3387 net_auth = -1;
3388 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3389 {
3390 net_no = net_no_list[index];
3391 break;
3392 }
you.chen35020192022-05-06 11:30:57 +08003393 }
3394
you.chen70f377f2023-04-14 18:17:09 +08003395 if (net_no < 0)
3396 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003397 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3398 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3399 {
3400 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3401 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3402 DO_OK_FAIL_REQUEST(rm_net_cmd);
3403 }
you.chen70f377f2023-04-14 18:17:09 +08003404 net_no = lynq_add_network(CTRL_STA);
3405 if (net_no == -1)
3406 {
3407 return -1;
3408 }
3409
3410 RLOGD("net no is %d\n", net_no);
3411 if (0 != inner_set_sta_ssid(net_no, ssid))
3412 {
3413 return -1;
3414 }
you.chen35020192022-05-06 11:30:57 +08003415 }
3416 }
3417
qs.xiong9fbf74e2023-03-28 13:38:22 +08003418 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3419 {
you.chen35020192022-05-06 11:30:57 +08003420 return -1;
3421 }
3422
you.chen70f377f2023-04-14 18:17:09 +08003423
3424 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003425 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003426 usleep(200*1000);
3427
3428 ret = inner_sta_start_stop(net_no, 1, 1);
3429
3430 pthread_mutex_lock(&s_global_check_mutex);
3431 s_sta_status = INNER_STA_STATUS_CONNECTING;
3432 strcpy(s_sta_current_connecting_ssid, ssid);
3433 struct timeval now;
3434 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003435 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003436 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3437 pthread_cond_signal(&s_global_check_cond);
3438 pthread_mutex_unlock(&s_global_check_mutex);
3439 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003440}
3441
you.chenb95401e2023-05-12 19:39:06 +08003442int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3443{
3444 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3445}
3446
you.chen35020192022-05-06 11:30:57 +08003447int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003448{
you.chen35020192022-05-06 11:30:57 +08003449 ap_info_s ap;
3450 curr_status_info curr_state;
3451 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003452
qs.xiong9fbf74e2023-03-28 13:38:22 +08003453 if (ssid == NULL || *ssid == '\0')
3454 {
3455 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003456 return -1;
3457 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003458
you.chen35020192022-05-06 11:30:57 +08003459 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003460
you.chen35020192022-05-06 11:30:57 +08003461 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003462 curr_state.state = NULL;
3463
qs.xiong9fbf74e2023-03-28 13:38:22 +08003464 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3465 {
you.chen35020192022-05-06 11:30:57 +08003466 return 0;
3467 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003468
qs.xiong9fbf74e2023-03-28 13:38:22 +08003469 if (strcmp(ap.ap_ssid, ssid) != 0)
3470 {
you.chen35020192022-05-06 11:30:57 +08003471 return 0;
3472 }
3473
you.chen70f377f2023-04-14 18:17:09 +08003474 pthread_mutex_lock(&s_global_check_mutex);
3475 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3476 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003477 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003478}
qs.xiong97fa59b2022-04-07 05:41:29 -04003479
qs.xiongc93bf2b2023-08-25 10:22:08 +08003480int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3481{
3482 ap_info_s ap;
3483 curr_status_info curr_state;
3484 ap.ap_ssid[0] = '\0';
3485
3486 if (ssid == NULL || *ssid == '\0')
3487 {
3488 RLOGE("input ssid is NULL\n");
3489 return -1;
3490 }
3491
3492 CHECK_IDX(idx, CTRL_STA);
3493
3494
3495 curr_state.ap = &ap;
3496 curr_state.state = NULL;
3497
3498 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3499 {
3500 return 0;
3501 }
3502
3503 if (strcmp(ap.ap_ssid, ssid) != 0)
3504 {
3505 return 0;
3506 }
3507
3508 pthread_mutex_lock(&s_global_check_mutex);
3509 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3510 pthread_mutex_unlock(&s_global_check_mutex);
3511 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3512
3513}
3514
3515
you.chena6cd55a2022-05-08 12:20:18 +08003516int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3517{
qs.xiongad2f89d2023-01-18 13:17:41 +08003518// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3519// const char *lynq_reconnect_cmd = "RECONNECT";
3520 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3521 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003522
you.chen35020192022-05-06 11:30:57 +08003523 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003524 CHECK_WPA_CTRL(CTRL_STA);
3525
you.chenc9928582023-04-24 15:39:37 +08003526 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3527 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003528 {
you.chenc9928582023-04-24 15:39:37 +08003529 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003530 return -1;
3531 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003532
qs.xiongad2f89d2023-01-18 13:17:41 +08003533 system(lynq_enable_sta_cmd);
3534 system(lynq_reconnect_cmd);
3535// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003536 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003537}
3538
you.chen6d247052023-06-01 16:39:54 +08003539static int inner_get_status_info_state (int interface, char *state) {
3540 curr_status_info curr_state;
3541 curr_state.ap = NULL;
3542 curr_state.state = state;
3543 return inner_get_status_info(interface, &curr_state);
3544}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003545
3546int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3547{
3548
3549 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3550
3551 CHECK_IDX(idx, CTRL_STA);
3552 CHECK_WPA_CTRL(CTRL_STA);
3553
3554 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3555 if (ret != 0)
3556 {
3557 RLOGE("lynq_wifi_ap_start excute script fail");
3558 return -1;
3559 }
3560
3561// system(lynq_enable_sta_cmd);
3562 system(lynq_reconnect_cmd);
3563
3564 return 0;
3565}
3566
3567
you.chen35020192022-05-06 11:30:57 +08003568int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003569{
qs.xiongad2f89d2023-01-18 13:17:41 +08003570// char lynq_disable_network_cmd[MAX_CMD];
3571// curr_status_info curr_state;
3572// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003573 int i=0;
3574 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003575
you.chen6d247052023-06-01 16:39:54 +08003576// 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 +08003577 CHECK_IDX(idx, CTRL_STA);
3578 CHECK_WPA_CTRL(CTRL_STA);
3579
you.chen6d247052023-06-01 16:39:54 +08003580// system(lynq_disable_sta_cmd);
3581 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003582 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003583
3584 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3585 if (ret != 0)
3586 {
3587 RLOGE("lynq_wifi_ap_start excute script fail");
3588 return -1;
3589 }
3590
you.chen6d247052023-06-01 16:39:54 +08003591 for (i=0; i < 30; i++) // to check if sta is realy stoped
3592 {
3593 if (inner_get_status_info_state(idx, state) != 0)
3594 {
3595 break;
3596 }
3597
3598 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3599 {
3600 break;
3601 }
3602 RLOGD("lynq_wifi_ap_start curr state %s", state);
3603 usleep(SLEEP_TIME_ON_IDLE);
3604 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003605
you.chena6cd55a2022-05-08 12:20:18 +08003606 return 0;
3607// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003608}
qs.xiongfcc914b2023-07-06 21:16:20 +08003609int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3610{
3611 char LYNQ_DISABLE_CMD[128]={0};
3612 CHECK_IDX(idx, CTRL_STA);
3613 CHECK_WPA_CTRL(CTRL_STA);
3614 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3615 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3616 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3617 return 0;
3618}
qs.xiong7a105ce2022-03-02 09:43:11 -05003619
you.chen35020192022-05-06 11:30:57 +08003620//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3621// int i, count;
3622// char *p;
3623// const char * FLAG_SSID = "ssid=";
3624// const char * FLAG_SBSID = "bssid=";
3625// const char * FLAG_KEY_MGMT = "key_mgmt=";
3626// const char * FLAG_FREQ = "freq=";
3627// char lynq_sta_cmd[MAX_CMD];
3628// char *split_lines[128] = {0};
3629
3630// CHECK_WPA_CTRL(CTRL_AP);
3631
3632// sprintf(lynq_sta_cmd, "STA %s", bssid);
3633
3634// DO_REQUEST(lynq_sta_cmd);
3635
3636// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3637
3638// for(i=0; i < count; i++) {
3639// p = strstr(split_lines[i], FLAG_SSID);
3640// if (p != NULL) {
3641// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3642// continue;
3643// }
3644// }
3645
3646// lynq_get_interface_ip(idx, ap->ap_ip);
3647// lynq_ap_password_set(idx, ap->psw);
3648
3649// return 0;
3650//}
3651
3652static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3653 curr_status_info curr_state;
3654 curr_state.ap = ap;
3655 curr_state.state = NULL;
3656 return inner_get_status_info(interface, &curr_state);
3657}
3658
3659int 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 -04003660{
qs.xiong5071c802023-09-06 14:04:15 +08003661 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003662 int index, line_count;
3663 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003664 const char *lynq_first_sta_cmd = "STA-FIRST";
3665 char lynq_next_sta_cmd[MAX_CMD];
3666 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003667 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003668
you.chen35020192022-05-06 11:30:57 +08003669 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003670
you.chen35020192022-05-06 11:30:57 +08003671 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003672
you.chenb95401e2023-05-12 19:39:06 +08003673 // ap_info_s * tmp_ap;
3674 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003675 if (ap == NULL || list == NULL || len == NULL)
3676 {
3677 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003678 return -1;
3679 }
3680
you.chenb95401e2023-05-12 19:39:06 +08003681 // ap = &tmp_ap;
3682 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003683 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003684 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003685
you.chenb95401e2023-05-12 19:39:06 +08003686 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003687 {
you.chenb95401e2023-05-12 19:39:06 +08003688 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003689 return -1;
3690 }
3691
3692 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3693 lynq_ap_password_get(idx, (*ap)->psw);
3694
you.chen35020192022-05-06 11:30:57 +08003695 DO_REQUEST(lynq_first_sta_cmd);
3696
3697 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003698 while (reply_len > 0)
3699 {
3700 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003701 {
you.chen35020192022-05-06 11:30:57 +08003702 break;
3703 }
3704 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3705 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3706 strcpy(bssid[index], split_lines[0]);
3707 index++;
3708 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3709 reply_len = MAX_RET;
3710 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003711 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 +08003712 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003713 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003714 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003715 break;
3716 }
3717 }
3718
3719 *len = index;
3720
3721 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003722 for (index=0; index < *len; index++)
3723 {
you.chend2fef3f2023-02-13 10:50:35 +08003724 dev_info = &(*list)[index];
3725 memset(dev_info, 0, sizeof(device_info_s));
3726 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3727 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3728 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3729 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003730 free(bssid[index]);
3731 }
qs.xiong5071c802023-09-06 14:04:15 +08003732 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003733 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003734}
3735
you.chen35020192022-05-06 11:30:57 +08003736int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003737{
you.chen35020192022-05-06 11:30:57 +08003738 int i, count, index, count_words;
3739 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3740 char *split_lines[128] = {0};
3741 char *split_words[128] = {0};
3742 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003743
qs.xiong9fbf74e2023-03-28 13:38:22 +08003744 if (list == NULL || len == NULL)
3745 {
you.chen35020192022-05-06 11:30:57 +08003746 return -1;
3747 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003748
you.chen9ac66392022-08-06 17:01:16 +08003749 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3750 {
3751 usleep(100 * 1000);
3752 }
3753
you.chen35020192022-05-06 11:30:57 +08003754 CHECK_IDX(idx, CTRL_STA);
3755
3756 CHECK_WPA_CTRL(CTRL_STA);
3757
3758 DO_REQUEST(lynq_scan_result_cmd);
3759
3760 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3761 *len = count - 1;
3762 *list = malloc(sizeof (scan_info_s) * *len);
3763
3764 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003765 for (index=0; index <count_words; index++)
3766 {
3767 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003768 }
3769
qs.xiong9fbf74e2023-03-28 13:38:22 +08003770 for(index = 1;index < count; index++)
3771 {
3772 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003773 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003774 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3775 if (count_words < 4)
3776 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003777 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003778 //bssid / frequency / signal level / flags / ssid
3779 p = (*list) + index - 1;
3780 strcpy(p->mac, split_words[0]);
3781 p->band = convert_band_from_freq(atoi(split_words[1]));
3782 p->rssi = -1 * atoi( split_words[2]);
3783 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003784 if (count_words == 4) // ssid hided
3785 {
3786 p->ssid[0] = '\0';
3787 }
3788 else
3789 {
3790 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3791 }
you.chen35020192022-05-06 11:30:57 +08003792 }
3793
3794 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003795}
qs.xiong97fa59b2022-04-07 05:41:29 -04003796
you.chen35020192022-05-06 11:30:57 +08003797int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3798{
3799 int count, net_no, index;
3800 int net_no_list[128];
3801 lynq_wifi_auth_s net_auth;
3802 char lynq_remove_cmd[MAX_CMD];
3803
qs.xiong9fbf74e2023-03-28 13:38:22 +08003804 if (ssid == NULL || *ssid == '\0')
3805 {
3806 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003807 return -1;
3808 }
3809
3810 CHECK_IDX(idx, CTRL_STA);
3811
3812 CHECK_WPA_CTRL(CTRL_STA);
3813
3814 net_no = -1;
3815 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3816
qs.xiong9fbf74e2023-03-28 13:38:22 +08003817 for (index=0; index < count; index++)
3818 {
you.chen35020192022-05-06 11:30:57 +08003819 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003820 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3821 {
you.chen35020192022-05-06 11:30:57 +08003822 net_no = net_no_list[index];
3823 break;
3824 }
3825 }
3826
qs.xiong9fbf74e2023-03-28 13:38:22 +08003827 if (net_no < 0)
3828 {
you.chen35020192022-05-06 11:30:57 +08003829 return 0;
3830 }
3831
3832 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3833
3834 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3835 DO_OK_FAIL_REQUEST(cmd_save_config);
3836
3837 return 0;
3838}
3839
3840int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003841{
you.chend2fef3f2023-02-13 10:50:35 +08003842 int count, index;
you.chen35020192022-05-06 11:30:57 +08003843 int net_no_list[128];
3844 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003845 RLOGD("enter lynq_get_sta_saved_ap api\n");
3846 if (list == NULL || len == NULL)
3847 {
3848 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003849 return -1;
3850 }
3851
3852 CHECK_IDX(idx, CTRL_STA);
3853
3854// CHECK_WPA_CTRL(CTRL_STA);
3855
3856 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003857 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003858
you.chen057aac42023-04-13 14:06:58 +08003859 if (count < 0)
3860 {
3861 RLOGE("list network fail");
3862 return count;
3863 }
3864 else if (count == 0)
3865 {
3866 *list = NULL;
3867 *len = 0;
3868 return 0;
3869 }
3870
you.chen35020192022-05-06 11:30:57 +08003871 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003872 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003873 *len = count;
3874
qs.xiong9fbf74e2023-03-28 13:38:22 +08003875 for (index=0; index < count; index++)
3876 {
you.chen35020192022-05-06 11:30:57 +08003877 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003878 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003879 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003880 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003881 {
you.chen35020192022-05-06 11:30:57 +08003882 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3883 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003884 else
you.chen057aac42023-04-13 14:06:58 +08003885 {
you.chen35020192022-05-06 11:30:57 +08003886 (*list)[index].base_info.band = -1;
3887 }
you.chen057aac42023-04-13 14:06:58 +08003888 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003889 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003890 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003891 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003892 return 0;
3893}
3894
3895int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3896{
qs.xiong20202422023-09-06 18:01:18 +08003897 if ( g_sta_conncet_status_flag != 0 )
3898 {
3899 RLOGD("current sta is connecting dest ap");
3900 return -1;
3901 }
qs.xiongc8d92a62023-03-29 17:36:14 +08003902 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003903 const char *lynq_scan_cmd = "SCAN";
3904
3905 CHECK_IDX(idx, CTRL_STA);
3906
3907 CHECK_WPA_CTRL(CTRL_STA);
3908
you.chen0df3e7e2023-05-10 15:56:26 +08003909 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3910 {
3911 RLOGD("tmp clear scanlist");
3912 system(clean_last_re);
3913 }
you.chen9ac66392022-08-06 17:01:16 +08003914 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003915 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003916 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3917 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003918 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003919 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3920 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003921 g_sta_scan_finish_flag = 1;
3922 return -1;
3923 }
you.chen35020192022-05-06 11:30:57 +08003924
3925 return 0;
3926}
3927
3928int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003929 if (cb == NULL)
3930 {
3931 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003932 return -1;
3933 }
3934
you.chen6d247052023-06-01 16:39:54 +08003935 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003936 g_ap_callback_priv = priv;
3937 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003938 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003939
you.chen6d247052023-06-01 16:39:54 +08003940 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003941 if (g_ap_watcher_pid == 0 )
3942 {
3943 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3944 {
3945 g_ap_watcher_pid = 0;
3946 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3947 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3948 return -1;
3949 }
3950 }
3951
3952 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3953 RLOGD("creat APWatcherTheradProc susccs");
3954
you.chen35020192022-05-06 11:30:57 +08003955 return 0;
3956}
3957
3958int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003959 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003960 if (g_ap_callback_priv == priv)
3961 {
you.chen35020192022-05-06 11:30:57 +08003962 g_ap_callback_func = NULL;
3963 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003964 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003965 return 0;
3966 }
you.chen6d247052023-06-01 16:39:54 +08003967 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003968 return -1;
3969}
3970
3971int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003972 if (cb == NULL)
3973 {
3974 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003975 return -1;
3976 }
3977
you.chen6d247052023-06-01 16:39:54 +08003978 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003979 g_sta_callback_priv = priv;
3980 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003981 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003982
you.chen6d247052023-06-01 16:39:54 +08003983 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003984 if (g_sta_watcher_pid == 0 ) {
3985 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3986 {
3987 g_sta_watcher_pid = 0;
3988 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3989 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3990 return -1;
3991 }
3992 }
3993
3994 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3995 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003996 return 0;
3997}
3998
3999int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004000 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004001 if (g_sta_callback_priv == priv)
4002 {
you.chen35020192022-05-06 11:30:57 +08004003 g_sta_callback_func = NULL;
4004 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004005 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004006 return 0;
4007 }
you.chen6d247052023-06-01 16:39:54 +08004008 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004009 return -1;
4010}
4011
qs.xiongfcc914b2023-07-06 21:16:20 +08004012int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4013 if (cb == NULL)
4014 {
4015 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4016 return -1;
4017 }
4018 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4019 g_sta_auto_callback_priv = priv;
4020 g_sta_auto_callback_func = cb;
4021 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4022 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4023 if (g_sta_auto_watcher_pid == 0 ) {
4024 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4025 {
4026 g_sta_auto_watcher_pid = 0;
4027 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4028 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4029 return -1;
4030 }
4031 }
4032 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4033 RLOGD("creat STAWatcherTheradProc susccs");
4034 return 0;
4035}
4036int lynq_unreg_sta_auto_event_callback(void * priv) {
4037 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4038 if (g_sta_auto_callback_priv == priv)
4039 {
4040 g_sta_auto_watcher_stop_flag = 1;
4041 if (g_sta_auto_watcher_pid != 0)
4042 {
4043 pthread_join(g_sta_auto_watcher_pid, NULL);
4044 }
4045 g_sta_auto_watcher_pid = 0;
4046 g_sta_auto_callback_func = NULL;
4047 g_sta_auto_callback_priv = NULL;
4048 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4049 return 0;
4050 }
4051 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4052 return -1;
4053}
you.chen35020192022-05-06 11:30:57 +08004054int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4055{
4056 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004057 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004058 CHECK_IDX(idx, CTRL_AP);
4059
qs.xiong9fbf74e2023-03-28 13:38:22 +08004060 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4061 {
you.chen35020192022-05-06 11:30:57 +08004062 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4063 return 0;
4064 }
4065
qs.xiong9fbf74e2023-03-28 13:38:22 +08004066 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4067 {
you.chen35020192022-05-06 11:30:57 +08004068 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4069 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004070 else
4071 {
you.chen35020192022-05-06 11:30:57 +08004072 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4073 }
4074
4075 return 0;
4076}
4077
4078int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4079 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004080 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004081 CHECK_IDX(idx, CTRL_STA);
4082
qs.xiong9fbf74e2023-03-28 13:38:22 +08004083 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4084 {
you.chen35020192022-05-06 11:30:57 +08004085 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4086 return 0;
4087 }
4088
qs.xiong9fbf74e2023-03-28 13:38:22 +08004089 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4090 {
you.chen35020192022-05-06 11:30:57 +08004091 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4092 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004093 else
4094 {
you.chen35020192022-05-06 11:30:57 +08004095 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4096 }
4097
4098 return 0;
4099}
4100
4101int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4102// CHECK_IDX(idx, CTRL_AP);
4103// int ret = 0;
4104// size_t reply_len = MAX_RET;
4105// char cmd_reply[MAX_RET]={0};
4106// const char * cmd_str = "GET country";
4107// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4108// do{
4109// if (NULL == s_lynq_wpa_ctrl) {
4110// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4111// if (NULL == s_lynq_wpa_ctrl ) {
4112// printf("wpa_ctrl_open fail\n");
4113// return -1;
4114// }
4115// }
4116// }while(0);
4117
4118// do {
4119// reply_len = MAX_RET;
4120// cmd_reply[0] = '\0';
4121// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004122// 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 +08004123// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004124// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004125// return ret;
4126// }
4127// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004128// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004129// }while(0);
4130
4131 FILE *fp;
4132 size_t i = 0;
4133 char lynq_cmd_ret[MAX_RET]={0};
4134
4135// CHECK_IDX(idx, CTRL_AP);
4136
4137 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004138 {
4139 perror("popen error!");
4140 return -1;
4141 }
you.chen35020192022-05-06 11:30:57 +08004142 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4143 {
4144 perror("fread fail!");
4145 return -1;
4146 }
4147
qs.xiong9fbf74e2023-03-28 13:38:22 +08004148 for(i=0; i < strlen(lynq_cmd_ret); i++)
4149 {
4150 if (lynq_cmd_ret[i] == ' ')
4151 {
you.chen35020192022-05-06 11:30:57 +08004152 lynq_cmd_ret[i] = '\0';
4153 break;
4154 }
4155 }
4156
4157 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004158 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004159
4160 int ret=pclose(fp);
4161 if(ret==-1)
4162 {
4163 perror("close file faild");
4164 }
4165
4166 return 0;
4167}
4168
qs.xiong44fac672023-08-29 16:15:55 +08004169
you.chen705a7ef2023-06-01 22:06:45 +08004170static int check_and_init_uci_config(char * country_code)
4171{
4172 FILE * fp;
4173 int is_different = 0;
4174 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4175 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4176 const char * commit_uci_cmd ="uci commit";
4177 char set_country_cmd[MAX_CMD];
4178 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004179
you.chen705a7ef2023-06-01 22:06:45 +08004180 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004181
you.chen705a7ef2023-06-01 22:06:45 +08004182 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004183 {
you.chen705a7ef2023-06-01 22:06:45 +08004184 if (0 != system(create_uci_cmd))
4185 {
4186 RLOGE("creat_uci_cmd fail");
4187 return -1;
4188 }
4189 is_different = 1;
4190 }
4191
4192 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4193 {
4194 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004195 return -1;
4196 }
4197
you.chen705a7ef2023-06-01 22:06:45 +08004198 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004199 {
you.chen705a7ef2023-06-01 22:06:45 +08004200 RLOGE("fread fail!");
4201 fclose(fp);
4202 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004203 }
4204
you.chen705a7ef2023-06-01 22:06:45 +08004205 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4206 {
qs.xiong44fac672023-08-29 16:15:55 +08004207 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 +08004208 is_different = 1;
4209 }
4210
4211 fclose(fp);
4212
4213 if (is_different)
4214 {
4215 if ( 0 != system(set_country_cmd))
4216 {
4217 RLOGE("set_country_cmd fail");
4218 return -1;
4219 }
4220 if (0 != system(commit_uci_cmd))
4221 {
4222 RLOGE("commmit fail");
4223 }
4224 }
4225
4226 return is_different;
4227}
4228
4229int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4230 char check_current_code[10];
4231 const char * support_country[] = {"CN", "EU"};
4232
4233 int ret,is_different, i, cc_count;
4234
4235 if (country_code == NULL || country_code[0] == '\0')
4236 {
4237 RLOGE("bad country code\n");
4238 return -1;
4239 }
4240
4241 cc_count = sizeof (support_country) / sizeof (char*);
4242 for(i=0; i < cc_count; i++)
4243 {
4244 if (strcmp(support_country[i], country_code) == 0)
4245 {
4246 break;
4247 }
4248 }
4249
4250 if (i >= cc_count)
4251 {
4252 RLOGE("unspported country code %s\n", country_code);
4253 return -1;
4254 }
4255
4256 is_different = check_and_init_uci_config(country_code);
4257 if( is_different < 0 )
4258 {
4259 RLOGE("init set uci fail\n");
4260 return -1;
4261 }
4262
4263 ret = lynq_get_country_code(idx,check_current_code);
4264 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4265 {
4266 ret = lynq_wifi_disable();
4267 if(ret != 0 )
4268 {
4269 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4270 return -1;
4271 }
4272 }
4273
4274 return 0;
you.chen35020192022-05-06 11:30:57 +08004275}
4276
4277int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4278{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004279 RLOGD("enter lynq_get_connect_ap_mac\n");
4280 if (mac == NULL)
4281 {
4282 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004283 return -1;
4284 }
4285
4286 CHECK_IDX(idx, CTRL_STA);
4287 ap_info_s ap;
4288 ap.ap_mac[0] = '\0';
4289
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4291 {
you.chen35020192022-05-06 11:30:57 +08004292 return -1;
4293 }
4294 strcpy(mac, ap.ap_mac);
4295
4296 return 0;
4297}
4298
4299int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4300{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004301 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004302 struct ifaddrs *ifaddr_header, *ifaddr;
4303 struct in_addr * ifa;
4304 const char * ifaName = "wlan0";
4305 if (ip == NULL)
4306 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004307 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004308 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004309 }
you.chenf58b3c92022-06-21 16:53:48 +08004310
qs.xiong9fbf74e2023-03-28 13:38:22 +08004311 if (idx == 1)
4312 {
you.chen0df3e7e2023-05-10 15:56:26 +08004313 ifaName = inner_get_ap_interface_name();
4314 if (ifaName == NULL)
4315 {
4316 RLOGE("[lynq_get_interface_ip] ap name get fail");
4317 return -1;
4318 }
you.chen9ac66392022-08-06 17:01:16 +08004319 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004320 else if (idx != 0)
4321 {
you.chen35020192022-05-06 11:30:57 +08004322 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004323 }
you.chen35020192022-05-06 11:30:57 +08004324
you.chen9ac66392022-08-06 17:01:16 +08004325 if (getifaddrs(&ifaddr_header) == -1)
4326 {
you.chen35020192022-05-06 11:30:57 +08004327 perror("getifaddrs");
4328 return -1;
4329 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004330 }
you.chen35020192022-05-06 11:30:57 +08004331
4332
you.chen9ac66392022-08-06 17:01:16 +08004333 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4334 {
4335 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004336 continue;
you.chen9ac66392022-08-06 17:01:16 +08004337 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4338 {
4339 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4340 {
4341 // is a valid IP4 Address
4342 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4343 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004344 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004345 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004346 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004347 return 0;
4348 }
4349 }
4350 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004351 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004352 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004353 return -1;
you.chen35020192022-05-06 11:30:57 +08004354}
4355
4356int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4357{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004358 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004359 int count;
4360 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004361 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004362 char *split_words[128] = {0};
4363 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4364
4365 CHECK_WPA_CTRL(idx);
4366
4367 DO_REQUEST(lynq_get_mac_cmd);
4368
qs.xiong9fbf74e2023-03-28 13:38:22 +08004369 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4370 {
4371 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004372 return -1;
4373 }
4374
4375 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4376
qs.xiong9fbf74e2023-03-28 13:38:22 +08004377 if (count < 2)
4378 {
you.chen35020192022-05-06 11:30:57 +08004379 return -1;
4380 }
4381
qs.xiong9fbf74e2023-03-28 13:38:22 +08004382 for (i=0; i < strlen(split_words[1]); i++ )
4383 {
4384 if (split_words[1][i] != ' ')
4385 {
you.chen35020192022-05-06 11:30:57 +08004386 break;
4387 }
4388 }
4389
qs.xiongdd6e44c2023-08-08 15:02:53 +08004390 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004391
4392 return 0;
4393}
4394
4395int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4396{
4397// int count;
4398// char *split_words[128] = {0};
4399// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4400
4401// if (rssi == NULL) {
4402// return -1;
4403// }
4404
4405// CHECK_IDX(idx, CTRL_STA);
4406
4407// CHECK_WPA_CTRL(CTRL_STA);
4408
4409// DO_REQUEST(lynq_get_rssi_cmd);
4410
4411// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4412// return -1;
4413// }
4414
4415// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4416
4417// if (count < 2) {
4418// return -1;
4419// }
4420
4421// *rssi = atoi(split_words[1]) * -1;
4422
you.chen35020192022-05-06 11:30:57 +08004423 char lynq_cmd_ret[MAX_RET]={0};
4424
qs.xiongff0ae0f2022-10-11 15:47:14 +08004425/*******change other cmd to get rssi*******
4426 *
4427 *wl rssi ---> wl -i wlan0 rssi
4428 *
4429 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004430 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004431 {
you.chen23c4a5f2023-04-12 16:46:00 +08004432 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004433 return -1;
4434 }
you.chen9f17e4d2022-06-06 17:18:18 +08004435 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004436/****** if got rssi is 0,means sta didn't connected any device****/
4437 if(*rssi == 0)
4438 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004439 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004440 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004441 }
you.chen35020192022-05-06 11:30:57 +08004442
4443 return 0;
4444}
4445
4446int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4447{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004448 RLOGD("enter lynq_get_connect_ap_band\n");
4449 if (band == NULL)
4450 {
you.chen35020192022-05-06 11:30:57 +08004451 return -1;
4452 }
4453
4454 CHECK_IDX(idx, CTRL_STA);
4455 ap_info_s ap;
4456 ap.band = -1;
4457
qs.xiong9fbf74e2023-03-28 13:38:22 +08004458 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4459 {
you.chen35020192022-05-06 11:30:57 +08004460 return -1;
4461 }
4462 *band = ap.band;
4463
4464 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004465}
you.chenf58b3c92022-06-21 16:53:48 +08004466
4467int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4468{
you.chenb95401e2023-05-12 19:39:06 +08004469 int ret;
4470 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004471 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004472
4473 if (ip == NULL)
4474 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004475 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004476 return -1;
4477 }
4478
4479 CHECK_IDX(idx, CTRL_STA);
4480
qs.xionge4cbf1c2023-02-28 18:22:49 +08004481 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004482 {
4483 return -1;
4484 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004485
you.chenb95401e2023-05-12 19:39:06 +08004486 ip[0] = '\0';
4487 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4488 if (ret != 0)
4489 {
4490 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4491 return -1;
4492 }
4493
4494 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4495 {
4496 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004497 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004498 if (ret != 0)
4499 {
4500 ip[0] = '\0';
4501 return 0;
4502 }
4503 else
4504 {
4505 p = strchr(ip, '\n');
4506 if (p != NULL)
4507 {
4508 *p = '\0';
4509 }
4510 }
4511 }
4512 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004513}
4514
qs.xiong026c5c72022-10-17 11:15:45 +08004515int lynq_ap_connect_num(int sta_number)
4516{
4517 char lynq_limit_cmd[32]={0};
4518 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004519 if((sta_number < 1 ) && (sta_number > 15))
4520 {
4521 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004522 return -1;
4523 }
4524 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4525 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004526 if(ret != 0)
4527 {
4528 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004529 }
4530 return 0;
4531}
you.chenf58b3c92022-06-21 16:53:48 +08004532
qs.xiong77905552022-10-17 11:19:57 +08004533int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4534{
4535
4536 char lynq_wifi_acs_cmd[128]={0};
4537 char lynq_cmd_mode[128]={0};
4538 char lynq_cmd_slect[128]={0};
4539
qs.xiong9fbf74e2023-03-28 13:38:22 +08004540 if((acs_mode != 2) && (acs_mode != 5))
4541 {
qs.xiong77905552022-10-17 11:19:57 +08004542 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4543 }
4544
qs.xiong9fbf74e2023-03-28 13:38:22 +08004545 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4546 {
qs.xiong77905552022-10-17 11:19:57 +08004547 return -1;
4548 }
4549
4550 CHECK_IDX(idx, CTRL_AP);
4551
4552 CHECK_WPA_CTRL(CTRL_AP);
4553
4554 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4555 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4556 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4557
4558 DO_OK_FAIL_REQUEST(cmd_disconnect);
4559 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4560 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4561 DO_OK_FAIL_REQUEST(cmd_save_config);
4562 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4563
4564 return 0;
4565}
you.chen0f5c6432022-11-07 18:31:14 +08004566//you.chen add for tv-box start
4567static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4568 FILE *fp;
4569 //printf("to exec cmd:%s\n", str_cmd);
4570 if((fp=popen(str_cmd,"r"))==NULL)
4571 {
4572 perror("popen error!");
4573 return -1;
4574 }
4575 if((fread(str_cmd_ret,max_len,1,fp))<0)
4576 {
4577 perror("fread fail!");
4578 fclose(fp);
4579 return -1;
4580 }
4581 fclose(fp);
4582 return 0;
4583}
4584
4585static int get_netmask_length(const char* mask)
4586{
4587 int masklen=0, i=0;
4588 int netmask=0;
4589
4590 if(mask == NULL)
4591 {
4592 return 0;
4593 }
4594
4595 struct in_addr ip_addr;
4596 if( inet_aton(mask, &ip_addr) )
4597 {
4598 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004599 }else
4600 {
you.chen0f5c6432022-11-07 18:31:14 +08004601 netmask = 0;
4602 return 0;
4603 }
4604
4605 while(0 == (netmask & 0x01) && i<32)
4606 {
4607 i++;
4608 netmask = netmask>>1;
4609 }
4610 masklen = 32-i;
4611 return masklen;
4612}
4613
4614static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4615 int mask_len;
4616 char *p;
4617 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004618 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4619 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004620 return -1;
4621 p = strstr(str_cmd_ret, "Mask:");
4622 if (p == NULL)
4623 return -1;
4624 mask_len = get_netmask_length(p + 5);
4625 if (mask_len == 0)
4626 return -1;
4627 p = strstr(str_cmd_ret, "inet addr:");
4628 if (p == NULL)
4629 return -1;
4630 strcpy(tmp, p + 10);
4631 p = strstr(tmp, " ");
4632 if (p != NULL)
4633 *p = '\0';
4634 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4635 return 0;
4636}
4637
4638static void GBWWatchThreadProc() {
4639 int i,n, nloop, nmax, ncheckcount, nidlecount;
4640 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4641 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4642 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4643 char *results[16] = {0};
4644 char str_cmd[256] = {0};
4645 char str_cmd_ret[128] = {0};
4646 char dest_ip[32] = {0};
4647 lastAP1Bytes = lastAP2Bytes = 0;
4648 lastAP1Drop = lastAP2Drop = 0;
4649 lastAP1Speed = lastAP2Speed = 0;
4650 setAP1Speed = 50;
4651 setAP2Speed = 80;
4652 nloop = 0;
4653 nmax = 6;
4654 ncheckcount = nidlecount = 0;
4655
you.chen0df3e7e2023-05-10 15:56:26 +08004656 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004657 {
4658 RLOGE("------gbw thread run\n");
4659 return;
4660 }
4661
qs.xiong9fbf74e2023-03-28 13:38:22 +08004662 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004663 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4664 while (dest_ip[0] == '\0') {
4665 sleep(1);
4666 str_cmd_ret[0] = '\0';
4667 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4668 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4669 if (str_cmd_ret[n] == '\n'){
4670 str_cmd_ret[n] = '\0';
4671 break;
4672 }
4673 }
4674 if (str_cmd_ret[0] != '\0')
4675 {
4676 strcpy(dest_ip, str_cmd_ret);
4677 }
4678 }
4679
you.chenc9928582023-04-24 15:39:37 +08004680 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4681 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4682 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 +08004683 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4684 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004685 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004686 return;
4687 }
you.chenc9928582023-04-24 15:39:37 +08004688 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);
4689 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);
4690 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 +08004691
4692 while (1) {
4693 sleep(1);
4694 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004695 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4696 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4697 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004698 continue;
4699 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004700 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004701 if (n > 9) {
4702 if (strcmp(results[1], "Sent") == 0) {
4703 currAP1Bytes = atoll(results[2]);
4704 }
4705 if (strcmp(results[6], "(dropped") == 0) {
4706 currAP1Drop = atoi(results[7]);
4707 }
4708 }
4709
4710 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004711 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4712 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4713 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004714 continue;
4715 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004716 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004717 if (n > 9) {
4718 if (strcmp(results[1], "Sent") == 0) {
4719 currAP2Bytes = atoll(results[2]);
4720 }
4721 if (strcmp(results[6], "(dropped") == 0) {
4722 currAP2Drop = atoi(results[7]);
4723 }
4724 }
4725
4726 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4727 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4728 lastAP1Bytes = currAP1Bytes;
4729 lastAP2Bytes = currAP2Bytes;
4730 continue;
4731 }
4732
4733 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4734 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4735 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4736 lastAP1Speed = currAP1Speed;
4737 lastAP2Speed = currAP2Speed;
4738 lastAP1Bytes = currAP1Bytes;
4739 lastAP2Bytes = currAP2Bytes;
4740
4741 currSetAP1Speed = setAP1Speed;
4742 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4743 ncheckcount++;
4744 if (ncheckcount > 3) {
4745 ncheckcount = 0;
4746 currSetAP1Speed = 5;
4747 }
4748 }
4749 else {
4750 ncheckcount = 0;
4751 if (currAP1Speed < 5)
4752 nidlecount++;
4753 else
4754 nidlecount = 0;
4755
4756 }
4757
4758 if (nidlecount > 60 ){
4759 currSetAP1Speed = 50;
4760 }
4761
4762 if (currSetAP1Speed != setAP1Speed) {
4763 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004764 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4765 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004766 }
4767 }
4768}
4769
4770int enableGBW(const char* mac) {
4771 int i,len;
4772 char get_ipaddr_cmd[128]={0};
4773 ap_info_s *ap;
4774 device_info_s * list;
4775
4776 if (mac == NULL || g_gbw_enabled == 1)
4777 return -1;
4778 len = strlen(mac);
4779 g_gbw_mac = malloc(len + 1);
4780 for(i=0;i<len;i++) {
4781 if (mac[i] >= 'A' && mac[i] <= 'Z')
4782 {
4783 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4784 }
4785 else
4786 g_gbw_mac[i] = mac[i];
4787 }
4788 g_gbw_mac[i] = '\0';
4789 g_gbw_enabled = 1;
4790
4791 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4792 if (system(get_ipaddr_cmd) == 0) {
4793 //startGBW();
4794 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4795 for (i=0;i<len;i++) {
4796 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4797 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4798 startGBW();
4799 }
4800 free(ap);
4801 free(list);
4802 }
4803 }
4804 return 0;
4805}
4806
4807int disableGBW() {
4808 stopGBW();
4809 free(g_gbw_mac);
4810 g_gbw_mac = NULL;
4811 g_gbw_enabled = 1;
4812 return 0;
4813}
4814
4815static int startGBW() {
4816 if (g_gbw_watcher_pid != 0) {
4817 stopGBW();
4818 }
4819 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4820}
4821
4822static int stopGBW() {
4823 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004824 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004825 pthread_cancel(g_gbw_watcher_pid);
4826 pthread_join(g_gbw_watcher_pid, &retval);
4827 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004828 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4829 if (s_ap_iterface_name[0] != '\0')
4830 {
4831 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4832 system(cmd);
4833 }
you.chen0f5c6432022-11-07 18:31:14 +08004834}
4835//you.chen add for tv-box end