blob: 39ab92d0d0591adf18e2291950ff2b21fc5e8c1b [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.xiong08e6aac2023-09-06 23:12:27 +0800737 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800738 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800739 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800740 {
741 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
742 {
743 system("wl down");
744 system("wl up");
745 }
qs.xiong44fac672023-08-29 16:15:55 +0800746 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800747 }
748 if ( i == delytime )
749 {
qs.xiong44fac672023-08-29 16:15:55 +0800750 i = 0;
751 }
752 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
753 {
754 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
755 return;
756 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800757
qs.xiong44fac672023-08-29 16:15:55 +0800758 }
759
760}
761
qs.xiongf0128b12023-06-29 17:29:39 +0800762static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
763{
764 char LYNQ_DISABLE_CMD[128]={0};
765
766 CHECK_IDX(idx, CTRL_STA);
767 CHECK_WPA_CTRL(CTRL_STA);
768
769 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
770 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
771 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800772 DO_OK_FAIL_REQUEST(cmd_save_config);
773
qs.xiongf0128b12023-06-29 17:29:39 +0800774 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800775
qs.xiongf0128b12023-06-29 17:29:39 +0800776}
777
778
qs.xiong455c30b2023-04-12 11:40:02 +0800779void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
780{
781 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800782 char *wpanetid;
783 char destid[3] = {0};
784 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800785 *error = LYNQ_WAIT_CONNECT_ACTIVE;
786 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
787 {
788 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
789 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
790 return;
791 }
792
qs.xiong20202422023-09-06 18:01:18 +0800793 if (strstr(modify, "Trying to associate") != NULL)
794 {
795 RLOGD("Current sta is Trying to associate");
796 *state = LYNQ_WIFI_STATUS_EGNORE;
797 g_sta_conncet_status_flag = 1;
798 return;
799 }
800
qs.xiong455c30b2023-04-12 11:40:02 +0800801 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
802 {
803 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
804 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800805 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800806 return;
807 }
808
809 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
810 {
qs.xiongf0128b12023-06-29 17:29:39 +0800811 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
812 wpanetid = strstr(modify,"id=");
813 if ( wpanetid != NULL )
814 {
815 wpanetid +=strlen("id=");
816 memcpy(destid,wpanetid,2);
817 tmpdisid = atoi(destid);
818
819 }
qs.xiong455c30b2023-04-12 11:40:02 +0800820 pReason = strstr(modify, "reason=");
821 if (pReason != NULL)
822 {
823 pReason += strlen("reason=");
824 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
825 {
826 *error = LYNQ_TIME_OUT;
827 }
828 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
829 {
830 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800831 // tmp fix sta autoconnect connect and disconnect
832 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
833 {
834 RLOGE("stop wlan0 network %d fail",tmpdisid);
835 }
qs.xiong455c30b2023-04-12 11:40:02 +0800836 }
837 else
838 {
839 *error = LYNQ_UNSPECIFIED_REASON;
840 }
qs.xiong455c30b2023-04-12 11:40:02 +0800841 }
842 else
843 {
844 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800845 }
qs.xiongf0128b12023-06-29 17:29:39 +0800846 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800847 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800848 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800849
850 }
851
852 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
853 {
854 *error = LYNQ_NOT_FIND_AP;
855 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
856 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800857 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800858 return;
859 }
860
861
862 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
863 {
864 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
865 pReason = strstr(modify, "status_code=");
866 if (pReason != NULL)
867 {
868 pReason += strlen("status_code=");
869 if (memcmp(pReason, "17", 2) == 0)
870 {
871 *error = LYNQ_AP_UNABLE_HANDLE;
872 }
873 else if (memcmp(pReason, "1",1) == 0)
874 {
875 *error = LYNQ_UNSPECIFIED_REASON;
876 }
877 else
878 {
879 *error = LYNQ_UNSPECIFIED_REASON;
880 }
881
882 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
883 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 +0800884 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800885 return;
886 }
887 else
888 {
889 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
890 *error = LYNQ_UNSPECIFIED_REASON;
891 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
892 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
893 return;
894 }
895 }
896
897 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
898 {
899 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
900 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
901 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
902 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
903 return;
904 }
905
906 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
907 {
908 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
909 *error = LYNQ_WAIT_CONNECT_ACTIVE;
910 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
911 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
912 return;
913 }
914
you.chen32cb31e2023-04-13 14:05:45 +0800915 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800916 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800917 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800918 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
919 return;
920
921}
922
qs.xiongfcc914b2023-07-06 21:16:20 +0800923void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
924{
925 char *pReason;
926 char *wpanetid;
927 char destid[3];
928 *error = LYNQ_WAIT_CONNECT_ACTIVE;
929 *networkid = -1;
930 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
931 {
932 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
933 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
934 return;
935 }
936 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
937 {
938 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
939 wpanetid = strstr(modify,"id=");
940 if ( wpanetid != NULL )
941 {
942 wpanetid +=strlen("id=");
943 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
944 if (memcpy(destid,wpanetid,2) != NULL)
945 {
946 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
947 *networkid = atoi(destid);
948 RLOGD("get networkid is %d\n",*networkid);
949 }
950 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
951 }
952 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
953 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
954 return;
955 }
956 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
957 {
958 wpanetid = strstr(modify,"id=");
959 if ( wpanetid != NULL )
960 {
961 wpanetid +=strlen("id=");
962 if (memcpy(destid,wpanetid,2) != NULL)
963 {
964 *networkid = atoi(destid);
965 RLOGD("get networkid is %d\n",*networkid);
966 }
967 }
968 pReason = strstr(modify, "reason=");
969 if (pReason != NULL)
970 {
971 pReason += strlen("reason=");
972 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
973 {
974 *error = LYNQ_TIME_OUT;
975 }
976 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
977 {
978 *error = LYNQ_PSW_ERROR;
979 }
980 else
981 {
982 *error = LYNQ_UNSPECIFIED_REASON;
983 }
984 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
985 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
986 return;
987 }
988 else
989 {
990 *error = LYNQ_UNSPECIFIED_REASON;
991 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
992 return;
993 }
994 }
995 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
996 {
997 wpanetid = strstr(modify,"id=");
998 if ( wpanetid != NULL )
999 {
1000 wpanetid +=strlen("id=");
1001 if (memcpy(destid,wpanetid,2) != NULL)
1002 {
1003 *networkid = atoi(destid);
1004 RLOGD("get networkid is %d\n",*networkid);
1005 }
1006 }
1007 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1008 pReason = strstr(modify, "status_code=");
1009 if (pReason != NULL)
1010 {
1011 pReason += strlen("status_code=");
1012 if (memcmp(pReason, "17", 2) == 0)
1013 {
1014 *error = LYNQ_AP_UNABLE_HANDLE;
1015 }
1016 else if (memcmp(pReason, "1",1) == 0)
1017 {
1018 *error = LYNQ_UNSPECIFIED_REASON;
1019 }
1020 else
1021 {
1022 *error = LYNQ_UNSPECIFIED_REASON;
1023 }
1024 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1025 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1026 return;
1027 }
1028 else
1029 {
1030 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1031 *error = LYNQ_UNSPECIFIED_REASON;
1032 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001033 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001034 return;
1035 }
1036 }
1037 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1038 {
1039 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1040 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1041 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1042 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1043 return;
1044 }
1045 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1046 {
1047 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1048 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1049 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1050 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1051 return;
1052 }
1053 RLOGD("EVENT : %s\n", modify);
1054 *error = LYNQ_UNSPECIFIED_REASON;
1055 *state = LYNQ_WIFI_STATUS_EGNORE;
1056 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1057 return;
1058}
you.chen70f377f2023-04-14 18:17:09 +08001059static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1060{
you.chen6d247052023-06-01 16:39:54 +08001061 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001062 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1063 {
1064 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1065 g_sta_callback_func(g_sta_callback_priv, state, error);
1066 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1067 }
you.chen6d247052023-06-01 16:39:54 +08001068 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001069}
qs.xiongfcc914b2023-07-06 21:16:20 +08001070static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1071{
1072 pthread_mutex_lock(&s_sta_callback_mutex);
1073 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1074 {
1075 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1076 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1077 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1078 }
1079 pthread_mutex_unlock(&s_sta_callback_mutex);
1080}
you.chen70f377f2023-04-14 18:17:09 +08001081
you.chen35020192022-05-06 11:30:57 +08001082static void STAWatcherThreadProc() {
1083 size_t len = MAX_RET;
1084 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001085 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001086 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001087 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001088
you.chen6c2dd9c2022-05-16 17:55:28 +08001089 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001090 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001091
you.chen70f377f2023-04-14 18:17:09 +08001092 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001093 while (g_sta_watcher_stop_flag == 0)
1094 {
you.chenf711c8a2023-04-13 13:49:45 +08001095 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001096 {
you.chen35020192022-05-06 11:30:57 +08001097 continue;
1098 }
you.chenf711c8a2023-04-13 13:49:45 +08001099
you.chen6c2dd9c2022-05-16 17:55:28 +08001100 memset(msg_notify, 0, MAX_RET);
1101 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001102 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001103 {
you.chen35020192022-05-06 11:30:57 +08001104 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001105 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1106 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001107 {
you.chen35020192022-05-06 11:30:57 +08001108 g_sta_scan_finish_flag = 1;
1109 }
1110
qs.xiong9fbf74e2023-03-28 13:38:22 +08001111 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001112 {
you.chen35020192022-05-06 11:30:57 +08001113 continue;
1114 }
qs.xiong455c30b2023-04-12 11:40:02 +08001115 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001116 notify_connect_status(state, error);
1117
1118 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001119 {
you.chen70f377f2023-04-14 18:17:09 +08001120 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001121 if (last_state != state)
1122 {
1123 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1124 {
1125 system_call_v("%s %s", sta_status_change_script, "connect");
1126 }
1127 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1128 {
1129 system_call_v("%s %s", sta_status_change_script, "disconnect");
1130 }
1131 }
1132
1133 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001134 }
you.chen35020192022-05-06 11:30:57 +08001135 }
1136 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001137 if (lynq_wpa_ctrl != NULL)
1138 {
you.chen92fd5d32022-05-25 10:09:47 +08001139 wpa_ctrl_detach(lynq_wpa_ctrl);
1140 wpa_ctrl_close(lynq_wpa_ctrl);
1141 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001142}
qs.xiongfcc914b2023-07-06 21:16:20 +08001143static void STAAutoWatcherThreadProc() {
1144 size_t len = MAX_RET;
1145 char msg_notify[MAX_RET];
1146 error_number_s error;
1147 lynq_wifi_sta_status_s state, last_state = -1;
1148 int idle_count = 0;
1149 int networkid;
1150 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1151 g_sta_auto_watcher_stop_flag = 0;
1152 RLOGD("STAAutoWatcherThreadProc thread started ------");
1153 while (g_sta_auto_watcher_stop_flag == 0)
1154 {
1155 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1156 {
1157 continue;
1158 }
1159 memset(msg_notify, 0, MAX_RET);
1160 len = MAX_RET;
1161 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1162 {
1163 msg_notify[len+1] = '\0';
1164 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1165 if (strstr(msg_notify, state_scan_result) != NULL)
1166 {
1167 g_sta_auto_scan_finish_flag = 1;
1168 }
1169 if (g_sta_auto_callback_func == NULL)
1170 {
1171 continue;
1172 }
1173 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1174 notify_auto_connect_status(state, error,networkid);
1175 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1176 {
1177 inner_check_connect_error(msg_notify, state, error);
1178 if (last_state != state)
1179 {
1180 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1181 {
1182 system_call_v("%s %s", sta_status_change_script, "connect");
1183 }
1184 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1185 {
1186 system_call_v("%s %s", sta_status_change_script, "disconnect");
1187 }
1188 }
1189 last_state = state;
1190 }
1191 }
1192 }
1193 if (lynq_wpa_ctrl != NULL)
1194 {
1195 wpa_ctrl_detach(lynq_wpa_ctrl);
1196 wpa_ctrl_close(lynq_wpa_ctrl);
1197 }
1198}
qs.xiongf1b525b2022-03-31 00:58:23 -04001199
you.chen70f377f2023-04-14 18:17:09 +08001200// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1201static void GlobalWatcherThreadProc()
1202{
1203 int ret, connect_timeout, service_abnormal;
1204 error_number_s error_num = -1;
1205 inner_sta_status_s sta_status;
1206 scan_info_s *scan_list = NULL;
1207 int i, scan_len=0;
1208 char connecting_ssid[64];
1209 struct timeval now;
1210
1211 RLOGD("GlobalWatcherThreadProc start to run");
1212
1213 while (1)
1214 {
1215 pthread_mutex_lock(&s_global_check_mutex);
1216 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1217 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1218 {
1219 pthread_mutex_unlock(&s_global_check_mutex);
1220 usleep(50*1000);
1221 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1222 continue;
1223 }
1224
1225 connect_timeout = 0;
1226 service_abnormal = 0;
1227 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1228 {
1229 while (1)
1230 {
1231 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1232 if (ret == ETIME)
1233 {
1234 connect_timeout = 1;
1235 }
1236 else if (ret != 0)
1237 {
1238 gettimeofday(&now,NULL);
1239 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1240 {
1241 usleep(SLEEP_TIME_ON_IDLE);
1242 continue;
1243 }
1244 connect_timeout = 1;
1245 }
1246 sta_status = s_sta_status;
1247 error_num = s_sta_error_number;
1248 s_sta_status = INNER_STA_STATUS_INIT;
1249 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1250 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1251 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1252 break;
1253 }
1254 }
1255 if (s_service_invoke_timeout_cnt > 10)
1256 {
1257 service_abnormal = 1;
1258 s_service_invoke_timeout_cnt = 0;
1259 }
1260 pthread_mutex_unlock(&s_global_check_mutex);
1261
1262 if (service_abnormal == 1)
1263 {
1264 sleep(1);
1265 RLOGE("wpa service is abnormal info app to exit");
1266 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001267
1268 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1269
you.chen70f377f2023-04-14 18:17:09 +08001270 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1271 }
1272
1273 if (sta_status == INNER_STA_STATUS_CANCEL)
1274 {
1275 continue;
1276 }
1277 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1278 {
1279 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1280 }
1281 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1282 {
1283 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1284 {
1285 for(i=0; i < scan_len;i++)
1286 {
1287 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1288 {
1289 error_num = LYNQ_AUTH_ERROR;
1290 break;
1291 }
1292 }
1293 free(scan_list);
1294 }
1295 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1296 }
1297 else if (connect_timeout == 0)
1298 {
1299 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1300 }
1301 else // wait timeout
1302 {
1303 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1304 {
1305 ; // wpa service abnormal
1306 }
1307 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1308 {
1309 RLOGD("GlobalWatcherThreadProc notify connected");
1310 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1311 }
1312 else
1313 {
1314 RLOGD("GlobalWatcherThreadProc notify timeout");
1315 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1316 }
1317 }
1318 } // while (1)
1319}
1320
qs.xiong1af5daf2022-03-14 09:12:12 -04001321int lynq_wifi_enable(void)
1322{
you.chen35020192022-05-06 11:30:57 +08001323 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001324 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001325 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001326 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1327
qs.xiong9fbf74e2023-03-28 13:38:22 +08001328 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1329 {
you.chend2fef3f2023-02-13 10:50:35 +08001330 goto out_enable;
1331 }
1332
you.chenc9928582023-04-24 15:39:37 +08001333 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001334 if (ret != 0)
1335 {
1336 //printf("service state %d\n", ret);
1337 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001338 ret = -1;
1339 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001340 }
lhfe8da902022-10-11 18:55:36 +08001341
you.chen70f377f2023-04-14 18:17:09 +08001342 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1343 {
1344 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1345 if(ret<0)
1346 {
1347 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1348 ret = -1;
1349 goto out_enable;
1350 }
1351 }
1352
you.chend2fef3f2023-02-13 10:50:35 +08001353 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1354 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1355 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1356 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1357out_enable:
1358 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001359 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001360}
1361
qs.xiong1af5daf2022-03-14 09:12:12 -04001362int lynq_wifi_disable(void)
1363{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001364 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001365 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001366 g_ap_watcher_stop_flag = 1;
1367 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001368 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001369 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001370 if (g_ap_watcher_pid != 0)
1371 pthread_join(g_ap_watcher_pid, NULL);
1372 if (g_sta_watcher_pid != 0)
1373 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001374 if (g_sta_auto_watcher_pid != 0)
1375 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001376 if (g_lynq_wpa_ctrl[0] != NULL)
1377 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1378 if (g_lynq_wpa_ctrl[1] != NULL)
1379 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001380 if (g_ap_tmp_watcher_pid != 0)
1381 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001382 g_ap_watcher_pid = 0;
1383 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001384 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001385 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001386 g_lynq_wpa_ctrl[0] = NULL;
1387 g_lynq_wpa_ctrl[1] = NULL;
1388 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001389 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1390 return 0;
1391}
1392
1393static inline char inner_convert_char(char in)
1394{
1395 if (in >= '0' && in <= '9')
1396 {
1397 return in - '0';
1398 }
1399 else if (in >= 'a' && in <= 'f')
1400 {
1401 return in - 'a' + 10;
1402 }
1403 else if (in >= 'A' && in <= 'F')
1404 {
1405 return in - 'A' + 10;
1406 }
1407 else
1408 {
1409 return '\xff';
1410 }
1411}
1412
1413static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1414{
1415 char *p;
1416 size_t pos = 0;
1417 if (NULL == out_ssid)
1418 return;
1419 //printf("input ssid=[%s]\n", ssid);
1420 memset(out_ssid, 0, out_ssid_len);
1421 if (NULL == ssid)
1422 return;
1423 p = strchr(ssid, '\\');
1424 if (NULL == p)
1425 {
1426 strncpy(out_ssid, ssid, out_ssid_len);
1427 //printf(" first %s\n", out_ssid);
1428 }
1429 else
1430 {
1431 pos = p - ssid;
1432 memcpy(out_ssid, ssid, pos);
1433 //printf("pos %lu -- %s\n", pos, out_ssid);
1434 for(; pos < out_ssid_len; pos ++)
1435 {
1436 if (p[0] == '\0')
1437 {
1438 //printf(" out %s\n", out_ssid);
1439 return;
1440 }
1441 else if (p[0] != '\\')
1442 {
1443 out_ssid[pos] = p[0];
1444 p += 1;
1445 }
1446 else if (p[1] == 'x' || p[1] == 'X')
1447 {
1448 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1449 p += 4;
1450 }
1451 else if (p[1] == '\\')
1452 {
1453 out_ssid[pos] = '\\';
1454 p += 2;
1455 }
1456 else if (p[1] == 't')
1457 {
1458 out_ssid[pos] = '\t';
1459 p += 2;
1460 }
1461 else if (p[1] == 'r')
1462 {
1463 out_ssid[pos] = '\r';
1464 p += 2;
1465 }
1466 else if (p[1] == 'n')
1467 {
1468 out_ssid[pos] = '\n';
1469 p += 2;
1470 }//todo find a better way to convert?
1471 }
1472 }
1473 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001474}
qs.xiong1af5daf2022-03-14 09:12:12 -04001475
you.chen35020192022-05-06 11:30:57 +08001476static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001477 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001478 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001479 RLOGD("enter inner_get_param");
1480 if (out_put == NULL)
1481 {
1482 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001483 return -1;
1484 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001485 if (param_name == NULL)
1486 {
1487 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001488 return -1;
1489 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001490 if (param_name[0] == '\0')
1491 {
1492 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001493 return -1;
1494 }
1495
1496 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1497
1498 CHECK_WPA_CTRL(interface);
1499
1500 DO_REQUEST(lynq_cmd_get);
1501
qs.xiong9fbf74e2023-03-28 13:38:22 +08001502 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1503 {
1504 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001505 return -1;
1506 }
1507
you.chena6fa5b22022-05-18 10:28:19 +08001508// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001509 if (strcmp(param_name, "ssid") == 0)
1510 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001511 if (cmd_reply[0] == '\"')
1512 {
you.chend2fef3f2023-02-13 10:50:35 +08001513 ssid_len = reply_len - 1;
1514 memcpy(out_put, cmd_reply + 1, ssid_len);
1515 if (out_put[ssid_len-1] == '\"')
1516 {
1517 out_put[ssid_len-1] = '\0';
1518 }
1519 else
1520 {
1521 out_put[ssid_len] = '\0';
1522 }
1523 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001524 else
1525 {
you.chend2fef3f2023-02-13 10:50:35 +08001526 ssid_len = reply_len / 2;
1527 for(i=0; i<ssid_len; i++)
1528 {
1529 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1530 }
1531 out_put[ssid_len] = '\0';
1532 }
1533 }
1534 else
1535 {
1536 memcpy(out_put, cmd_reply, reply_len + 1);
1537 }
you.chen35020192022-05-06 11:30:57 +08001538 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001539}
qs.xiong1af5daf2022-03-14 09:12:12 -04001540
you.chen35020192022-05-06 11:30:57 +08001541static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1542 int ret = 0;
1543 char * end = str + len - 1;
1544 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001545 while(str < end)
1546 {
1547 if (*str == delimiter)
1548 {
you.chen35020192022-05-06 11:30:57 +08001549 *str++ = '\0';
1550 results[ret++] = str;
1551 continue;
1552 }
1553 str++;
1554 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001555 if (*str == delimiter)
1556 {
you.chen35020192022-05-06 11:30:57 +08001557 *str = '\0';
1558 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001559
you.chen6ed36a62023-04-27 17:51:56 +08001560 results[ret] = NULL;
1561
you.chen35020192022-05-06 11:30:57 +08001562 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001563}
1564
you.chend2fef3f2023-02-13 10:50:35 +08001565static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1566{
1567 char * p;
1568 int ret = 0;
1569 char cmd[256]={0};
1570 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001571 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001572 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001573 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001574 ret = exec_cmd(cmd, ip, ip_len);
1575 p = strchr(ip, '\n');
1576 if (NULL != p)
1577 {
1578 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001579 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001580 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001581 return ret;
1582}
1583
you.chend2fef3f2023-02-13 10:50:35 +08001584static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001585 struct in_addr addr ={0};
1586 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001587 char cmd[64] = {0};
1588 char * p;
1589 int ret;
you.chen35020192022-05-06 11:30:57 +08001590
qs.xiong9fbf74e2023-03-28 13:38:22 +08001591 if (ip == NULL || *ip == '\0' || hostname == NULL)
1592 {
1593 RLOGE("ip == NULL or hostname == NULL");
1594 return -1;
you.chen35020192022-05-06 11:30:57 +08001595 }
1596
you.chend2fef3f2023-02-13 10:50:35 +08001597 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001598 if (inet_aton(ip, &addr) == 0)
1599 {
you.chen35020192022-05-06 11:30:57 +08001600 printf("---inet_aton fail\n");
1601 return -1;
1602 }
1603
1604 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1605
qs.xiong9fbf74e2023-03-28 13:38:22 +08001606 if (ht == NULL)
1607 {
you.chen186d3c32023-05-18 14:19:46 +08001608 hostname[0] = '\0';
1609 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1610 ret = exec_cmd(cmd, hostname, 32);
1611 if (ret == 0)
1612 {
1613 p = strchr(hostname, '\n');
1614 if (p != NULL)
1615 {
1616 *p = '\0';
1617 }
1618 return 0;
1619 }
1620 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001621 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001622 herror(NULL);
1623 return -1;
1624 }
1625
1626 strcpy(hostname, ht->h_name);
1627
1628 return 0;
1629}
1630
1631static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1632{
1633 int count, index, words_count;
1634 char * split_lines[128]= {0};
1635 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001636 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001637 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001638 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001639
1640 CHECK_WPA_CTRL(ap_sta);
1641
1642 DO_REQUEST(lynq_wifi_list_networks);
1643
1644 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1645
1646 //@todo check ssid field to compatible
1647
1648 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001649 for(index=1; index < count; index++)
1650 {
you.chen35020192022-05-06 11:30:57 +08001651 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001652 if (words_count > 2)
1653 {
you.chend2fef3f2023-02-13 10:50:35 +08001654 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001655 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1656 {
you.chen35020192022-05-06 11:30:57 +08001657 net_no_list[ret++] = atoi(split_words[0]);
1658 }
1659 }
1660 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001661 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001662 return ret;
1663}
1664
1665static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001666 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001667 CHECK_WPA_CTRL(ap_sta);
1668 const char *lynq_wifi_add_network = "ADD_NETWORK";
1669
qs.xiong9fbf74e2023-03-28 13:38:22 +08001670 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001671 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001672 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1673 {
1674 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001675 return -1;
1676 }
1677
qs.xiong9fbf74e2023-03-28 13:38:22 +08001678 for(i=0;i<reply_len;i++)
1679 {
1680 if(cmd_reply[i] == '\n')
1681 {
you.chen35020192022-05-06 11:30:57 +08001682 cmd_reply[i] = '\0';
1683 break;
1684 }
1685 }
1686 return atoi(cmd_reply);
1687}
you.chena6cd55a2022-05-08 12:20:18 +08001688
you.chen35020192022-05-06 11:30:57 +08001689static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1690{
1691 int count, index;
1692 int net_no_list[128];
1693
qs.xiong9fbf74e2023-03-28 13:38:22 +08001694 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001695 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001696 for (index=0; index < count; index++)
1697 {
1698 if (net_no_list[index] == net_no)
1699 {
you.chen35020192022-05-06 11:30:57 +08001700 return 0;
1701 }
1702 }
1703
1704 if (count >= 1)
1705 index = net_no_list[count - 1];
1706 else
1707 index = -1;
1708
qs.xiong9fbf74e2023-03-28 13:38:22 +08001709 while (index < net_no )
1710 {
you.chen35020192022-05-06 11:30:57 +08001711 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001712 if (index >= net_no)
1713 { // required network no created
1714 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001715 return 0;
1716 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001717 else if( index < 0)
1718 {
1719 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001720 return -1;
1721 }
you.chen35020192022-05-06 11:30:57 +08001722 }
1723
1724 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001725 {
1726 RLOGE("[lynq_check_network_number] network index < 0");
1727 return -1;
1728 }
1729 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001730 return 0;
1731}
1732
1733static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001734 if (freq > 5000 && freq < 6000)
1735 {
you.chen35020192022-05-06 11:30:57 +08001736 return LYNQ_WIFI_5G_band;
1737 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001738 else if (freq > 2000 && freq < 3000)
1739 {
you.chen35020192022-05-06 11:30:57 +08001740 return LYNQ_WIFI_2G_band;
1741 }
1742 return LYNQ_WIFI_2_and_5G_band;
1743}
1744
1745static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001746 if (key_mgmt != NULL)
1747 {
1748 if (memcmp( key_mgmt, "NONE", 4) == 0)
1749 {
you.chen35020192022-05-06 11:30:57 +08001750 return LYNQ_WIFI_AUTH_OPEN;
1751 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001752 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1753 {
you.chen35020192022-05-06 11:30:57 +08001754 return LYNQ_WIFI_AUTH_WEP;
1755 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1757 {
you.chen35020192022-05-06 11:30:57 +08001758 return LYNQ_WIFI_AUTH_WPA_PSK;
1759 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001760 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1761 {
you.chen35020192022-05-06 11:30:57 +08001762 return LYNQ_WIFI_AUTH_WPA2_PSK;
1763 }
1764 }
1765
1766 return -1;
1767}
1768
1769static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001770 if (flag != NULL)
1771 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001772 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 +08001773 {
1774 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001775 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001776 {
1777 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1778 }else if (strstr( flag, "WPA2-PSK") != NULL)
1779 {
you.chen35020192022-05-06 11:30:57 +08001780 return LYNQ_WIFI_AUTH_WPA2_PSK;
1781 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001782 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001783 {
you.chen35020192022-05-06 11:30:57 +08001784 return LYNQ_WIFI_AUTH_WPA_PSK;
1785 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001786 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001787 {
you.chen35020192022-05-06 11:30:57 +08001788 return LYNQ_WIFI_AUTH_WEP;
1789 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001790 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001791 {
you.chen35020192022-05-06 11:30:57 +08001792 return LYNQ_WIFI_AUTH_OPEN;
1793 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001794 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001795 {
you.chend2fef3f2023-02-13 10:50:35 +08001796 return LYNQ_WIFI_AUTH_OPEN;
1797 }
qs.xiong46f41562023-07-11 21:06:47 +08001798 else
1799 {
1800 RLOGD("convert_max_auth_from_flag not-found auth mode");
1801 }
you.chen35020192022-05-06 11:30:57 +08001802 }
1803
1804 return -1;
1805}
1806
1807static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1808 switch (bw) {
1809 case 10:
1810 return LYNQ_WIFI_BANDWIDTH_HT10;
1811 break;
1812 case 20:
1813 return LYNQ_WIFI_BANDWIDTH_HT20;
1814 break;
1815 case 40:
1816 return LYNQ_WIFI_BANDWIDTH_HT40;
1817 break;
1818 case 80:
1819 return LYNQ_WIFI_BANDWIDTH_HT80;
1820 break;
1821 default:
1822 break;
1823 }
1824
1825 return -1;
1826}
1827
you.chen70f377f2023-04-14 18:17:09 +08001828static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001829static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1830 int i, count;
1831 char *p;
1832 const char *lynq_status_cmd = "STATUS";
1833 const char * FLAG_SSID = "ssid=";
1834 const char * FLAG_SBSID = "bssid=";
1835 const char * FLAG_KEY_MGMT = "key_mgmt=";
1836 const char * FLAG_FREQ = "freq=";
1837 const char * FLAG_STATE = "wpa_state=";
1838 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001839 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001840 char *split_lines[128] = {0};
1841
1842 CHECK_WPA_CTRL(interface);
1843
qs.xiong9fbf74e2023-03-28 13:38:22 +08001844 if (curr_state == NULL)
1845 {
1846 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001847 return -1;
1848 }
1849
1850 DO_REQUEST(lynq_status_cmd);
1851
1852 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1853
1854 curr_state->net_no = -1;
1855 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 for(i=0; i < count; i++)
1857 {
1858 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001859 {
you.chen35020192022-05-06 11:30:57 +08001860 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001861 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001862 {
you.chend2fef3f2023-02-13 10:50:35 +08001863 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001864 ret = 0;
1865 continue;
1866 }
you.chenf58b3c92022-06-21 16:53:48 +08001867 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001868 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001869 {
you.chend2fef3f2023-02-13 10:50:35 +08001870 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 +08001871 ret = 0;
1872 continue;
1873 }
you.chen35020192022-05-06 11:30:57 +08001874 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001875 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001876 {
you.chen450d0172022-07-15 17:56:48 +08001877 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001879 ret = 0;
1880 continue;
1881 }
1882 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001883 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001884 {
you.chen35020192022-05-06 11:30:57 +08001885 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1886 ret = 0;
1887 continue;
1888 }
you.chend2fef3f2023-02-13 10:50:35 +08001889 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001890 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001891 {
you.chend2fef3f2023-02-13 10:50:35 +08001892 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1893 ret = 0;
1894 continue;
1895 }
you.chen35020192022-05-06 11:30:57 +08001896 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001897 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001898 {
you.chen35020192022-05-06 11:30:57 +08001899 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001900 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001901 {
you.chen35020192022-05-06 11:30:57 +08001902 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1903 ret = 0;
1904 continue;
1905 }
1906
1907 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001908 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001909 {
you.chen35020192022-05-06 11:30:57 +08001910 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001911 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001912 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001913 }
1914 }
1915
you.chen70f377f2023-04-14 18:17:09 +08001916 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1917 {
1918 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1919 }
1920
you.chen35020192022-05-06 11:30:57 +08001921 return ret;
1922}
1923
qs.xiongf1b525b2022-03-31 00:58:23 -04001924int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001925{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001926 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001927 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001928
qs.xiong9fbf74e2023-03-28 13:38:22 +08001929 if (ap_ssid == NULL)
1930 {
1931 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001932 return -1;
1933 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 else
1935 {
1936 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001937 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001938
qs.xiong9fbf74e2023-03-28 13:38:22 +08001939 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1940 {
1941 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001942 return -1;
1943 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001944
you.chen35020192022-05-06 11:30:57 +08001945 CHECK_IDX(idx, CTRL_AP);
1946
1947 CHECK_WPA_CTRL(CTRL_AP);
1948
1949 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1950
1951 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1952 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001953 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1954 return 0;
you.chen35020192022-05-06 11:30:57 +08001955
qs.xiong7a105ce2022-03-02 09:43:11 -05001956}
1957
you.chen35020192022-05-06 11:30:57 +08001958int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001959{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001960 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001961 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001962 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001963}
1964
qs.xiongc9c79f72022-10-17 15:27:18 +08001965/*****
1966 *frequency <------>channel
1967 *
1968 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1969 *
1970 *
1971 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1972 *
1973 *
1974 * */
1975static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001976 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};
1977 int i;
1978 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1979
qs.xiong69a332b2022-12-02 09:58:57 +08001980 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001981 {
1982 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001983 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001984 }
qs.xiongc00b6032022-11-29 16:28:03 +08001985
1986 if(i == arr_len)
1987 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001988 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001989 return -1;
1990 }
qs.xiongc00b6032022-11-29 16:28:03 +08001991
qs.xiongc9c79f72022-10-17 15:27:18 +08001992 return 0;
1993}
qs.xiong13673462023-02-21 19:12:54 +08001994
1995static int lynq_check_frequencyby_country_code(int input_frequency)
1996{
1997 char str_cnc[]="CN";
1998 char str_dest[20]="";
1999
2000 if( lynq_get_country_code(1,str_dest) != 0 )
2001 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002002 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002003 return -1;
2004 }
2005 if( strncmp(str_dest,str_cnc,2) != 0 )
2006 {
2007 return 0;
2008 }else if( 2473 < input_frequency && input_frequency < 5744)
2009 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002010 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002011 return -1;
2012 }
2013 return 0;
2014}
qs.xiongf1b525b2022-03-31 00:58:23 -04002015int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002016{
qs.xiongc00b6032022-11-29 16:28:03 +08002017 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002018 char lynq_wifi_frequency_cmd[128]={0};
2019 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002020 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002021 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002022 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002023 check = lynq_check_set_frequency(lynq_wifi_frequency);
2024 if(check != 0)
2025 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002026 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002027 return -1;
you.chen35020192022-05-06 11:30:57 +08002028 }
qs.xiong13673462023-02-21 19:12:54 +08002029 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2030 if(check != 0)
2031 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002032 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002033 return -1;
2034 }
2035
qs.xiongc00b6032022-11-29 16:28:03 +08002036 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2037 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002038 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002039 return -1;
2040 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002041
you.chen35020192022-05-06 11:30:57 +08002042 CHECK_IDX(idx, CTRL_AP);
2043
2044 CHECK_WPA_CTRL(CTRL_AP);
2045
2046 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2047 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2048 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2049
you.chen6c2dd9c2022-05-16 17:55:28 +08002050 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002051 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2052 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2053 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002054
qs.xiong9fbf74e2023-03-28 13:38:22 +08002055 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002056}
2057
qs.xiongf1b525b2022-03-31 00:58:23 -04002058int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002059{
you.chen35020192022-05-06 11:30:57 +08002060 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002061 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002062 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002063
qs.xiong9fbf74e2023-03-28 13:38:22 +08002064 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2065 {
2066 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002067 return -1;
2068 }
2069 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002070
qs.xiong9fbf74e2023-03-28 13:38:22 +08002071 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002072}
2073
qs.xiongf1b525b2022-03-31 00:58:23 -04002074int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2075{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002076 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002077 CHECK_IDX(idx, CTRL_AP);
2078 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002079 case LYNQ_WIFI_BANDWIDTH_HT10:
2080 {
2081 RLOGE("bandwith [%d] not support now\n", bandwidth);
2082 return -1;
2083 }
2084 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002085 {
2086 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2087 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002088 if (system(lynq_cmd_bandwith) != 0 )
2089 {
2090 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002091 return -1;
2092 }
2093 system("wl up");
2094 break;
2095 }
2096 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002097 {
qs.xiong10379192023-02-21 13:19:42 +08002098 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002099 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2100 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002101 if (system(lynq_cmd_bandwith) != 0 )
2102 {
2103 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002104 return -1;
2105 }
2106 system("wl up");
2107 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002108 }
you.chen35020192022-05-06 11:30:57 +08002109 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 {
qs.xiong10379192023-02-21 13:19:42 +08002111 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002112 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002113 if (system(lynq_cmd_bandwith) != 0 )
2114 {
2115 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002116 return -1;
2117 }
2118 system("wl up");
2119 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002120 }
2121 default:
you.chen35020192022-05-06 11:30:57 +08002122 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002123 RLOGE("auth type [%d] not support now\n", bandwidth);
2124 return -1;
you.chen35020192022-05-06 11:30:57 +08002125 }
2126 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002127
2128
you.chen35020192022-05-06 11:30:57 +08002129 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002130}
you.chen35020192022-05-06 11:30:57 +08002131
qs.xiongf1b525b2022-03-31 00:58:23 -04002132int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2133{
you.chen35020192022-05-06 11:30:57 +08002134 int count = 0;
2135 int index = 0;
2136 char *split_words[128] = {0};
2137 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002138 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002139 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002140
you.chen35020192022-05-06 11:30:57 +08002141 CHECK_WPA_CTRL(CTRL_AP);
2142
2143 DO_REQUEST(lynq_chanspec_cmd);
2144
2145 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2146 for(;index < count; index++) {
2147 if (strncmp(split_words[index], "bw", 2) != 0) {
2148 continue;
2149 }
2150
2151 index++;
2152 if (index >= count) {
2153 return -1;
2154 }
2155
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002157 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2158 return 0;
2159 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002160 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002161 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002162}
qs.xiong0fb469a2022-04-14 03:50:45 -04002163
qs.xiongf1b525b2022-03-31 00:58:23 -04002164int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002165{
you.chen35020192022-05-06 11:30:57 +08002166 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002167 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002168 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002169
you.chen35020192022-05-06 11:30:57 +08002170 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002171
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2173 {
you.chen35020192022-05-06 11:30:57 +08002174 return -1;
2175 }
2176
2177 system("wl down");
2178 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002179 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002180 return -1;
2181 }
2182 system("wl up");
2183 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002184}
qs.xiong0fb469a2022-04-14 03:50:45 -04002185
qs.xiongf1b525b2022-03-31 00:58:23 -04002186int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002187{
you.chen35020192022-05-06 11:30:57 +08002188 int count = 0;
2189 int index = 0;
2190 char *split_words[128] = {0};
2191 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002192 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002193 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002194
you.chen35020192022-05-06 11:30:57 +08002195 CHECK_WPA_CTRL(CTRL_AP);
2196
2197 DO_REQUEST(lynq_chanspec_cmd);
2198
2199 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002200 for(;index < count; index++)
2201 {
2202 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002203 if (strncmp(split_words[index], "channel", 2) != 0) {
2204 continue;
2205 }
2206
2207 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002208 if (index >= count)
2209 {
you.chen35020192022-05-06 11:30:57 +08002210 return -1;
2211 }
2212
2213 *channel = atoi(split_words[index]);
2214 return 0;
2215 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002216 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002217 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002218}
2219
2220
you.chen35020192022-05-06 11:30:57 +08002221int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002222{
you.chen6c2dd9c2022-05-16 17:55:28 +08002223 char ssid[MAX_CMD] = {0};
2224 int freq = 0;
2225 char lynq_auth_cmd[64]={0};
2226 char lynq_auth_alg_cmd[64]={0};
2227 char lynq_psk_cmd[64]={0};
2228 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002229 char lynq_ieee80211_cmd[64]={0};
2230 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002231 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002232 CHECK_IDX(idx, CTRL_AP);
2233
you.chen6c2dd9c2022-05-16 17:55:28 +08002234 CHECK_WPA_CTRL(CTRL_AP);
2235
qs.xiong9fbf74e2023-03-28 13:38:22 +08002236 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2237 {
2238 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002239 return -1;
2240 }
2241
you.chen92fd5d32022-05-25 10:09:47 +08002242 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002243 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002244 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002245 return 0;
2246 }
2247 else {
2248 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2249 ssid[0] = '\0';
2250 }
2251 lynq_wifi_ap_frequency_get(idx, &freq);
2252
2253 DO_OK_FAIL_REQUEST(cmd_disconnect);
2254 DO_OK_FAIL_REQUEST(cmd_remove_all);
2255 if (ssid[0] != '\0') {
2256 lynq_wifi_ap_ssid_set(idx, ssid);
2257 }
2258 if (freq != 0) {
2259 lynq_wifi_ap_frequency_set(idx, freq);
2260 }
2261 }
2262 }
you.chen35020192022-05-06 11:30:57 +08002263
qs.xiong9fbf74e2023-03-28 13:38:22 +08002264 switch(auth){
2265 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002266 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002267 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002268 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002269 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002270 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002271 break;
2272 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002273 case LYNQ_WIFI_AUTH_WEP:
2274 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002275 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002276 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002277 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002278 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2279
2280 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2281 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2282 break;
2283 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002284 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002285 {
2286 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2287 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2288 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2289
2290 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2291 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2292 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2293 break;
2294
2295 }
you.chen35020192022-05-06 11:30:57 +08002296 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002297 {
2298 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2299 {
you.chen35020192022-05-06 11:30:57 +08002300 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2301 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2302 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002303 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2304 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002305 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002306 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002307 }
2308// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2309// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2310 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002311
you.chen35020192022-05-06 11:30:57 +08002312 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2313 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2314 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 break;
2316 }
2317 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2318 {
2319 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2320 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2321 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2322 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2323
2324 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2325 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2326 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2327 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2328 break;
2329 }
2330 case LYNQ_WIFI_AUTH_WPA3_PSK:
2331 {
2332 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2333 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002334 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2336
2337 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2338 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2339 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2340 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2341 break;
2342 }
2343 default:
you.chen35020192022-05-06 11:30:57 +08002344 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002345 RLOGE("auth type [%d] not support now\n", auth);
2346 return -1;
you.chen35020192022-05-06 11:30:57 +08002347 }
2348 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002349 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002352}
2353
you.chen35020192022-05-06 11:30:57 +08002354int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002355{
you.chen35020192022-05-06 11:30:57 +08002356 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002357 char lynq_auth_alg_str[MAX_RET] = {0};
2358 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002359 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002360 CHECK_IDX(idx, CTRL_AP);
2361
qs.xiong9fbf74e2023-03-28 13:38:22 +08002362 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2363 {
2364 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002365 return -1;
2366 }
2367
qs.xiong9fbf74e2023-03-28 13:38:22 +08002368 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2369 {
2370 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2371 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002372 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002373 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002374 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002375 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002376 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2377 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002378 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002379 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002380 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002381 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002382 else
2383 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002384 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002385 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002386 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002387 }
you.chen35020192022-05-06 11:30:57 +08002388 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002389 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2390 {
2391 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2392 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002393 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002394 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002395 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002396 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2397 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002398 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002399 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002400 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002401 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002402 else
2403 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002404 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002405 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002406 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002407 }
you.chen35020192022-05-06 11:30:57 +08002408 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409
2410 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2411 {
2412 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2413 return -1;
2414 }
2415
2416 if (memcmp(lynq_auth_str,"1",1) == 0 )
2417 {
2418 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2419 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002420 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2422 {
2423 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2424 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002425 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002426 }
2427 else
2428 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002429 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002430 *auth = -1;
2431 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002432
you.chen6c2dd9c2022-05-16 17:55:28 +08002433 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002434}
qs.xiong1af5daf2022-03-14 09:12:12 -04002435
you.chenb95401e2023-05-12 19:39:06 +08002436static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2437{
2438 char status[64];
you.chencba13492023-05-19 13:53:43 +08002439 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002440 curr_status_info curr_state;
2441
2442 CHECK_WPA_CTRL(CTRL_AP);
2443
2444 memset(status, 0, sizeof (status));
2445
2446 curr_state.ap = NULL;
2447 curr_state.state = status;
2448
2449 printf("inner_check_ap_connected %d\n", retry_count);
2450 usleep(500*1000);
2451 if (0 == inner_get_status_info(idx, &curr_state))
2452 {
2453 if (strcmp(status, STATE_COMPLETED) == 0)
2454 {
2455 return 0;
2456 }
2457 else if (retry_count == 4) //not ok in 2s, do reconnect
2458 {
2459 DO_REQUEST("RECONNECT");
2460 return inner_check_ap_connected(idx, retry_count+1);
2461 }
you.chencba13492023-05-19 13:53:43 +08002462 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002463 {
2464 printf("retry 10 time\n");
2465 return -1;
2466 }
2467 else
2468 {
you.chen6d247052023-06-01 16:39:54 +08002469 if (strcmp(status, STATE_DISCONNECTED) == 0)
2470 {
2471 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2472 DO_REQUEST(LYNQ_WIFI_CMD);
2473 }
you.chenb95401e2023-05-12 19:39:06 +08002474 return inner_check_ap_connected(idx, retry_count+1);
2475 }
2476 }
2477 return -1;
2478}
qs.xiong1af5daf2022-03-14 09:12:12 -04002479
qs.xiongf1b525b2022-03-31 00:58:23 -04002480int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002481{
you.chen35020192022-05-06 11:30:57 +08002482 char LYNQ_WIFI_CMD[128]={0};
2483 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2484 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002485 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002486 CHECK_IDX(idx, CTRL_AP);
2487
2488 CHECK_WPA_CTRL(CTRL_AP);
2489
you.chen0df3e7e2023-05-10 15:56:26 +08002490 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002491 {
you.chen0df3e7e2023-05-10 15:56:26 +08002492 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002493 return -1;
2494 }
you.chen35020192022-05-06 11:30:57 +08002495
2496 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2497 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2498
2499 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2500 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2501
you.chenc9928582023-04-24 15:39:37 +08002502 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2503 if (ret != 0)
2504 {
2505 RLOGE("lynq_wifi_ap_start excute script fail");
2506 return -1;
2507 }
2508
you.chenb95401e2023-05-12 19:39:06 +08002509 if (inner_check_ap_connected(idx, 0) != 0)
2510 {
2511 return -1;
2512 }
2513
you.chen0df3e7e2023-05-10 15:56:26 +08002514 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002515 if (g_ap_tmp_watcher_pid == 0)
2516 {
2517 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2518 {
2519 g_ap_tmp_watcher_pid = 0;
2520 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2521 return -1;
2522 }
2523 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2524 }
you.chen0df3e7e2023-05-10 15:56:26 +08002525
qs.xiong9fbf74e2023-03-28 13:38:22 +08002526 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002527}
2528
qs.xiongf1b525b2022-03-31 00:58:23 -04002529int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002530{
you.chen35020192022-05-06 11:30:57 +08002531 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002532}
2533
qs.xiongf1b525b2022-03-31 00:58:23 -04002534int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002535{
you.chen35020192022-05-06 11:30:57 +08002536 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002537
you.chen35020192022-05-06 11:30:57 +08002538 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002539
you.chen35020192022-05-06 11:30:57 +08002540 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002541
you.chen35020192022-05-06 11:30:57 +08002542 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2543
2544 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2545
you.chenb4b121c2022-05-06 17:50:16 +08002546// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002547
2548 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2549 if (ret != 0)
2550 {
2551 RLOGE("lynq_wifi_ap_start excute script fail");
2552 return -1;
2553 }
qs.xiong44fac672023-08-29 16:15:55 +08002554 g_ap_tmp_watcher_stop_flag = 1;
2555 if (g_ap_tmp_watcher_pid != 0)
2556 pthread_join(g_ap_tmp_watcher_pid, NULL);
2557 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002558
qs.xiong9fbf74e2023-03-28 13:38:22 +08002559 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002560}
qs.xiong1af5daf2022-03-14 09:12:12 -04002561
qs.xiongf1b525b2022-03-31 00:58:23 -04002562int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002563{
you.chen35020192022-05-06 11:30:57 +08002564 char lynq_disable_cmd[128] = {0};
2565 char lynq_select_cmd[128] = {0};
2566 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002567 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002568 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002569
you.chen35020192022-05-06 11:30:57 +08002570 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002571 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2572 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2573
2574 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2575 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2576 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002577
qs.xiong9fbf74e2023-03-28 13:38:22 +08002578 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002579}
2580
qs.xiongf1b525b2022-03-31 00:58:23 -04002581int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002582{
you.chen35020192022-05-06 11:30:57 +08002583 char lynq_disable_cmd[128] = {0};
2584 char lynq_select_cmd[128] = {0};
2585 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002586 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002587 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002588
you.chen35020192022-05-06 11:30:57 +08002589 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002590
you.chen35020192022-05-06 11:30:57 +08002591 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2592 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2593
2594 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2595 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2596 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002597
qs.xiong9fbf74e2023-03-28 13:38:22 +08002598 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002599}
qs.xiongf1b525b2022-03-31 00:58:23 -04002600
you.chen35020192022-05-06 11:30:57 +08002601int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002602{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002603 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002604 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002605 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002606 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002607 RLOGD("enter lynq_ap_password_set");
2608 if( password == NULL )
2609 {
2610 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002611 return -1;
2612 }
2613 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002614 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002615 if(pass_len < 8 || pass_len >= 64)
2616 {
2617 RLOGE("[lynq_ap_password_set]input password len not in rage");
2618 return -1;
you.chen35020192022-05-06 11:30:57 +08002619 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002620
you.chen35020192022-05-06 11:30:57 +08002621 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002622
qs.xiong9fbf74e2023-03-28 13:38:22 +08002623 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2624 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002625 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002626 return -1;
2627 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002628 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2629 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002630 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2631 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002632 }
2633
you.chen35020192022-05-06 11:30:57 +08002634 CHECK_WPA_CTRL(CTRL_AP);
2635
qs.xiong9fbf74e2023-03-28 13:38:22 +08002636 if (auth == LYNQ_WIFI_AUTH_WEP)
2637 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002638 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002639 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2640 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2641 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2642 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2643 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002644 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2645 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002646 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 +08002647 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2648 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2649 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002650 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2651 {
2652
qs.xiongc8d92a62023-03-29 17:36:14 +08002653 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 +08002654 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002655 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002656 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2657 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2658
2659 }
2660 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002661 {
2662 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002663 return -1;
2664 }
you.chen35020192022-05-06 11:30:57 +08002665
you.chen35020192022-05-06 11:30:57 +08002666 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002667
qs.xiong9fbf74e2023-03-28 13:38:22 +08002668 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002669}
2670
you.chen35020192022-05-06 11:30:57 +08002671int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002672{
you.chen35020192022-05-06 11:30:57 +08002673 FILE * fp;
2674 int len, ret;
2675 int count, index;
2676 char *split_lines[128] = {0};
2677 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002678 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002679
you.chen35020192022-05-06 11:30:57 +08002680 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002681
you.chen35020192022-05-06 11:30:57 +08002682 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2683// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002684 if (NULL == fp)
2685 {
2686 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002687 return -1;
2688 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002689
you.chen35020192022-05-06 11:30:57 +08002690 buff = alloca(MAX_RET);
2691 fseek(fp, 0, SEEK_SET);
2692 len = fread(buff, 1, MAX_RET, fp);
2693 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002694
qs.xiong9fbf74e2023-03-28 13:38:22 +08002695 for(index=0; index < len; index ++)
2696 {
2697 if (memcmp(buff + index, "network={", 9) != 0)
2698 {
you.chen35020192022-05-06 11:30:57 +08002699 continue;
2700 }
2701 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002702 for (; index < len; index ++ )
2703 {
2704 if (buff[index] != '}')
2705 {
you.chen35020192022-05-06 11:30:57 +08002706 continue;
2707 }
2708 buff[index] = '\0';
2709 break;
2710 }
2711 len = buff + index - p;
2712 }
2713
2714 count = lynq_split(p, len, '\n', split_lines);
2715
2716 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002717 for(index=0; index < count; index++)
2718 {
you.chen35020192022-05-06 11:30:57 +08002719 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002720 if (p != NULL)
2721 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002722 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 if (*p == '\"')
2724 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002725 p++;
2726 }
you.chen35020192022-05-06 11:30:57 +08002727 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002728 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2729 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002730 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 if (*p == '\"')
2732 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002733 p++;
2734 }
2735 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002736 else
2737 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002738 continue;
you.chen35020192022-05-06 11:30:57 +08002739 }
2740
2741 strcpy(password, p);
2742
qs.xiong9fbf74e2023-03-28 13:38:22 +08002743 while(*password != '\0')
2744 {
2745 if (*password == '\"')
2746 {
you.chen35020192022-05-06 11:30:57 +08002747 *password = '\0';
2748 break;
2749 }
2750 password++;
2751 }
2752 ret = 0;
2753 break;
2754 } //end for(index=0; index < count; index++)
2755
2756 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002757}
2758
you.chen35020192022-05-06 11:30:57 +08002759static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2760 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002761 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002762
qs.xiong9fbf74e2023-03-28 13:38:22 +08002763 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2764 {
you.chen35020192022-05-06 11:30:57 +08002765 return -1;
2766 }
2767
2768 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002769
qs.xiong9fbf74e2023-03-28 13:38:22 +08002770 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2771 {
2772 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002773 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002774 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002775 {
you.chena6cd55a2022-05-08 12:20:18 +08002776 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002777 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002778 }
you.chen70f377f2023-04-14 18:17:09 +08002779 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2780 {
2781 return 0;
2782 }
you.chena6cd55a2022-05-08 12:20:18 +08002783 }
2784 }
you.chen70f377f2023-04-14 18:17:09 +08002785 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2786 {
2787 return 0;
2788 }
2789
qs.xiong9fbf74e2023-03-28 13:38:22 +08002790 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2791 {
you.chen70f377f2023-04-14 18:17:09 +08002792 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002793 return -1;
2794 }
2795 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2796 {
2797
you.chen70f377f2023-04-14 18:17:09 +08002798 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2799 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002800 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2801 {
2802
2803 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002804 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002805 }else
2806 {
you.chen70f377f2023-04-14 18:17:09 +08002807 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002808 *auth = -1;
2809 return -1;
2810 }
you.chen35020192022-05-06 11:30:57 +08002811 return 0;
2812}
2813
2814int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002815{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002816 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002817 int pass_len, net_no, count, index;
2818 char lynq_tmp_cmd[300]={0};
2819 int net_no_list[128];
2820 lynq_wifi_auth_s net_auth;
2821 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002822 if(pass_len < 8 || pass_len >= 64)
2823 {
2824 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002825 return -1;
2826 }
2827
2828 CHECK_IDX(idx, CTRL_STA);
2829
2830 net_no = -1;
2831 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2832
qs.xiong9fbf74e2023-03-28 13:38:22 +08002833 for (index=0; index < count; index++)
2834 {
you.chen35020192022-05-06 11:30:57 +08002835 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002836 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2837 {
you.chen35020192022-05-06 11:30:57 +08002838 net_no = net_no_list[index];
2839 break;
2840 }
2841 }
2842
qs.xiong9fbf74e2023-03-28 13:38:22 +08002843 if (net_no < 0)
2844 {
you.chen35020192022-05-06 11:30:57 +08002845 return -1;
2846 }
2847
2848 CHECK_WPA_CTRL(CTRL_STA);
2849
2850 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2851
2852 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2853 DO_OK_FAIL_REQUEST(cmd_save_config);
2854
2855 return 0;
2856}
2857
2858int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2859
2860 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002861 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002862 int count, index;
2863 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002864 char *buff, *p, *ssid, *ssid_end_flag;
2865 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002866 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002867
you.chen755332b2022-08-06 16:59:10 +08002868 network_len = 0;
2869 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002870 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002871
you.chen35020192022-05-06 11:30:57 +08002872 CHECK_IDX(idx, CTRL_STA);
2873
qs.xiong9fbf74e2023-03-28 13:38:22 +08002874 if (NULL == password)
2875 {
2876 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002877 return -1;
2878 }
2879
you.chen35020192022-05-06 11:30:57 +08002880 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002881 if (NULL == fp)
2882 {
2883 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002884 return -1;
2885 }
2886
you.chen6d247052023-06-01 16:39:54 +08002887 fseek(fp, 0, SEEK_END);
2888 len = ftell(fp);
2889 buff = malloc(len + 1);
2890
2891 if (buff == NULL)
2892 {
2893 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2894 return -1;
2895 }
2896
you.chen35020192022-05-06 11:30:57 +08002897 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002898 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002899 fclose(fp);
2900
qs.xiong9fbf74e2023-03-28 13:38:22 +08002901 for(index=0; index < len; index ++)
2902 {
2903 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002904 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002905 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002906 {
you.chen35020192022-05-06 11:30:57 +08002907 continue;
2908 }
you.chen6d247052023-06-01 16:39:54 +08002909 p = buff + index + 9;
2910 for (; index < len; index ++ )
2911 {
2912 if (buff[index] != '}')
2913 {
2914 continue;
2915 }
2916 buff[index] = '\0';
2917 break;
2918 }
2919 network_len = buff + index - p;
2920 break;
you.chen35020192022-05-06 11:30:57 +08002921 }
2922
qs.xiongb3f26af2023-02-17 18:41:07 +08002923 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002924 {
2925 if (buff != NULL)
2926 {
2927 free(buff);
2928 }
2929
qs.xiongb3f26af2023-02-17 18:41:07 +08002930 return -1;
you.chen6d247052023-06-01 16:39:54 +08002931 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002932
you.chend2fef3f2023-02-13 10:50:35 +08002933 ssid = strstr(p, "ssid=");
2934 if (ssid != NULL) {
2935 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002936 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002937 {
you.chend2fef3f2023-02-13 10:50:35 +08002938 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2939 break;
2940 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002941 else
you.chen6d247052023-06-01 16:39:54 +08002942 {
you.chend2fef3f2023-02-13 10:50:35 +08002943 ssid_end_flag = strstr(ssid, "\n");
2944 if (ssid_end_flag != NULL)
2945 {
2946 ssid_len = (ssid_end_flag - ssid) / 2;
2947 for(i=0; i<ssid_len; i++)
2948 {
2949 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2950 }
2951 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2952 break;
2953 }
2954 }
you.chen35020192022-05-06 11:30:57 +08002955 }
you.chend2fef3f2023-02-13 10:50:35 +08002956
you.chen35020192022-05-06 11:30:57 +08002957 }
2958
qs.xiong9fbf74e2023-03-28 13:38:22 +08002959 if (index >= len || NULL == p || network_len <= 0)
2960 {
you.chen6d247052023-06-01 16:39:54 +08002961 if (buff != NULL)
2962 {
2963 free(buff);
2964 }
you.chen35020192022-05-06 11:30:57 +08002965 return -1;
2966 }
2967
you.chen755332b2022-08-06 16:59:10 +08002968 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002969
2970 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002971 for(index=0; index < count; index++)
2972 {
you.chen35020192022-05-06 11:30:57 +08002973 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002974 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002975 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002976 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002977 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002978 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002979 p++;
2980 }
you.chen35020192022-05-06 11:30:57 +08002981 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002982 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002983 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002984 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002985 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002986 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002987 p++;
2988 }
2989 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 else
you.chen6d247052023-06-01 16:39:54 +08002991 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002992 continue;
you.chen35020192022-05-06 11:30:57 +08002993 }
2994
qs.xiong13673462023-02-21 19:12:54 +08002995 if (*p == '\"')
2996 p++;
2997 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002998
qs.xiong13673462023-02-21 19:12:54 +08002999 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003000 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08003001 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003002 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08003003 {
you.chen35020192022-05-06 11:30:57 +08003004 *password = '\0';
3005 break;
3006 }
3007 password++;
3008 }
3009 ret = 0;
3010 break;
3011 } //end for(index=0; index < count; index++)
3012
you.chen6d247052023-06-01 16:39:54 +08003013 if (buff != NULL)
3014 {
3015 free(buff);
3016 }
3017
you.chen35020192022-05-06 11:30:57 +08003018 return ret;
3019}
3020
3021static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3022{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003023 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003024
qs.xiong9fbf74e2023-03-28 13:38:22 +08003025 if (sta_ssid == NULL)
3026 {
3027 RLOGE("sta_ssid is null\n");
3028 return -1;
you.chen35020192022-05-06 11:30:57 +08003029 }
3030
qs.xiong9fbf74e2023-03-28 13:38:22 +08003031 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003032
3033 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3034
3035 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3036// DO_OK_FAIL_REQUEST(cmd_save_config);
3037
qs.xiong9fbf74e2023-03-28 13:38:22 +08003038 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003039
3040}
3041
you.chen35020192022-05-06 11:30:57 +08003042static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003043{
you.chen35020192022-05-06 11:30:57 +08003044 char lynq_disable_cmd[128]={0};
3045 char lynq_select_cmd[128]={0};
3046
3047 CHECK_WPA_CTRL(CTRL_STA);
3048
qs.xiong9fbf74e2023-03-28 13:38:22 +08003049 if (save != 0)
3050 {
you.chenc29444e2022-06-07 18:01:16 +08003051 if (start_flag != 0)
3052 {
3053 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3054 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3055 }
3056 else
3057 {
3058 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3059 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3060 }
you.chen35020192022-05-06 11:30:57 +08003061 DO_OK_FAIL_REQUEST(cmd_save_config);
3062 }
3063
qs.xiong9fbf74e2023-03-28 13:38:22 +08003064 if (start_flag == 0)
3065 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003066 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003067 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3068 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003069 else
3070 {
you.chen35020192022-05-06 11:30:57 +08003071 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3072 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3073 }
3074
3075 return 0;
3076}
3077
3078int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3079{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003080 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003081 CHECK_IDX(idx, CTRL_STA);
3082
you.chen6c2dd9c2022-05-16 17:55:28 +08003083 curr_status_info curr_state;
3084 ap_info_s ap_info;
3085 curr_state.ap = &ap_info;
3086 curr_state.state = NULL;
3087
qs.xiong9fbf74e2023-03-28 13:38:22 +08003088 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3089 {
you.chend2fef3f2023-02-13 10:50:35 +08003090 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003091 return 0;
3092 }
3093
3094 return -1;
you.chen35020192022-05-06 11:30:57 +08003095}
3096
3097int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3098{
you.chen9ac66392022-08-06 17:01:16 +08003099 scan_info_s *scan_list = NULL;
3100 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003101 int scan_len=0;
3102 int save_len=0;
3103 int best_index = -1;
3104 int best_scan_index = -1;
3105 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003106 int i, j, ret;
3107
3108 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003109
3110 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003111 if (info == NULL)
3112 {
you.chen35020192022-05-06 11:30:57 +08003113 return -1;
3114 }
3115
3116 curr_status_info curr_state;
3117 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003118 char status[64];
you.chen35020192022-05-06 11:30:57 +08003119
you.chen9ac66392022-08-06 17:01:16 +08003120 memset(&ap_info, 0, sizeof (ap_info));
3121 memset(status, 0, sizeof (status));
3122
3123 curr_state.ap = &ap_info;
3124 curr_state.state = status;
3125
qs.xiong9fbf74e2023-03-28 13:38:22 +08003126 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3127 {
you.chen35020192022-05-06 11:30:57 +08003128 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003129 if (strcmp(status, STATE_COMPLETED) == 0)
3130 {
3131 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3132 }
3133 else
3134 {
3135 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3136 }
you.chen593621d2023-04-27 17:52:44 +08003137 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003138 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003139 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003140 return 0;
3141 }
3142
you.chen9ac66392022-08-06 17:01:16 +08003143 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003144 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003145 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3146 {
you.chen9ac66392022-08-06 17:01:16 +08003147 if (NULL != scan_list)
3148 {
3149 free(scan_list);
3150 }
you.chen35020192022-05-06 11:30:57 +08003151 return -1;
3152 }
3153
qs.xiong9fbf74e2023-03-28 13:38:22 +08003154 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3155 {
you.chen9ac66392022-08-06 17:01:16 +08003156 if (NULL != scan_list)
3157 {
3158 free(scan_list);
3159 }
3160 if (NULL != save_list)
3161 {
3162 free(save_list);
3163 }
you.chen35020192022-05-06 11:30:57 +08003164 return -1;
3165 }
3166
qs.xiong9fbf74e2023-03-28 13:38:22 +08003167 for (i=0; i < save_len; i++)
3168 {
3169 for (j=0; j < scan_len; j++)
3170 {
you.chen35020192022-05-06 11:30:57 +08003171 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003172 && save_list[i].base_info.auth == scan_list[j].auth)
3173 {
3174 if (best_rssi == 0)
3175 {
you.chen9ac66392022-08-06 17:01:16 +08003176 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003177 best_rssi = scan_list[j].rssi;
3178 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003179 else if (best_rssi > scan_list[j].rssi)
3180 {
you.chen35020192022-05-06 11:30:57 +08003181 best_index = i;
3182 best_scan_index = j;
3183 best_rssi = scan_list[j].rssi;
3184 }
you.chend2fef3f2023-02-13 10:50:35 +08003185 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 +08003186 break;
3187 }
3188 }
3189 }
3190
qs.xiong9fbf74e2023-03-28 13:38:22 +08003191 if (best_index >= 0)
3192 {
you.chen35020192022-05-06 11:30:57 +08003193 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003194 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 +08003195 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3196 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003197 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003198 }
3199
you.chen9ac66392022-08-06 17:01:16 +08003200 if (NULL != scan_list)
3201 {
3202 free(scan_list);
3203 }
3204 if (NULL != save_list)
3205 {
3206 free(save_list);
3207 }
3208
3209 return ret;
you.chen35020192022-05-06 11:30:57 +08003210}
3211
3212static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3213{
qs.xiongc8d92a62023-03-29 17:36:14 +08003214 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003215 char lynq_ket_mgmt_cmd[64]={0};
3216 char lynq_pairwise_cmd[64]={0};
3217 char lynq_psk_cmd[64]={0};
3218
3219 CHECK_WPA_CTRL(CTRL_STA);
3220
qs.xiong9fbf74e2023-03-28 13:38:22 +08003221 switch(auth)
3222 {
3223 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003224 {
3225 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003226
you.chen35020192022-05-06 11:30:57 +08003227 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003228// DO_OK_FAIL_REQUEST(cmd_save_config);
3229 break;
3230 }
3231 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003232 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003233 {
3234 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3235 {
you.chen35020192022-05-06 11:30:57 +08003236 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3237 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003238 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3239 {
you.chena6cd55a2022-05-08 12:20:18 +08003240 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003241 }
3242 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3243 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003244
you.chen35020192022-05-06 11:30:57 +08003245 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3246 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3247 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003248
qs.xiong9fbf74e2023-03-28 13:38:22 +08003249 if (password != NULL)
3250 {
you.chen35020192022-05-06 11:30:57 +08003251 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3252 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003253 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003254 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003255
you.chen35020192022-05-06 11:30:57 +08003256// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003257 break;
3258 }
3259 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3260 {
qs.xiong3e506812023-04-06 11:08:48 +08003261 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003262 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003263 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3264 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3265
qs.xiong3e506812023-04-06 11:08:48 +08003266 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003267 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3268 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3269 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3270
3271 break;
3272 }
3273 case LYNQ_WIFI_AUTH_WPA3_PSK:
3274 {
qs.xiong3e506812023-04-06 11:08:48 +08003275 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003276 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003277 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3278 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3279
qs.xiong3e506812023-04-06 11:08:48 +08003280 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003281 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3282 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3283 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3284
3285 break;
3286 }
3287 default:
3288 return -1;
you.chen35020192022-05-06 11:30:57 +08003289 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003290
qs.xiong9fbf74e2023-03-28 13:38:22 +08003291 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003292}
qs.xiong7a105ce2022-03-02 09:43:11 -05003293
you.chen35020192022-05-06 11:30:57 +08003294static int inner_get_curr_net_no(int interface) {
3295 curr_status_info curr_state;
3296 curr_state.ap = NULL;
3297 curr_state.state = NULL;
3298
qs.xiong9fbf74e2023-03-28 13:38:22 +08003299 if (0 != inner_get_status_info(interface, &curr_state))
3300 {
you.chen35020192022-05-06 11:30:57 +08003301 return -1;
3302 }
3303
3304 return curr_state.net_no;
3305}
3306
3307int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003308{
you.chen35020192022-05-06 11:30:57 +08003309 int net_no;
3310 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003311
you.chen35020192022-05-06 11:30:57 +08003312 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003313
qs.xiong9fbf74e2023-03-28 13:38:22 +08003314 if (net_no < 0)
3315 {
you.chen35020192022-05-06 11:30:57 +08003316 return -1;
3317 }
3318
3319 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003320}
3321
you.chenb95401e2023-05-12 19:39:06 +08003322int 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 -05003323{
you.chen35020192022-05-06 11:30:57 +08003324 int count, net_no, index;
3325 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003326 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003327 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003328 curr_status_info curr_state;
3329 ap_info_s ap_info;
3330 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003331
qs.xiong9fbf74e2023-03-28 13:38:22 +08003332 if (ssid == NULL || *ssid == '\0')
3333 {
3334 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003335 return -1;
3336 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003337
qs.xiong9fbf74e2023-03-28 13:38:22 +08003338 if (LYNQ_WIFI_AUTH_OPEN != auth)
3339 {
3340 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003341 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003342 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003343 return -1;
3344 }
3345 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003346
you.chen70f377f2023-04-14 18:17:09 +08003347
3348 pthread_mutex_lock(&s_global_check_mutex);
3349 if (s_sta_status != INNER_STA_STATUS_INIT)
3350 {
3351 s_sta_status = INNER_STA_STATUS_CANCEL;
3352 pthread_cond_signal(&s_global_check_cond);
3353 }
3354 pthread_mutex_unlock(&s_global_check_mutex);
3355
you.chen35020192022-05-06 11:30:57 +08003356 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003357 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003358
3359 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003360 memset(&ap_info, 0, sizeof (ap_info));
3361 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003362
you.chen70f377f2023-04-14 18:17:09 +08003363 curr_state.ap = &ap_info;
3364 curr_state.state = status;
3365
3366 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003367 {
you.chen70f377f2023-04-14 18:17:09 +08003368 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3369 {
3370 net_no = curr_state.net_no;
3371 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3372 && strcmp(ap_info.psw, psw) == 0)
3373 {
3374 RLOGD("already connected\n");
3375
3376 pthread_mutex_lock(&s_global_check_mutex);
3377 s_sta_status = INNER_STA_STATUS_CONNECTED;
3378 pthread_cond_signal(&s_global_check_cond);
3379 pthread_mutex_unlock(&s_global_check_mutex);
3380 return 0;
3381 }
you.chen35020192022-05-06 11:30:57 +08003382 }
3383 }
3384
you.chen70f377f2023-04-14 18:17:09 +08003385 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003386 {
you.chen70f377f2023-04-14 18:17:09 +08003387 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3388
3389 for (index=0; index < count; index++)
3390 {
3391 net_auth = -1;
3392 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3393 {
3394 net_no = net_no_list[index];
3395 break;
3396 }
you.chen35020192022-05-06 11:30:57 +08003397 }
3398
you.chen70f377f2023-04-14 18:17:09 +08003399 if (net_no < 0)
3400 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003401 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3402 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3403 {
3404 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3405 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3406 DO_OK_FAIL_REQUEST(rm_net_cmd);
3407 }
you.chen70f377f2023-04-14 18:17:09 +08003408 net_no = lynq_add_network(CTRL_STA);
3409 if (net_no == -1)
3410 {
3411 return -1;
3412 }
3413
3414 RLOGD("net no is %d\n", net_no);
3415 if (0 != inner_set_sta_ssid(net_no, ssid))
3416 {
3417 return -1;
3418 }
you.chen35020192022-05-06 11:30:57 +08003419 }
3420 }
3421
qs.xiong9fbf74e2023-03-28 13:38:22 +08003422 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3423 {
you.chen35020192022-05-06 11:30:57 +08003424 return -1;
3425 }
3426
you.chen70f377f2023-04-14 18:17:09 +08003427
3428 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003429 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003430 usleep(200*1000);
3431
3432 ret = inner_sta_start_stop(net_no, 1, 1);
3433
3434 pthread_mutex_lock(&s_global_check_mutex);
3435 s_sta_status = INNER_STA_STATUS_CONNECTING;
3436 strcpy(s_sta_current_connecting_ssid, ssid);
3437 struct timeval now;
3438 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003439 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003440 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3441 pthread_cond_signal(&s_global_check_cond);
3442 pthread_mutex_unlock(&s_global_check_mutex);
3443 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003444}
3445
you.chenb95401e2023-05-12 19:39:06 +08003446int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3447{
3448 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3449}
3450
you.chen35020192022-05-06 11:30:57 +08003451int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003452{
you.chen35020192022-05-06 11:30:57 +08003453 ap_info_s ap;
3454 curr_status_info curr_state;
3455 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003456
qs.xiong9fbf74e2023-03-28 13:38:22 +08003457 if (ssid == NULL || *ssid == '\0')
3458 {
3459 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003460 return -1;
3461 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003462
you.chen35020192022-05-06 11:30:57 +08003463 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003464
you.chen35020192022-05-06 11:30:57 +08003465 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003466 curr_state.state = NULL;
3467
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3469 {
you.chen35020192022-05-06 11:30:57 +08003470 return 0;
3471 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003472
qs.xiong9fbf74e2023-03-28 13:38:22 +08003473 if (strcmp(ap.ap_ssid, ssid) != 0)
3474 {
you.chen35020192022-05-06 11:30:57 +08003475 return 0;
3476 }
3477
you.chen70f377f2023-04-14 18:17:09 +08003478 pthread_mutex_lock(&s_global_check_mutex);
3479 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3480 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003481 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003482}
qs.xiong97fa59b2022-04-07 05:41:29 -04003483
qs.xiongc93bf2b2023-08-25 10:22:08 +08003484int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3485{
3486 ap_info_s ap;
3487 curr_status_info curr_state;
3488 ap.ap_ssid[0] = '\0';
3489
3490 if (ssid == NULL || *ssid == '\0')
3491 {
3492 RLOGE("input ssid is NULL\n");
3493 return -1;
3494 }
3495
3496 CHECK_IDX(idx, CTRL_STA);
3497
3498
3499 curr_state.ap = &ap;
3500 curr_state.state = NULL;
3501
3502 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3503 {
3504 return 0;
3505 }
3506
3507 if (strcmp(ap.ap_ssid, ssid) != 0)
3508 {
3509 return 0;
3510 }
3511
3512 pthread_mutex_lock(&s_global_check_mutex);
3513 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3514 pthread_mutex_unlock(&s_global_check_mutex);
3515 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3516
3517}
3518
3519
you.chena6cd55a2022-05-08 12:20:18 +08003520int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3521{
qs.xiongad2f89d2023-01-18 13:17:41 +08003522// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3523// const char *lynq_reconnect_cmd = "RECONNECT";
3524 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3525 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003526
you.chen35020192022-05-06 11:30:57 +08003527 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003528 CHECK_WPA_CTRL(CTRL_STA);
3529
you.chenc9928582023-04-24 15:39:37 +08003530 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3531 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003532 {
you.chenc9928582023-04-24 15:39:37 +08003533 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003534 return -1;
3535 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003536
qs.xiongad2f89d2023-01-18 13:17:41 +08003537 system(lynq_enable_sta_cmd);
3538 system(lynq_reconnect_cmd);
3539// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003540 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003541}
3542
you.chen6d247052023-06-01 16:39:54 +08003543static int inner_get_status_info_state (int interface, char *state) {
3544 curr_status_info curr_state;
3545 curr_state.ap = NULL;
3546 curr_state.state = state;
3547 return inner_get_status_info(interface, &curr_state);
3548}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003549
3550int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3551{
3552
3553 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3554
3555 CHECK_IDX(idx, CTRL_STA);
3556 CHECK_WPA_CTRL(CTRL_STA);
3557
3558 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3559 if (ret != 0)
3560 {
3561 RLOGE("lynq_wifi_ap_start excute script fail");
3562 return -1;
3563 }
3564
3565// system(lynq_enable_sta_cmd);
3566 system(lynq_reconnect_cmd);
3567
3568 return 0;
3569}
3570
3571
you.chen35020192022-05-06 11:30:57 +08003572int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003573{
qs.xiongad2f89d2023-01-18 13:17:41 +08003574// char lynq_disable_network_cmd[MAX_CMD];
3575// curr_status_info curr_state;
3576// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003577 int i=0;
3578 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003579
you.chen6d247052023-06-01 16:39:54 +08003580// 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 +08003581 CHECK_IDX(idx, CTRL_STA);
3582 CHECK_WPA_CTRL(CTRL_STA);
3583
you.chen6d247052023-06-01 16:39:54 +08003584// system(lynq_disable_sta_cmd);
3585 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003586 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003587
3588 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3589 if (ret != 0)
3590 {
3591 RLOGE("lynq_wifi_ap_start excute script fail");
3592 return -1;
3593 }
3594
you.chen6d247052023-06-01 16:39:54 +08003595 for (i=0; i < 30; i++) // to check if sta is realy stoped
3596 {
3597 if (inner_get_status_info_state(idx, state) != 0)
3598 {
3599 break;
3600 }
3601
3602 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3603 {
3604 break;
3605 }
3606 RLOGD("lynq_wifi_ap_start curr state %s", state);
3607 usleep(SLEEP_TIME_ON_IDLE);
3608 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003609
you.chena6cd55a2022-05-08 12:20:18 +08003610 return 0;
3611// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003612}
qs.xiongfcc914b2023-07-06 21:16:20 +08003613int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3614{
3615 char LYNQ_DISABLE_CMD[128]={0};
3616 CHECK_IDX(idx, CTRL_STA);
3617 CHECK_WPA_CTRL(CTRL_STA);
3618 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3619 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3620 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3621 return 0;
3622}
qs.xiong7a105ce2022-03-02 09:43:11 -05003623
you.chen35020192022-05-06 11:30:57 +08003624//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3625// int i, count;
3626// char *p;
3627// const char * FLAG_SSID = "ssid=";
3628// const char * FLAG_SBSID = "bssid=";
3629// const char * FLAG_KEY_MGMT = "key_mgmt=";
3630// const char * FLAG_FREQ = "freq=";
3631// char lynq_sta_cmd[MAX_CMD];
3632// char *split_lines[128] = {0};
3633
3634// CHECK_WPA_CTRL(CTRL_AP);
3635
3636// sprintf(lynq_sta_cmd, "STA %s", bssid);
3637
3638// DO_REQUEST(lynq_sta_cmd);
3639
3640// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3641
3642// for(i=0; i < count; i++) {
3643// p = strstr(split_lines[i], FLAG_SSID);
3644// if (p != NULL) {
3645// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3646// continue;
3647// }
3648// }
3649
3650// lynq_get_interface_ip(idx, ap->ap_ip);
3651// lynq_ap_password_set(idx, ap->psw);
3652
3653// return 0;
3654//}
3655
3656static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3657 curr_status_info curr_state;
3658 curr_state.ap = ap;
3659 curr_state.state = NULL;
3660 return inner_get_status_info(interface, &curr_state);
3661}
3662
3663int 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 -04003664{
qs.xiong5071c802023-09-06 14:04:15 +08003665 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003666 int index, line_count;
3667 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003668 const char *lynq_first_sta_cmd = "STA-FIRST";
3669 char lynq_next_sta_cmd[MAX_CMD];
3670 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003671 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003672
you.chen35020192022-05-06 11:30:57 +08003673 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003674
you.chen35020192022-05-06 11:30:57 +08003675 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003676
you.chenb95401e2023-05-12 19:39:06 +08003677 // ap_info_s * tmp_ap;
3678 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003679 if (ap == NULL || list == NULL || len == NULL)
3680 {
3681 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003682 return -1;
3683 }
3684
you.chenb95401e2023-05-12 19:39:06 +08003685 // ap = &tmp_ap;
3686 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003687 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003688 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003689
you.chenb95401e2023-05-12 19:39:06 +08003690 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003691 {
you.chenb95401e2023-05-12 19:39:06 +08003692 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003693 return -1;
3694 }
3695
3696 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3697 lynq_ap_password_get(idx, (*ap)->psw);
3698
you.chen35020192022-05-06 11:30:57 +08003699 DO_REQUEST(lynq_first_sta_cmd);
3700
3701 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003702 while (reply_len > 0)
3703 {
3704 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003705 {
you.chen35020192022-05-06 11:30:57 +08003706 break;
3707 }
3708 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3709 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3710 strcpy(bssid[index], split_lines[0]);
3711 index++;
3712 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3713 reply_len = MAX_RET;
3714 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003715 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 +08003716 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003717 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003718 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003719 break;
3720 }
3721 }
3722
3723 *len = index;
3724
3725 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003726 for (index=0; index < *len; index++)
3727 {
you.chend2fef3f2023-02-13 10:50:35 +08003728 dev_info = &(*list)[index];
3729 memset(dev_info, 0, sizeof(device_info_s));
3730 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3731 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3732 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3733 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003734 free(bssid[index]);
3735 }
qs.xiong5071c802023-09-06 14:04:15 +08003736 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003737 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003738}
3739
you.chen35020192022-05-06 11:30:57 +08003740int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003741{
you.chen35020192022-05-06 11:30:57 +08003742 int i, count, index, count_words;
3743 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3744 char *split_lines[128] = {0};
3745 char *split_words[128] = {0};
3746 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003747
qs.xiong9fbf74e2023-03-28 13:38:22 +08003748 if (list == NULL || len == NULL)
3749 {
you.chen35020192022-05-06 11:30:57 +08003750 return -1;
3751 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003752
you.chen9ac66392022-08-06 17:01:16 +08003753 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3754 {
3755 usleep(100 * 1000);
3756 }
3757
you.chen35020192022-05-06 11:30:57 +08003758 CHECK_IDX(idx, CTRL_STA);
3759
3760 CHECK_WPA_CTRL(CTRL_STA);
3761
3762 DO_REQUEST(lynq_scan_result_cmd);
3763
3764 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3765 *len = count - 1;
3766 *list = malloc(sizeof (scan_info_s) * *len);
3767
3768 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003769 for (index=0; index <count_words; index++)
3770 {
3771 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003772 }
3773
qs.xiong9fbf74e2023-03-28 13:38:22 +08003774 for(index = 1;index < count; index++)
3775 {
3776 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003777 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003778 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3779 if (count_words < 4)
3780 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003781 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003782 //bssid / frequency / signal level / flags / ssid
3783 p = (*list) + index - 1;
3784 strcpy(p->mac, split_words[0]);
3785 p->band = convert_band_from_freq(atoi(split_words[1]));
3786 p->rssi = -1 * atoi( split_words[2]);
3787 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003788 if (count_words == 4) // ssid hided
3789 {
3790 p->ssid[0] = '\0';
3791 }
3792 else
3793 {
3794 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3795 }
you.chen35020192022-05-06 11:30:57 +08003796 }
3797
3798 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003799}
qs.xiong97fa59b2022-04-07 05:41:29 -04003800
you.chen35020192022-05-06 11:30:57 +08003801int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3802{
3803 int count, net_no, index;
3804 int net_no_list[128];
3805 lynq_wifi_auth_s net_auth;
3806 char lynq_remove_cmd[MAX_CMD];
3807
qs.xiong9fbf74e2023-03-28 13:38:22 +08003808 if (ssid == NULL || *ssid == '\0')
3809 {
3810 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003811 return -1;
3812 }
3813
3814 CHECK_IDX(idx, CTRL_STA);
3815
3816 CHECK_WPA_CTRL(CTRL_STA);
3817
3818 net_no = -1;
3819 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3820
qs.xiong9fbf74e2023-03-28 13:38:22 +08003821 for (index=0; index < count; index++)
3822 {
you.chen35020192022-05-06 11:30:57 +08003823 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003824 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3825 {
you.chen35020192022-05-06 11:30:57 +08003826 net_no = net_no_list[index];
3827 break;
3828 }
3829 }
3830
qs.xiong9fbf74e2023-03-28 13:38:22 +08003831 if (net_no < 0)
3832 {
you.chen35020192022-05-06 11:30:57 +08003833 return 0;
3834 }
3835
3836 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3837
3838 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3839 DO_OK_FAIL_REQUEST(cmd_save_config);
3840
3841 return 0;
3842}
3843
3844int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003845{
you.chend2fef3f2023-02-13 10:50:35 +08003846 int count, index;
you.chen35020192022-05-06 11:30:57 +08003847 int net_no_list[128];
3848 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003849 RLOGD("enter lynq_get_sta_saved_ap api\n");
3850 if (list == NULL || len == NULL)
3851 {
3852 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003853 return -1;
3854 }
3855
3856 CHECK_IDX(idx, CTRL_STA);
3857
3858// CHECK_WPA_CTRL(CTRL_STA);
3859
3860 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003861 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003862
you.chen057aac42023-04-13 14:06:58 +08003863 if (count < 0)
3864 {
3865 RLOGE("list network fail");
3866 return count;
3867 }
3868 else if (count == 0)
3869 {
3870 *list = NULL;
3871 *len = 0;
3872 return 0;
3873 }
3874
you.chen35020192022-05-06 11:30:57 +08003875 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003876 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003877 *len = count;
3878
qs.xiong9fbf74e2023-03-28 13:38:22 +08003879 for (index=0; index < count; index++)
3880 {
you.chen35020192022-05-06 11:30:57 +08003881 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003882 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003883 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003884 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003885 {
you.chen35020192022-05-06 11:30:57 +08003886 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3887 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003888 else
you.chen057aac42023-04-13 14:06:58 +08003889 {
you.chen35020192022-05-06 11:30:57 +08003890 (*list)[index].base_info.band = -1;
3891 }
you.chen057aac42023-04-13 14:06:58 +08003892 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003893 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003894 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003895 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003896 return 0;
3897}
3898
3899int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3900{
qs.xiong20202422023-09-06 18:01:18 +08003901 if ( g_sta_conncet_status_flag != 0 )
3902 {
3903 RLOGD("current sta is connecting dest ap");
3904 return -1;
3905 }
qs.xiongc8d92a62023-03-29 17:36:14 +08003906 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003907 const char *lynq_scan_cmd = "SCAN";
3908
3909 CHECK_IDX(idx, CTRL_STA);
3910
3911 CHECK_WPA_CTRL(CTRL_STA);
3912
you.chen0df3e7e2023-05-10 15:56:26 +08003913 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3914 {
3915 RLOGD("tmp clear scanlist");
3916 system(clean_last_re);
3917 }
you.chen9ac66392022-08-06 17:01:16 +08003918 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003919 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003920 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3921 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003922 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003923 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3924 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003925 g_sta_scan_finish_flag = 1;
3926 return -1;
3927 }
you.chen35020192022-05-06 11:30:57 +08003928
3929 return 0;
3930}
3931
3932int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003933 if (cb == NULL)
3934 {
3935 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003936 return -1;
3937 }
3938
you.chen6d247052023-06-01 16:39:54 +08003939 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003940 g_ap_callback_priv = priv;
3941 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003942 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003943
you.chen6d247052023-06-01 16:39:54 +08003944 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003945 if (g_ap_watcher_pid == 0 )
3946 {
3947 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3948 {
3949 g_ap_watcher_pid = 0;
3950 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3951 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3952 return -1;
3953 }
3954 }
3955
3956 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3957 RLOGD("creat APWatcherTheradProc susccs");
3958
you.chen35020192022-05-06 11:30:57 +08003959 return 0;
3960}
3961
3962int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003963 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003964 if (g_ap_callback_priv == priv)
3965 {
you.chen35020192022-05-06 11:30:57 +08003966 g_ap_callback_func = NULL;
3967 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003968 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003969 return 0;
3970 }
you.chen6d247052023-06-01 16:39:54 +08003971 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003972 return -1;
3973}
3974
3975int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003976 if (cb == NULL)
3977 {
3978 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003979 return -1;
3980 }
3981
you.chen6d247052023-06-01 16:39:54 +08003982 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003983 g_sta_callback_priv = priv;
3984 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003985 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003986
you.chen6d247052023-06-01 16:39:54 +08003987 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003988 if (g_sta_watcher_pid == 0 ) {
3989 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3990 {
3991 g_sta_watcher_pid = 0;
3992 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3993 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3994 return -1;
3995 }
3996 }
3997
3998 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3999 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08004000 return 0;
4001}
4002
4003int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004004 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004005 if (g_sta_callback_priv == priv)
4006 {
you.chen35020192022-05-06 11:30:57 +08004007 g_sta_callback_func = NULL;
4008 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004009 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004010 return 0;
4011 }
you.chen6d247052023-06-01 16:39:54 +08004012 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004013 return -1;
4014}
4015
qs.xiongfcc914b2023-07-06 21:16:20 +08004016int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4017 if (cb == NULL)
4018 {
4019 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4020 return -1;
4021 }
4022 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4023 g_sta_auto_callback_priv = priv;
4024 g_sta_auto_callback_func = cb;
4025 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4026 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4027 if (g_sta_auto_watcher_pid == 0 ) {
4028 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4029 {
4030 g_sta_auto_watcher_pid = 0;
4031 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4032 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4033 return -1;
4034 }
4035 }
4036 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4037 RLOGD("creat STAWatcherTheradProc susccs");
4038 return 0;
4039}
4040int lynq_unreg_sta_auto_event_callback(void * priv) {
4041 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4042 if (g_sta_auto_callback_priv == priv)
4043 {
4044 g_sta_auto_watcher_stop_flag = 1;
4045 if (g_sta_auto_watcher_pid != 0)
4046 {
4047 pthread_join(g_sta_auto_watcher_pid, NULL);
4048 }
4049 g_sta_auto_watcher_pid = 0;
4050 g_sta_auto_callback_func = NULL;
4051 g_sta_auto_callback_priv = NULL;
4052 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4053 return 0;
4054 }
4055 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4056 return -1;
4057}
you.chen35020192022-05-06 11:30:57 +08004058int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4059{
4060 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004062 CHECK_IDX(idx, CTRL_AP);
4063
qs.xiong9fbf74e2023-03-28 13:38:22 +08004064 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4065 {
you.chen35020192022-05-06 11:30:57 +08004066 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4067 return 0;
4068 }
4069
qs.xiong9fbf74e2023-03-28 13:38:22 +08004070 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4071 {
you.chen35020192022-05-06 11:30:57 +08004072 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4073 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004074 else
4075 {
you.chen35020192022-05-06 11:30:57 +08004076 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4077 }
4078
4079 return 0;
4080}
4081
4082int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4083 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004084 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004085 CHECK_IDX(idx, CTRL_STA);
4086
qs.xiong9fbf74e2023-03-28 13:38:22 +08004087 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4088 {
you.chen35020192022-05-06 11:30:57 +08004089 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4090 return 0;
4091 }
4092
qs.xiong9fbf74e2023-03-28 13:38:22 +08004093 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4094 {
you.chen35020192022-05-06 11:30:57 +08004095 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4096 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004097 else
4098 {
you.chen35020192022-05-06 11:30:57 +08004099 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4100 }
4101
4102 return 0;
4103}
4104
4105int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4106// CHECK_IDX(idx, CTRL_AP);
4107// int ret = 0;
4108// size_t reply_len = MAX_RET;
4109// char cmd_reply[MAX_RET]={0};
4110// const char * cmd_str = "GET country";
4111// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4112// do{
4113// if (NULL == s_lynq_wpa_ctrl) {
4114// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4115// if (NULL == s_lynq_wpa_ctrl ) {
4116// printf("wpa_ctrl_open fail\n");
4117// return -1;
4118// }
4119// }
4120// }while(0);
4121
4122// do {
4123// reply_len = MAX_RET;
4124// cmd_reply[0] = '\0';
4125// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004126// 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 +08004127// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004128// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004129// return ret;
4130// }
4131// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004132// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004133// }while(0);
4134
4135 FILE *fp;
4136 size_t i = 0;
4137 char lynq_cmd_ret[MAX_RET]={0};
4138
4139// CHECK_IDX(idx, CTRL_AP);
4140
4141 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004142 {
4143 perror("popen error!");
4144 return -1;
4145 }
you.chen35020192022-05-06 11:30:57 +08004146 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4147 {
4148 perror("fread fail!");
4149 return -1;
4150 }
4151
qs.xiong9fbf74e2023-03-28 13:38:22 +08004152 for(i=0; i < strlen(lynq_cmd_ret); i++)
4153 {
4154 if (lynq_cmd_ret[i] == ' ')
4155 {
you.chen35020192022-05-06 11:30:57 +08004156 lynq_cmd_ret[i] = '\0';
4157 break;
4158 }
4159 }
4160
4161 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004162 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004163
4164 int ret=pclose(fp);
4165 if(ret==-1)
4166 {
4167 perror("close file faild");
4168 }
4169
4170 return 0;
4171}
4172
qs.xiong44fac672023-08-29 16:15:55 +08004173
you.chen705a7ef2023-06-01 22:06:45 +08004174static int check_and_init_uci_config(char * country_code)
4175{
4176 FILE * fp;
4177 int is_different = 0;
4178 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4179 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4180 const char * commit_uci_cmd ="uci commit";
4181 char set_country_cmd[MAX_CMD];
4182 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004183
you.chen705a7ef2023-06-01 22:06:45 +08004184 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004185
you.chen705a7ef2023-06-01 22:06:45 +08004186 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004187 {
you.chen705a7ef2023-06-01 22:06:45 +08004188 if (0 != system(create_uci_cmd))
4189 {
4190 RLOGE("creat_uci_cmd fail");
4191 return -1;
4192 }
4193 is_different = 1;
4194 }
4195
4196 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4197 {
4198 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004199 return -1;
4200 }
4201
you.chen705a7ef2023-06-01 22:06:45 +08004202 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004203 {
you.chen705a7ef2023-06-01 22:06:45 +08004204 RLOGE("fread fail!");
4205 fclose(fp);
4206 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004207 }
4208
you.chen705a7ef2023-06-01 22:06:45 +08004209 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4210 {
qs.xiong44fac672023-08-29 16:15:55 +08004211 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 +08004212 is_different = 1;
4213 }
4214
4215 fclose(fp);
4216
4217 if (is_different)
4218 {
4219 if ( 0 != system(set_country_cmd))
4220 {
4221 RLOGE("set_country_cmd fail");
4222 return -1;
4223 }
4224 if (0 != system(commit_uci_cmd))
4225 {
4226 RLOGE("commmit fail");
4227 }
4228 }
4229
4230 return is_different;
4231}
4232
4233int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4234 char check_current_code[10];
4235 const char * support_country[] = {"CN", "EU"};
4236
4237 int ret,is_different, i, cc_count;
4238
4239 if (country_code == NULL || country_code[0] == '\0')
4240 {
4241 RLOGE("bad country code\n");
4242 return -1;
4243 }
4244
4245 cc_count = sizeof (support_country) / sizeof (char*);
4246 for(i=0; i < cc_count; i++)
4247 {
4248 if (strcmp(support_country[i], country_code) == 0)
4249 {
4250 break;
4251 }
4252 }
4253
4254 if (i >= cc_count)
4255 {
4256 RLOGE("unspported country code %s\n", country_code);
4257 return -1;
4258 }
4259
4260 is_different = check_and_init_uci_config(country_code);
4261 if( is_different < 0 )
4262 {
4263 RLOGE("init set uci fail\n");
4264 return -1;
4265 }
4266
4267 ret = lynq_get_country_code(idx,check_current_code);
4268 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4269 {
4270 ret = lynq_wifi_disable();
4271 if(ret != 0 )
4272 {
4273 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4274 return -1;
4275 }
4276 }
4277
4278 return 0;
you.chen35020192022-05-06 11:30:57 +08004279}
4280
4281int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4282{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004283 RLOGD("enter lynq_get_connect_ap_mac\n");
4284 if (mac == NULL)
4285 {
4286 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004287 return -1;
4288 }
4289
4290 CHECK_IDX(idx, CTRL_STA);
4291 ap_info_s ap;
4292 ap.ap_mac[0] = '\0';
4293
qs.xiong9fbf74e2023-03-28 13:38:22 +08004294 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4295 {
you.chen35020192022-05-06 11:30:57 +08004296 return -1;
4297 }
4298 strcpy(mac, ap.ap_mac);
4299
4300 return 0;
4301}
4302
4303int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4304{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004305 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004306 struct ifaddrs *ifaddr_header, *ifaddr;
4307 struct in_addr * ifa;
4308 const char * ifaName = "wlan0";
4309 if (ip == NULL)
4310 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004311 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004312 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004313 }
you.chenf58b3c92022-06-21 16:53:48 +08004314
qs.xiong9fbf74e2023-03-28 13:38:22 +08004315 if (idx == 1)
4316 {
you.chen0df3e7e2023-05-10 15:56:26 +08004317 ifaName = inner_get_ap_interface_name();
4318 if (ifaName == NULL)
4319 {
4320 RLOGE("[lynq_get_interface_ip] ap name get fail");
4321 return -1;
4322 }
you.chen9ac66392022-08-06 17:01:16 +08004323 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004324 else if (idx != 0)
4325 {
you.chen35020192022-05-06 11:30:57 +08004326 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004327 }
you.chen35020192022-05-06 11:30:57 +08004328
you.chen9ac66392022-08-06 17:01:16 +08004329 if (getifaddrs(&ifaddr_header) == -1)
4330 {
you.chen35020192022-05-06 11:30:57 +08004331 perror("getifaddrs");
4332 return -1;
4333 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004334 }
you.chen35020192022-05-06 11:30:57 +08004335
4336
you.chen9ac66392022-08-06 17:01:16 +08004337 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4338 {
4339 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004340 continue;
you.chen9ac66392022-08-06 17:01:16 +08004341 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4342 {
4343 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4344 {
4345 // is a valid IP4 Address
4346 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4347 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004348 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004349 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004350 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004351 return 0;
4352 }
4353 }
4354 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004355 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004356 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004357 return -1;
you.chen35020192022-05-06 11:30:57 +08004358}
4359
4360int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4361{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004362 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004363 int count;
4364 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004365 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004366 char *split_words[128] = {0};
4367 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4368
4369 CHECK_WPA_CTRL(idx);
4370
4371 DO_REQUEST(lynq_get_mac_cmd);
4372
qs.xiong9fbf74e2023-03-28 13:38:22 +08004373 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4374 {
4375 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004376 return -1;
4377 }
4378
4379 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4380
qs.xiong9fbf74e2023-03-28 13:38:22 +08004381 if (count < 2)
4382 {
you.chen35020192022-05-06 11:30:57 +08004383 return -1;
4384 }
4385
qs.xiong9fbf74e2023-03-28 13:38:22 +08004386 for (i=0; i < strlen(split_words[1]); i++ )
4387 {
4388 if (split_words[1][i] != ' ')
4389 {
you.chen35020192022-05-06 11:30:57 +08004390 break;
4391 }
4392 }
4393
qs.xiongdd6e44c2023-08-08 15:02:53 +08004394 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004395
4396 return 0;
4397}
4398
4399int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4400{
4401// int count;
4402// char *split_words[128] = {0};
4403// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4404
4405// if (rssi == NULL) {
4406// return -1;
4407// }
4408
4409// CHECK_IDX(idx, CTRL_STA);
4410
4411// CHECK_WPA_CTRL(CTRL_STA);
4412
4413// DO_REQUEST(lynq_get_rssi_cmd);
4414
4415// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4416// return -1;
4417// }
4418
4419// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4420
4421// if (count < 2) {
4422// return -1;
4423// }
4424
4425// *rssi = atoi(split_words[1]) * -1;
4426
you.chen35020192022-05-06 11:30:57 +08004427 char lynq_cmd_ret[MAX_RET]={0};
4428
qs.xiongff0ae0f2022-10-11 15:47:14 +08004429/*******change other cmd to get rssi*******
4430 *
4431 *wl rssi ---> wl -i wlan0 rssi
4432 *
4433 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004434 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004435 {
you.chen23c4a5f2023-04-12 16:46:00 +08004436 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004437 return -1;
4438 }
you.chen9f17e4d2022-06-06 17:18:18 +08004439 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004440/****** if got rssi is 0,means sta didn't connected any device****/
4441 if(*rssi == 0)
4442 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004443 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004444 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004445 }
you.chen35020192022-05-06 11:30:57 +08004446
4447 return 0;
4448}
4449
4450int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4451{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004452 RLOGD("enter lynq_get_connect_ap_band\n");
4453 if (band == NULL)
4454 {
you.chen35020192022-05-06 11:30:57 +08004455 return -1;
4456 }
4457
4458 CHECK_IDX(idx, CTRL_STA);
4459 ap_info_s ap;
4460 ap.band = -1;
4461
qs.xiong9fbf74e2023-03-28 13:38:22 +08004462 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4463 {
you.chen35020192022-05-06 11:30:57 +08004464 return -1;
4465 }
4466 *band = ap.band;
4467
4468 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004469}
you.chenf58b3c92022-06-21 16:53:48 +08004470
4471int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4472{
you.chenb95401e2023-05-12 19:39:06 +08004473 int ret;
4474 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004475 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004476
4477 if (ip == NULL)
4478 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004479 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004480 return -1;
4481 }
4482
4483 CHECK_IDX(idx, CTRL_STA);
4484
qs.xionge4cbf1c2023-02-28 18:22:49 +08004485 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004486 {
4487 return -1;
4488 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004489
you.chenb95401e2023-05-12 19:39:06 +08004490 ip[0] = '\0';
4491 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4492 if (ret != 0)
4493 {
4494 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4495 return -1;
4496 }
4497
4498 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4499 {
4500 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004501 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004502 if (ret != 0)
4503 {
4504 ip[0] = '\0';
4505 return 0;
4506 }
4507 else
4508 {
4509 p = strchr(ip, '\n');
4510 if (p != NULL)
4511 {
4512 *p = '\0';
4513 }
4514 }
4515 }
4516 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004517}
4518
qs.xiong026c5c72022-10-17 11:15:45 +08004519int lynq_ap_connect_num(int sta_number)
4520{
4521 char lynq_limit_cmd[32]={0};
4522 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004523 if((sta_number < 1 ) && (sta_number > 15))
4524 {
4525 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004526 return -1;
4527 }
4528 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4529 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004530 if(ret != 0)
4531 {
4532 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004533 }
4534 return 0;
4535}
you.chenf58b3c92022-06-21 16:53:48 +08004536
qs.xiong77905552022-10-17 11:19:57 +08004537int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4538{
4539
4540 char lynq_wifi_acs_cmd[128]={0};
4541 char lynq_cmd_mode[128]={0};
4542 char lynq_cmd_slect[128]={0};
4543
qs.xiong9fbf74e2023-03-28 13:38:22 +08004544 if((acs_mode != 2) && (acs_mode != 5))
4545 {
qs.xiong77905552022-10-17 11:19:57 +08004546 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4547 }
4548
qs.xiong9fbf74e2023-03-28 13:38:22 +08004549 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4550 {
qs.xiong77905552022-10-17 11:19:57 +08004551 return -1;
4552 }
4553
4554 CHECK_IDX(idx, CTRL_AP);
4555
4556 CHECK_WPA_CTRL(CTRL_AP);
4557
4558 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4559 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4560 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4561
4562 DO_OK_FAIL_REQUEST(cmd_disconnect);
4563 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4564 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4565 DO_OK_FAIL_REQUEST(cmd_save_config);
4566 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4567
4568 return 0;
4569}
you.chen0f5c6432022-11-07 18:31:14 +08004570//you.chen add for tv-box start
4571static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4572 FILE *fp;
4573 //printf("to exec cmd:%s\n", str_cmd);
4574 if((fp=popen(str_cmd,"r"))==NULL)
4575 {
4576 perror("popen error!");
4577 return -1;
4578 }
4579 if((fread(str_cmd_ret,max_len,1,fp))<0)
4580 {
4581 perror("fread fail!");
4582 fclose(fp);
4583 return -1;
4584 }
4585 fclose(fp);
4586 return 0;
4587}
4588
4589static int get_netmask_length(const char* mask)
4590{
4591 int masklen=0, i=0;
4592 int netmask=0;
4593
4594 if(mask == NULL)
4595 {
4596 return 0;
4597 }
4598
4599 struct in_addr ip_addr;
4600 if( inet_aton(mask, &ip_addr) )
4601 {
4602 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004603 }else
4604 {
you.chen0f5c6432022-11-07 18:31:14 +08004605 netmask = 0;
4606 return 0;
4607 }
4608
4609 while(0 == (netmask & 0x01) && i<32)
4610 {
4611 i++;
4612 netmask = netmask>>1;
4613 }
4614 masklen = 32-i;
4615 return masklen;
4616}
4617
4618static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4619 int mask_len;
4620 char *p;
4621 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004622 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4623 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004624 return -1;
4625 p = strstr(str_cmd_ret, "Mask:");
4626 if (p == NULL)
4627 return -1;
4628 mask_len = get_netmask_length(p + 5);
4629 if (mask_len == 0)
4630 return -1;
4631 p = strstr(str_cmd_ret, "inet addr:");
4632 if (p == NULL)
4633 return -1;
4634 strcpy(tmp, p + 10);
4635 p = strstr(tmp, " ");
4636 if (p != NULL)
4637 *p = '\0';
4638 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4639 return 0;
4640}
4641
4642static void GBWWatchThreadProc() {
4643 int i,n, nloop, nmax, ncheckcount, nidlecount;
4644 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4645 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4646 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4647 char *results[16] = {0};
4648 char str_cmd[256] = {0};
4649 char str_cmd_ret[128] = {0};
4650 char dest_ip[32] = {0};
4651 lastAP1Bytes = lastAP2Bytes = 0;
4652 lastAP1Drop = lastAP2Drop = 0;
4653 lastAP1Speed = lastAP2Speed = 0;
4654 setAP1Speed = 50;
4655 setAP2Speed = 80;
4656 nloop = 0;
4657 nmax = 6;
4658 ncheckcount = nidlecount = 0;
4659
you.chen0df3e7e2023-05-10 15:56:26 +08004660 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004661 {
4662 RLOGE("------gbw thread run\n");
4663 return;
4664 }
4665
qs.xiong9fbf74e2023-03-28 13:38:22 +08004666 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004667 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4668 while (dest_ip[0] == '\0') {
4669 sleep(1);
4670 str_cmd_ret[0] = '\0';
4671 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4672 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4673 if (str_cmd_ret[n] == '\n'){
4674 str_cmd_ret[n] = '\0';
4675 break;
4676 }
4677 }
4678 if (str_cmd_ret[0] != '\0')
4679 {
4680 strcpy(dest_ip, str_cmd_ret);
4681 }
4682 }
4683
you.chenc9928582023-04-24 15:39:37 +08004684 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4685 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4686 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 +08004687 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4688 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004689 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004690 return;
4691 }
you.chenc9928582023-04-24 15:39:37 +08004692 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);
4693 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);
4694 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 +08004695
4696 while (1) {
4697 sleep(1);
4698 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004699 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4700 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4701 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004702 continue;
4703 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004704 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004705 if (n > 9) {
4706 if (strcmp(results[1], "Sent") == 0) {
4707 currAP1Bytes = atoll(results[2]);
4708 }
4709 if (strcmp(results[6], "(dropped") == 0) {
4710 currAP1Drop = atoi(results[7]);
4711 }
4712 }
4713
4714 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004715 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4716 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4717 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004718 continue;
4719 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004720 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004721 if (n > 9) {
4722 if (strcmp(results[1], "Sent") == 0) {
4723 currAP2Bytes = atoll(results[2]);
4724 }
4725 if (strcmp(results[6], "(dropped") == 0) {
4726 currAP2Drop = atoi(results[7]);
4727 }
4728 }
4729
4730 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4731 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4732 lastAP1Bytes = currAP1Bytes;
4733 lastAP2Bytes = currAP2Bytes;
4734 continue;
4735 }
4736
4737 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4738 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4739 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4740 lastAP1Speed = currAP1Speed;
4741 lastAP2Speed = currAP2Speed;
4742 lastAP1Bytes = currAP1Bytes;
4743 lastAP2Bytes = currAP2Bytes;
4744
4745 currSetAP1Speed = setAP1Speed;
4746 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4747 ncheckcount++;
4748 if (ncheckcount > 3) {
4749 ncheckcount = 0;
4750 currSetAP1Speed = 5;
4751 }
4752 }
4753 else {
4754 ncheckcount = 0;
4755 if (currAP1Speed < 5)
4756 nidlecount++;
4757 else
4758 nidlecount = 0;
4759
4760 }
4761
4762 if (nidlecount > 60 ){
4763 currSetAP1Speed = 50;
4764 }
4765
4766 if (currSetAP1Speed != setAP1Speed) {
4767 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004768 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4769 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004770 }
4771 }
4772}
4773
4774int enableGBW(const char* mac) {
4775 int i,len;
4776 char get_ipaddr_cmd[128]={0};
4777 ap_info_s *ap;
4778 device_info_s * list;
4779
4780 if (mac == NULL || g_gbw_enabled == 1)
4781 return -1;
4782 len = strlen(mac);
4783 g_gbw_mac = malloc(len + 1);
4784 for(i=0;i<len;i++) {
4785 if (mac[i] >= 'A' && mac[i] <= 'Z')
4786 {
4787 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4788 }
4789 else
4790 g_gbw_mac[i] = mac[i];
4791 }
4792 g_gbw_mac[i] = '\0';
4793 g_gbw_enabled = 1;
4794
4795 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4796 if (system(get_ipaddr_cmd) == 0) {
4797 //startGBW();
4798 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4799 for (i=0;i<len;i++) {
4800 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4801 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4802 startGBW();
4803 }
4804 free(ap);
4805 free(list);
4806 }
4807 }
4808 return 0;
4809}
4810
4811int disableGBW() {
4812 stopGBW();
4813 free(g_gbw_mac);
4814 g_gbw_mac = NULL;
4815 g_gbw_enabled = 1;
4816 return 0;
4817}
4818
4819static int startGBW() {
4820 if (g_gbw_watcher_pid != 0) {
4821 stopGBW();
4822 }
4823 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4824}
4825
4826static int stopGBW() {
4827 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004828 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004829 pthread_cancel(g_gbw_watcher_pid);
4830 pthread_join(g_gbw_watcher_pid, &retval);
4831 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004832 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4833 if (s_ap_iterface_name[0] != '\0')
4834 {
4835 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4836 system(cmd);
4837 }
you.chen0f5c6432022-11-07 18:31:14 +08004838}
4839//you.chen add for tv-box end