blob: 476be41cb0bc43fbe868e5566c7e89affc07adb7 [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";
qs.xiong5a2ba932023-09-13 16:30:21 +080079const char * STATE_SCANNING = "SCANNING";
you.chen6d247052023-06-01 16:39:54 +080080const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080081
you.chenf711c8a2023-04-13 13:49:45 +080082const char * cmd_ping = "PING";
83const char * rsp_pong = "PONG";
84const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
85const int MAX_IDLE_COUNT = 600; // 60s
86
you.chenc9928582023-04-24 15:39:37 +080087const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
88const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
89const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
90const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
91const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
92
93static char s_ap_iterface_name[64] = {0};
94
you.chend2fef3f2023-02-13 10:50:35 +080095struct local_wpa_ctrl{
96 struct wpa_ctrl *ctrl;
97 pthread_mutex_t mutex;
98};
99
you.chen70f377f2023-04-14 18:17:09 +0800100
you.chend2fef3f2023-02-13 10:50:35 +0800101static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800102static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
103static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800104// add for auto connect
105static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800106
107static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800108
you.chen0f5c6432022-11-07 18:31:14 +0800109//you.chen add for tv-box start
110volatile int g_gbw_enabled = 0;
111char * g_gbw_mac = NULL;
112pthread_t g_gbw_watcher_pid = 0;
113static int startGBW();
114static int stopGBW();
115//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800116
117typedef struct __curr_status_info {
118 ap_info_s *ap;
119 char * state;
120 int net_no;
121}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400122
you.chen70f377f2023-04-14 18:17:09 +0800123typedef enum {
124 INNER_STA_STATUS_INIT = 0,
125 INNER_STA_STATUS_CONNECTING,
126 INNER_STA_STATUS_ASSOCIATING,
127 INNER_STA_STATUS_ASSOCIATED,
128 INNER_STA_STATUS_CONNECTED,
129 INNER_STA_STATUS_DISCONNECTING,
130 INNER_STA_STATUS_DISCONNECTED,
131 INNER_STA_STATUS_CANCEL,
132}inner_sta_status_s;
133
134static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
135static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
136static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
137static error_number_s s_sta_error_number = -1;
138static char s_sta_current_connecting_ssid[64] = {0};
139static struct timespec s_sta_connect_timeout;
140const int MAX_CONNNECT_TIME = 15; // second
141pthread_t g_global_watcher_pid = 0;
142static int s_service_invoke_timeout_cnt=0;
143const int FAKE_MAX_INT_VALUE = 99999;
144
145static void notify_service_invoke_fail(int error)
146{
147 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
148 pthread_mutex_lock(&s_global_check_mutex);
149 if (error == -2) //timeout
150 {
151 s_service_invoke_timeout_cnt++;
152 if (s_service_invoke_timeout_cnt > 10)
153 {
154 pthread_cond_signal(&s_global_check_cond);
155 }
156 }
157 else if (error == -1)
158 {
159 // check if can connect wpa service
160 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
161 if (lynq_wpa_ctrl == NULL)
162 {
163 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
164 pthread_cond_signal(&s_global_check_cond);
165 }
166 wpa_ctrl_close(lynq_wpa_ctrl);
167 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
168 if (lynq_wpa_ctrl == NULL)
169 {
170 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
171 pthread_cond_signal(&s_global_check_cond);
172 }
173 wpa_ctrl_close(lynq_wpa_ctrl);
174 }
175
176 pthread_mutex_unlock(&s_global_check_mutex);
177}
178
you.chenc9928582023-04-24 15:39:37 +0800179static int system_call_v(const char * fmt, ...)
180{
181 char str_cmd[256] = {0};
182 va_list args;
183 va_start(args, fmt);
184 vsprintf(str_cmd, fmt, args);
185 va_end(args);
186 printf("system call----------%s\n", str_cmd);
187 return system(str_cmd);
188}
189
you.chen0df3e7e2023-05-10 15:56:26 +0800190static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
191
192static const char * inner_get_ap_interface_name()
193{
194 char * p;
195 char cmd[128]={0};
196
197 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
198 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
199 {
200 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
201 return NULL;
202 }
203 p = strchr(s_ap_iterface_name, ' ');
204 if (NULL != p)
205 {
206 *p = '\0';
207 }
208 p = strchr(s_ap_iterface_name, '\n');
209 if (NULL != p)
210 {
211 *p = '\0';
212 }
213 if (s_ap_iterface_name[0] == '\0')
214 {
215 return NULL;
216 }
217
218 return s_ap_iterface_name;
219}
220
you.chen70f377f2023-04-14 18:17:09 +0800221static void check_tether_and_notify()
222{
223 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800224 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 +0800225 {
226 return;
227 }
228 pthread_mutex_lock(&s_global_check_mutex);
229 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
230 pthread_cond_signal(&s_global_check_cond);
231 pthread_mutex_unlock(&s_global_check_mutex);
232}
233
you.chend2fef3f2023-02-13 10:50:35 +0800234static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
235 char *reply, size_t *reply_len,
236 void (*msg_cb)(char *msg, size_t len))
237{
238 int ret;
239 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800240 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800241 return -1;
242 }
243 pthread_mutex_lock(&ctrl->mutex);
244 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
245 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800246 if (ret != 0)
247 {
248 notify_service_invoke_fail(ret);
249 }
you.chend2fef3f2023-02-13 10:50:35 +0800250 return ret;
251}
252
253static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
254 int repeat_cnt;
255 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
256 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800257 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800258 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
259 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
260// printf("wait enable finish\n");
261 usleep(500 * 1000);
262 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
263 }
264 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800265 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800266 goto out_addr;
267 }
268 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
269 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
270 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800271 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800272 goto out_addr;
273 }
274 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
275 }
276 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
277out_addr:
278 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
279 return lynq_wpa_ctrl;
280}
281
qs.xiong97fa59b2022-04-07 05:41:29 -0400282#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400283{\
you.chen35020192022-05-06 11:30:57 +0800284 perror((str));\
285 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400286}
287
you.chen35020192022-05-06 11:30:57 +0800288#define CHECK_IDX(idx, type) do { \
289 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
290 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800291 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800292 return -1; \
293 } \
294 }while (0)
295
296#define CHECK_WPA_CTRL(index) int ret = 0;\
297 size_t reply_len = MAX_RET; \
298 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800299 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800300 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800301 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
302 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800303 }while(0)
304
305#define DO_REQUEST(cmd_str) do { \
306 reply_len = MAX_RET;\
307 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800308 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800309 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 +0800310 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800311 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800312 return ret; \
313 } \
314 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800315 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800316 }while(0)
317
318#define DO_OK_FAIL_REQUEST(cmd_str) do { \
319 DO_REQUEST(cmd_str); \
320 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800321 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800322 return -1; \
323 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800324 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800325 return -1; \
326 } \
327 }while (0)
328
329
you.chenf711c8a2023-04-13 13:49:45 +0800330static int check_connection(struct wpa_ctrl * wpa_ctrl)
331{
332 size_t reply_len = MAX_RET;
333 char cmd_reply[MAX_RET]={0};
334 int ret;
335
336 RLOGD("check_connection [%p]", wpa_ctrl);
337 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
338
339 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
340 {
341 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 +0800342 if (ret != 0)
343 {
344 notify_service_invoke_fail(ret);
345 }
you.chenf711c8a2023-04-13 13:49:45 +0800346 return -1;
347 }
348
349 return 0;
350}
351
352/**
353 * @brief check_pending_msg
354 * @param lynq_wpa_ctrl
355 * @return 1 has msg, 0 no msg, -1 error
356 */
357static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
358{
359 int ret;
360
361 if (*pp_lynq_wpa_ctrl == NULL) // need connect
362 {
363 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
364 if (*pp_lynq_wpa_ctrl == NULL)
365 {
366 usleep(SLEEP_TIME_ON_IDLE);
367 return -1;
368 }
369
370 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
371 if (ret == 0) // attach success
372 {
373 *started_flag = 1;
374 }
375 else
376 {
you.chen70f377f2023-04-14 18:17:09 +0800377 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800378 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
379 *pp_lynq_wpa_ctrl = NULL;
380 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800381 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800382 usleep(SLEEP_TIME_ON_IDLE);
383 return -1;
384 }
385 }
386
387 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
388 if ( ret == 0) // no pending messages
389 {
390 usleep(SLEEP_TIME_ON_IDLE);
391 *idle_count += 1;
392 if (*idle_count > MAX_IDLE_COUNT)
393 {
394 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
395 {
396 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
397 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
398 *pp_lynq_wpa_ctrl = NULL;
399 *idle_count = 0;
400 return -1;
401 }
402 *idle_count = 0;
403 }
404 return 0;
405 }
406 else if ( ret == -1) // on error
407 {
408 RLOGE("[wifi error]sta wpa_ctrl_pending");
409 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
410 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
411 *pp_lynq_wpa_ctrl = NULL;
412 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800413 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800414 return -1;
415 }
416
417 *idle_count = 0;
418 return 1;
419}
420
you.chen6d247052023-06-01 16:39:54 +0800421static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
422{
423 pthread_mutex_lock(&s_ap_callback_mutex);
424 if (g_ap_callback_func != NULL)
425 g_ap_callback_func(g_ap_callback_priv, status);
426 pthread_mutex_unlock(&s_ap_callback_mutex);
427}
428
you.chen35020192022-05-06 11:30:57 +0800429static void APWatcherThreadProc() {
430 size_t len = MAX_RET;
431 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800432 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800433
you.chen6c2dd9c2022-05-16 17:55:28 +0800434 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800435 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800436
qs.xiong9fbf74e2023-03-28 13:38:22 +0800437 while (g_ap_watcher_stop_flag == 0)
438 {
you.chenf711c8a2023-04-13 13:49:45 +0800439 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
440 {
you.chen70f377f2023-04-14 18:17:09 +0800441 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
442 {
443 check_tether_and_notify();
444 }
445
you.chen35020192022-05-06 11:30:57 +0800446 continue;
447 }
you.chenf711c8a2023-04-13 13:49:45 +0800448
you.chen6c2dd9c2022-05-16 17:55:28 +0800449 memset(msg_notify, 0, MAX_RET);
450 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800451 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800452 {
you.chen35020192022-05-06 11:30:57 +0800453 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800454 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800455 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800456 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800457 {
you.chen6d247052023-06-01 16:39:54 +0800458 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800459 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800460 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800461 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
462 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800463 {
you.chen0f5c6432022-11-07 18:31:14 +0800464 stopGBW();
465 }
466 }
you.chen35020192022-05-06 11:30:57 +0800467 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800468 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800469 {
you.chen6d247052023-06-01 16:39:54 +0800470 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800471 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800472 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800473 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
474 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800475 {
you.chen0f5c6432022-11-07 18:31:14 +0800476 startGBW();
477 }
478 }
you.chen35020192022-05-06 11:30:57 +0800479 }
qs.xiong31163d62023-07-11 18:54:40 +0800480 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 )
481 {
482 RLOGD("APWatcherThreadProc ap------> service error");
483 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
484 }
485 else
486 {
487 RLOGD("APWatcherThreadProc ap------> going on check next msg");
488 }
you.chenf711c8a2023-04-13 13:49:45 +0800489 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800490 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
491 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800492 if (lynq_wpa_ctrl != NULL)
493 {
you.chen92fd5d32022-05-25 10:09:47 +0800494 wpa_ctrl_detach(lynq_wpa_ctrl);
495 wpa_ctrl_close(lynq_wpa_ctrl);
496 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400497}
498
you.chen70f377f2023-04-14 18:17:09 +0800499static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
500{
501 char * p;
502 const char * try_associat_flag = "Trying to associate";
503 const char * associated_flag = "Associated with ";
504
505 pthread_mutex_lock(&s_global_check_mutex);
506 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
507 {
508 pthread_mutex_unlock(&s_global_check_mutex);
509 return;
510 }
511
512 if (state == LYNQ_WIFI_STATUS_EGNORE)
513 {
514 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
515 {
516 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
517 }
518 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
519 {
520 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
521 }
522 }
523 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
524 {
525 s_sta_error_number = error_num;
526 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
527 {
528 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
529 pthread_cond_signal(&s_global_check_cond);
530 }
531 }
532 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
533 {
534 s_sta_status = INNER_STA_STATUS_CONNECTED;
535 pthread_cond_signal(&s_global_check_cond);
536 }
537 pthread_mutex_unlock(&s_global_check_mutex);
538}
539
qs.xiong44fac672023-08-29 16:15:55 +0800540/*
541just tmp add for fix sta connect ap fail check ap connect info
542return 0 --->Current no sta device connect this AP
543*/
544static int lynq_connected_ap_sta_status() {
545
546 FILE *fp;
547 size_t i = 0;
548 int ret;
549 char lynq_cmd_ret[MAX_RET]={0};
550
551 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
552 {
553 perror("popen error!");
554 return -1;
555 }
556 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
557 {
558 perror("fread fail!");
559 ret=pclose(fp);
560 if(ret == -1)
561 perror("close file faild");
562 return -1;
563 }
564 if( strlen(lynq_cmd_ret) < MAC_LEN)
565 {
566 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
567 ret=pclose(fp);
568 if(ret==-1)
569 {
570 perror("close file faild");
571 }
572 return 0;
573 }else{
574 ret=pclose(fp);
575 if(ret==-1)
576 {
577 perror("close file faild");
578 }
579 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
580 return 1;
581 }
582}
583
584/*
585 just tmp add for fix sta connect ap fail; check fw status
586 return 1 ----> fw status error; need wl down/up
587*/
588static int check_current_fw_status() {
589
590 FILE *fp;
591 FILE *fp1;
592 size_t i = 0;
593 int ret;
594 char lynq_cmd_ret_2g[MAX_RET]={0};
595 char lynq_cmd_ret_5g[MAX_RET]={0};
596
597 const char * fw_status = "0x0096"; //0x0096 is normal fw status
598
599 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
600 {
601 perror("popen error!");
602 return -1;
603 }
604 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
605 {
606 perror("fread fail!");
607 if(pclose(fp) == -1)
608 perror("close fp file faild");
609 return -1;
610 }
611
612 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
613 {
614 perror("popen error!");
615 if(pclose(fp) == -1)
616 perror("clsoe fp file faild");
617 return -1;
618 }
619 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
620 {
621 perror("fread fail!");
622 if(pclose(fp1) == -1)
623 perror("clsoe fp1 file faild");
624 if(pclose(fp) == -1)
625 perror("clsoe fp file faild");
626 return -1;
627 }
628
629 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
630 {
631 ret=pclose(fp);
632 if(ret==-1)
633 {
634 perror("close fp file faild");
635 }
636 ret=pclose(fp1);
637 if(ret==-1)
638 {
639 perror("close fp1 file faild");
640 }
641 return 0;
642 }else
643 {
644 ret=pclose(fp);
645 if(ret==-1)
646 {
647 perror("close file faild");
648 }
649 if(pclose(fp1) == -1)
650 {
651 perror("clsoe file fp1 faild");
652 }
653 RLOGD("current fw status --error--");
654 return 1;
655 }
656}
657
qs.xiong1d4263a2023-09-06 10:46:23 +0800658/*
659eg: wl counters info
660sh-3.2# wl counters
661counters_version 30
662datalen 1648
663Slice_index: 0
664reinitreason_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)
665reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
666
667check reinit status
668return 0 ===> fw did wl reinit cmd
669*/
670static int check_current_reinit_info()
671{
672 FILE *fp;
673 int ret;
674 char lynq_cmd_ret[MAX_RET]={0};
675 char * dest;
676 char destid[3]={0};
677 if((fp=popen("wl counters","r"))==NULL)
678 {
679 perror("popen error!");
680 return -1;
681 }
682 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
683 {
684 perror("fread fail!");
685 if(pclose(fp) == -1)
686 {
687 perror("close fp file faild");
688 }
689 return -1;
690 }
691 dest = strstr(lynq_cmd_ret,"reinit ");
692 if(dest != NULL)
693 {
694 dest +=strlen("reinit ");
695 RLOGD("current get dest str is %s",dest);
696 memcpy(destid,dest,2);
697 ret = atoi(destid);
698 RLOGD("get current wl counters cmd return counts is %d",ret);
699 if( ret != 0 )
700 {
701 RLOGD("current fw did run cmd wl reinit");
702 if( pclose(fp) == -1 )
703 {
704 perror("close fp file faild");
705 }
706 return 0;
707 }
708 }
709 if( pclose(fp) == -1 )
710 {
711 perror("close fp file faild");
712 }
713 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
714 return -1;
715}
716
qs.xiong44fac672023-08-29 16:15:55 +0800717static void APTmpWatcherThreadProc() {
718
719 int i = 0;
720 int delytime = 300;
721 g_ap_tmp_watcher_stop_flag = 0;
722
723 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
724 while(1)
725 {
726 sleep(1);
727 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800728 if ( (i % 30) == 0 )
729 {
730 if ( check_current_reinit_info() == 0 )
731 {
732 system("wl reset_cnts");
733 system("wl down");
734 system("wl up");
735 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
736 }
737 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800738 if ( (i % 10) == 0 )
qs.xiong44fac672023-08-29 16:15:55 +0800739 {
qs.xiong08e6aac2023-09-06 23:12:27 +0800740 if( lynq_connected_ap_sta_status() == 0 ) //0 --->no sta device connect this ap
qs.xiong44fac672023-08-29 16:15:55 +0800741 {
742 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
743 {
744 system("wl down");
745 system("wl up");
746 }
qs.xiong44fac672023-08-29 16:15:55 +0800747 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800748 }
749 if ( i == delytime )
750 {
qs.xiong44fac672023-08-29 16:15:55 +0800751 i = 0;
752 }
753 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
754 {
755 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
756 return;
757 }
qs.xiong08e6aac2023-09-06 23:12:27 +0800758
qs.xiong44fac672023-08-29 16:15:55 +0800759 }
760
761}
762
qs.xiongf0128b12023-06-29 17:29:39 +0800763static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
764{
765 char LYNQ_DISABLE_CMD[128]={0};
766
767 CHECK_IDX(idx, CTRL_STA);
768 CHECK_WPA_CTRL(CTRL_STA);
769
770 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
771 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
772 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800773 DO_OK_FAIL_REQUEST(cmd_save_config);
774
qs.xiongf0128b12023-06-29 17:29:39 +0800775 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800776
qs.xiongf0128b12023-06-29 17:29:39 +0800777}
778
779
qs.xiong455c30b2023-04-12 11:40:02 +0800780void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
781{
782 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800783 char *wpanetid;
784 char destid[3] = {0};
785 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800786 *error = LYNQ_WAIT_CONNECT_ACTIVE;
787 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
788 {
789 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
790 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
791 return;
792 }
793
qs.xiong20202422023-09-06 18:01:18 +0800794 if (strstr(modify, "Trying to associate") != NULL)
795 {
796 RLOGD("Current sta is Trying to associate");
797 *state = LYNQ_WIFI_STATUS_EGNORE;
798 g_sta_conncet_status_flag = 1;
799 return;
800 }
801
qs.xiong455c30b2023-04-12 11:40:02 +0800802 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
803 {
804 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
805 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800806 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800807 return;
808 }
809
810 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
811 {
qs.xiongf0128b12023-06-29 17:29:39 +0800812 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
813 wpanetid = strstr(modify,"id=");
814 if ( wpanetid != NULL )
815 {
816 wpanetid +=strlen("id=");
817 memcpy(destid,wpanetid,2);
818 tmpdisid = atoi(destid);
819
820 }
qs.xiong455c30b2023-04-12 11:40:02 +0800821 pReason = strstr(modify, "reason=");
822 if (pReason != NULL)
823 {
824 pReason += strlen("reason=");
825 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
826 {
827 *error = LYNQ_TIME_OUT;
828 }
829 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
830 {
831 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800832 // tmp fix sta autoconnect connect and disconnect
833 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
834 {
835 RLOGE("stop wlan0 network %d fail",tmpdisid);
836 }
qs.xiong455c30b2023-04-12 11:40:02 +0800837 }
838 else
839 {
840 *error = LYNQ_UNSPECIFIED_REASON;
841 }
qs.xiong455c30b2023-04-12 11:40:02 +0800842 }
843 else
844 {
845 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800846 }
qs.xiongf0128b12023-06-29 17:29:39 +0800847 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
qs.xiong20202422023-09-06 18:01:18 +0800848 g_sta_conncet_status_flag = 0;
qs.xiongf0128b12023-06-29 17:29:39 +0800849 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800850
851 }
852
853 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
854 {
855 *error = LYNQ_NOT_FIND_AP;
856 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
857 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
qs.xiong20202422023-09-06 18:01:18 +0800858 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800859 return;
860 }
861
862
863 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
864 {
865 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
866 pReason = strstr(modify, "status_code=");
867 if (pReason != NULL)
868 {
869 pReason += strlen("status_code=");
870 if (memcmp(pReason, "17", 2) == 0)
871 {
872 *error = LYNQ_AP_UNABLE_HANDLE;
873 }
874 else if (memcmp(pReason, "1",1) == 0)
875 {
876 *error = LYNQ_UNSPECIFIED_REASON;
877 }
878 else
879 {
880 *error = LYNQ_UNSPECIFIED_REASON;
881 }
882
883 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
884 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 +0800885 g_sta_conncet_status_flag = 0;
qs.xiong455c30b2023-04-12 11:40:02 +0800886 return;
887 }
888 else
889 {
890 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
891 *error = LYNQ_UNSPECIFIED_REASON;
892 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
893 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
894 return;
895 }
896 }
897
898 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
899 {
900 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
901 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
902 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
903 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
904 return;
905 }
906
907 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
908 {
909 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
910 *error = LYNQ_WAIT_CONNECT_ACTIVE;
911 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
912 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
913 return;
914 }
915
you.chen32cb31e2023-04-13 14:05:45 +0800916 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800917 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800918 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800919 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
920 return;
921
922}
923
qs.xiongfcc914b2023-07-06 21:16:20 +0800924void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
925{
926 char *pReason;
927 char *wpanetid;
928 char destid[3];
929 *error = LYNQ_WAIT_CONNECT_ACTIVE;
930 *networkid = -1;
931 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
932 {
933 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
934 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
935 return;
936 }
937 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
938 {
939 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
940 wpanetid = strstr(modify,"id=");
941 if ( wpanetid != NULL )
942 {
943 wpanetid +=strlen("id=");
944 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
945 if (memcpy(destid,wpanetid,2) != NULL)
946 {
947 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
948 *networkid = atoi(destid);
949 RLOGD("get networkid is %d\n",*networkid);
950 }
951 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
952 }
953 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
954 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
955 return;
956 }
957 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
958 {
959 wpanetid = strstr(modify,"id=");
960 if ( wpanetid != NULL )
961 {
962 wpanetid +=strlen("id=");
963 if (memcpy(destid,wpanetid,2) != NULL)
964 {
965 *networkid = atoi(destid);
966 RLOGD("get networkid is %d\n",*networkid);
967 }
968 }
969 pReason = strstr(modify, "reason=");
970 if (pReason != NULL)
971 {
972 pReason += strlen("reason=");
973 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
974 {
975 *error = LYNQ_TIME_OUT;
976 }
977 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
978 {
979 *error = LYNQ_PSW_ERROR;
980 }
981 else
982 {
983 *error = LYNQ_UNSPECIFIED_REASON;
984 }
985 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
986 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
987 return;
988 }
989 else
990 {
991 *error = LYNQ_UNSPECIFIED_REASON;
992 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
993 return;
994 }
995 }
996 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
997 {
998 wpanetid = strstr(modify,"id=");
999 if ( wpanetid != NULL )
1000 {
1001 wpanetid +=strlen("id=");
1002 if (memcpy(destid,wpanetid,2) != NULL)
1003 {
1004 *networkid = atoi(destid);
1005 RLOGD("get networkid is %d\n",*networkid);
1006 }
1007 }
1008 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
1009 pReason = strstr(modify, "status_code=");
1010 if (pReason != NULL)
1011 {
1012 pReason += strlen("status_code=");
1013 if (memcmp(pReason, "17", 2) == 0)
1014 {
1015 *error = LYNQ_AP_UNABLE_HANDLE;
1016 }
1017 else if (memcmp(pReason, "1",1) == 0)
1018 {
1019 *error = LYNQ_UNSPECIFIED_REASON;
1020 }
1021 else
1022 {
1023 *error = LYNQ_UNSPECIFIED_REASON;
1024 }
1025 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1026 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1027 return;
1028 }
1029 else
1030 {
1031 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1032 *error = LYNQ_UNSPECIFIED_REASON;
1033 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001034 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001035 return;
1036 }
1037 }
1038 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1039 {
1040 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1041 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1042 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1043 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1044 return;
1045 }
1046 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1047 {
1048 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1049 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1050 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1051 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1052 return;
1053 }
1054 RLOGD("EVENT : %s\n", modify);
1055 *error = LYNQ_UNSPECIFIED_REASON;
1056 *state = LYNQ_WIFI_STATUS_EGNORE;
1057 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1058 return;
1059}
you.chen70f377f2023-04-14 18:17:09 +08001060static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1061{
you.chen6d247052023-06-01 16:39:54 +08001062 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001063 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1064 {
1065 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1066 g_sta_callback_func(g_sta_callback_priv, state, error);
1067 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1068 }
you.chen6d247052023-06-01 16:39:54 +08001069 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001070}
qs.xiongfcc914b2023-07-06 21:16:20 +08001071static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1072{
1073 pthread_mutex_lock(&s_sta_callback_mutex);
1074 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1075 {
1076 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1077 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1078 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1079 }
1080 pthread_mutex_unlock(&s_sta_callback_mutex);
1081}
you.chen70f377f2023-04-14 18:17:09 +08001082
you.chen35020192022-05-06 11:30:57 +08001083static void STAWatcherThreadProc() {
1084 size_t len = MAX_RET;
1085 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001086 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001087 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001088 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001089
you.chen6c2dd9c2022-05-16 17:55:28 +08001090 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001091 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001092
you.chen70f377f2023-04-14 18:17:09 +08001093 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001094 while (g_sta_watcher_stop_flag == 0)
1095 {
you.chenf711c8a2023-04-13 13:49:45 +08001096 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001097 {
you.chen35020192022-05-06 11:30:57 +08001098 continue;
1099 }
you.chenf711c8a2023-04-13 13:49:45 +08001100
you.chen6c2dd9c2022-05-16 17:55:28 +08001101 memset(msg_notify, 0, MAX_RET);
1102 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001103 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001104 {
you.chen35020192022-05-06 11:30:57 +08001105 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001106 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1107 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001108 {
you.chen35020192022-05-06 11:30:57 +08001109 g_sta_scan_finish_flag = 1;
1110 }
1111
qs.xiong9fbf74e2023-03-28 13:38:22 +08001112 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001113 {
you.chen35020192022-05-06 11:30:57 +08001114 continue;
1115 }
qs.xiong455c30b2023-04-12 11:40:02 +08001116 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001117 notify_connect_status(state, error);
1118
1119 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001120 {
you.chen70f377f2023-04-14 18:17:09 +08001121 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001122 if (last_state != state)
1123 {
1124 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1125 {
1126 system_call_v("%s %s", sta_status_change_script, "connect");
1127 }
1128 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1129 {
1130 system_call_v("%s %s", sta_status_change_script, "disconnect");
1131 }
1132 }
1133
1134 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001135 }
you.chen35020192022-05-06 11:30:57 +08001136 }
1137 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001138 if (lynq_wpa_ctrl != NULL)
1139 {
you.chen92fd5d32022-05-25 10:09:47 +08001140 wpa_ctrl_detach(lynq_wpa_ctrl);
1141 wpa_ctrl_close(lynq_wpa_ctrl);
1142 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001143}
qs.xiongfcc914b2023-07-06 21:16:20 +08001144static void STAAutoWatcherThreadProc() {
1145 size_t len = MAX_RET;
1146 char msg_notify[MAX_RET];
1147 error_number_s error;
1148 lynq_wifi_sta_status_s state, last_state = -1;
1149 int idle_count = 0;
1150 int networkid;
1151 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1152 g_sta_auto_watcher_stop_flag = 0;
1153 RLOGD("STAAutoWatcherThreadProc thread started ------");
1154 while (g_sta_auto_watcher_stop_flag == 0)
1155 {
1156 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1157 {
1158 continue;
1159 }
1160 memset(msg_notify, 0, MAX_RET);
1161 len = MAX_RET;
1162 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1163 {
1164 msg_notify[len+1] = '\0';
1165 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1166 if (strstr(msg_notify, state_scan_result) != NULL)
1167 {
1168 g_sta_auto_scan_finish_flag = 1;
1169 }
1170 if (g_sta_auto_callback_func == NULL)
1171 {
1172 continue;
1173 }
1174 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1175 notify_auto_connect_status(state, error,networkid);
1176 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1177 {
1178 inner_check_connect_error(msg_notify, state, error);
1179 if (last_state != state)
1180 {
1181 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1182 {
1183 system_call_v("%s %s", sta_status_change_script, "connect");
1184 }
1185 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1186 {
1187 system_call_v("%s %s", sta_status_change_script, "disconnect");
1188 }
1189 }
1190 last_state = state;
1191 }
1192 }
1193 }
1194 if (lynq_wpa_ctrl != NULL)
1195 {
1196 wpa_ctrl_detach(lynq_wpa_ctrl);
1197 wpa_ctrl_close(lynq_wpa_ctrl);
1198 }
1199}
qs.xiongf1b525b2022-03-31 00:58:23 -04001200
you.chen70f377f2023-04-14 18:17:09 +08001201// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1202static void GlobalWatcherThreadProc()
1203{
1204 int ret, connect_timeout, service_abnormal;
1205 error_number_s error_num = -1;
1206 inner_sta_status_s sta_status;
1207 scan_info_s *scan_list = NULL;
1208 int i, scan_len=0;
1209 char connecting_ssid[64];
1210 struct timeval now;
1211
1212 RLOGD("GlobalWatcherThreadProc start to run");
1213
1214 while (1)
1215 {
1216 pthread_mutex_lock(&s_global_check_mutex);
1217 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1218 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1219 {
1220 pthread_mutex_unlock(&s_global_check_mutex);
1221 usleep(50*1000);
1222 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1223 continue;
1224 }
1225
1226 connect_timeout = 0;
1227 service_abnormal = 0;
1228 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1229 {
1230 while (1)
1231 {
1232 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1233 if (ret == ETIME)
1234 {
1235 connect_timeout = 1;
1236 }
1237 else if (ret != 0)
1238 {
1239 gettimeofday(&now,NULL);
1240 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1241 {
1242 usleep(SLEEP_TIME_ON_IDLE);
1243 continue;
1244 }
1245 connect_timeout = 1;
1246 }
1247 sta_status = s_sta_status;
1248 error_num = s_sta_error_number;
1249 s_sta_status = INNER_STA_STATUS_INIT;
1250 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1251 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1252 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1253 break;
1254 }
1255 }
1256 if (s_service_invoke_timeout_cnt > 10)
1257 {
1258 service_abnormal = 1;
1259 s_service_invoke_timeout_cnt = 0;
1260 }
1261 pthread_mutex_unlock(&s_global_check_mutex);
1262
1263 if (service_abnormal == 1)
1264 {
1265 sleep(1);
1266 RLOGE("wpa service is abnormal info app to exit");
1267 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001268
1269 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1270
you.chen70f377f2023-04-14 18:17:09 +08001271 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1272 }
1273
1274 if (sta_status == INNER_STA_STATUS_CANCEL)
1275 {
1276 continue;
1277 }
1278 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1279 {
1280 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1281 }
1282 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1283 {
1284 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1285 {
1286 for(i=0; i < scan_len;i++)
1287 {
1288 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1289 {
1290 error_num = LYNQ_AUTH_ERROR;
1291 break;
1292 }
1293 }
1294 free(scan_list);
1295 }
1296 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1297 }
1298 else if (connect_timeout == 0)
1299 {
1300 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1301 }
1302 else // wait timeout
1303 {
1304 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1305 {
1306 ; // wpa service abnormal
1307 }
1308 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1309 {
1310 RLOGD("GlobalWatcherThreadProc notify connected");
1311 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1312 }
1313 else
1314 {
1315 RLOGD("GlobalWatcherThreadProc notify timeout");
1316 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1317 }
1318 }
1319 } // while (1)
1320}
1321
qs.xiong1af5daf2022-03-14 09:12:12 -04001322int lynq_wifi_enable(void)
1323{
you.chen35020192022-05-06 11:30:57 +08001324 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001325 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001326 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001327 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1328
qs.xiong9fbf74e2023-03-28 13:38:22 +08001329 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1330 {
you.chend2fef3f2023-02-13 10:50:35 +08001331 goto out_enable;
1332 }
1333
you.chenc9928582023-04-24 15:39:37 +08001334 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001335 if (ret != 0)
1336 {
1337 //printf("service state %d\n", ret);
1338 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001339 ret = -1;
1340 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001341 }
lhfe8da902022-10-11 18:55:36 +08001342
you.chen70f377f2023-04-14 18:17:09 +08001343 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1344 {
1345 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1346 if(ret<0)
1347 {
1348 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1349 ret = -1;
1350 goto out_enable;
1351 }
1352 }
1353
you.chend2fef3f2023-02-13 10:50:35 +08001354 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1355 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1356 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1357 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1358out_enable:
1359 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001360 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001361}
1362
qs.xiong1af5daf2022-03-14 09:12:12 -04001363int lynq_wifi_disable(void)
1364{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001365 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001366 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001367 g_ap_watcher_stop_flag = 1;
1368 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001369 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001370 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001371 if (g_ap_watcher_pid != 0)
1372 pthread_join(g_ap_watcher_pid, NULL);
1373 if (g_sta_watcher_pid != 0)
1374 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001375 if (g_sta_auto_watcher_pid != 0)
1376 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001377 if (g_lynq_wpa_ctrl[0] != NULL)
1378 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1379 if (g_lynq_wpa_ctrl[1] != NULL)
1380 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001381 if (g_ap_tmp_watcher_pid != 0)
1382 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001383 g_ap_watcher_pid = 0;
1384 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001385 g_sta_auto_watcher_pid = 0;
qs.xiong08e6aac2023-09-06 23:12:27 +08001386 g_ap_tmp_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001387 g_lynq_wpa_ctrl[0] = NULL;
1388 g_lynq_wpa_ctrl[1] = NULL;
1389 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001390 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1391 return 0;
1392}
1393
1394static inline char inner_convert_char(char in)
1395{
1396 if (in >= '0' && in <= '9')
1397 {
1398 return in - '0';
1399 }
1400 else if (in >= 'a' && in <= 'f')
1401 {
1402 return in - 'a' + 10;
1403 }
1404 else if (in >= 'A' && in <= 'F')
1405 {
1406 return in - 'A' + 10;
1407 }
1408 else
1409 {
1410 return '\xff';
1411 }
1412}
1413
1414static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1415{
1416 char *p;
1417 size_t pos = 0;
1418 if (NULL == out_ssid)
1419 return;
1420 //printf("input ssid=[%s]\n", ssid);
1421 memset(out_ssid, 0, out_ssid_len);
1422 if (NULL == ssid)
1423 return;
1424 p = strchr(ssid, '\\');
1425 if (NULL == p)
1426 {
1427 strncpy(out_ssid, ssid, out_ssid_len);
1428 //printf(" first %s\n", out_ssid);
1429 }
1430 else
1431 {
1432 pos = p - ssid;
1433 memcpy(out_ssid, ssid, pos);
1434 //printf("pos %lu -- %s\n", pos, out_ssid);
1435 for(; pos < out_ssid_len; pos ++)
1436 {
1437 if (p[0] == '\0')
1438 {
1439 //printf(" out %s\n", out_ssid);
1440 return;
1441 }
1442 else if (p[0] != '\\')
1443 {
1444 out_ssid[pos] = p[0];
1445 p += 1;
1446 }
1447 else if (p[1] == 'x' || p[1] == 'X')
1448 {
1449 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1450 p += 4;
1451 }
1452 else if (p[1] == '\\')
1453 {
1454 out_ssid[pos] = '\\';
1455 p += 2;
1456 }
1457 else if (p[1] == 't')
1458 {
1459 out_ssid[pos] = '\t';
1460 p += 2;
1461 }
1462 else if (p[1] == 'r')
1463 {
1464 out_ssid[pos] = '\r';
1465 p += 2;
1466 }
1467 else if (p[1] == 'n')
1468 {
1469 out_ssid[pos] = '\n';
1470 p += 2;
1471 }//todo find a better way to convert?
1472 }
1473 }
1474 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001475}
qs.xiong1af5daf2022-03-14 09:12:12 -04001476
you.chen35020192022-05-06 11:30:57 +08001477static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001478 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001479 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001480 RLOGD("enter inner_get_param");
1481 if (out_put == NULL)
1482 {
1483 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001484 return -1;
1485 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001486 if (param_name == NULL)
1487 {
1488 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001489 return -1;
1490 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001491 if (param_name[0] == '\0')
1492 {
1493 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001494 return -1;
1495 }
1496
1497 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1498
1499 CHECK_WPA_CTRL(interface);
1500
1501 DO_REQUEST(lynq_cmd_get);
1502
qs.xiong9fbf74e2023-03-28 13:38:22 +08001503 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1504 {
1505 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001506 return -1;
1507 }
1508
you.chena6fa5b22022-05-18 10:28:19 +08001509// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001510 if (strcmp(param_name, "ssid") == 0)
1511 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001512 if (cmd_reply[0] == '\"')
1513 {
you.chend2fef3f2023-02-13 10:50:35 +08001514 ssid_len = reply_len - 1;
1515 memcpy(out_put, cmd_reply + 1, ssid_len);
1516 if (out_put[ssid_len-1] == '\"')
1517 {
1518 out_put[ssid_len-1] = '\0';
1519 }
1520 else
1521 {
1522 out_put[ssid_len] = '\0';
1523 }
1524 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001525 else
1526 {
you.chend2fef3f2023-02-13 10:50:35 +08001527 ssid_len = reply_len / 2;
1528 for(i=0; i<ssid_len; i++)
1529 {
1530 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1531 }
1532 out_put[ssid_len] = '\0';
1533 }
1534 }
1535 else
1536 {
1537 memcpy(out_put, cmd_reply, reply_len + 1);
1538 }
you.chen35020192022-05-06 11:30:57 +08001539 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001540}
qs.xiong1af5daf2022-03-14 09:12:12 -04001541
you.chen35020192022-05-06 11:30:57 +08001542static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1543 int ret = 0;
1544 char * end = str + len - 1;
1545 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001546 while(str < end)
1547 {
1548 if (*str == delimiter)
1549 {
you.chen35020192022-05-06 11:30:57 +08001550 *str++ = '\0';
1551 results[ret++] = str;
1552 continue;
1553 }
1554 str++;
1555 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001556 if (*str == delimiter)
1557 {
you.chen35020192022-05-06 11:30:57 +08001558 *str = '\0';
1559 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001560
you.chen6ed36a62023-04-27 17:51:56 +08001561 results[ret] = NULL;
1562
you.chen35020192022-05-06 11:30:57 +08001563 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001564}
1565
you.chend2fef3f2023-02-13 10:50:35 +08001566static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1567{
1568 char * p;
1569 int ret = 0;
1570 char cmd[256]={0};
1571 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001572 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001573 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001574 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001575 ret = exec_cmd(cmd, ip, ip_len);
1576 p = strchr(ip, '\n');
1577 if (NULL != p)
1578 {
1579 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001580 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001581 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001582 return ret;
1583}
1584
you.chend2fef3f2023-02-13 10:50:35 +08001585static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001586 struct in_addr addr ={0};
1587 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001588 char cmd[64] = {0};
1589 char * p;
1590 int ret;
you.chen35020192022-05-06 11:30:57 +08001591
qs.xiong9fbf74e2023-03-28 13:38:22 +08001592 if (ip == NULL || *ip == '\0' || hostname == NULL)
1593 {
1594 RLOGE("ip == NULL or hostname == NULL");
1595 return -1;
you.chen35020192022-05-06 11:30:57 +08001596 }
1597
you.chend2fef3f2023-02-13 10:50:35 +08001598 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001599 if (inet_aton(ip, &addr) == 0)
1600 {
you.chen35020192022-05-06 11:30:57 +08001601 printf("---inet_aton fail\n");
1602 return -1;
1603 }
1604
1605 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1606
qs.xiong9fbf74e2023-03-28 13:38:22 +08001607 if (ht == NULL)
1608 {
you.chen186d3c32023-05-18 14:19:46 +08001609 hostname[0] = '\0';
1610 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1611 ret = exec_cmd(cmd, hostname, 32);
1612 if (ret == 0)
1613 {
1614 p = strchr(hostname, '\n');
1615 if (p != NULL)
1616 {
1617 *p = '\0';
1618 }
1619 return 0;
1620 }
1621 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001622 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001623 herror(NULL);
1624 return -1;
1625 }
1626
1627 strcpy(hostname, ht->h_name);
1628
1629 return 0;
1630}
1631
1632static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1633{
1634 int count, index, words_count;
1635 char * split_lines[128]= {0};
1636 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001637 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001638 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001639 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001640
1641 CHECK_WPA_CTRL(ap_sta);
1642
1643 DO_REQUEST(lynq_wifi_list_networks);
1644
1645 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1646
1647 //@todo check ssid field to compatible
1648
1649 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001650 for(index=1; index < count; index++)
1651 {
you.chen35020192022-05-06 11:30:57 +08001652 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001653 if (words_count > 2)
1654 {
you.chend2fef3f2023-02-13 10:50:35 +08001655 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001656 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1657 {
you.chen35020192022-05-06 11:30:57 +08001658 net_no_list[ret++] = atoi(split_words[0]);
1659 }
1660 }
1661 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001662 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001663 return ret;
1664}
1665
1666static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001667 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001668 CHECK_WPA_CTRL(ap_sta);
1669 const char *lynq_wifi_add_network = "ADD_NETWORK";
1670
qs.xiong9fbf74e2023-03-28 13:38:22 +08001671 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001672 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001673 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1674 {
1675 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001676 return -1;
1677 }
1678
qs.xiong9fbf74e2023-03-28 13:38:22 +08001679 for(i=0;i<reply_len;i++)
1680 {
1681 if(cmd_reply[i] == '\n')
1682 {
you.chen35020192022-05-06 11:30:57 +08001683 cmd_reply[i] = '\0';
1684 break;
1685 }
1686 }
1687 return atoi(cmd_reply);
1688}
you.chena6cd55a2022-05-08 12:20:18 +08001689
you.chen35020192022-05-06 11:30:57 +08001690static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1691{
1692 int count, index;
1693 int net_no_list[128];
1694
qs.xiong9fbf74e2023-03-28 13:38:22 +08001695 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001696 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001697 for (index=0; index < count; index++)
1698 {
1699 if (net_no_list[index] == net_no)
1700 {
you.chen35020192022-05-06 11:30:57 +08001701 return 0;
1702 }
1703 }
1704
1705 if (count >= 1)
1706 index = net_no_list[count - 1];
1707 else
1708 index = -1;
1709
qs.xiong9fbf74e2023-03-28 13:38:22 +08001710 while (index < net_no )
1711 {
you.chen35020192022-05-06 11:30:57 +08001712 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001713 if (index >= net_no)
1714 { // required network no created
1715 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001716 return 0;
1717 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001718 else if( index < 0)
1719 {
1720 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001721 return -1;
1722 }
you.chen35020192022-05-06 11:30:57 +08001723 }
1724
1725 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001726 {
1727 RLOGE("[lynq_check_network_number] network index < 0");
1728 return -1;
1729 }
1730 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001731 return 0;
1732}
1733
1734static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001735 if (freq > 5000 && freq < 6000)
1736 {
you.chen35020192022-05-06 11:30:57 +08001737 return LYNQ_WIFI_5G_band;
1738 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001739 else if (freq > 2000 && freq < 3000)
1740 {
you.chen35020192022-05-06 11:30:57 +08001741 return LYNQ_WIFI_2G_band;
1742 }
1743 return LYNQ_WIFI_2_and_5G_band;
1744}
1745
1746static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001747 if (key_mgmt != NULL)
1748 {
1749 if (memcmp( key_mgmt, "NONE", 4) == 0)
1750 {
you.chen35020192022-05-06 11:30:57 +08001751 return LYNQ_WIFI_AUTH_OPEN;
1752 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001753 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1754 {
you.chen35020192022-05-06 11:30:57 +08001755 return LYNQ_WIFI_AUTH_WEP;
1756 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001757 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1758 {
you.chen35020192022-05-06 11:30:57 +08001759 return LYNQ_WIFI_AUTH_WPA_PSK;
1760 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001761 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1762 {
you.chen35020192022-05-06 11:30:57 +08001763 return LYNQ_WIFI_AUTH_WPA2_PSK;
1764 }
1765 }
1766
1767 return -1;
1768}
1769
1770static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001771 if (flag != NULL)
1772 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001773 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 +08001774 {
1775 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001776 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001777 {
1778 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1779 }else if (strstr( flag, "WPA2-PSK") != NULL)
1780 {
you.chen35020192022-05-06 11:30:57 +08001781 return LYNQ_WIFI_AUTH_WPA2_PSK;
1782 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001783 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001784 {
you.chen35020192022-05-06 11:30:57 +08001785 return LYNQ_WIFI_AUTH_WPA_PSK;
1786 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001788 {
you.chen35020192022-05-06 11:30:57 +08001789 return LYNQ_WIFI_AUTH_WEP;
1790 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001791 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001792 {
you.chen35020192022-05-06 11:30:57 +08001793 return LYNQ_WIFI_AUTH_OPEN;
1794 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001795 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001796 {
you.chend2fef3f2023-02-13 10:50:35 +08001797 return LYNQ_WIFI_AUTH_OPEN;
1798 }
qs.xiong46f41562023-07-11 21:06:47 +08001799 else
1800 {
1801 RLOGD("convert_max_auth_from_flag not-found auth mode");
1802 }
you.chen35020192022-05-06 11:30:57 +08001803 }
1804
1805 return -1;
1806}
1807
1808static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1809 switch (bw) {
1810 case 10:
1811 return LYNQ_WIFI_BANDWIDTH_HT10;
1812 break;
1813 case 20:
1814 return LYNQ_WIFI_BANDWIDTH_HT20;
1815 break;
1816 case 40:
1817 return LYNQ_WIFI_BANDWIDTH_HT40;
1818 break;
1819 case 80:
1820 return LYNQ_WIFI_BANDWIDTH_HT80;
1821 break;
1822 default:
1823 break;
1824 }
1825
1826 return -1;
1827}
1828
you.chen70f377f2023-04-14 18:17:09 +08001829static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001830static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1831 int i, count;
1832 char *p;
1833 const char *lynq_status_cmd = "STATUS";
1834 const char * FLAG_SSID = "ssid=";
1835 const char * FLAG_SBSID = "bssid=";
1836 const char * FLAG_KEY_MGMT = "key_mgmt=";
1837 const char * FLAG_FREQ = "freq=";
1838 const char * FLAG_STATE = "wpa_state=";
1839 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001840 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001841 char *split_lines[128] = {0};
1842
1843 CHECK_WPA_CTRL(interface);
1844
qs.xiong9fbf74e2023-03-28 13:38:22 +08001845 if (curr_state == NULL)
1846 {
1847 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001848 return -1;
1849 }
1850
1851 DO_REQUEST(lynq_status_cmd);
1852
1853 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1854
1855 curr_state->net_no = -1;
1856 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001857 for(i=0; i < count; i++)
1858 {
1859 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001860 {
you.chen35020192022-05-06 11:30:57 +08001861 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001862 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001863 {
you.chend2fef3f2023-02-13 10:50:35 +08001864 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001865 ret = 0;
1866 continue;
1867 }
you.chenf58b3c92022-06-21 16:53:48 +08001868 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001869 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001870 {
you.chend2fef3f2023-02-13 10:50:35 +08001871 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 +08001872 ret = 0;
1873 continue;
1874 }
you.chen35020192022-05-06 11:30:57 +08001875 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001876 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001877 {
you.chen450d0172022-07-15 17:56:48 +08001878 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001879 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001880 ret = 0;
1881 continue;
1882 }
1883 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001884 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001885 {
you.chen35020192022-05-06 11:30:57 +08001886 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1887 ret = 0;
1888 continue;
1889 }
you.chend2fef3f2023-02-13 10:50:35 +08001890 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001891 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001892 {
you.chend2fef3f2023-02-13 10:50:35 +08001893 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1894 ret = 0;
1895 continue;
1896 }
you.chen35020192022-05-06 11:30:57 +08001897 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001898 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001899 {
you.chen35020192022-05-06 11:30:57 +08001900 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001901 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001902 {
you.chen35020192022-05-06 11:30:57 +08001903 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1904 ret = 0;
1905 continue;
1906 }
1907
1908 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001909 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001910 {
you.chen35020192022-05-06 11:30:57 +08001911 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001912 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001913 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001914 }
1915 }
1916
you.chen70f377f2023-04-14 18:17:09 +08001917 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1918 {
1919 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1920 }
1921
you.chen35020192022-05-06 11:30:57 +08001922 return ret;
1923}
1924
qs.xiongf1b525b2022-03-31 00:58:23 -04001925int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001926{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001927 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001928 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001929
qs.xiong9fbf74e2023-03-28 13:38:22 +08001930 if (ap_ssid == NULL)
1931 {
1932 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001933 return -1;
1934 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001935 else
1936 {
1937 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001938 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001939
qs.xiong9fbf74e2023-03-28 13:38:22 +08001940 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1941 {
1942 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001943 return -1;
1944 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001945
you.chen35020192022-05-06 11:30:57 +08001946 CHECK_IDX(idx, CTRL_AP);
1947
1948 CHECK_WPA_CTRL(CTRL_AP);
1949
1950 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1951
1952 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1953 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001954 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1955 return 0;
you.chen35020192022-05-06 11:30:57 +08001956
qs.xiong7a105ce2022-03-02 09:43:11 -05001957}
1958
you.chen35020192022-05-06 11:30:57 +08001959int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001960{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001961 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001962 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001963 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001964}
1965
qs.xiongc9c79f72022-10-17 15:27:18 +08001966/*****
1967 *frequency <------>channel
1968 *
1969 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1970 *
1971 *
1972 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1973 *
1974 *
1975 * */
1976static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001977 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};
1978 int i;
1979 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1980
qs.xiong69a332b2022-12-02 09:58:57 +08001981 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001982 {
1983 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001984 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001985 }
qs.xiongc00b6032022-11-29 16:28:03 +08001986
1987 if(i == arr_len)
1988 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001989 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001990 return -1;
1991 }
qs.xiongc00b6032022-11-29 16:28:03 +08001992
qs.xiongc9c79f72022-10-17 15:27:18 +08001993 return 0;
1994}
qs.xiong13673462023-02-21 19:12:54 +08001995
1996static int lynq_check_frequencyby_country_code(int input_frequency)
1997{
1998 char str_cnc[]="CN";
1999 char str_dest[20]="";
2000
2001 if( lynq_get_country_code(1,str_dest) != 0 )
2002 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002003 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08002004 return -1;
2005 }
2006 if( strncmp(str_dest,str_cnc,2) != 0 )
2007 {
2008 return 0;
2009 }else if( 2473 < input_frequency && input_frequency < 5744)
2010 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002011 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002012 return -1;
2013 }
2014 return 0;
2015}
qs.xiongf1b525b2022-03-31 00:58:23 -04002016int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002017{
qs.xiongc00b6032022-11-29 16:28:03 +08002018 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002019 char lynq_wifi_frequency_cmd[128]={0};
2020 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002021 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002023 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002024 check = lynq_check_set_frequency(lynq_wifi_frequency);
2025 if(check != 0)
2026 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002027 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002028 return -1;
you.chen35020192022-05-06 11:30:57 +08002029 }
qs.xiong13673462023-02-21 19:12:54 +08002030 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2031 if(check != 0)
2032 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002033 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002034 return -1;
2035 }
2036
qs.xiongc00b6032022-11-29 16:28:03 +08002037 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2038 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002040 return -1;
2041 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002042
you.chen35020192022-05-06 11:30:57 +08002043 CHECK_IDX(idx, CTRL_AP);
2044
2045 CHECK_WPA_CTRL(CTRL_AP);
2046
2047 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2048 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2049 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2050
you.chen6c2dd9c2022-05-16 17:55:28 +08002051 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002052 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2053 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2054 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002055
qs.xiong9fbf74e2023-03-28 13:38:22 +08002056 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002057}
2058
qs.xiongf1b525b2022-03-31 00:58:23 -04002059int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002060{
you.chen35020192022-05-06 11:30:57 +08002061 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002062 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002063 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002064
qs.xiong9fbf74e2023-03-28 13:38:22 +08002065 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2066 {
2067 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002068 return -1;
2069 }
2070 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002071
qs.xiong9fbf74e2023-03-28 13:38:22 +08002072 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002073}
2074
qs.xiongf1b525b2022-03-31 00:58:23 -04002075int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2076{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002077 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002078 CHECK_IDX(idx, CTRL_AP);
2079 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002080 case LYNQ_WIFI_BANDWIDTH_HT10:
2081 {
2082 RLOGE("bandwith [%d] not support now\n", bandwidth);
2083 return -1;
2084 }
2085 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002086 {
2087 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2088 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002089 if (system(lynq_cmd_bandwith) != 0 )
2090 {
2091 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002092 return -1;
2093 }
2094 system("wl up");
2095 break;
2096 }
2097 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002098 {
qs.xiong10379192023-02-21 13:19:42 +08002099 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002100 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2101 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002102 if (system(lynq_cmd_bandwith) != 0 )
2103 {
2104 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002105 return -1;
2106 }
2107 system("wl up");
2108 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002109 }
you.chen35020192022-05-06 11:30:57 +08002110 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002111 {
qs.xiong10379192023-02-21 13:19:42 +08002112 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002113 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002114 if (system(lynq_cmd_bandwith) != 0 )
2115 {
2116 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002117 return -1;
2118 }
2119 system("wl up");
2120 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002121 }
2122 default:
you.chen35020192022-05-06 11:30:57 +08002123 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002124 RLOGE("auth type [%d] not support now\n", bandwidth);
2125 return -1;
you.chen35020192022-05-06 11:30:57 +08002126 }
2127 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002128
2129
you.chen35020192022-05-06 11:30:57 +08002130 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002131}
you.chen35020192022-05-06 11:30:57 +08002132
qs.xiongf1b525b2022-03-31 00:58:23 -04002133int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2134{
you.chen35020192022-05-06 11:30:57 +08002135 int count = 0;
2136 int index = 0;
2137 char *split_words[128] = {0};
2138 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002139 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002140 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002141
you.chen35020192022-05-06 11:30:57 +08002142 CHECK_WPA_CTRL(CTRL_AP);
2143
2144 DO_REQUEST(lynq_chanspec_cmd);
2145
2146 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2147 for(;index < count; index++) {
2148 if (strncmp(split_words[index], "bw", 2) != 0) {
2149 continue;
2150 }
2151
2152 index++;
2153 if (index >= count) {
2154 return -1;
2155 }
2156
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002158 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2159 return 0;
2160 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002161 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002162 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002163}
qs.xiong0fb469a2022-04-14 03:50:45 -04002164
qs.xiongf1b525b2022-03-31 00:58:23 -04002165int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002166{
you.chen35020192022-05-06 11:30:57 +08002167 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002168 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002169 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002170
you.chen35020192022-05-06 11:30:57 +08002171 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002172
qs.xiong9fbf74e2023-03-28 13:38:22 +08002173 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2174 {
you.chen35020192022-05-06 11:30:57 +08002175 return -1;
2176 }
2177
2178 system("wl down");
2179 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002180 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002181 return -1;
2182 }
2183 system("wl up");
2184 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002185}
qs.xiong0fb469a2022-04-14 03:50:45 -04002186
qs.xiongf1b525b2022-03-31 00:58:23 -04002187int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002188{
you.chen35020192022-05-06 11:30:57 +08002189 int count = 0;
2190 int index = 0;
2191 char *split_words[128] = {0};
2192 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002193 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002194 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002195
you.chen35020192022-05-06 11:30:57 +08002196 CHECK_WPA_CTRL(CTRL_AP);
2197
2198 DO_REQUEST(lynq_chanspec_cmd);
2199
2200 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002201 for(;index < count; index++)
2202 {
2203 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002204 if (strncmp(split_words[index], "channel", 2) != 0) {
2205 continue;
2206 }
2207
2208 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002209 if (index >= count)
2210 {
you.chen35020192022-05-06 11:30:57 +08002211 return -1;
2212 }
2213
2214 *channel = atoi(split_words[index]);
2215 return 0;
2216 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002217 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002218 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002219}
2220
2221
you.chen35020192022-05-06 11:30:57 +08002222int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002223{
you.chen6c2dd9c2022-05-16 17:55:28 +08002224 char ssid[MAX_CMD] = {0};
2225 int freq = 0;
2226 char lynq_auth_cmd[64]={0};
2227 char lynq_auth_alg_cmd[64]={0};
2228 char lynq_psk_cmd[64]={0};
2229 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002230 char lynq_ieee80211_cmd[64]={0};
2231 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002232 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002233 CHECK_IDX(idx, CTRL_AP);
2234
you.chen6c2dd9c2022-05-16 17:55:28 +08002235 CHECK_WPA_CTRL(CTRL_AP);
2236
qs.xiong9fbf74e2023-03-28 13:38:22 +08002237 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2238 {
2239 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002240 return -1;
2241 }
2242
you.chen92fd5d32022-05-25 10:09:47 +08002243 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002244 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002245 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002246 return 0;
2247 }
2248 else {
2249 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2250 ssid[0] = '\0';
2251 }
2252 lynq_wifi_ap_frequency_get(idx, &freq);
2253
2254 DO_OK_FAIL_REQUEST(cmd_disconnect);
2255 DO_OK_FAIL_REQUEST(cmd_remove_all);
2256 if (ssid[0] != '\0') {
2257 lynq_wifi_ap_ssid_set(idx, ssid);
2258 }
2259 if (freq != 0) {
2260 lynq_wifi_ap_frequency_set(idx, freq);
2261 }
2262 }
2263 }
you.chen35020192022-05-06 11:30:57 +08002264
qs.xiong9fbf74e2023-03-28 13:38:22 +08002265 switch(auth){
2266 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002267 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002268 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002269 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002270 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002271 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002272 break;
2273 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002274 case LYNQ_WIFI_AUTH_WEP:
2275 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002276 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002277 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002278 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002279 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2280
2281 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2282 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2283 break;
2284 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002286 {
2287 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2288 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2289 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2290
2291 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2292 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2293 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2294 break;
2295
2296 }
you.chen35020192022-05-06 11:30:57 +08002297 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002298 {
2299 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2300 {
you.chen35020192022-05-06 11:30:57 +08002301 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2302 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2303 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002304 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2305 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002306 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002307 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002308 }
2309// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2310// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2311 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002312
you.chen35020192022-05-06 11:30:57 +08002313 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2314 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2315 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002316 break;
2317 }
2318 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2319 {
2320 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2321 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2322 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2323 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2324
2325 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2326 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2327 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2328 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2329 break;
2330 }
2331 case LYNQ_WIFI_AUTH_WPA3_PSK:
2332 {
2333 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2334 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002335 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002336 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2337
2338 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2339 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2340 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2341 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2342 break;
2343 }
2344 default:
you.chen35020192022-05-06 11:30:57 +08002345 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 RLOGE("auth type [%d] not support now\n", auth);
2347 return -1;
you.chen35020192022-05-06 11:30:57 +08002348 }
2349 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002350 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002351
qs.xiong9fbf74e2023-03-28 13:38:22 +08002352 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002353}
2354
you.chen35020192022-05-06 11:30:57 +08002355int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002356{
you.chen35020192022-05-06 11:30:57 +08002357 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002358 char lynq_auth_alg_str[MAX_RET] = {0};
2359 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002360 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002361 CHECK_IDX(idx, CTRL_AP);
2362
qs.xiong9fbf74e2023-03-28 13:38:22 +08002363 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2364 {
2365 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002366 return -1;
2367 }
2368
qs.xiong9fbf74e2023-03-28 13:38:22 +08002369 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2370 {
2371 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2372 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002373 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002374 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002375 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002376 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002377 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2378 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002379 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002380 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002381 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002382 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002383 else
2384 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002385 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002386 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002387 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002388 }
you.chen35020192022-05-06 11:30:57 +08002389 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2391 {
2392 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2393 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002394 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002395 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002396 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002397 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2398 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002399 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002400 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002401 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002402 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002403 else
2404 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002405 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002406 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002407 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002408 }
you.chen35020192022-05-06 11:30:57 +08002409 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002410
2411 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2412 {
2413 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2414 return -1;
2415 }
2416
2417 if (memcmp(lynq_auth_str,"1",1) == 0 )
2418 {
2419 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2420 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002421 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2423 {
2424 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2425 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002426 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002427 }
2428 else
2429 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002430 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002431 *auth = -1;
2432 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002433
you.chen6c2dd9c2022-05-16 17:55:28 +08002434 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002435}
qs.xiong1af5daf2022-03-14 09:12:12 -04002436
you.chenb95401e2023-05-12 19:39:06 +08002437static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2438{
2439 char status[64];
you.chencba13492023-05-19 13:53:43 +08002440 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002441 curr_status_info curr_state;
2442
2443 CHECK_WPA_CTRL(CTRL_AP);
2444
2445 memset(status, 0, sizeof (status));
2446
2447 curr_state.ap = NULL;
2448 curr_state.state = status;
2449
2450 printf("inner_check_ap_connected %d\n", retry_count);
qs.xiong5a2ba932023-09-13 16:30:21 +08002451 usleep(250*1000);
you.chenb95401e2023-05-12 19:39:06 +08002452 if (0 == inner_get_status_info(idx, &curr_state))
2453 {
qs.xiong5a2ba932023-09-13 16:30:21 +08002454 if ((strcmp(status, STATE_SCANNING) == 0)|| (strcmp(status, STATE_COMPLETED) == 0))
you.chenb95401e2023-05-12 19:39:06 +08002455 {
2456 return 0;
2457 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002458 else if (retry_count == 8) //not ok in 2s, do reconnect
you.chenb95401e2023-05-12 19:39:06 +08002459 {
2460 DO_REQUEST("RECONNECT");
2461 return inner_check_ap_connected(idx, retry_count+1);
2462 }
you.chencba13492023-05-19 13:53:43 +08002463 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002464 {
2465 printf("retry 10 time\n");
2466 return -1;
2467 }
2468 else
2469 {
you.chen6d247052023-06-01 16:39:54 +08002470 if (strcmp(status, STATE_DISCONNECTED) == 0)
2471 {
2472 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2473 DO_REQUEST(LYNQ_WIFI_CMD);
2474 }
you.chenb95401e2023-05-12 19:39:06 +08002475 return inner_check_ap_connected(idx, retry_count+1);
2476 }
2477 }
2478 return -1;
2479}
qs.xiong1af5daf2022-03-14 09:12:12 -04002480
qs.xiongf1b525b2022-03-31 00:58:23 -04002481int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002482{
qs.xiong5a2ba932023-09-13 16:30:21 +08002483 RLOGD("[lynq_wifi]----enter lynq_wifi_ap_start");
you.chen35020192022-05-06 11:30:57 +08002484 char LYNQ_WIFI_CMD[128]={0};
qs.xiong5a2ba932023-09-13 16:30:21 +08002485
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
you.chen35020192022-05-06 11:30:57 +08002496 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2497 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2498
you.chenc9928582023-04-24 15:39:37 +08002499 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2500 if (ret != 0)
2501 {
2502 RLOGE("lynq_wifi_ap_start excute script fail");
2503 return -1;
2504 }
2505
you.chenb95401e2023-05-12 19:39:06 +08002506 if (inner_check_ap_connected(idx, 0) != 0)
2507 {
2508 return -1;
2509 }
2510
you.chen0df3e7e2023-05-10 15:56:26 +08002511 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002512 if (g_ap_tmp_watcher_pid == 0)
2513 {
2514 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2515 {
2516 g_ap_tmp_watcher_pid = 0;
2517 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2518 return -1;
2519 }
2520 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2521 }
qs.xiong5a2ba932023-09-13 16:30:21 +08002522 RLOGD("[lynq_wifi]----end lynq_wifi_ap_start");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002523 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002524}
2525
qs.xiongf1b525b2022-03-31 00:58:23 -04002526int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002527{
you.chen35020192022-05-06 11:30:57 +08002528 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002529}
2530
qs.xiongf1b525b2022-03-31 00:58:23 -04002531int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002532{
you.chen35020192022-05-06 11:30:57 +08002533 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002534
you.chen35020192022-05-06 11:30:57 +08002535 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002536
you.chen35020192022-05-06 11:30:57 +08002537 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002538
you.chen35020192022-05-06 11:30:57 +08002539 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2540
2541 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2542
you.chenb4b121c2022-05-06 17:50:16 +08002543// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002544
2545 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2546 if (ret != 0)
2547 {
2548 RLOGE("lynq_wifi_ap_start excute script fail");
2549 return -1;
2550 }
qs.xiong44fac672023-08-29 16:15:55 +08002551 g_ap_tmp_watcher_stop_flag = 1;
2552 if (g_ap_tmp_watcher_pid != 0)
2553 pthread_join(g_ap_tmp_watcher_pid, NULL);
2554 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002555
qs.xiong9fbf74e2023-03-28 13:38:22 +08002556 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002557}
qs.xiong1af5daf2022-03-14 09:12:12 -04002558
qs.xiongf1b525b2022-03-31 00:58:23 -04002559int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002560{
you.chen35020192022-05-06 11:30:57 +08002561 char lynq_disable_cmd[128] = {0};
2562 char lynq_select_cmd[128] = {0};
2563 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002564 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002565 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002566
you.chen35020192022-05-06 11:30:57 +08002567 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002568 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2569 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2570
2571 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2572 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2573 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002574
qs.xiong9fbf74e2023-03-28 13:38:22 +08002575 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002576}
2577
qs.xiongf1b525b2022-03-31 00:58:23 -04002578int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002579{
you.chen35020192022-05-06 11:30:57 +08002580 char lynq_disable_cmd[128] = {0};
2581 char lynq_select_cmd[128] = {0};
2582 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002583 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002584 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002585
you.chen35020192022-05-06 11:30:57 +08002586 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002587
you.chen35020192022-05-06 11:30:57 +08002588 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2589 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2590
2591 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2592 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2593 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002594
qs.xiong9fbf74e2023-03-28 13:38:22 +08002595 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002596}
qs.xiongf1b525b2022-03-31 00:58:23 -04002597
you.chen35020192022-05-06 11:30:57 +08002598int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002599{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002600 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002601 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002602 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002603 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002604 RLOGD("enter lynq_ap_password_set");
2605 if( password == NULL )
2606 {
2607 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002608 return -1;
2609 }
2610 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002611 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002612 if(pass_len < 8 || pass_len >= 64)
2613 {
2614 RLOGE("[lynq_ap_password_set]input password len not in rage");
2615 return -1;
you.chen35020192022-05-06 11:30:57 +08002616 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002617
you.chen35020192022-05-06 11:30:57 +08002618 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002619
qs.xiong9fbf74e2023-03-28 13:38:22 +08002620 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2621 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002622 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002623 return -1;
2624 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002625 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2626 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002627 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2628 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002629 }
2630
you.chen35020192022-05-06 11:30:57 +08002631 CHECK_WPA_CTRL(CTRL_AP);
2632
qs.xiong9fbf74e2023-03-28 13:38:22 +08002633 if (auth == LYNQ_WIFI_AUTH_WEP)
2634 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002635 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002636 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2637 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2638 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2639 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2640 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002641 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2642 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002643 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 +08002644 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2645 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2646 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002647 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2648 {
2649
qs.xiongc8d92a62023-03-29 17:36:14 +08002650 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 +08002651 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002652 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002653 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2654 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2655
2656 }
2657 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002658 {
2659 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002660 return -1;
2661 }
you.chen35020192022-05-06 11:30:57 +08002662
you.chen35020192022-05-06 11:30:57 +08002663 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002664
qs.xiong9fbf74e2023-03-28 13:38:22 +08002665 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002666}
2667
you.chen35020192022-05-06 11:30:57 +08002668int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002669{
you.chen35020192022-05-06 11:30:57 +08002670 FILE * fp;
2671 int len, ret;
2672 int count, index;
2673 char *split_lines[128] = {0};
2674 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002675 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002676
you.chen35020192022-05-06 11:30:57 +08002677 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002678
you.chen35020192022-05-06 11:30:57 +08002679 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2680// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002681 if (NULL == fp)
2682 {
2683 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002684 return -1;
2685 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002686
you.chen35020192022-05-06 11:30:57 +08002687 buff = alloca(MAX_RET);
2688 fseek(fp, 0, SEEK_SET);
2689 len = fread(buff, 1, MAX_RET, fp);
2690 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002691
qs.xiong9fbf74e2023-03-28 13:38:22 +08002692 for(index=0; index < len; index ++)
2693 {
2694 if (memcmp(buff + index, "network={", 9) != 0)
2695 {
you.chen35020192022-05-06 11:30:57 +08002696 continue;
2697 }
2698 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002699 for (; index < len; index ++ )
2700 {
2701 if (buff[index] != '}')
2702 {
you.chen35020192022-05-06 11:30:57 +08002703 continue;
2704 }
2705 buff[index] = '\0';
2706 break;
2707 }
2708 len = buff + index - p;
2709 }
2710
2711 count = lynq_split(p, len, '\n', split_lines);
2712
2713 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002714 for(index=0; index < count; index++)
2715 {
you.chen35020192022-05-06 11:30:57 +08002716 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002717 if (p != NULL)
2718 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002719 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002720 if (*p == '\"')
2721 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002722 p++;
2723 }
you.chen35020192022-05-06 11:30:57 +08002724 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002725 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2726 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002727 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002728 if (*p == '\"')
2729 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002730 p++;
2731 }
2732 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002733 else
2734 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002735 continue;
you.chen35020192022-05-06 11:30:57 +08002736 }
2737
2738 strcpy(password, p);
2739
qs.xiong9fbf74e2023-03-28 13:38:22 +08002740 while(*password != '\0')
2741 {
2742 if (*password == '\"')
2743 {
you.chen35020192022-05-06 11:30:57 +08002744 *password = '\0';
2745 break;
2746 }
2747 password++;
2748 }
2749 ret = 0;
2750 break;
2751 } //end for(index=0; index < count; index++)
2752
2753 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002754}
2755
you.chen35020192022-05-06 11:30:57 +08002756static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2757 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002758 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002759
qs.xiong9fbf74e2023-03-28 13:38:22 +08002760 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2761 {
you.chen35020192022-05-06 11:30:57 +08002762 return -1;
2763 }
2764
2765 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002766
qs.xiong9fbf74e2023-03-28 13:38:22 +08002767 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2768 {
2769 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002770 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002771 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002772 {
you.chena6cd55a2022-05-08 12:20:18 +08002773 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002774 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002775 }
you.chen70f377f2023-04-14 18:17:09 +08002776 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2777 {
2778 return 0;
2779 }
you.chena6cd55a2022-05-08 12:20:18 +08002780 }
2781 }
you.chen70f377f2023-04-14 18:17:09 +08002782 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2783 {
2784 return 0;
2785 }
2786
qs.xiong9fbf74e2023-03-28 13:38:22 +08002787 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2788 {
you.chen70f377f2023-04-14 18:17:09 +08002789 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002790 return -1;
2791 }
2792 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2793 {
2794
you.chen70f377f2023-04-14 18:17:09 +08002795 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2796 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002797 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2798 {
2799
2800 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002801 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002802 }else
2803 {
you.chen70f377f2023-04-14 18:17:09 +08002804 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002805 *auth = -1;
2806 return -1;
2807 }
you.chen35020192022-05-06 11:30:57 +08002808 return 0;
2809}
2810
2811int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002812{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002813 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002814 int pass_len, net_no, count, index;
2815 char lynq_tmp_cmd[300]={0};
2816 int net_no_list[128];
2817 lynq_wifi_auth_s net_auth;
2818 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002819 if(pass_len < 8 || pass_len >= 64)
2820 {
2821 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002822 return -1;
2823 }
2824
2825 CHECK_IDX(idx, CTRL_STA);
2826
2827 net_no = -1;
2828 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2829
qs.xiong9fbf74e2023-03-28 13:38:22 +08002830 for (index=0; index < count; index++)
2831 {
you.chen35020192022-05-06 11:30:57 +08002832 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002833 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2834 {
you.chen35020192022-05-06 11:30:57 +08002835 net_no = net_no_list[index];
2836 break;
2837 }
2838 }
2839
qs.xiong9fbf74e2023-03-28 13:38:22 +08002840 if (net_no < 0)
2841 {
you.chen35020192022-05-06 11:30:57 +08002842 return -1;
2843 }
2844
2845 CHECK_WPA_CTRL(CTRL_STA);
2846
2847 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2848
2849 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2850 DO_OK_FAIL_REQUEST(cmd_save_config);
2851
2852 return 0;
2853}
2854
2855int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2856
2857 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002858 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002859 int count, index;
2860 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002861 char *buff, *p, *ssid, *ssid_end_flag;
2862 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002863 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002864
you.chen755332b2022-08-06 16:59:10 +08002865 network_len = 0;
2866 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002867 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002868
you.chen35020192022-05-06 11:30:57 +08002869 CHECK_IDX(idx, CTRL_STA);
2870
qs.xiong9fbf74e2023-03-28 13:38:22 +08002871 if (NULL == password)
2872 {
2873 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002874 return -1;
2875 }
2876
you.chen35020192022-05-06 11:30:57 +08002877 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002878 if (NULL == fp)
2879 {
2880 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002881 return -1;
2882 }
2883
you.chen6d247052023-06-01 16:39:54 +08002884 fseek(fp, 0, SEEK_END);
2885 len = ftell(fp);
2886 buff = malloc(len + 1);
2887
2888 if (buff == NULL)
2889 {
2890 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2891 return -1;
2892 }
2893
you.chen35020192022-05-06 11:30:57 +08002894 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002895 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002896 fclose(fp);
2897
qs.xiong9fbf74e2023-03-28 13:38:22 +08002898 for(index=0; index < len; index ++)
2899 {
2900 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002901 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002902 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002903 {
you.chen35020192022-05-06 11:30:57 +08002904 continue;
2905 }
you.chen6d247052023-06-01 16:39:54 +08002906 p = buff + index + 9;
2907 for (; index < len; index ++ )
2908 {
2909 if (buff[index] != '}')
2910 {
2911 continue;
2912 }
2913 buff[index] = '\0';
2914 break;
2915 }
2916 network_len = buff + index - p;
2917 break;
you.chen35020192022-05-06 11:30:57 +08002918 }
2919
qs.xiongb3f26af2023-02-17 18:41:07 +08002920 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002921 {
2922 if (buff != NULL)
2923 {
2924 free(buff);
2925 }
2926
qs.xiongb3f26af2023-02-17 18:41:07 +08002927 return -1;
you.chen6d247052023-06-01 16:39:54 +08002928 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002929
you.chend2fef3f2023-02-13 10:50:35 +08002930 ssid = strstr(p, "ssid=");
2931 if (ssid != NULL) {
2932 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002933 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002934 {
you.chend2fef3f2023-02-13 10:50:35 +08002935 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2936 break;
2937 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002938 else
you.chen6d247052023-06-01 16:39:54 +08002939 {
you.chend2fef3f2023-02-13 10:50:35 +08002940 ssid_end_flag = strstr(ssid, "\n");
2941 if (ssid_end_flag != NULL)
2942 {
2943 ssid_len = (ssid_end_flag - ssid) / 2;
2944 for(i=0; i<ssid_len; i++)
2945 {
2946 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2947 }
2948 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2949 break;
2950 }
2951 }
you.chen35020192022-05-06 11:30:57 +08002952 }
you.chend2fef3f2023-02-13 10:50:35 +08002953
you.chen35020192022-05-06 11:30:57 +08002954 }
2955
qs.xiong9fbf74e2023-03-28 13:38:22 +08002956 if (index >= len || NULL == p || network_len <= 0)
2957 {
you.chen6d247052023-06-01 16:39:54 +08002958 if (buff != NULL)
2959 {
2960 free(buff);
2961 }
you.chen35020192022-05-06 11:30:57 +08002962 return -1;
2963 }
2964
you.chen755332b2022-08-06 16:59:10 +08002965 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002966
2967 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002968 for(index=0; index < count; index++)
2969 {
you.chen35020192022-05-06 11:30:57 +08002970 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002971 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002972 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002973 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002974 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002975 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002976 p++;
2977 }
you.chen35020192022-05-06 11:30:57 +08002978 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002979 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002980 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002981 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002982 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002983 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002984 p++;
2985 }
2986 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002987 else
you.chen6d247052023-06-01 16:39:54 +08002988 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002989 continue;
you.chen35020192022-05-06 11:30:57 +08002990 }
2991
qs.xiong13673462023-02-21 19:12:54 +08002992 if (*p == '\"')
2993 p++;
2994 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002995
qs.xiong13673462023-02-21 19:12:54 +08002996 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002997 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002998 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002999 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08003000 {
you.chen35020192022-05-06 11:30:57 +08003001 *password = '\0';
3002 break;
3003 }
3004 password++;
3005 }
3006 ret = 0;
3007 break;
3008 } //end for(index=0; index < count; index++)
3009
you.chen6d247052023-06-01 16:39:54 +08003010 if (buff != NULL)
3011 {
3012 free(buff);
3013 }
3014
you.chen35020192022-05-06 11:30:57 +08003015 return ret;
3016}
3017
3018static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3019{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003020 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003021
qs.xiong9fbf74e2023-03-28 13:38:22 +08003022 if (sta_ssid == NULL)
3023 {
3024 RLOGE("sta_ssid is null\n");
3025 return -1;
you.chen35020192022-05-06 11:30:57 +08003026 }
3027
qs.xiong9fbf74e2023-03-28 13:38:22 +08003028 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003029
3030 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3031
3032 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3033// DO_OK_FAIL_REQUEST(cmd_save_config);
3034
qs.xiong9fbf74e2023-03-28 13:38:22 +08003035 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003036
3037}
3038
you.chen35020192022-05-06 11:30:57 +08003039static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003040{
you.chen35020192022-05-06 11:30:57 +08003041 char lynq_disable_cmd[128]={0};
3042 char lynq_select_cmd[128]={0};
3043
3044 CHECK_WPA_CTRL(CTRL_STA);
3045
qs.xiong9fbf74e2023-03-28 13:38:22 +08003046 if (save != 0)
3047 {
you.chenc29444e2022-06-07 18:01:16 +08003048 if (start_flag != 0)
3049 {
3050 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3051 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3052 }
3053 else
3054 {
3055 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3056 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3057 }
you.chen35020192022-05-06 11:30:57 +08003058 DO_OK_FAIL_REQUEST(cmd_save_config);
3059 }
3060
qs.xiong9fbf74e2023-03-28 13:38:22 +08003061 if (start_flag == 0)
3062 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003063 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003064 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3065 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003066 else
3067 {
you.chen35020192022-05-06 11:30:57 +08003068 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3069 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3070 }
3071
3072 return 0;
3073}
3074
3075int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3076{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003077 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003078 CHECK_IDX(idx, CTRL_STA);
3079
you.chen6c2dd9c2022-05-16 17:55:28 +08003080 curr_status_info curr_state;
3081 ap_info_s ap_info;
3082 curr_state.ap = &ap_info;
3083 curr_state.state = NULL;
3084
qs.xiong9fbf74e2023-03-28 13:38:22 +08003085 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3086 {
you.chend2fef3f2023-02-13 10:50:35 +08003087 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003088 return 0;
3089 }
3090
3091 return -1;
you.chen35020192022-05-06 11:30:57 +08003092}
3093
3094int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3095{
you.chen9ac66392022-08-06 17:01:16 +08003096 scan_info_s *scan_list = NULL;
3097 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003098 int scan_len=0;
3099 int save_len=0;
3100 int best_index = -1;
3101 int best_scan_index = -1;
3102 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003103 int i, j, ret;
3104
3105 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003106
3107 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003108 if (info == NULL)
3109 {
you.chen35020192022-05-06 11:30:57 +08003110 return -1;
3111 }
3112
3113 curr_status_info curr_state;
3114 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003115 char status[64];
you.chen35020192022-05-06 11:30:57 +08003116
you.chen9ac66392022-08-06 17:01:16 +08003117 memset(&ap_info, 0, sizeof (ap_info));
3118 memset(status, 0, sizeof (status));
3119
3120 curr_state.ap = &ap_info;
3121 curr_state.state = status;
3122
qs.xiong9fbf74e2023-03-28 13:38:22 +08003123 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3124 {
you.chen35020192022-05-06 11:30:57 +08003125 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003126 if (strcmp(status, STATE_COMPLETED) == 0)
3127 {
3128 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3129 }
3130 else
3131 {
3132 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3133 }
you.chen593621d2023-04-27 17:52:44 +08003134 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003135 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003136 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003137 return 0;
3138 }
3139
you.chen9ac66392022-08-06 17:01:16 +08003140 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003141 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003142 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3143 {
you.chen9ac66392022-08-06 17:01:16 +08003144 if (NULL != scan_list)
3145 {
3146 free(scan_list);
3147 }
you.chen35020192022-05-06 11:30:57 +08003148 return -1;
3149 }
3150
qs.xiong9fbf74e2023-03-28 13:38:22 +08003151 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3152 {
you.chen9ac66392022-08-06 17:01:16 +08003153 if (NULL != scan_list)
3154 {
3155 free(scan_list);
3156 }
3157 if (NULL != save_list)
3158 {
3159 free(save_list);
3160 }
you.chen35020192022-05-06 11:30:57 +08003161 return -1;
3162 }
3163
qs.xiong9fbf74e2023-03-28 13:38:22 +08003164 for (i=0; i < save_len; i++)
3165 {
3166 for (j=0; j < scan_len; j++)
3167 {
you.chen35020192022-05-06 11:30:57 +08003168 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003169 && save_list[i].base_info.auth == scan_list[j].auth)
3170 {
3171 if (best_rssi == 0)
3172 {
you.chen9ac66392022-08-06 17:01:16 +08003173 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003174 best_rssi = scan_list[j].rssi;
3175 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003176 else if (best_rssi > scan_list[j].rssi)
3177 {
you.chen35020192022-05-06 11:30:57 +08003178 best_index = i;
3179 best_scan_index = j;
3180 best_rssi = scan_list[j].rssi;
3181 }
you.chend2fef3f2023-02-13 10:50:35 +08003182 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 +08003183 break;
3184 }
3185 }
3186 }
3187
qs.xiong9fbf74e2023-03-28 13:38:22 +08003188 if (best_index >= 0)
3189 {
you.chen35020192022-05-06 11:30:57 +08003190 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003191 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 +08003192 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3193 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003194 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003195 }
3196
you.chen9ac66392022-08-06 17:01:16 +08003197 if (NULL != scan_list)
3198 {
3199 free(scan_list);
3200 }
3201 if (NULL != save_list)
3202 {
3203 free(save_list);
3204 }
3205
3206 return ret;
you.chen35020192022-05-06 11:30:57 +08003207}
3208
3209static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3210{
qs.xiongc8d92a62023-03-29 17:36:14 +08003211 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003212 char lynq_ket_mgmt_cmd[64]={0};
3213 char lynq_pairwise_cmd[64]={0};
3214 char lynq_psk_cmd[64]={0};
3215
3216 CHECK_WPA_CTRL(CTRL_STA);
3217
qs.xiong9fbf74e2023-03-28 13:38:22 +08003218 switch(auth)
3219 {
3220 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003221 {
3222 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003223
you.chen35020192022-05-06 11:30:57 +08003224 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003225// DO_OK_FAIL_REQUEST(cmd_save_config);
3226 break;
3227 }
3228 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003229 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003230 {
3231 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3232 {
you.chen35020192022-05-06 11:30:57 +08003233 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3234 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003235 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3236 {
you.chena6cd55a2022-05-08 12:20:18 +08003237 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003238 }
3239 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3240 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003241
you.chen35020192022-05-06 11:30:57 +08003242 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3243 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3244 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003245
qs.xiong9fbf74e2023-03-28 13:38:22 +08003246 if (password != NULL)
3247 {
you.chen35020192022-05-06 11:30:57 +08003248 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3249 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003250 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003251 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003252
you.chen35020192022-05-06 11:30:57 +08003253// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003254 break;
3255 }
3256 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3257 {
qs.xiong3e506812023-04-06 11:08:48 +08003258 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003259 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003260 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3261 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3262
qs.xiong3e506812023-04-06 11:08:48 +08003263 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003264 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3265 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3266 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3267
3268 break;
3269 }
3270 case LYNQ_WIFI_AUTH_WPA3_PSK:
3271 {
qs.xiong3e506812023-04-06 11:08:48 +08003272 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003273 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003274 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3275 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3276
qs.xiong3e506812023-04-06 11:08:48 +08003277 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003278 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3279 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3280 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3281
3282 break;
3283 }
3284 default:
3285 return -1;
you.chen35020192022-05-06 11:30:57 +08003286 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003287
qs.xiong9fbf74e2023-03-28 13:38:22 +08003288 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003289}
qs.xiong7a105ce2022-03-02 09:43:11 -05003290
you.chen35020192022-05-06 11:30:57 +08003291static int inner_get_curr_net_no(int interface) {
3292 curr_status_info curr_state;
3293 curr_state.ap = NULL;
3294 curr_state.state = NULL;
3295
qs.xiong9fbf74e2023-03-28 13:38:22 +08003296 if (0 != inner_get_status_info(interface, &curr_state))
3297 {
you.chen35020192022-05-06 11:30:57 +08003298 return -1;
3299 }
3300
3301 return curr_state.net_no;
3302}
3303
3304int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003305{
you.chen35020192022-05-06 11:30:57 +08003306 int net_no;
3307 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003308
you.chen35020192022-05-06 11:30:57 +08003309 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003310
qs.xiong9fbf74e2023-03-28 13:38:22 +08003311 if (net_no < 0)
3312 {
you.chen35020192022-05-06 11:30:57 +08003313 return -1;
3314 }
3315
3316 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003317}
3318
you.chenb95401e2023-05-12 19:39:06 +08003319int 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 -05003320{
you.chen35020192022-05-06 11:30:57 +08003321 int count, net_no, index;
3322 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003323 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003324 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003325 curr_status_info curr_state;
3326 ap_info_s ap_info;
3327 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003328
qs.xiong9fbf74e2023-03-28 13:38:22 +08003329 if (ssid == NULL || *ssid == '\0')
3330 {
3331 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003332 return -1;
3333 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003334
qs.xiong9fbf74e2023-03-28 13:38:22 +08003335 if (LYNQ_WIFI_AUTH_OPEN != auth)
3336 {
3337 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003338 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003339 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003340 return -1;
3341 }
3342 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003343
you.chen70f377f2023-04-14 18:17:09 +08003344
3345 pthread_mutex_lock(&s_global_check_mutex);
3346 if (s_sta_status != INNER_STA_STATUS_INIT)
3347 {
3348 s_sta_status = INNER_STA_STATUS_CANCEL;
3349 pthread_cond_signal(&s_global_check_cond);
3350 }
3351 pthread_mutex_unlock(&s_global_check_mutex);
3352
you.chen35020192022-05-06 11:30:57 +08003353 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003354 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003355
3356 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003357 memset(&ap_info, 0, sizeof (ap_info));
3358 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003359
you.chen70f377f2023-04-14 18:17:09 +08003360 curr_state.ap = &ap_info;
3361 curr_state.state = status;
3362
3363 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003364 {
you.chen70f377f2023-04-14 18:17:09 +08003365 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3366 {
3367 net_no = curr_state.net_no;
3368 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3369 && strcmp(ap_info.psw, psw) == 0)
3370 {
3371 RLOGD("already connected\n");
3372
3373 pthread_mutex_lock(&s_global_check_mutex);
3374 s_sta_status = INNER_STA_STATUS_CONNECTED;
3375 pthread_cond_signal(&s_global_check_cond);
3376 pthread_mutex_unlock(&s_global_check_mutex);
3377 return 0;
3378 }
you.chen35020192022-05-06 11:30:57 +08003379 }
3380 }
3381
you.chen70f377f2023-04-14 18:17:09 +08003382 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003383 {
you.chen70f377f2023-04-14 18:17:09 +08003384 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3385
3386 for (index=0; index < count; index++)
3387 {
3388 net_auth = -1;
3389 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3390 {
3391 net_no = net_no_list[index];
3392 break;
3393 }
you.chen35020192022-05-06 11:30:57 +08003394 }
3395
you.chen70f377f2023-04-14 18:17:09 +08003396 if (net_no < 0)
3397 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003398 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3399 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3400 {
3401 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3402 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3403 DO_OK_FAIL_REQUEST(rm_net_cmd);
3404 }
you.chen70f377f2023-04-14 18:17:09 +08003405 net_no = lynq_add_network(CTRL_STA);
3406 if (net_no == -1)
3407 {
3408 return -1;
3409 }
3410
3411 RLOGD("net no is %d\n", net_no);
3412 if (0 != inner_set_sta_ssid(net_no, ssid))
3413 {
3414 return -1;
3415 }
you.chen35020192022-05-06 11:30:57 +08003416 }
3417 }
3418
qs.xiong9fbf74e2023-03-28 13:38:22 +08003419 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3420 {
you.chen35020192022-05-06 11:30:57 +08003421 return -1;
3422 }
3423
you.chen70f377f2023-04-14 18:17:09 +08003424
3425 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003426 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003427 usleep(200*1000);
3428
3429 ret = inner_sta_start_stop(net_no, 1, 1);
3430
3431 pthread_mutex_lock(&s_global_check_mutex);
3432 s_sta_status = INNER_STA_STATUS_CONNECTING;
3433 strcpy(s_sta_current_connecting_ssid, ssid);
3434 struct timeval now;
3435 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003436 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003437 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3438 pthread_cond_signal(&s_global_check_cond);
3439 pthread_mutex_unlock(&s_global_check_mutex);
3440 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003441}
3442
you.chenb95401e2023-05-12 19:39:06 +08003443int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3444{
3445 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3446}
3447
you.chen35020192022-05-06 11:30:57 +08003448int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003449{
you.chen35020192022-05-06 11:30:57 +08003450 ap_info_s ap;
3451 curr_status_info curr_state;
3452 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003453
qs.xiong9fbf74e2023-03-28 13:38:22 +08003454 if (ssid == NULL || *ssid == '\0')
3455 {
3456 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003457 return -1;
3458 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003459
you.chen35020192022-05-06 11:30:57 +08003460 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003461
you.chen35020192022-05-06 11:30:57 +08003462 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003463 curr_state.state = NULL;
3464
qs.xiong9fbf74e2023-03-28 13:38:22 +08003465 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3466 {
you.chen35020192022-05-06 11:30:57 +08003467 return 0;
3468 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003469
qs.xiong9fbf74e2023-03-28 13:38:22 +08003470 if (strcmp(ap.ap_ssid, ssid) != 0)
3471 {
you.chen35020192022-05-06 11:30:57 +08003472 return 0;
3473 }
3474
you.chen70f377f2023-04-14 18:17:09 +08003475 pthread_mutex_lock(&s_global_check_mutex);
3476 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3477 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003478 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003479}
qs.xiong97fa59b2022-04-07 05:41:29 -04003480
qs.xiongc93bf2b2023-08-25 10:22:08 +08003481int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3482{
3483 ap_info_s ap;
3484 curr_status_info curr_state;
3485 ap.ap_ssid[0] = '\0';
3486
3487 if (ssid == NULL || *ssid == '\0')
3488 {
3489 RLOGE("input ssid is NULL\n");
3490 return -1;
3491 }
3492
3493 CHECK_IDX(idx, CTRL_STA);
3494
3495
3496 curr_state.ap = &ap;
3497 curr_state.state = NULL;
3498
3499 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3500 {
3501 return 0;
3502 }
3503
3504 if (strcmp(ap.ap_ssid, ssid) != 0)
3505 {
3506 return 0;
3507 }
3508
3509 pthread_mutex_lock(&s_global_check_mutex);
3510 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3511 pthread_mutex_unlock(&s_global_check_mutex);
3512 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3513
3514}
3515
3516
you.chena6cd55a2022-05-08 12:20:18 +08003517int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3518{
qs.xiongad2f89d2023-01-18 13:17:41 +08003519// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3520// const char *lynq_reconnect_cmd = "RECONNECT";
3521 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3522 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
qs.xiong7a105ce2022-03-02 09:43:11 -05003523
you.chen35020192022-05-06 11:30:57 +08003524 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003525 CHECK_WPA_CTRL(CTRL_STA);
3526
you.chenc9928582023-04-24 15:39:37 +08003527 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3528 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003529 {
you.chenc9928582023-04-24 15:39:37 +08003530 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003531 return -1;
3532 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003533
qs.xiongad2f89d2023-01-18 13:17:41 +08003534 system(lynq_enable_sta_cmd);
3535 system(lynq_reconnect_cmd);
3536// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003537 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003538}
3539
you.chen6d247052023-06-01 16:39:54 +08003540static int inner_get_status_info_state (int interface, char *state) {
3541 curr_status_info curr_state;
3542 curr_state.ap = NULL;
3543 curr_state.state = state;
3544 return inner_get_status_info(interface, &curr_state);
3545}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003546
3547int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3548{
3549
3550 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3551
3552 CHECK_IDX(idx, CTRL_STA);
3553 CHECK_WPA_CTRL(CTRL_STA);
3554
3555 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3556 if (ret != 0)
3557 {
3558 RLOGE("lynq_wifi_ap_start excute script fail");
3559 return -1;
3560 }
3561
3562// system(lynq_enable_sta_cmd);
3563 system(lynq_reconnect_cmd);
3564
3565 return 0;
3566}
3567
3568
you.chen35020192022-05-06 11:30:57 +08003569int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003570{
qs.xiongad2f89d2023-01-18 13:17:41 +08003571// char lynq_disable_network_cmd[MAX_CMD];
3572// curr_status_info curr_state;
3573// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003574 int i=0;
3575 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003576
you.chen6d247052023-06-01 16:39:54 +08003577// 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 +08003578 CHECK_IDX(idx, CTRL_STA);
3579 CHECK_WPA_CTRL(CTRL_STA);
3580
you.chen6d247052023-06-01 16:39:54 +08003581// system(lynq_disable_sta_cmd);
3582 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003583 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003584
3585 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3586 if (ret != 0)
3587 {
3588 RLOGE("lynq_wifi_ap_start excute script fail");
3589 return -1;
3590 }
3591
you.chen6d247052023-06-01 16:39:54 +08003592 for (i=0; i < 30; i++) // to check if sta is realy stoped
3593 {
3594 if (inner_get_status_info_state(idx, state) != 0)
3595 {
3596 break;
3597 }
3598
3599 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3600 {
3601 break;
3602 }
3603 RLOGD("lynq_wifi_ap_start curr state %s", state);
3604 usleep(SLEEP_TIME_ON_IDLE);
3605 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003606
you.chena6cd55a2022-05-08 12:20:18 +08003607 return 0;
3608// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003609}
qs.xiongfcc914b2023-07-06 21:16:20 +08003610int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3611{
3612 char LYNQ_DISABLE_CMD[128]={0};
3613 CHECK_IDX(idx, CTRL_STA);
3614 CHECK_WPA_CTRL(CTRL_STA);
3615 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3616 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3617 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3618 return 0;
3619}
qs.xiong7a105ce2022-03-02 09:43:11 -05003620
you.chen35020192022-05-06 11:30:57 +08003621//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3622// int i, count;
3623// char *p;
3624// const char * FLAG_SSID = "ssid=";
3625// const char * FLAG_SBSID = "bssid=";
3626// const char * FLAG_KEY_MGMT = "key_mgmt=";
3627// const char * FLAG_FREQ = "freq=";
3628// char lynq_sta_cmd[MAX_CMD];
3629// char *split_lines[128] = {0};
3630
3631// CHECK_WPA_CTRL(CTRL_AP);
3632
3633// sprintf(lynq_sta_cmd, "STA %s", bssid);
3634
3635// DO_REQUEST(lynq_sta_cmd);
3636
3637// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3638
3639// for(i=0; i < count; i++) {
3640// p = strstr(split_lines[i], FLAG_SSID);
3641// if (p != NULL) {
3642// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3643// continue;
3644// }
3645// }
3646
3647// lynq_get_interface_ip(idx, ap->ap_ip);
3648// lynq_ap_password_set(idx, ap->psw);
3649
3650// return 0;
3651//}
3652
3653static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3654 curr_status_info curr_state;
3655 curr_state.ap = ap;
3656 curr_state.state = NULL;
3657 return inner_get_status_info(interface, &curr_state);
3658}
3659
3660int 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 -04003661{
qs.xiong5071c802023-09-06 14:04:15 +08003662 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003663 int index, line_count;
3664 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003665 const char *lynq_first_sta_cmd = "STA-FIRST";
3666 char lynq_next_sta_cmd[MAX_CMD];
3667 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003668 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003669
you.chen35020192022-05-06 11:30:57 +08003670 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003671
you.chen35020192022-05-06 11:30:57 +08003672 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003673
you.chenb95401e2023-05-12 19:39:06 +08003674 // ap_info_s * tmp_ap;
3675 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003676 if (ap == NULL || list == NULL || len == NULL)
3677 {
3678 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003679 return -1;
3680 }
3681
you.chenb95401e2023-05-12 19:39:06 +08003682 // ap = &tmp_ap;
3683 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003684 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003685 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003686
you.chenb95401e2023-05-12 19:39:06 +08003687 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003688 {
you.chenb95401e2023-05-12 19:39:06 +08003689 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003690 return -1;
3691 }
3692
3693 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3694 lynq_ap_password_get(idx, (*ap)->psw);
3695
you.chen35020192022-05-06 11:30:57 +08003696 DO_REQUEST(lynq_first_sta_cmd);
3697
3698 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003699 while (reply_len > 0)
3700 {
3701 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003702 {
you.chen35020192022-05-06 11:30:57 +08003703 break;
3704 }
3705 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3706 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3707 strcpy(bssid[index], split_lines[0]);
3708 index++;
3709 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3710 reply_len = MAX_RET;
3711 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003712 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 +08003713 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003714 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003715 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003716 break;
3717 }
3718 }
3719
3720 *len = index;
3721
3722 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003723 for (index=0; index < *len; index++)
3724 {
you.chend2fef3f2023-02-13 10:50:35 +08003725 dev_info = &(*list)[index];
3726 memset(dev_info, 0, sizeof(device_info_s));
3727 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3728 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3729 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3730 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003731 free(bssid[index]);
3732 }
qs.xiong5071c802023-09-06 14:04:15 +08003733 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003734 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003735}
3736
you.chen35020192022-05-06 11:30:57 +08003737int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003738{
you.chen35020192022-05-06 11:30:57 +08003739 int i, count, index, count_words;
3740 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3741 char *split_lines[128] = {0};
3742 char *split_words[128] = {0};
3743 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003744
qs.xiong9fbf74e2023-03-28 13:38:22 +08003745 if (list == NULL || len == NULL)
3746 {
you.chen35020192022-05-06 11:30:57 +08003747 return -1;
3748 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003749
you.chen9ac66392022-08-06 17:01:16 +08003750 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3751 {
3752 usleep(100 * 1000);
3753 }
3754
you.chen35020192022-05-06 11:30:57 +08003755 CHECK_IDX(idx, CTRL_STA);
3756
3757 CHECK_WPA_CTRL(CTRL_STA);
3758
3759 DO_REQUEST(lynq_scan_result_cmd);
3760
3761 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3762 *len = count - 1;
3763 *list = malloc(sizeof (scan_info_s) * *len);
3764
3765 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003766 for (index=0; index <count_words; index++)
3767 {
3768 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003769 }
3770
qs.xiong9fbf74e2023-03-28 13:38:22 +08003771 for(index = 1;index < count; index++)
3772 {
3773 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003774 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003775 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3776 if (count_words < 4)
3777 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003778 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003779 //bssid / frequency / signal level / flags / ssid
3780 p = (*list) + index - 1;
3781 strcpy(p->mac, split_words[0]);
3782 p->band = convert_band_from_freq(atoi(split_words[1]));
3783 p->rssi = -1 * atoi( split_words[2]);
3784 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003785 if (count_words == 4) // ssid hided
3786 {
3787 p->ssid[0] = '\0';
3788 }
3789 else
3790 {
3791 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3792 }
you.chen35020192022-05-06 11:30:57 +08003793 }
3794
3795 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003796}
qs.xiong97fa59b2022-04-07 05:41:29 -04003797
you.chen35020192022-05-06 11:30:57 +08003798int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3799{
3800 int count, net_no, index;
3801 int net_no_list[128];
3802 lynq_wifi_auth_s net_auth;
3803 char lynq_remove_cmd[MAX_CMD];
3804
qs.xiong9fbf74e2023-03-28 13:38:22 +08003805 if (ssid == NULL || *ssid == '\0')
3806 {
3807 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003808 return -1;
3809 }
3810
3811 CHECK_IDX(idx, CTRL_STA);
3812
3813 CHECK_WPA_CTRL(CTRL_STA);
3814
3815 net_no = -1;
3816 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3817
qs.xiong9fbf74e2023-03-28 13:38:22 +08003818 for (index=0; index < count; index++)
3819 {
you.chen35020192022-05-06 11:30:57 +08003820 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003821 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3822 {
you.chen35020192022-05-06 11:30:57 +08003823 net_no = net_no_list[index];
3824 break;
3825 }
3826 }
3827
qs.xiong9fbf74e2023-03-28 13:38:22 +08003828 if (net_no < 0)
3829 {
you.chen35020192022-05-06 11:30:57 +08003830 return 0;
3831 }
3832
3833 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3834
3835 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3836 DO_OK_FAIL_REQUEST(cmd_save_config);
3837
3838 return 0;
3839}
3840
3841int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003842{
you.chend2fef3f2023-02-13 10:50:35 +08003843 int count, index;
you.chen35020192022-05-06 11:30:57 +08003844 int net_no_list[128];
3845 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003846 RLOGD("enter lynq_get_sta_saved_ap api\n");
3847 if (list == NULL || len == NULL)
3848 {
3849 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003850 return -1;
3851 }
3852
3853 CHECK_IDX(idx, CTRL_STA);
3854
3855// CHECK_WPA_CTRL(CTRL_STA);
3856
3857 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003858 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003859
you.chen057aac42023-04-13 14:06:58 +08003860 if (count < 0)
3861 {
3862 RLOGE("list network fail");
3863 return count;
3864 }
3865 else if (count == 0)
3866 {
3867 *list = NULL;
3868 *len = 0;
3869 return 0;
3870 }
3871
you.chen35020192022-05-06 11:30:57 +08003872 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003873 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003874 *len = count;
3875
qs.xiong9fbf74e2023-03-28 13:38:22 +08003876 for (index=0; index < count; index++)
3877 {
you.chen35020192022-05-06 11:30:57 +08003878 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003879 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003880 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003881 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003882 {
you.chen35020192022-05-06 11:30:57 +08003883 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3884 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003885 else
you.chen057aac42023-04-13 14:06:58 +08003886 {
you.chen35020192022-05-06 11:30:57 +08003887 (*list)[index].base_info.band = -1;
3888 }
you.chen057aac42023-04-13 14:06:58 +08003889 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003890 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003891 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003892 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003893 return 0;
3894}
3895
3896int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3897{
qs.xiong20202422023-09-06 18:01:18 +08003898 if ( g_sta_conncet_status_flag != 0 )
3899 {
3900 RLOGD("current sta is connecting dest ap");
3901 return -1;
3902 }
qs.xiongc8d92a62023-03-29 17:36:14 +08003903 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003904 const char *lynq_scan_cmd = "SCAN";
3905
3906 CHECK_IDX(idx, CTRL_STA);
3907
3908 CHECK_WPA_CTRL(CTRL_STA);
3909
you.chen0df3e7e2023-05-10 15:56:26 +08003910 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3911 {
3912 RLOGD("tmp clear scanlist");
3913 system(clean_last_re);
3914 }
you.chen9ac66392022-08-06 17:01:16 +08003915 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003916 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003917 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3918 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003919 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003920 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3921 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003922 g_sta_scan_finish_flag = 1;
3923 return -1;
3924 }
you.chen35020192022-05-06 11:30:57 +08003925
3926 return 0;
3927}
3928
3929int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003930 if (cb == NULL)
3931 {
3932 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003933 return -1;
3934 }
3935
you.chen6d247052023-06-01 16:39:54 +08003936 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003937 g_ap_callback_priv = priv;
3938 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003939 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003940
you.chen6d247052023-06-01 16:39:54 +08003941 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003942 if (g_ap_watcher_pid == 0 )
3943 {
3944 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3945 {
3946 g_ap_watcher_pid = 0;
3947 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3948 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3949 return -1;
3950 }
3951 }
3952
3953 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3954 RLOGD("creat APWatcherTheradProc susccs");
3955
you.chen35020192022-05-06 11:30:57 +08003956 return 0;
3957}
3958
3959int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003960 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003961 if (g_ap_callback_priv == priv)
3962 {
you.chen35020192022-05-06 11:30:57 +08003963 g_ap_callback_func = NULL;
3964 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003965 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003966 return 0;
3967 }
you.chen6d247052023-06-01 16:39:54 +08003968 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003969 return -1;
3970}
3971
3972int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003973 if (cb == NULL)
3974 {
3975 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003976 return -1;
3977 }
3978
you.chen6d247052023-06-01 16:39:54 +08003979 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003980 g_sta_callback_priv = priv;
3981 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003982 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003983
you.chen6d247052023-06-01 16:39:54 +08003984 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003985 if (g_sta_watcher_pid == 0 ) {
3986 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3987 {
3988 g_sta_watcher_pid = 0;
3989 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3990 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3991 return -1;
3992 }
3993 }
3994
3995 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3996 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003997 return 0;
3998}
3999
4000int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08004001 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004002 if (g_sta_callback_priv == priv)
4003 {
you.chen35020192022-05-06 11:30:57 +08004004 g_sta_callback_func = NULL;
4005 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08004006 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004007 return 0;
4008 }
you.chen6d247052023-06-01 16:39:54 +08004009 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004010 return -1;
4011}
4012
qs.xiongfcc914b2023-07-06 21:16:20 +08004013int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4014 if (cb == NULL)
4015 {
4016 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4017 return -1;
4018 }
4019 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4020 g_sta_auto_callback_priv = priv;
4021 g_sta_auto_callback_func = cb;
4022 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4023 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4024 if (g_sta_auto_watcher_pid == 0 ) {
4025 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4026 {
4027 g_sta_auto_watcher_pid = 0;
4028 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4029 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4030 return -1;
4031 }
4032 }
4033 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4034 RLOGD("creat STAWatcherTheradProc susccs");
4035 return 0;
4036}
4037int lynq_unreg_sta_auto_event_callback(void * priv) {
4038 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4039 if (g_sta_auto_callback_priv == priv)
4040 {
4041 g_sta_auto_watcher_stop_flag = 1;
4042 if (g_sta_auto_watcher_pid != 0)
4043 {
4044 pthread_join(g_sta_auto_watcher_pid, NULL);
4045 }
4046 g_sta_auto_watcher_pid = 0;
4047 g_sta_auto_callback_func = NULL;
4048 g_sta_auto_callback_priv = NULL;
4049 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4050 return 0;
4051 }
4052 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4053 return -1;
4054}
you.chen35020192022-05-06 11:30:57 +08004055int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4056{
4057 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004058 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004059 CHECK_IDX(idx, CTRL_AP);
4060
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4062 {
you.chen35020192022-05-06 11:30:57 +08004063 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4064 return 0;
4065 }
4066
qs.xiong9fbf74e2023-03-28 13:38:22 +08004067 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4068 {
you.chen35020192022-05-06 11:30:57 +08004069 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4070 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004071 else
4072 {
you.chen35020192022-05-06 11:30:57 +08004073 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4074 }
4075
4076 return 0;
4077}
4078
4079int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4080 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004081 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004082 CHECK_IDX(idx, CTRL_STA);
4083
qs.xiong9fbf74e2023-03-28 13:38:22 +08004084 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4085 {
you.chen35020192022-05-06 11:30:57 +08004086 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4087 return 0;
4088 }
4089
qs.xiong9fbf74e2023-03-28 13:38:22 +08004090 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4091 {
you.chen35020192022-05-06 11:30:57 +08004092 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4093 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004094 else
4095 {
you.chen35020192022-05-06 11:30:57 +08004096 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4097 }
4098
4099 return 0;
4100}
4101
4102int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4103// CHECK_IDX(idx, CTRL_AP);
4104// int ret = 0;
4105// size_t reply_len = MAX_RET;
4106// char cmd_reply[MAX_RET]={0};
4107// const char * cmd_str = "GET country";
4108// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4109// do{
4110// if (NULL == s_lynq_wpa_ctrl) {
4111// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4112// if (NULL == s_lynq_wpa_ctrl ) {
4113// printf("wpa_ctrl_open fail\n");
4114// return -1;
4115// }
4116// }
4117// }while(0);
4118
4119// do {
4120// reply_len = MAX_RET;
4121// cmd_reply[0] = '\0';
4122// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004123// 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 +08004124// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004125// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004126// return ret;
4127// }
4128// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004129// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004130// }while(0);
4131
4132 FILE *fp;
4133 size_t i = 0;
4134 char lynq_cmd_ret[MAX_RET]={0};
4135
4136// CHECK_IDX(idx, CTRL_AP);
4137
4138 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004139 {
4140 perror("popen error!");
4141 return -1;
4142 }
you.chen35020192022-05-06 11:30:57 +08004143 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4144 {
4145 perror("fread fail!");
4146 return -1;
4147 }
4148
qs.xiong9fbf74e2023-03-28 13:38:22 +08004149 for(i=0; i < strlen(lynq_cmd_ret); i++)
4150 {
4151 if (lynq_cmd_ret[i] == ' ')
4152 {
you.chen35020192022-05-06 11:30:57 +08004153 lynq_cmd_ret[i] = '\0';
4154 break;
4155 }
4156 }
4157
4158 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004159 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004160
4161 int ret=pclose(fp);
4162 if(ret==-1)
4163 {
4164 perror("close file faild");
4165 }
4166
4167 return 0;
4168}
4169
qs.xiong44fac672023-08-29 16:15:55 +08004170
you.chen705a7ef2023-06-01 22:06:45 +08004171static int check_and_init_uci_config(char * country_code)
4172{
4173 FILE * fp;
4174 int is_different = 0;
4175 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4176 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4177 const char * commit_uci_cmd ="uci commit";
4178 char set_country_cmd[MAX_CMD];
4179 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004180
you.chen705a7ef2023-06-01 22:06:45 +08004181 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004182
you.chen705a7ef2023-06-01 22:06:45 +08004183 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004184 {
you.chen705a7ef2023-06-01 22:06:45 +08004185 if (0 != system(create_uci_cmd))
4186 {
4187 RLOGE("creat_uci_cmd fail");
4188 return -1;
4189 }
4190 is_different = 1;
4191 }
4192
4193 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4194 {
4195 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004196 return -1;
4197 }
4198
you.chen705a7ef2023-06-01 22:06:45 +08004199 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004200 {
you.chen705a7ef2023-06-01 22:06:45 +08004201 RLOGE("fread fail!");
4202 fclose(fp);
4203 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004204 }
4205
you.chen705a7ef2023-06-01 22:06:45 +08004206 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4207 {
qs.xiong44fac672023-08-29 16:15:55 +08004208 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 +08004209 is_different = 1;
4210 }
4211
4212 fclose(fp);
4213
4214 if (is_different)
4215 {
4216 if ( 0 != system(set_country_cmd))
4217 {
4218 RLOGE("set_country_cmd fail");
4219 return -1;
4220 }
4221 if (0 != system(commit_uci_cmd))
4222 {
4223 RLOGE("commmit fail");
4224 }
4225 }
4226
4227 return is_different;
4228}
4229
4230int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4231 char check_current_code[10];
4232 const char * support_country[] = {"CN", "EU"};
4233
4234 int ret,is_different, i, cc_count;
4235
4236 if (country_code == NULL || country_code[0] == '\0')
4237 {
4238 RLOGE("bad country code\n");
4239 return -1;
4240 }
4241
4242 cc_count = sizeof (support_country) / sizeof (char*);
4243 for(i=0; i < cc_count; i++)
4244 {
4245 if (strcmp(support_country[i], country_code) == 0)
4246 {
4247 break;
4248 }
4249 }
4250
4251 if (i >= cc_count)
4252 {
4253 RLOGE("unspported country code %s\n", country_code);
4254 return -1;
4255 }
4256
4257 is_different = check_and_init_uci_config(country_code);
4258 if( is_different < 0 )
4259 {
4260 RLOGE("init set uci fail\n");
4261 return -1;
4262 }
4263
4264 ret = lynq_get_country_code(idx,check_current_code);
4265 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4266 {
4267 ret = lynq_wifi_disable();
4268 if(ret != 0 )
4269 {
4270 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4271 return -1;
4272 }
4273 }
4274
4275 return 0;
you.chen35020192022-05-06 11:30:57 +08004276}
4277
4278int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4279{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004280 RLOGD("enter lynq_get_connect_ap_mac\n");
4281 if (mac == NULL)
4282 {
4283 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004284 return -1;
4285 }
4286
4287 CHECK_IDX(idx, CTRL_STA);
4288 ap_info_s ap;
4289 ap.ap_mac[0] = '\0';
4290
qs.xiong9fbf74e2023-03-28 13:38:22 +08004291 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4292 {
you.chen35020192022-05-06 11:30:57 +08004293 return -1;
4294 }
4295 strcpy(mac, ap.ap_mac);
4296
4297 return 0;
4298}
4299
4300int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4301{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004302 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004303 struct ifaddrs *ifaddr_header, *ifaddr;
4304 struct in_addr * ifa;
4305 const char * ifaName = "wlan0";
4306 if (ip == NULL)
4307 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004308 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004309 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004310 }
you.chenf58b3c92022-06-21 16:53:48 +08004311
qs.xiong9fbf74e2023-03-28 13:38:22 +08004312 if (idx == 1)
4313 {
you.chen0df3e7e2023-05-10 15:56:26 +08004314 ifaName = inner_get_ap_interface_name();
4315 if (ifaName == NULL)
4316 {
4317 RLOGE("[lynq_get_interface_ip] ap name get fail");
4318 return -1;
4319 }
you.chen9ac66392022-08-06 17:01:16 +08004320 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004321 else if (idx != 0)
4322 {
you.chen35020192022-05-06 11:30:57 +08004323 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004324 }
you.chen35020192022-05-06 11:30:57 +08004325
you.chen9ac66392022-08-06 17:01:16 +08004326 if (getifaddrs(&ifaddr_header) == -1)
4327 {
you.chen35020192022-05-06 11:30:57 +08004328 perror("getifaddrs");
4329 return -1;
4330 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004331 }
you.chen35020192022-05-06 11:30:57 +08004332
4333
you.chen9ac66392022-08-06 17:01:16 +08004334 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4335 {
4336 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004337 continue;
you.chen9ac66392022-08-06 17:01:16 +08004338 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4339 {
4340 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4341 {
4342 // is a valid IP4 Address
4343 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4344 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004345 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004346 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004347 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004348 return 0;
4349 }
4350 }
4351 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004352 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004353 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004354 return -1;
you.chen35020192022-05-06 11:30:57 +08004355}
4356
4357int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4358{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004359 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004360 int count;
4361 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004362 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004363 char *split_words[128] = {0};
4364 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4365
4366 CHECK_WPA_CTRL(idx);
4367
4368 DO_REQUEST(lynq_get_mac_cmd);
4369
qs.xiong9fbf74e2023-03-28 13:38:22 +08004370 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4371 {
4372 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004373 return -1;
4374 }
4375
4376 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4377
qs.xiong9fbf74e2023-03-28 13:38:22 +08004378 if (count < 2)
4379 {
you.chen35020192022-05-06 11:30:57 +08004380 return -1;
4381 }
4382
qs.xiong9fbf74e2023-03-28 13:38:22 +08004383 for (i=0; i < strlen(split_words[1]); i++ )
4384 {
4385 if (split_words[1][i] != ' ')
4386 {
you.chen35020192022-05-06 11:30:57 +08004387 break;
4388 }
4389 }
4390
qs.xiongdd6e44c2023-08-08 15:02:53 +08004391 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004392
4393 return 0;
4394}
4395
4396int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4397{
4398// int count;
4399// char *split_words[128] = {0};
4400// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4401
4402// if (rssi == NULL) {
4403// return -1;
4404// }
4405
4406// CHECK_IDX(idx, CTRL_STA);
4407
4408// CHECK_WPA_CTRL(CTRL_STA);
4409
4410// DO_REQUEST(lynq_get_rssi_cmd);
4411
4412// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4413// return -1;
4414// }
4415
4416// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4417
4418// if (count < 2) {
4419// return -1;
4420// }
4421
4422// *rssi = atoi(split_words[1]) * -1;
4423
you.chen35020192022-05-06 11:30:57 +08004424 char lynq_cmd_ret[MAX_RET]={0};
4425
qs.xiongff0ae0f2022-10-11 15:47:14 +08004426/*******change other cmd to get rssi*******
4427 *
4428 *wl rssi ---> wl -i wlan0 rssi
4429 *
4430 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004431 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004432 {
you.chen23c4a5f2023-04-12 16:46:00 +08004433 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004434 return -1;
4435 }
you.chen9f17e4d2022-06-06 17:18:18 +08004436 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004437/****** if got rssi is 0,means sta didn't connected any device****/
4438 if(*rssi == 0)
4439 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004440 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004441 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004442 }
you.chen35020192022-05-06 11:30:57 +08004443
4444 return 0;
4445}
4446
4447int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4448{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004449 RLOGD("enter lynq_get_connect_ap_band\n");
4450 if (band == NULL)
4451 {
you.chen35020192022-05-06 11:30:57 +08004452 return -1;
4453 }
4454
4455 CHECK_IDX(idx, CTRL_STA);
4456 ap_info_s ap;
4457 ap.band = -1;
4458
qs.xiong9fbf74e2023-03-28 13:38:22 +08004459 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4460 {
you.chen35020192022-05-06 11:30:57 +08004461 return -1;
4462 }
4463 *band = ap.band;
4464
4465 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004466}
you.chenf58b3c92022-06-21 16:53:48 +08004467
4468int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4469{
you.chenb95401e2023-05-12 19:39:06 +08004470 int ret;
4471 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004472 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004473
4474 if (ip == NULL)
4475 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004476 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004477 return -1;
4478 }
4479
4480 CHECK_IDX(idx, CTRL_STA);
4481
qs.xionge4cbf1c2023-02-28 18:22:49 +08004482 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004483 {
4484 return -1;
4485 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004486
you.chenb95401e2023-05-12 19:39:06 +08004487 ip[0] = '\0';
4488 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4489 if (ret != 0)
4490 {
4491 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4492 return -1;
4493 }
4494
4495 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4496 {
4497 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004498 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004499 if (ret != 0)
4500 {
4501 ip[0] = '\0';
4502 return 0;
4503 }
4504 else
4505 {
4506 p = strchr(ip, '\n');
4507 if (p != NULL)
4508 {
4509 *p = '\0';
4510 }
4511 }
4512 }
4513 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004514}
4515
qs.xiong026c5c72022-10-17 11:15:45 +08004516int lynq_ap_connect_num(int sta_number)
4517{
4518 char lynq_limit_cmd[32]={0};
4519 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004520 if((sta_number < 1 ) && (sta_number > 15))
4521 {
4522 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004523 return -1;
4524 }
4525 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4526 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004527 if(ret != 0)
4528 {
4529 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004530 }
4531 return 0;
4532}
you.chenf58b3c92022-06-21 16:53:48 +08004533
qs.xiong77905552022-10-17 11:19:57 +08004534int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4535{
4536
4537 char lynq_wifi_acs_cmd[128]={0};
4538 char lynq_cmd_mode[128]={0};
4539 char lynq_cmd_slect[128]={0};
4540
qs.xiong9fbf74e2023-03-28 13:38:22 +08004541 if((acs_mode != 2) && (acs_mode != 5))
4542 {
qs.xiong77905552022-10-17 11:19:57 +08004543 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4544 }
4545
qs.xiong9fbf74e2023-03-28 13:38:22 +08004546 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4547 {
qs.xiong77905552022-10-17 11:19:57 +08004548 return -1;
4549 }
4550
4551 CHECK_IDX(idx, CTRL_AP);
4552
4553 CHECK_WPA_CTRL(CTRL_AP);
4554
4555 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4556 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4557 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4558
4559 DO_OK_FAIL_REQUEST(cmd_disconnect);
4560 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4561 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4562 DO_OK_FAIL_REQUEST(cmd_save_config);
4563 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4564
4565 return 0;
4566}
you.chen0f5c6432022-11-07 18:31:14 +08004567//you.chen add for tv-box start
4568static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4569 FILE *fp;
4570 //printf("to exec cmd:%s\n", str_cmd);
4571 if((fp=popen(str_cmd,"r"))==NULL)
4572 {
4573 perror("popen error!");
4574 return -1;
4575 }
4576 if((fread(str_cmd_ret,max_len,1,fp))<0)
4577 {
4578 perror("fread fail!");
4579 fclose(fp);
4580 return -1;
4581 }
4582 fclose(fp);
4583 return 0;
4584}
4585
4586static int get_netmask_length(const char* mask)
4587{
4588 int masklen=0, i=0;
4589 int netmask=0;
4590
4591 if(mask == NULL)
4592 {
4593 return 0;
4594 }
4595
4596 struct in_addr ip_addr;
4597 if( inet_aton(mask, &ip_addr) )
4598 {
4599 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004600 }else
4601 {
you.chen0f5c6432022-11-07 18:31:14 +08004602 netmask = 0;
4603 return 0;
4604 }
4605
4606 while(0 == (netmask & 0x01) && i<32)
4607 {
4608 i++;
4609 netmask = netmask>>1;
4610 }
4611 masklen = 32-i;
4612 return masklen;
4613}
4614
4615static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4616 int mask_len;
4617 char *p;
4618 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004619 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4620 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004621 return -1;
4622 p = strstr(str_cmd_ret, "Mask:");
4623 if (p == NULL)
4624 return -1;
4625 mask_len = get_netmask_length(p + 5);
4626 if (mask_len == 0)
4627 return -1;
4628 p = strstr(str_cmd_ret, "inet addr:");
4629 if (p == NULL)
4630 return -1;
4631 strcpy(tmp, p + 10);
4632 p = strstr(tmp, " ");
4633 if (p != NULL)
4634 *p = '\0';
4635 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4636 return 0;
4637}
4638
4639static void GBWWatchThreadProc() {
4640 int i,n, nloop, nmax, ncheckcount, nidlecount;
4641 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4642 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4643 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4644 char *results[16] = {0};
4645 char str_cmd[256] = {0};
4646 char str_cmd_ret[128] = {0};
4647 char dest_ip[32] = {0};
4648 lastAP1Bytes = lastAP2Bytes = 0;
4649 lastAP1Drop = lastAP2Drop = 0;
4650 lastAP1Speed = lastAP2Speed = 0;
4651 setAP1Speed = 50;
4652 setAP2Speed = 80;
4653 nloop = 0;
4654 nmax = 6;
4655 ncheckcount = nidlecount = 0;
4656
you.chen0df3e7e2023-05-10 15:56:26 +08004657 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004658 {
4659 RLOGE("------gbw thread run\n");
4660 return;
4661 }
4662
qs.xiong9fbf74e2023-03-28 13:38:22 +08004663 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004664 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4665 while (dest_ip[0] == '\0') {
4666 sleep(1);
4667 str_cmd_ret[0] = '\0';
4668 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4669 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4670 if (str_cmd_ret[n] == '\n'){
4671 str_cmd_ret[n] = '\0';
4672 break;
4673 }
4674 }
4675 if (str_cmd_ret[0] != '\0')
4676 {
4677 strcpy(dest_ip, str_cmd_ret);
4678 }
4679 }
4680
you.chenc9928582023-04-24 15:39:37 +08004681 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4682 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4683 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 +08004684 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4685 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004686 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004687 return;
4688 }
you.chenc9928582023-04-24 15:39:37 +08004689 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);
4690 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);
4691 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 +08004692
4693 while (1) {
4694 sleep(1);
4695 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004696 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4697 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4698 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004699 continue;
4700 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004701 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004702 if (n > 9) {
4703 if (strcmp(results[1], "Sent") == 0) {
4704 currAP1Bytes = atoll(results[2]);
4705 }
4706 if (strcmp(results[6], "(dropped") == 0) {
4707 currAP1Drop = atoi(results[7]);
4708 }
4709 }
4710
4711 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004712 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4713 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4714 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004715 continue;
4716 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004717 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004718 if (n > 9) {
4719 if (strcmp(results[1], "Sent") == 0) {
4720 currAP2Bytes = atoll(results[2]);
4721 }
4722 if (strcmp(results[6], "(dropped") == 0) {
4723 currAP2Drop = atoi(results[7]);
4724 }
4725 }
4726
4727 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4728 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4729 lastAP1Bytes = currAP1Bytes;
4730 lastAP2Bytes = currAP2Bytes;
4731 continue;
4732 }
4733
4734 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4735 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4736 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4737 lastAP1Speed = currAP1Speed;
4738 lastAP2Speed = currAP2Speed;
4739 lastAP1Bytes = currAP1Bytes;
4740 lastAP2Bytes = currAP2Bytes;
4741
4742 currSetAP1Speed = setAP1Speed;
4743 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4744 ncheckcount++;
4745 if (ncheckcount > 3) {
4746 ncheckcount = 0;
4747 currSetAP1Speed = 5;
4748 }
4749 }
4750 else {
4751 ncheckcount = 0;
4752 if (currAP1Speed < 5)
4753 nidlecount++;
4754 else
4755 nidlecount = 0;
4756
4757 }
4758
4759 if (nidlecount > 60 ){
4760 currSetAP1Speed = 50;
4761 }
4762
4763 if (currSetAP1Speed != setAP1Speed) {
4764 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004765 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4766 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004767 }
4768 }
4769}
4770
4771int enableGBW(const char* mac) {
4772 int i,len;
4773 char get_ipaddr_cmd[128]={0};
4774 ap_info_s *ap;
4775 device_info_s * list;
4776
4777 if (mac == NULL || g_gbw_enabled == 1)
4778 return -1;
4779 len = strlen(mac);
4780 g_gbw_mac = malloc(len + 1);
4781 for(i=0;i<len;i++) {
4782 if (mac[i] >= 'A' && mac[i] <= 'Z')
4783 {
4784 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4785 }
4786 else
4787 g_gbw_mac[i] = mac[i];
4788 }
4789 g_gbw_mac[i] = '\0';
4790 g_gbw_enabled = 1;
4791
4792 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4793 if (system(get_ipaddr_cmd) == 0) {
4794 //startGBW();
4795 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4796 for (i=0;i<len;i++) {
4797 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4798 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4799 startGBW();
4800 }
4801 free(ap);
4802 free(list);
4803 }
4804 }
4805 return 0;
4806}
4807
4808int disableGBW() {
4809 stopGBW();
4810 free(g_gbw_mac);
4811 g_gbw_mac = NULL;
4812 g_gbw_enabled = 1;
4813 return 0;
4814}
4815
4816static int startGBW() {
4817 if (g_gbw_watcher_pid != 0) {
4818 stopGBW();
4819 }
4820 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4821}
4822
4823static int stopGBW() {
4824 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004825 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004826 pthread_cancel(g_gbw_watcher_pid);
4827 pthread_join(g_gbw_watcher_pid, &retval);
4828 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004829 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4830 if (s_ap_iterface_name[0] != '\0')
4831 {
4832 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4833 system(cmd);
4834 }
you.chen0f5c6432022-11-07 18:31:14 +08004835}
4836//you.chen add for tv-box end