blob: 3657a6f2f14efdbef6e26c1e6febe048afdd87c5 [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;
you.chen35020192022-05-06 11:30:57 +080057
qs.xiongfcc914b2023-07-06 21:16:20 +080058pthread_t g_sta_auto_watcher_pid = 0;
59volatile int g_sta_auto_watcher_stop_flag = 0;
60volatile int g_sta_auto_scan_finish_flag = 1;
61volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080062void * g_ap_callback_priv = NULL;
63AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
64void * g_sta_callback_priv = NULL;
65STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080066void * g_sta_auto_callback_priv = NULL;
67STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080068
69//const char * CTRL_PATH="/var/run/wpa_supplicant";
70const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
71//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
72const char * cmd_list_networks = "LIST_NETWORKS";
73const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080074const char * cmd_disconnect = "DISCONNECT";
75const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080076const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080077const char * STATE_COMPLETED = "COMPLETED";
you.chen6d247052023-06-01 16:39:54 +080078const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080079
you.chenf711c8a2023-04-13 13:49:45 +080080const char * cmd_ping = "PING";
81const char * rsp_pong = "PONG";
82const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
83const int MAX_IDLE_COUNT = 600; // 60s
84
you.chenc9928582023-04-24 15:39:37 +080085const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
86const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
87const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
88const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
89const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
90
91static char s_ap_iterface_name[64] = {0};
92
you.chend2fef3f2023-02-13 10:50:35 +080093struct local_wpa_ctrl{
94 struct wpa_ctrl *ctrl;
95 pthread_mutex_t mutex;
96};
97
you.chen70f377f2023-04-14 18:17:09 +080098
you.chend2fef3f2023-02-13 10:50:35 +080099static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +0800100static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
101static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +0800102// add for auto connect
103static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800104
105static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800106
you.chen0f5c6432022-11-07 18:31:14 +0800107//you.chen add for tv-box start
108volatile int g_gbw_enabled = 0;
109char * g_gbw_mac = NULL;
110pthread_t g_gbw_watcher_pid = 0;
111static int startGBW();
112static int stopGBW();
113//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800114
115typedef struct __curr_status_info {
116 ap_info_s *ap;
117 char * state;
118 int net_no;
119}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400120
you.chen70f377f2023-04-14 18:17:09 +0800121typedef enum {
122 INNER_STA_STATUS_INIT = 0,
123 INNER_STA_STATUS_CONNECTING,
124 INNER_STA_STATUS_ASSOCIATING,
125 INNER_STA_STATUS_ASSOCIATED,
126 INNER_STA_STATUS_CONNECTED,
127 INNER_STA_STATUS_DISCONNECTING,
128 INNER_STA_STATUS_DISCONNECTED,
129 INNER_STA_STATUS_CANCEL,
130}inner_sta_status_s;
131
132static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
133static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
134static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
135static error_number_s s_sta_error_number = -1;
136static char s_sta_current_connecting_ssid[64] = {0};
137static struct timespec s_sta_connect_timeout;
138const int MAX_CONNNECT_TIME = 15; // second
139pthread_t g_global_watcher_pid = 0;
140static int s_service_invoke_timeout_cnt=0;
141const int FAKE_MAX_INT_VALUE = 99999;
142
143static void notify_service_invoke_fail(int error)
144{
145 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
146 pthread_mutex_lock(&s_global_check_mutex);
147 if (error == -2) //timeout
148 {
149 s_service_invoke_timeout_cnt++;
150 if (s_service_invoke_timeout_cnt > 10)
151 {
152 pthread_cond_signal(&s_global_check_cond);
153 }
154 }
155 else if (error == -1)
156 {
157 // check if can connect wpa service
158 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
159 if (lynq_wpa_ctrl == NULL)
160 {
161 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
162 pthread_cond_signal(&s_global_check_cond);
163 }
164 wpa_ctrl_close(lynq_wpa_ctrl);
165 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
166 if (lynq_wpa_ctrl == NULL)
167 {
168 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
169 pthread_cond_signal(&s_global_check_cond);
170 }
171 wpa_ctrl_close(lynq_wpa_ctrl);
172 }
173
174 pthread_mutex_unlock(&s_global_check_mutex);
175}
176
you.chenc9928582023-04-24 15:39:37 +0800177static int system_call_v(const char * fmt, ...)
178{
179 char str_cmd[256] = {0};
180 va_list args;
181 va_start(args, fmt);
182 vsprintf(str_cmd, fmt, args);
183 va_end(args);
184 printf("system call----------%s\n", str_cmd);
185 return system(str_cmd);
186}
187
you.chen0df3e7e2023-05-10 15:56:26 +0800188static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
189
190static const char * inner_get_ap_interface_name()
191{
192 char * p;
193 char cmd[128]={0};
194
195 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
196 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
197 {
198 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
199 return NULL;
200 }
201 p = strchr(s_ap_iterface_name, ' ');
202 if (NULL != p)
203 {
204 *p = '\0';
205 }
206 p = strchr(s_ap_iterface_name, '\n');
207 if (NULL != p)
208 {
209 *p = '\0';
210 }
211 if (s_ap_iterface_name[0] == '\0')
212 {
213 return NULL;
214 }
215
216 return s_ap_iterface_name;
217}
218
you.chen70f377f2023-04-14 18:17:09 +0800219static void check_tether_and_notify()
220{
221 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800222 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 +0800223 {
224 return;
225 }
226 pthread_mutex_lock(&s_global_check_mutex);
227 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
228 pthread_cond_signal(&s_global_check_cond);
229 pthread_mutex_unlock(&s_global_check_mutex);
230}
231
you.chend2fef3f2023-02-13 10:50:35 +0800232static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
233 char *reply, size_t *reply_len,
234 void (*msg_cb)(char *msg, size_t len))
235{
236 int ret;
237 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800238 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800239 return -1;
240 }
241 pthread_mutex_lock(&ctrl->mutex);
242 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
243 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800244 if (ret != 0)
245 {
246 notify_service_invoke_fail(ret);
247 }
you.chend2fef3f2023-02-13 10:50:35 +0800248 return ret;
249}
250
251static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
252 int repeat_cnt;
253 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
254 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800255 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800256 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
257 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
258// printf("wait enable finish\n");
259 usleep(500 * 1000);
260 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
261 }
262 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800263 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800264 goto out_addr;
265 }
266 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
267 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
268 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800269 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800270 goto out_addr;
271 }
272 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
273 }
274 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
275out_addr:
276 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
277 return lynq_wpa_ctrl;
278}
279
qs.xiong97fa59b2022-04-07 05:41:29 -0400280#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400281{\
you.chen35020192022-05-06 11:30:57 +0800282 perror((str));\
283 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400284}
285
you.chen35020192022-05-06 11:30:57 +0800286#define CHECK_IDX(idx, type) do { \
287 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
288 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800289 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800290 return -1; \
291 } \
292 }while (0)
293
294#define CHECK_WPA_CTRL(index) int ret = 0;\
295 size_t reply_len = MAX_RET; \
296 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800297 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800298 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800299 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
300 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800301 }while(0)
302
303#define DO_REQUEST(cmd_str) do { \
304 reply_len = MAX_RET;\
305 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800306 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800307 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 +0800308 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800310 return ret; \
311 } \
312 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800313 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800314 }while(0)
315
316#define DO_OK_FAIL_REQUEST(cmd_str) do { \
317 DO_REQUEST(cmd_str); \
318 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800319 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800320 return -1; \
321 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800322 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800323 return -1; \
324 } \
325 }while (0)
326
327
you.chenf711c8a2023-04-13 13:49:45 +0800328static int check_connection(struct wpa_ctrl * wpa_ctrl)
329{
330 size_t reply_len = MAX_RET;
331 char cmd_reply[MAX_RET]={0};
332 int ret;
333
334 RLOGD("check_connection [%p]", wpa_ctrl);
335 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
336
337 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
338 {
339 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 +0800340 if (ret != 0)
341 {
342 notify_service_invoke_fail(ret);
343 }
you.chenf711c8a2023-04-13 13:49:45 +0800344 return -1;
345 }
346
347 return 0;
348}
349
350/**
351 * @brief check_pending_msg
352 * @param lynq_wpa_ctrl
353 * @return 1 has msg, 0 no msg, -1 error
354 */
355static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
356{
357 int ret;
358
359 if (*pp_lynq_wpa_ctrl == NULL) // need connect
360 {
361 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
362 if (*pp_lynq_wpa_ctrl == NULL)
363 {
364 usleep(SLEEP_TIME_ON_IDLE);
365 return -1;
366 }
367
368 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
369 if (ret == 0) // attach success
370 {
371 *started_flag = 1;
372 }
373 else
374 {
you.chen70f377f2023-04-14 18:17:09 +0800375 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800376 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
377 *pp_lynq_wpa_ctrl = NULL;
378 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800379 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800380 usleep(SLEEP_TIME_ON_IDLE);
381 return -1;
382 }
383 }
384
385 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
386 if ( ret == 0) // no pending messages
387 {
388 usleep(SLEEP_TIME_ON_IDLE);
389 *idle_count += 1;
390 if (*idle_count > MAX_IDLE_COUNT)
391 {
392 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
393 {
394 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
395 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
396 *pp_lynq_wpa_ctrl = NULL;
397 *idle_count = 0;
398 return -1;
399 }
400 *idle_count = 0;
401 }
402 return 0;
403 }
404 else if ( ret == -1) // on error
405 {
406 RLOGE("[wifi error]sta wpa_ctrl_pending");
407 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
408 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
409 *pp_lynq_wpa_ctrl = NULL;
410 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800411 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800412 return -1;
413 }
414
415 *idle_count = 0;
416 return 1;
417}
418
you.chen6d247052023-06-01 16:39:54 +0800419static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
420{
421 pthread_mutex_lock(&s_ap_callback_mutex);
422 if (g_ap_callback_func != NULL)
423 g_ap_callback_func(g_ap_callback_priv, status);
424 pthread_mutex_unlock(&s_ap_callback_mutex);
425}
426
you.chen35020192022-05-06 11:30:57 +0800427static void APWatcherThreadProc() {
428 size_t len = MAX_RET;
429 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800430 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800431
you.chen6c2dd9c2022-05-16 17:55:28 +0800432 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800433 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800434
qs.xiong9fbf74e2023-03-28 13:38:22 +0800435 while (g_ap_watcher_stop_flag == 0)
436 {
you.chenf711c8a2023-04-13 13:49:45 +0800437 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
438 {
you.chen70f377f2023-04-14 18:17:09 +0800439 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
440 {
441 check_tether_and_notify();
442 }
443
you.chen35020192022-05-06 11:30:57 +0800444 continue;
445 }
you.chenf711c8a2023-04-13 13:49:45 +0800446
you.chen6c2dd9c2022-05-16 17:55:28 +0800447 memset(msg_notify, 0, MAX_RET);
448 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800449 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800450 {
you.chen35020192022-05-06 11:30:57 +0800451 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800452 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800453 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800454 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800455 {
you.chen6d247052023-06-01 16:39:54 +0800456 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800457 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800458 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800459 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
460 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800461 {
you.chen0f5c6432022-11-07 18:31:14 +0800462 stopGBW();
463 }
464 }
you.chen35020192022-05-06 11:30:57 +0800465 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800466 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800467 {
you.chen6d247052023-06-01 16:39:54 +0800468 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800469 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800470 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800471 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
472 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800473 {
you.chen0f5c6432022-11-07 18:31:14 +0800474 startGBW();
475 }
476 }
you.chen35020192022-05-06 11:30:57 +0800477 }
qs.xiong31163d62023-07-11 18:54:40 +0800478 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 )
479 {
480 RLOGD("APWatcherThreadProc ap------> service error");
481 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
482 }
483 else
484 {
485 RLOGD("APWatcherThreadProc ap------> going on check next msg");
486 }
you.chenf711c8a2023-04-13 13:49:45 +0800487 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800488 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
489 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800490 if (lynq_wpa_ctrl != NULL)
491 {
you.chen92fd5d32022-05-25 10:09:47 +0800492 wpa_ctrl_detach(lynq_wpa_ctrl);
493 wpa_ctrl_close(lynq_wpa_ctrl);
494 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400495}
496
you.chen70f377f2023-04-14 18:17:09 +0800497static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
498{
499 char * p;
500 const char * try_associat_flag = "Trying to associate";
501 const char * associated_flag = "Associated with ";
502
503 pthread_mutex_lock(&s_global_check_mutex);
504 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
505 {
506 pthread_mutex_unlock(&s_global_check_mutex);
507 return;
508 }
509
510 if (state == LYNQ_WIFI_STATUS_EGNORE)
511 {
512 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
513 {
514 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
515 }
516 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
517 {
518 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
519 }
520 }
521 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
522 {
523 s_sta_error_number = error_num;
524 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
525 {
526 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
527 pthread_cond_signal(&s_global_check_cond);
528 }
529 }
530 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
531 {
532 s_sta_status = INNER_STA_STATUS_CONNECTED;
533 pthread_cond_signal(&s_global_check_cond);
534 }
535 pthread_mutex_unlock(&s_global_check_mutex);
536}
537
qs.xiong44fac672023-08-29 16:15:55 +0800538/*
539just tmp add for fix sta connect ap fail check ap connect info
540return 0 --->Current no sta device connect this AP
541*/
542static int lynq_connected_ap_sta_status() {
543
544 FILE *fp;
545 size_t i = 0;
546 int ret;
547 char lynq_cmd_ret[MAX_RET]={0};
548
549 if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
550 {
551 perror("popen error!");
552 return -1;
553 }
554 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
555 {
556 perror("fread fail!");
557 ret=pclose(fp);
558 if(ret == -1)
559 perror("close file faild");
560 return -1;
561 }
562 if( strlen(lynq_cmd_ret) < MAC_LEN)
563 {
564 RLOGD("---Current no sta device connect this AP %s %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
565 ret=pclose(fp);
566 if(ret==-1)
567 {
568 perror("close file faild");
569 }
570 return 0;
571 }else{
572 ret=pclose(fp);
573 if(ret==-1)
574 {
575 perror("close file faild");
576 }
577 RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
578 return 1;
579 }
580}
581
582/*
583 just tmp add for fix sta connect ap fail; check fw status
584 return 1 ----> fw status error; need wl down/up
585*/
586static int check_current_fw_status() {
587
588 FILE *fp;
589 FILE *fp1;
590 size_t i = 0;
591 int ret;
592 char lynq_cmd_ret_2g[MAX_RET]={0};
593 char lynq_cmd_ret_5g[MAX_RET]={0};
594
595 const char * fw_status = "0x0096"; //0x0096 is normal fw status
596
597 if((fp=popen("wl shmem 0x15ee a","r"))==NULL)
598 {
599 perror("popen error!");
600 return -1;
601 }
602 if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
603 {
604 perror("fread fail!");
605 if(pclose(fp) == -1)
606 perror("close fp file faild");
607 return -1;
608 }
609
610 if((fp1=popen("wl shmem 0x15ee b","r"))==NULL)
611 {
612 perror("popen error!");
613 if(pclose(fp) == -1)
614 perror("clsoe fp file faild");
615 return -1;
616 }
617 if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
618 {
619 perror("fread fail!");
620 if(pclose(fp1) == -1)
621 perror("clsoe fp1 file faild");
622 if(pclose(fp) == -1)
623 perror("clsoe fp file faild");
624 return -1;
625 }
626
627 if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
628 {
629 ret=pclose(fp);
630 if(ret==-1)
631 {
632 perror("close fp file faild");
633 }
634 ret=pclose(fp1);
635 if(ret==-1)
636 {
637 perror("close fp1 file faild");
638 }
639 return 0;
640 }else
641 {
642 ret=pclose(fp);
643 if(ret==-1)
644 {
645 perror("close file faild");
646 }
647 if(pclose(fp1) == -1)
648 {
649 perror("clsoe file fp1 faild");
650 }
651 RLOGD("current fw status --error--");
652 return 1;
653 }
654}
655
qs.xiong1d4263a2023-09-06 10:46:23 +0800656/*
657eg: wl counters info
658sh-3.2# wl counters
659counters_version 30
660datalen 1648
661Slice_index: 0
662reinitreason_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)
663reinit 0 reset 2 pciereset 0 cfgrestore 0 dma_hang 0 ampdu_wds 0
664
665check reinit status
666return 0 ===> fw did wl reinit cmd
667*/
668static int check_current_reinit_info()
669{
670 FILE *fp;
671 int ret;
672 char lynq_cmd_ret[MAX_RET]={0};
673 char * dest;
674 char destid[3]={0};
675 if((fp=popen("wl counters","r"))==NULL)
676 {
677 perror("popen error!");
678 return -1;
679 }
680 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
681 {
682 perror("fread fail!");
683 if(pclose(fp) == -1)
684 {
685 perror("close fp file faild");
686 }
687 return -1;
688 }
689 dest = strstr(lynq_cmd_ret,"reinit ");
690 if(dest != NULL)
691 {
692 dest +=strlen("reinit ");
693 RLOGD("current get dest str is %s",dest);
694 memcpy(destid,dest,2);
695 ret = atoi(destid);
696 RLOGD("get current wl counters cmd return counts is %d",ret);
697 if( ret != 0 )
698 {
699 RLOGD("current fw did run cmd wl reinit");
700 if( pclose(fp) == -1 )
701 {
702 perror("close fp file faild");
703 }
704 return 0;
705 }
706 }
707 if( pclose(fp) == -1 )
708 {
709 perror("close fp file faild");
710 }
711 RLOGD("current fw didn't run cmd wl reinit,dest ptr is NULL");
712 return -1;
713}
714
qs.xiong44fac672023-08-29 16:15:55 +0800715static void APTmpWatcherThreadProc() {
716
717 int i = 0;
718 int delytime = 300;
719 g_ap_tmp_watcher_stop_flag = 0;
720
721 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
722 while(1)
723 {
724 sleep(1);
725 i++;
qs.xiong1d4263a2023-09-06 10:46:23 +0800726 if ( (i % 30) == 0 )
727 {
728 if ( check_current_reinit_info() == 0 )
729 {
730 system("wl reset_cnts");
731 system("wl down");
732 system("wl up");
733 RLOGD("APTmpWatcherThreadProc:check fw did reinit cmd,do down/up reset");
734 }
735 }
qs.xiong44fac672023-08-29 16:15:55 +0800736 if ( i == delytime )
737 {
738 if( lynq_connected_ap_sta_status() == 0 ) //o --->no sta device connect this ap
739 {
740 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
741 {
742 system("wl down");
743 system("wl up");
744 }
745
746 }
747 i = 0;
748 }
749 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
750 {
751 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
752 return;
753 }
754 }
755
756}
757
qs.xiongf0128b12023-06-29 17:29:39 +0800758static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
759{
760 char LYNQ_DISABLE_CMD[128]={0};
761
762 CHECK_IDX(idx, CTRL_STA);
763 CHECK_WPA_CTRL(CTRL_STA);
764
765 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
766 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
767 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800768 DO_OK_FAIL_REQUEST(cmd_save_config);
769
qs.xiongf0128b12023-06-29 17:29:39 +0800770 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800771
qs.xiongf0128b12023-06-29 17:29:39 +0800772}
773
774
qs.xiong455c30b2023-04-12 11:40:02 +0800775void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
776{
777 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800778 char *wpanetid;
779 char destid[3] = {0};
780 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800781 *error = LYNQ_WAIT_CONNECT_ACTIVE;
782 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
783 {
784 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
785 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
786 return;
787 }
788
789 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
790 {
791 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
792 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
793 return;
794 }
795
796 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
797 {
qs.xiongf0128b12023-06-29 17:29:39 +0800798 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
799 wpanetid = strstr(modify,"id=");
800 if ( wpanetid != NULL )
801 {
802 wpanetid +=strlen("id=");
803 memcpy(destid,wpanetid,2);
804 tmpdisid = atoi(destid);
805
806 }
qs.xiong455c30b2023-04-12 11:40:02 +0800807 pReason = strstr(modify, "reason=");
808 if (pReason != NULL)
809 {
810 pReason += strlen("reason=");
811 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
812 {
813 *error = LYNQ_TIME_OUT;
814 }
815 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
816 {
817 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800818 // tmp fix sta autoconnect connect and disconnect
819 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
820 {
821 RLOGE("stop wlan0 network %d fail",tmpdisid);
822 }
qs.xiong455c30b2023-04-12 11:40:02 +0800823 }
824 else
825 {
826 *error = LYNQ_UNSPECIFIED_REASON;
827 }
qs.xiong455c30b2023-04-12 11:40:02 +0800828 }
829 else
830 {
831 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800832 }
qs.xiongf0128b12023-06-29 17:29:39 +0800833 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
834 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800835
836 }
837
838 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
839 {
840 *error = LYNQ_NOT_FIND_AP;
841 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
842 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
843 return;
844 }
845
846
847 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
848 {
849 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
850 pReason = strstr(modify, "status_code=");
851 if (pReason != NULL)
852 {
853 pReason += strlen("status_code=");
854 if (memcmp(pReason, "17", 2) == 0)
855 {
856 *error = LYNQ_AP_UNABLE_HANDLE;
857 }
858 else if (memcmp(pReason, "1",1) == 0)
859 {
860 *error = LYNQ_UNSPECIFIED_REASON;
861 }
862 else
863 {
864 *error = LYNQ_UNSPECIFIED_REASON;
865 }
866
867 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
868 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
869 return;
870 }
871 else
872 {
873 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
874 *error = LYNQ_UNSPECIFIED_REASON;
875 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
876 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
877 return;
878 }
879 }
880
881 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
882 {
883 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
884 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
885 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
886 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
887 return;
888 }
889
890 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
891 {
892 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
893 *error = LYNQ_WAIT_CONNECT_ACTIVE;
894 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
895 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
896 return;
897 }
898
you.chen32cb31e2023-04-13 14:05:45 +0800899 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800900 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800901 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800902 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
903 return;
904
905}
906
qs.xiongfcc914b2023-07-06 21:16:20 +0800907void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
908{
909 char *pReason;
910 char *wpanetid;
911 char destid[3];
912 *error = LYNQ_WAIT_CONNECT_ACTIVE;
913 *networkid = -1;
914 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
915 {
916 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
917 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
918 return;
919 }
920 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
921 {
922 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
923 wpanetid = strstr(modify,"id=");
924 if ( wpanetid != NULL )
925 {
926 wpanetid +=strlen("id=");
927 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
928 if (memcpy(destid,wpanetid,2) != NULL)
929 {
930 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
931 *networkid = atoi(destid);
932 RLOGD("get networkid is %d\n",*networkid);
933 }
934 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
935 }
936 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
937 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
938 return;
939 }
940 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
941 {
942 wpanetid = strstr(modify,"id=");
943 if ( wpanetid != NULL )
944 {
945 wpanetid +=strlen("id=");
946 if (memcpy(destid,wpanetid,2) != NULL)
947 {
948 *networkid = atoi(destid);
949 RLOGD("get networkid is %d\n",*networkid);
950 }
951 }
952 pReason = strstr(modify, "reason=");
953 if (pReason != NULL)
954 {
955 pReason += strlen("reason=");
956 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
957 {
958 *error = LYNQ_TIME_OUT;
959 }
960 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
961 {
962 *error = LYNQ_PSW_ERROR;
963 }
964 else
965 {
966 *error = LYNQ_UNSPECIFIED_REASON;
967 }
968 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
969 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
970 return;
971 }
972 else
973 {
974 *error = LYNQ_UNSPECIFIED_REASON;
975 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
976 return;
977 }
978 }
979 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
980 {
981 wpanetid = strstr(modify,"id=");
982 if ( wpanetid != NULL )
983 {
984 wpanetid +=strlen("id=");
985 if (memcpy(destid,wpanetid,2) != NULL)
986 {
987 *networkid = atoi(destid);
988 RLOGD("get networkid is %d\n",*networkid);
989 }
990 }
991 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
992 pReason = strstr(modify, "status_code=");
993 if (pReason != NULL)
994 {
995 pReason += strlen("status_code=");
996 if (memcmp(pReason, "17", 2) == 0)
997 {
998 *error = LYNQ_AP_UNABLE_HANDLE;
999 }
1000 else if (memcmp(pReason, "1",1) == 0)
1001 {
1002 *error = LYNQ_UNSPECIFIED_REASON;
1003 }
1004 else
1005 {
1006 *error = LYNQ_UNSPECIFIED_REASON;
1007 }
1008 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1009 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1010 return;
1011 }
1012 else
1013 {
1014 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
1015 *error = LYNQ_UNSPECIFIED_REASON;
1016 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +08001017 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +08001018 return;
1019 }
1020 }
1021 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
1022 {
1023 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
1024 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
1025 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1026 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1027 return;
1028 }
1029 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
1030 {
1031 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
1032 *error = LYNQ_WAIT_CONNECT_ACTIVE;
1033 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
1034 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
1035 return;
1036 }
1037 RLOGD("EVENT : %s\n", modify);
1038 *error = LYNQ_UNSPECIFIED_REASON;
1039 *state = LYNQ_WIFI_STATUS_EGNORE;
1040 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
1041 return;
1042}
you.chen70f377f2023-04-14 18:17:09 +08001043static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
1044{
you.chen6d247052023-06-01 16:39:54 +08001045 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001046 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1047 {
1048 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
1049 g_sta_callback_func(g_sta_callback_priv, state, error);
1050 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
1051 }
you.chen6d247052023-06-01 16:39:54 +08001052 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +08001053}
qs.xiongfcc914b2023-07-06 21:16:20 +08001054static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
1055{
1056 pthread_mutex_lock(&s_sta_callback_mutex);
1057 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
1058 {
1059 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
1060 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
1061 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
1062 }
1063 pthread_mutex_unlock(&s_sta_callback_mutex);
1064}
you.chen70f377f2023-04-14 18:17:09 +08001065
you.chen35020192022-05-06 11:30:57 +08001066static void STAWatcherThreadProc() {
1067 size_t len = MAX_RET;
1068 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001069 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001070 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001071 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001072
you.chen6c2dd9c2022-05-16 17:55:28 +08001073 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001074 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001075
you.chen70f377f2023-04-14 18:17:09 +08001076 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001077 while (g_sta_watcher_stop_flag == 0)
1078 {
you.chenf711c8a2023-04-13 13:49:45 +08001079 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001080 {
you.chen35020192022-05-06 11:30:57 +08001081 continue;
1082 }
you.chenf711c8a2023-04-13 13:49:45 +08001083
you.chen6c2dd9c2022-05-16 17:55:28 +08001084 memset(msg_notify, 0, MAX_RET);
1085 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001086 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001087 {
you.chen35020192022-05-06 11:30:57 +08001088 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001089 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1090 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001091 {
you.chen35020192022-05-06 11:30:57 +08001092 g_sta_scan_finish_flag = 1;
1093 }
1094
qs.xiong9fbf74e2023-03-28 13:38:22 +08001095 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001096 {
you.chen35020192022-05-06 11:30:57 +08001097 continue;
1098 }
qs.xiong455c30b2023-04-12 11:40:02 +08001099 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001100 notify_connect_status(state, error);
1101
1102 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001103 {
you.chen70f377f2023-04-14 18:17:09 +08001104 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001105 if (last_state != state)
1106 {
1107 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1108 {
1109 system_call_v("%s %s", sta_status_change_script, "connect");
1110 }
1111 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1112 {
1113 system_call_v("%s %s", sta_status_change_script, "disconnect");
1114 }
1115 }
1116
1117 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001118 }
you.chen35020192022-05-06 11:30:57 +08001119 }
1120 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001121 if (lynq_wpa_ctrl != NULL)
1122 {
you.chen92fd5d32022-05-25 10:09:47 +08001123 wpa_ctrl_detach(lynq_wpa_ctrl);
1124 wpa_ctrl_close(lynq_wpa_ctrl);
1125 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001126}
qs.xiongfcc914b2023-07-06 21:16:20 +08001127static void STAAutoWatcherThreadProc() {
1128 size_t len = MAX_RET;
1129 char msg_notify[MAX_RET];
1130 error_number_s error;
1131 lynq_wifi_sta_status_s state, last_state = -1;
1132 int idle_count = 0;
1133 int networkid;
1134 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1135 g_sta_auto_watcher_stop_flag = 0;
1136 RLOGD("STAAutoWatcherThreadProc thread started ------");
1137 while (g_sta_auto_watcher_stop_flag == 0)
1138 {
1139 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1140 {
1141 continue;
1142 }
1143 memset(msg_notify, 0, MAX_RET);
1144 len = MAX_RET;
1145 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1146 {
1147 msg_notify[len+1] = '\0';
1148 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1149 if (strstr(msg_notify, state_scan_result) != NULL)
1150 {
1151 g_sta_auto_scan_finish_flag = 1;
1152 }
1153 if (g_sta_auto_callback_func == NULL)
1154 {
1155 continue;
1156 }
1157 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1158 notify_auto_connect_status(state, error,networkid);
1159 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1160 {
1161 inner_check_connect_error(msg_notify, state, error);
1162 if (last_state != state)
1163 {
1164 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1165 {
1166 system_call_v("%s %s", sta_status_change_script, "connect");
1167 }
1168 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1169 {
1170 system_call_v("%s %s", sta_status_change_script, "disconnect");
1171 }
1172 }
1173 last_state = state;
1174 }
1175 }
1176 }
1177 if (lynq_wpa_ctrl != NULL)
1178 {
1179 wpa_ctrl_detach(lynq_wpa_ctrl);
1180 wpa_ctrl_close(lynq_wpa_ctrl);
1181 }
1182}
qs.xiongf1b525b2022-03-31 00:58:23 -04001183
you.chen70f377f2023-04-14 18:17:09 +08001184// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1185static void GlobalWatcherThreadProc()
1186{
1187 int ret, connect_timeout, service_abnormal;
1188 error_number_s error_num = -1;
1189 inner_sta_status_s sta_status;
1190 scan_info_s *scan_list = NULL;
1191 int i, scan_len=0;
1192 char connecting_ssid[64];
1193 struct timeval now;
1194
1195 RLOGD("GlobalWatcherThreadProc start to run");
1196
1197 while (1)
1198 {
1199 pthread_mutex_lock(&s_global_check_mutex);
1200 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1201 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1202 {
1203 pthread_mutex_unlock(&s_global_check_mutex);
1204 usleep(50*1000);
1205 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1206 continue;
1207 }
1208
1209 connect_timeout = 0;
1210 service_abnormal = 0;
1211 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1212 {
1213 while (1)
1214 {
1215 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1216 if (ret == ETIME)
1217 {
1218 connect_timeout = 1;
1219 }
1220 else if (ret != 0)
1221 {
1222 gettimeofday(&now,NULL);
1223 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1224 {
1225 usleep(SLEEP_TIME_ON_IDLE);
1226 continue;
1227 }
1228 connect_timeout = 1;
1229 }
1230 sta_status = s_sta_status;
1231 error_num = s_sta_error_number;
1232 s_sta_status = INNER_STA_STATUS_INIT;
1233 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1234 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1235 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1236 break;
1237 }
1238 }
1239 if (s_service_invoke_timeout_cnt > 10)
1240 {
1241 service_abnormal = 1;
1242 s_service_invoke_timeout_cnt = 0;
1243 }
1244 pthread_mutex_unlock(&s_global_check_mutex);
1245
1246 if (service_abnormal == 1)
1247 {
1248 sleep(1);
1249 RLOGE("wpa service is abnormal info app to exit");
1250 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001251
1252 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1253
you.chen70f377f2023-04-14 18:17:09 +08001254 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1255 }
1256
1257 if (sta_status == INNER_STA_STATUS_CANCEL)
1258 {
1259 continue;
1260 }
1261 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1262 {
1263 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1264 }
1265 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1266 {
1267 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1268 {
1269 for(i=0; i < scan_len;i++)
1270 {
1271 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1272 {
1273 error_num = LYNQ_AUTH_ERROR;
1274 break;
1275 }
1276 }
1277 free(scan_list);
1278 }
1279 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1280 }
1281 else if (connect_timeout == 0)
1282 {
1283 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1284 }
1285 else // wait timeout
1286 {
1287 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1288 {
1289 ; // wpa service abnormal
1290 }
1291 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1292 {
1293 RLOGD("GlobalWatcherThreadProc notify connected");
1294 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1295 }
1296 else
1297 {
1298 RLOGD("GlobalWatcherThreadProc notify timeout");
1299 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1300 }
1301 }
1302 } // while (1)
1303}
1304
qs.xiong1af5daf2022-03-14 09:12:12 -04001305int lynq_wifi_enable(void)
1306{
you.chen35020192022-05-06 11:30:57 +08001307 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001308 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001309 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001310 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1311
qs.xiong9fbf74e2023-03-28 13:38:22 +08001312 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1313 {
you.chend2fef3f2023-02-13 10:50:35 +08001314 goto out_enable;
1315 }
1316
you.chenc9928582023-04-24 15:39:37 +08001317 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001318 if (ret != 0)
1319 {
1320 //printf("service state %d\n", ret);
1321 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001322 ret = -1;
1323 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001324 }
lhfe8da902022-10-11 18:55:36 +08001325
you.chen70f377f2023-04-14 18:17:09 +08001326 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1327 {
1328 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1329 if(ret<0)
1330 {
1331 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1332 ret = -1;
1333 goto out_enable;
1334 }
1335 }
1336
you.chend2fef3f2023-02-13 10:50:35 +08001337 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1338 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1339 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1340 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1341out_enable:
1342 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001343 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001344}
1345
qs.xiong1af5daf2022-03-14 09:12:12 -04001346int lynq_wifi_disable(void)
1347{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001348 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001349 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001350 g_ap_watcher_stop_flag = 1;
1351 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001352 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001353 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001354 if (g_ap_watcher_pid != 0)
1355 pthread_join(g_ap_watcher_pid, NULL);
1356 if (g_sta_watcher_pid != 0)
1357 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001358 if (g_sta_auto_watcher_pid != 0)
1359 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001360 if (g_lynq_wpa_ctrl[0] != NULL)
1361 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1362 if (g_lynq_wpa_ctrl[1] != NULL)
1363 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001364 if (g_ap_tmp_watcher_pid != 0)
1365 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001366 g_ap_watcher_pid = 0;
1367 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001368 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001369 g_lynq_wpa_ctrl[0] = NULL;
1370 g_lynq_wpa_ctrl[1] = NULL;
1371 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001372 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1373 return 0;
1374}
1375
1376static inline char inner_convert_char(char in)
1377{
1378 if (in >= '0' && in <= '9')
1379 {
1380 return in - '0';
1381 }
1382 else if (in >= 'a' && in <= 'f')
1383 {
1384 return in - 'a' + 10;
1385 }
1386 else if (in >= 'A' && in <= 'F')
1387 {
1388 return in - 'A' + 10;
1389 }
1390 else
1391 {
1392 return '\xff';
1393 }
1394}
1395
1396static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1397{
1398 char *p;
1399 size_t pos = 0;
1400 if (NULL == out_ssid)
1401 return;
1402 //printf("input ssid=[%s]\n", ssid);
1403 memset(out_ssid, 0, out_ssid_len);
1404 if (NULL == ssid)
1405 return;
1406 p = strchr(ssid, '\\');
1407 if (NULL == p)
1408 {
1409 strncpy(out_ssid, ssid, out_ssid_len);
1410 //printf(" first %s\n", out_ssid);
1411 }
1412 else
1413 {
1414 pos = p - ssid;
1415 memcpy(out_ssid, ssid, pos);
1416 //printf("pos %lu -- %s\n", pos, out_ssid);
1417 for(; pos < out_ssid_len; pos ++)
1418 {
1419 if (p[0] == '\0')
1420 {
1421 //printf(" out %s\n", out_ssid);
1422 return;
1423 }
1424 else if (p[0] != '\\')
1425 {
1426 out_ssid[pos] = p[0];
1427 p += 1;
1428 }
1429 else if (p[1] == 'x' || p[1] == 'X')
1430 {
1431 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1432 p += 4;
1433 }
1434 else if (p[1] == '\\')
1435 {
1436 out_ssid[pos] = '\\';
1437 p += 2;
1438 }
1439 else if (p[1] == 't')
1440 {
1441 out_ssid[pos] = '\t';
1442 p += 2;
1443 }
1444 else if (p[1] == 'r')
1445 {
1446 out_ssid[pos] = '\r';
1447 p += 2;
1448 }
1449 else if (p[1] == 'n')
1450 {
1451 out_ssid[pos] = '\n';
1452 p += 2;
1453 }//todo find a better way to convert?
1454 }
1455 }
1456 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001457}
qs.xiong1af5daf2022-03-14 09:12:12 -04001458
you.chen35020192022-05-06 11:30:57 +08001459static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001460 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001461 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001462 RLOGD("enter inner_get_param");
1463 if (out_put == NULL)
1464 {
1465 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001466 return -1;
1467 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001468 if (param_name == NULL)
1469 {
1470 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001471 return -1;
1472 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001473 if (param_name[0] == '\0')
1474 {
1475 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001476 return -1;
1477 }
1478
1479 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1480
1481 CHECK_WPA_CTRL(interface);
1482
1483 DO_REQUEST(lynq_cmd_get);
1484
qs.xiong9fbf74e2023-03-28 13:38:22 +08001485 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1486 {
1487 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001488 return -1;
1489 }
1490
you.chena6fa5b22022-05-18 10:28:19 +08001491// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001492 if (strcmp(param_name, "ssid") == 0)
1493 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001494 if (cmd_reply[0] == '\"')
1495 {
you.chend2fef3f2023-02-13 10:50:35 +08001496 ssid_len = reply_len - 1;
1497 memcpy(out_put, cmd_reply + 1, ssid_len);
1498 if (out_put[ssid_len-1] == '\"')
1499 {
1500 out_put[ssid_len-1] = '\0';
1501 }
1502 else
1503 {
1504 out_put[ssid_len] = '\0';
1505 }
1506 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001507 else
1508 {
you.chend2fef3f2023-02-13 10:50:35 +08001509 ssid_len = reply_len / 2;
1510 for(i=0; i<ssid_len; i++)
1511 {
1512 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1513 }
1514 out_put[ssid_len] = '\0';
1515 }
1516 }
1517 else
1518 {
1519 memcpy(out_put, cmd_reply, reply_len + 1);
1520 }
you.chen35020192022-05-06 11:30:57 +08001521 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001522}
qs.xiong1af5daf2022-03-14 09:12:12 -04001523
you.chen35020192022-05-06 11:30:57 +08001524static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1525 int ret = 0;
1526 char * end = str + len - 1;
1527 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001528 while(str < end)
1529 {
1530 if (*str == delimiter)
1531 {
you.chen35020192022-05-06 11:30:57 +08001532 *str++ = '\0';
1533 results[ret++] = str;
1534 continue;
1535 }
1536 str++;
1537 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001538 if (*str == delimiter)
1539 {
you.chen35020192022-05-06 11:30:57 +08001540 *str = '\0';
1541 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001542
you.chen6ed36a62023-04-27 17:51:56 +08001543 results[ret] = NULL;
1544
you.chen35020192022-05-06 11:30:57 +08001545 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001546}
1547
you.chend2fef3f2023-02-13 10:50:35 +08001548static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1549{
1550 char * p;
1551 int ret = 0;
1552 char cmd[256]={0};
1553 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001554 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001555 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001556 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001557 ret = exec_cmd(cmd, ip, ip_len);
1558 p = strchr(ip, '\n');
1559 if (NULL != p)
1560 {
1561 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001562 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001563 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001564 return ret;
1565}
1566
you.chend2fef3f2023-02-13 10:50:35 +08001567static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001568 struct in_addr addr ={0};
1569 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001570 char cmd[64] = {0};
1571 char * p;
1572 int ret;
you.chen35020192022-05-06 11:30:57 +08001573
qs.xiong9fbf74e2023-03-28 13:38:22 +08001574 if (ip == NULL || *ip == '\0' || hostname == NULL)
1575 {
1576 RLOGE("ip == NULL or hostname == NULL");
1577 return -1;
you.chen35020192022-05-06 11:30:57 +08001578 }
1579
you.chend2fef3f2023-02-13 10:50:35 +08001580 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001581 if (inet_aton(ip, &addr) == 0)
1582 {
you.chen35020192022-05-06 11:30:57 +08001583 printf("---inet_aton fail\n");
1584 return -1;
1585 }
1586
1587 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1588
qs.xiong9fbf74e2023-03-28 13:38:22 +08001589 if (ht == NULL)
1590 {
you.chen186d3c32023-05-18 14:19:46 +08001591 hostname[0] = '\0';
1592 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1593 ret = exec_cmd(cmd, hostname, 32);
1594 if (ret == 0)
1595 {
1596 p = strchr(hostname, '\n');
1597 if (p != NULL)
1598 {
1599 *p = '\0';
1600 }
1601 return 0;
qs.xiong6d64b542023-07-26 17:09:22 +08001602 }else{
1603 usleep( 10 * 1000);
1604 ret = exec_cmd(cmd, hostname, 32);
1605 if( ret == 0)
1606 {
1607 p= strchr(hostname, '\n');
1608 if(p != NULL )
1609 {
1610 *p = '\0';
1611 }
1612 return 0;
1613 }
you.chen186d3c32023-05-18 14:19:46 +08001614 }
1615 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001616 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001617 herror(NULL);
1618 return -1;
1619 }
1620
1621 strcpy(hostname, ht->h_name);
1622
1623 return 0;
1624}
1625
1626static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1627{
1628 int count, index, words_count;
1629 char * split_lines[128]= {0};
1630 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001631 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001632 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001633 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001634
1635 CHECK_WPA_CTRL(ap_sta);
1636
1637 DO_REQUEST(lynq_wifi_list_networks);
1638
1639 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1640
1641 //@todo check ssid field to compatible
1642
1643 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001644 for(index=1; index < count; index++)
1645 {
you.chen35020192022-05-06 11:30:57 +08001646 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001647 if (words_count > 2)
1648 {
you.chend2fef3f2023-02-13 10:50:35 +08001649 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001650 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1651 {
you.chen35020192022-05-06 11:30:57 +08001652 net_no_list[ret++] = atoi(split_words[0]);
1653 }
1654 }
1655 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001656 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001657 return ret;
1658}
1659
1660static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001661 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001662 CHECK_WPA_CTRL(ap_sta);
1663 const char *lynq_wifi_add_network = "ADD_NETWORK";
1664
qs.xiong9fbf74e2023-03-28 13:38:22 +08001665 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001666 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001667 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1668 {
1669 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001670 return -1;
1671 }
1672
qs.xiong9fbf74e2023-03-28 13:38:22 +08001673 for(i=0;i<reply_len;i++)
1674 {
1675 if(cmd_reply[i] == '\n')
1676 {
you.chen35020192022-05-06 11:30:57 +08001677 cmd_reply[i] = '\0';
1678 break;
1679 }
1680 }
1681 return atoi(cmd_reply);
1682}
you.chena6cd55a2022-05-08 12:20:18 +08001683
you.chen35020192022-05-06 11:30:57 +08001684static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1685{
1686 int count, index;
1687 int net_no_list[128];
1688
qs.xiong9fbf74e2023-03-28 13:38:22 +08001689 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001690 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001691 for (index=0; index < count; index++)
1692 {
1693 if (net_no_list[index] == net_no)
1694 {
you.chen35020192022-05-06 11:30:57 +08001695 return 0;
1696 }
1697 }
1698
1699 if (count >= 1)
1700 index = net_no_list[count - 1];
1701 else
1702 index = -1;
1703
qs.xiong9fbf74e2023-03-28 13:38:22 +08001704 while (index < net_no )
1705 {
you.chen35020192022-05-06 11:30:57 +08001706 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001707 if (index >= net_no)
1708 { // required network no created
1709 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001710 return 0;
1711 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001712 else if( index < 0)
1713 {
1714 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001715 return -1;
1716 }
you.chen35020192022-05-06 11:30:57 +08001717 }
1718
1719 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001720 {
1721 RLOGE("[lynq_check_network_number] network index < 0");
1722 return -1;
1723 }
1724 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001725 return 0;
1726}
1727
1728static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001729 if (freq > 5000 && freq < 6000)
1730 {
you.chen35020192022-05-06 11:30:57 +08001731 return LYNQ_WIFI_5G_band;
1732 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001733 else if (freq > 2000 && freq < 3000)
1734 {
you.chen35020192022-05-06 11:30:57 +08001735 return LYNQ_WIFI_2G_band;
1736 }
1737 return LYNQ_WIFI_2_and_5G_band;
1738}
1739
1740static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001741 if (key_mgmt != NULL)
1742 {
1743 if (memcmp( key_mgmt, "NONE", 4) == 0)
1744 {
you.chen35020192022-05-06 11:30:57 +08001745 return LYNQ_WIFI_AUTH_OPEN;
1746 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001747 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1748 {
you.chen35020192022-05-06 11:30:57 +08001749 return LYNQ_WIFI_AUTH_WEP;
1750 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001751 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1752 {
you.chen35020192022-05-06 11:30:57 +08001753 return LYNQ_WIFI_AUTH_WPA_PSK;
1754 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001755 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1756 {
you.chen35020192022-05-06 11:30:57 +08001757 return LYNQ_WIFI_AUTH_WPA2_PSK;
1758 }
1759 }
1760
1761 return -1;
1762}
1763
1764static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 if (flag != NULL)
1766 {
qs.xiong46f41562023-07-11 21:06:47 +08001767 if ( 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 +08001768 {
1769 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001770 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001771 {
1772 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1773 }else if (strstr( flag, "WPA2-PSK") != NULL)
1774 {
you.chen35020192022-05-06 11:30:57 +08001775 return LYNQ_WIFI_AUTH_WPA2_PSK;
1776 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001778 {
you.chen35020192022-05-06 11:30:57 +08001779 return LYNQ_WIFI_AUTH_WPA_PSK;
1780 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001781 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001782 {
you.chen35020192022-05-06 11:30:57 +08001783 return LYNQ_WIFI_AUTH_WEP;
1784 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001785 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001786 {
you.chen35020192022-05-06 11:30:57 +08001787 return LYNQ_WIFI_AUTH_OPEN;
1788 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001789 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001790 {
you.chend2fef3f2023-02-13 10:50:35 +08001791 return LYNQ_WIFI_AUTH_OPEN;
1792 }
qs.xiong46f41562023-07-11 21:06:47 +08001793 else
1794 {
1795 RLOGD("convert_max_auth_from_flag not-found auth mode");
1796 }
you.chen35020192022-05-06 11:30:57 +08001797 }
1798
1799 return -1;
1800}
1801
1802static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1803 switch (bw) {
1804 case 10:
1805 return LYNQ_WIFI_BANDWIDTH_HT10;
1806 break;
1807 case 20:
1808 return LYNQ_WIFI_BANDWIDTH_HT20;
1809 break;
1810 case 40:
1811 return LYNQ_WIFI_BANDWIDTH_HT40;
1812 break;
1813 case 80:
1814 return LYNQ_WIFI_BANDWIDTH_HT80;
1815 break;
1816 default:
1817 break;
1818 }
1819
1820 return -1;
1821}
1822
you.chen70f377f2023-04-14 18:17:09 +08001823static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001824static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1825 int i, count;
1826 char *p;
1827 const char *lynq_status_cmd = "STATUS";
1828 const char * FLAG_SSID = "ssid=";
1829 const char * FLAG_SBSID = "bssid=";
1830 const char * FLAG_KEY_MGMT = "key_mgmt=";
1831 const char * FLAG_FREQ = "freq=";
1832 const char * FLAG_STATE = "wpa_state=";
1833 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001834 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001835 char *split_lines[128] = {0};
1836
1837 CHECK_WPA_CTRL(interface);
1838
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 if (curr_state == NULL)
1840 {
1841 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001842 return -1;
1843 }
1844
1845 DO_REQUEST(lynq_status_cmd);
1846
1847 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1848
1849 curr_state->net_no = -1;
1850 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001851 for(i=0; i < count; i++)
1852 {
1853 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001854 {
you.chen35020192022-05-06 11:30:57 +08001855 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001857 {
you.chend2fef3f2023-02-13 10:50:35 +08001858 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001859 ret = 0;
1860 continue;
1861 }
you.chenf58b3c92022-06-21 16:53:48 +08001862 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001863 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001864 {
you.chend2fef3f2023-02-13 10:50:35 +08001865 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 +08001866 ret = 0;
1867 continue;
1868 }
you.chen35020192022-05-06 11:30:57 +08001869 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001870 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001871 {
you.chen450d0172022-07-15 17:56:48 +08001872 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001873 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001874 ret = 0;
1875 continue;
1876 }
1877 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001879 {
you.chen35020192022-05-06 11:30:57 +08001880 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1881 ret = 0;
1882 continue;
1883 }
you.chend2fef3f2023-02-13 10:50:35 +08001884 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001885 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001886 {
you.chend2fef3f2023-02-13 10:50:35 +08001887 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1888 ret = 0;
1889 continue;
1890 }
you.chen35020192022-05-06 11:30:57 +08001891 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001892 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001893 {
you.chen35020192022-05-06 11:30:57 +08001894 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001895 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001896 {
you.chen35020192022-05-06 11:30:57 +08001897 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1898 ret = 0;
1899 continue;
1900 }
1901
1902 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001903 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001904 {
you.chen35020192022-05-06 11:30:57 +08001905 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001906 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001907 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001908 }
1909 }
1910
you.chen70f377f2023-04-14 18:17:09 +08001911 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1912 {
1913 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1914 }
1915
you.chen35020192022-05-06 11:30:57 +08001916 return ret;
1917}
1918
qs.xiongf1b525b2022-03-31 00:58:23 -04001919int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001920{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001921 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001922 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001923
qs.xiong9fbf74e2023-03-28 13:38:22 +08001924 if (ap_ssid == NULL)
1925 {
1926 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001927 return -1;
1928 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001929 else
1930 {
1931 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001932 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001933
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1935 {
1936 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001937 return -1;
1938 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001939
you.chen35020192022-05-06 11:30:57 +08001940 CHECK_IDX(idx, CTRL_AP);
1941
1942 CHECK_WPA_CTRL(CTRL_AP);
1943
1944 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1945
1946 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1947 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001948 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1949 return 0;
you.chen35020192022-05-06 11:30:57 +08001950
qs.xiong7a105ce2022-03-02 09:43:11 -05001951}
1952
you.chen35020192022-05-06 11:30:57 +08001953int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001954{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001956 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001957 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001958}
1959
qs.xiongc9c79f72022-10-17 15:27:18 +08001960/*****
1961 *frequency <------>channel
1962 *
1963 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1964 *
1965 *
1966 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1967 *
1968 *
1969 * */
1970static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001971 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};
1972 int i;
1973 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1974
qs.xiong69a332b2022-12-02 09:58:57 +08001975 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001976 {
1977 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001978 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001979 }
qs.xiongc00b6032022-11-29 16:28:03 +08001980
1981 if(i == arr_len)
1982 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001983 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001984 return -1;
1985 }
qs.xiongc00b6032022-11-29 16:28:03 +08001986
qs.xiongc9c79f72022-10-17 15:27:18 +08001987 return 0;
1988}
qs.xiong13673462023-02-21 19:12:54 +08001989
1990static int lynq_check_frequencyby_country_code(int input_frequency)
1991{
1992 char str_cnc[]="CN";
1993 char str_dest[20]="";
1994
1995 if( lynq_get_country_code(1,str_dest) != 0 )
1996 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001997 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001998 return -1;
1999 }
2000 if( strncmp(str_dest,str_cnc,2) != 0 )
2001 {
2002 return 0;
2003 }else if( 2473 < input_frequency && input_frequency < 5744)
2004 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002005 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08002006 return -1;
2007 }
2008 return 0;
2009}
qs.xiongf1b525b2022-03-31 00:58:23 -04002010int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002011{
qs.xiongc00b6032022-11-29 16:28:03 +08002012 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002013 char lynq_wifi_frequency_cmd[128]={0};
2014 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002015 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002016 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002017 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002018 check = lynq_check_set_frequency(lynq_wifi_frequency);
2019 if(check != 0)
2020 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002021 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002022 return -1;
you.chen35020192022-05-06 11:30:57 +08002023 }
qs.xiong13673462023-02-21 19:12:54 +08002024 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2025 if(check != 0)
2026 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002027 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002028 return -1;
2029 }
2030
qs.xiongc00b6032022-11-29 16:28:03 +08002031 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2032 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002033 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002034 return -1;
2035 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002036
you.chen35020192022-05-06 11:30:57 +08002037 CHECK_IDX(idx, CTRL_AP);
2038
2039 CHECK_WPA_CTRL(CTRL_AP);
2040
2041 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2042 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2043 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2044
you.chen6c2dd9c2022-05-16 17:55:28 +08002045 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002046 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2047 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2048 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002049
qs.xiong9fbf74e2023-03-28 13:38:22 +08002050 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002051}
2052
qs.xiongf1b525b2022-03-31 00:58:23 -04002053int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002054{
you.chen35020192022-05-06 11:30:57 +08002055 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002056 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002057 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002058
qs.xiong9fbf74e2023-03-28 13:38:22 +08002059 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2060 {
2061 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002062 return -1;
2063 }
2064 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002065
qs.xiong9fbf74e2023-03-28 13:38:22 +08002066 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002067}
2068
qs.xiongf1b525b2022-03-31 00:58:23 -04002069int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2070{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002071 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002072 CHECK_IDX(idx, CTRL_AP);
2073 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002074 case LYNQ_WIFI_BANDWIDTH_HT10:
2075 {
2076 RLOGE("bandwith [%d] not support now\n", bandwidth);
2077 return -1;
2078 }
2079 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002080 {
2081 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2082 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002083 if (system(lynq_cmd_bandwith) != 0 )
2084 {
2085 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002086 return -1;
2087 }
2088 system("wl up");
2089 break;
2090 }
2091 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002092 {
qs.xiong10379192023-02-21 13:19:42 +08002093 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002094 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2095 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002096 if (system(lynq_cmd_bandwith) != 0 )
2097 {
2098 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002099 return -1;
2100 }
2101 system("wl up");
2102 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002103 }
you.chen35020192022-05-06 11:30:57 +08002104 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002105 {
qs.xiong10379192023-02-21 13:19:42 +08002106 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002107 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002108 if (system(lynq_cmd_bandwith) != 0 )
2109 {
2110 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002111 return -1;
2112 }
2113 system("wl up");
2114 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002115 }
2116 default:
you.chen35020192022-05-06 11:30:57 +08002117 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 RLOGE("auth type [%d] not support now\n", bandwidth);
2119 return -1;
you.chen35020192022-05-06 11:30:57 +08002120 }
2121 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002122
2123
you.chen35020192022-05-06 11:30:57 +08002124 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002125}
you.chen35020192022-05-06 11:30:57 +08002126
qs.xiongf1b525b2022-03-31 00:58:23 -04002127int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2128{
you.chen35020192022-05-06 11:30:57 +08002129 int count = 0;
2130 int index = 0;
2131 char *split_words[128] = {0};
2132 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002133 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002134 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002135
you.chen35020192022-05-06 11:30:57 +08002136 CHECK_WPA_CTRL(CTRL_AP);
2137
2138 DO_REQUEST(lynq_chanspec_cmd);
2139
2140 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2141 for(;index < count; index++) {
2142 if (strncmp(split_words[index], "bw", 2) != 0) {
2143 continue;
2144 }
2145
2146 index++;
2147 if (index >= count) {
2148 return -1;
2149 }
2150
qs.xiong9fbf74e2023-03-28 13:38:22 +08002151 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002152 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2153 return 0;
2154 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002156 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002157}
qs.xiong0fb469a2022-04-14 03:50:45 -04002158
qs.xiongf1b525b2022-03-31 00:58:23 -04002159int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002160{
you.chen35020192022-05-06 11:30:57 +08002161 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002162 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002163 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002164
you.chen35020192022-05-06 11:30:57 +08002165 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002166
qs.xiong9fbf74e2023-03-28 13:38:22 +08002167 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2168 {
you.chen35020192022-05-06 11:30:57 +08002169 return -1;
2170 }
2171
2172 system("wl down");
2173 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002174 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002175 return -1;
2176 }
2177 system("wl up");
2178 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002179}
qs.xiong0fb469a2022-04-14 03:50:45 -04002180
qs.xiongf1b525b2022-03-31 00:58:23 -04002181int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002182{
you.chen35020192022-05-06 11:30:57 +08002183 int count = 0;
2184 int index = 0;
2185 char *split_words[128] = {0};
2186 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002187 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002188 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002189
you.chen35020192022-05-06 11:30:57 +08002190 CHECK_WPA_CTRL(CTRL_AP);
2191
2192 DO_REQUEST(lynq_chanspec_cmd);
2193
2194 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002195 for(;index < count; index++)
2196 {
2197 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002198 if (strncmp(split_words[index], "channel", 2) != 0) {
2199 continue;
2200 }
2201
2202 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002203 if (index >= count)
2204 {
you.chen35020192022-05-06 11:30:57 +08002205 return -1;
2206 }
2207
2208 *channel = atoi(split_words[index]);
2209 return 0;
2210 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002211 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002212 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002213}
2214
2215
you.chen35020192022-05-06 11:30:57 +08002216int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002217{
you.chen6c2dd9c2022-05-16 17:55:28 +08002218 char ssid[MAX_CMD] = {0};
2219 int freq = 0;
2220 char lynq_auth_cmd[64]={0};
2221 char lynq_auth_alg_cmd[64]={0};
2222 char lynq_psk_cmd[64]={0};
2223 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002224 char lynq_ieee80211_cmd[64]={0};
2225 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002226 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002227 CHECK_IDX(idx, CTRL_AP);
2228
you.chen6c2dd9c2022-05-16 17:55:28 +08002229 CHECK_WPA_CTRL(CTRL_AP);
2230
qs.xiong9fbf74e2023-03-28 13:38:22 +08002231 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2232 {
2233 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002234 return -1;
2235 }
2236
you.chen92fd5d32022-05-25 10:09:47 +08002237 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002238 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002239 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002240 return 0;
2241 }
2242 else {
2243 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2244 ssid[0] = '\0';
2245 }
2246 lynq_wifi_ap_frequency_get(idx, &freq);
2247
2248 DO_OK_FAIL_REQUEST(cmd_disconnect);
2249 DO_OK_FAIL_REQUEST(cmd_remove_all);
2250 if (ssid[0] != '\0') {
2251 lynq_wifi_ap_ssid_set(idx, ssid);
2252 }
2253 if (freq != 0) {
2254 lynq_wifi_ap_frequency_set(idx, freq);
2255 }
2256 }
2257 }
you.chen35020192022-05-06 11:30:57 +08002258
qs.xiong9fbf74e2023-03-28 13:38:22 +08002259 switch(auth){
2260 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002261 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002262 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002263 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002264 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002265 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002266 break;
2267 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002268 case LYNQ_WIFI_AUTH_WEP:
2269 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002270 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002271 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002272 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002273 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2274
2275 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2276 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2277 break;
2278 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002279 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002280 {
2281 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2282 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2283 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2284
2285 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2286 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2287 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2288 break;
2289
2290 }
you.chen35020192022-05-06 11:30:57 +08002291 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002292 {
2293 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2294 {
you.chen35020192022-05-06 11:30:57 +08002295 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2296 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2297 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002298 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2299 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002300 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002301 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002302 }
2303// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2304// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2305 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002306
you.chen35020192022-05-06 11:30:57 +08002307 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2308 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2309 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002310 break;
2311 }
2312 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2313 {
2314 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2315 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2316 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2317 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2318
2319 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2320 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2321 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2322 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2323 break;
2324 }
2325 case LYNQ_WIFI_AUTH_WPA3_PSK:
2326 {
2327 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2328 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002329 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002330 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2331
2332 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2333 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2334 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2335 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2336 break;
2337 }
2338 default:
you.chen35020192022-05-06 11:30:57 +08002339 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002340 RLOGE("auth type [%d] not support now\n", auth);
2341 return -1;
you.chen35020192022-05-06 11:30:57 +08002342 }
2343 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002344 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002345
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002347}
2348
you.chen35020192022-05-06 11:30:57 +08002349int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002350{
you.chen35020192022-05-06 11:30:57 +08002351 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002352 char lynq_auth_alg_str[MAX_RET] = {0};
2353 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002354 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002355 CHECK_IDX(idx, CTRL_AP);
2356
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2358 {
2359 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002360 return -1;
2361 }
2362
qs.xiong9fbf74e2023-03-28 13:38:22 +08002363 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2364 {
2365 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2366 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002367 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002368 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002369 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002370 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002371 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2372 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002373 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002374 *auth = LYNQ_WIFI_AUTH_WEP;
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
2378 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002379 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002380 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002381 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002382 }
you.chen35020192022-05-06 11:30:57 +08002383 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002384 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2385 {
2386 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2387 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002388 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002389 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002390 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002391 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2392 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002393 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002394 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002395 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002396 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002397 else
2398 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002399 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002400 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002401 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002402 }
you.chen35020192022-05-06 11:30:57 +08002403 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002404
2405 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2406 {
2407 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2408 return -1;
2409 }
2410
2411 if (memcmp(lynq_auth_str,"1",1) == 0 )
2412 {
2413 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2414 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002415 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002416 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2417 {
2418 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2419 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002420 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 }
2422 else
2423 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002424 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002425 *auth = -1;
2426 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002427
you.chen6c2dd9c2022-05-16 17:55:28 +08002428 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002429}
qs.xiong1af5daf2022-03-14 09:12:12 -04002430
you.chenb95401e2023-05-12 19:39:06 +08002431static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2432{
2433 char status[64];
you.chencba13492023-05-19 13:53:43 +08002434 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002435 curr_status_info curr_state;
2436
2437 CHECK_WPA_CTRL(CTRL_AP);
2438
2439 memset(status, 0, sizeof (status));
2440
2441 curr_state.ap = NULL;
2442 curr_state.state = status;
2443
2444 printf("inner_check_ap_connected %d\n", retry_count);
2445 usleep(500*1000);
2446 if (0 == inner_get_status_info(idx, &curr_state))
2447 {
2448 if (strcmp(status, STATE_COMPLETED) == 0)
2449 {
2450 return 0;
2451 }
2452 else if (retry_count == 4) //not ok in 2s, do reconnect
2453 {
2454 DO_REQUEST("RECONNECT");
2455 return inner_check_ap_connected(idx, retry_count+1);
2456 }
you.chencba13492023-05-19 13:53:43 +08002457 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002458 {
2459 printf("retry 10 time\n");
2460 return -1;
2461 }
2462 else
2463 {
you.chen6d247052023-06-01 16:39:54 +08002464 if (strcmp(status, STATE_DISCONNECTED) == 0)
2465 {
2466 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2467 DO_REQUEST(LYNQ_WIFI_CMD);
2468 }
you.chenb95401e2023-05-12 19:39:06 +08002469 return inner_check_ap_connected(idx, retry_count+1);
2470 }
2471 }
2472 return -1;
2473}
qs.xiong1af5daf2022-03-14 09:12:12 -04002474
qs.xiongf1b525b2022-03-31 00:58:23 -04002475int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002476{
you.chen35020192022-05-06 11:30:57 +08002477 char LYNQ_WIFI_CMD[128]={0};
2478 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2479 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002480 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002481 CHECK_IDX(idx, CTRL_AP);
2482
2483 CHECK_WPA_CTRL(CTRL_AP);
2484
you.chen0df3e7e2023-05-10 15:56:26 +08002485 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002486 {
you.chen0df3e7e2023-05-10 15:56:26 +08002487 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002488 return -1;
2489 }
you.chen35020192022-05-06 11:30:57 +08002490
2491 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2492 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2493
2494 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2495 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2496
you.chenc9928582023-04-24 15:39:37 +08002497 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2498 if (ret != 0)
2499 {
2500 RLOGE("lynq_wifi_ap_start excute script fail");
2501 return -1;
2502 }
2503
you.chenb95401e2023-05-12 19:39:06 +08002504 if (inner_check_ap_connected(idx, 0) != 0)
2505 {
2506 return -1;
2507 }
2508
you.chen0df3e7e2023-05-10 15:56:26 +08002509 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002510 if (g_ap_tmp_watcher_pid == 0)
2511 {
2512 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2513 {
2514 g_ap_tmp_watcher_pid = 0;
2515 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2516 return -1;
2517 }
2518 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2519 }
you.chen0df3e7e2023-05-10 15:56:26 +08002520
qs.xiong9fbf74e2023-03-28 13:38:22 +08002521 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002522}
2523
qs.xiongf1b525b2022-03-31 00:58:23 -04002524int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002525{
you.chen35020192022-05-06 11:30:57 +08002526 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002527}
2528
qs.xiongf1b525b2022-03-31 00:58:23 -04002529int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002530{
you.chen35020192022-05-06 11:30:57 +08002531 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002532
you.chen35020192022-05-06 11:30:57 +08002533 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002534
you.chen35020192022-05-06 11:30:57 +08002535 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002536
you.chen35020192022-05-06 11:30:57 +08002537 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2538
2539 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2540
you.chenb4b121c2022-05-06 17:50:16 +08002541// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002542
2543 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2544 if (ret != 0)
2545 {
2546 RLOGE("lynq_wifi_ap_start excute script fail");
2547 return -1;
2548 }
qs.xiong44fac672023-08-29 16:15:55 +08002549 g_ap_tmp_watcher_stop_flag = 1;
2550 if (g_ap_tmp_watcher_pid != 0)
2551 pthread_join(g_ap_tmp_watcher_pid, NULL);
2552 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002553
qs.xiong9fbf74e2023-03-28 13:38:22 +08002554 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002555}
qs.xiong1af5daf2022-03-14 09:12:12 -04002556
qs.xiongf1b525b2022-03-31 00:58:23 -04002557int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002558{
you.chen35020192022-05-06 11:30:57 +08002559 char lynq_disable_cmd[128] = {0};
2560 char lynq_select_cmd[128] = {0};
2561 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002562 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002563 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002564
you.chen35020192022-05-06 11:30:57 +08002565 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002566 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2567 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2568
2569 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2570 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2571 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002572
qs.xiong9fbf74e2023-03-28 13:38:22 +08002573 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002574}
2575
qs.xiongf1b525b2022-03-31 00:58:23 -04002576int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002577{
you.chen35020192022-05-06 11:30:57 +08002578 char lynq_disable_cmd[128] = {0};
2579 char lynq_select_cmd[128] = {0};
2580 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002581 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002582 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002583
you.chen35020192022-05-06 11:30:57 +08002584 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002585
you.chen35020192022-05-06 11:30:57 +08002586 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2587 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2588
2589 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2590 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2591 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002592
qs.xiong9fbf74e2023-03-28 13:38:22 +08002593 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002594}
qs.xiongf1b525b2022-03-31 00:58:23 -04002595
you.chen35020192022-05-06 11:30:57 +08002596int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002597{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002598 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002599 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002600 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002601 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002602 RLOGD("enter lynq_ap_password_set");
2603 if( password == NULL )
2604 {
2605 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002606 return -1;
2607 }
2608 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002609 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002610 if(pass_len < 8 || pass_len >= 64)
2611 {
2612 RLOGE("[lynq_ap_password_set]input password len not in rage");
2613 return -1;
you.chen35020192022-05-06 11:30:57 +08002614 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002615
you.chen35020192022-05-06 11:30:57 +08002616 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002617
qs.xiong9fbf74e2023-03-28 13:38:22 +08002618 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2619 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002620 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002621 return -1;
2622 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002623 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2624 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002625 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2626 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002627 }
2628
you.chen35020192022-05-06 11:30:57 +08002629 CHECK_WPA_CTRL(CTRL_AP);
2630
qs.xiong9fbf74e2023-03-28 13:38:22 +08002631 if (auth == LYNQ_WIFI_AUTH_WEP)
2632 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002633 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002634 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2635 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2636 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2637 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2638 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002639 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2640 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002641 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 +08002642 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2643 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2644 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002645 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2646 {
2647
qs.xiongc8d92a62023-03-29 17:36:14 +08002648 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 +08002649 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002650 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002651 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2652 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2653
2654 }
2655 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002656 {
2657 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002658 return -1;
2659 }
you.chen35020192022-05-06 11:30:57 +08002660
you.chen35020192022-05-06 11:30:57 +08002661 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002662
qs.xiong9fbf74e2023-03-28 13:38:22 +08002663 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002664}
2665
you.chen35020192022-05-06 11:30:57 +08002666int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002667{
you.chen35020192022-05-06 11:30:57 +08002668 FILE * fp;
2669 int len, ret;
2670 int count, index;
2671 char *split_lines[128] = {0};
2672 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002673 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002674
you.chen35020192022-05-06 11:30:57 +08002675 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002676
you.chen35020192022-05-06 11:30:57 +08002677 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2678// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002679 if (NULL == fp)
2680 {
2681 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002682 return -1;
2683 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002684
you.chen35020192022-05-06 11:30:57 +08002685 buff = alloca(MAX_RET);
2686 fseek(fp, 0, SEEK_SET);
2687 len = fread(buff, 1, MAX_RET, fp);
2688 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002689
qs.xiong9fbf74e2023-03-28 13:38:22 +08002690 for(index=0; index < len; index ++)
2691 {
2692 if (memcmp(buff + index, "network={", 9) != 0)
2693 {
you.chen35020192022-05-06 11:30:57 +08002694 continue;
2695 }
2696 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002697 for (; index < len; index ++ )
2698 {
2699 if (buff[index] != '}')
2700 {
you.chen35020192022-05-06 11:30:57 +08002701 continue;
2702 }
2703 buff[index] = '\0';
2704 break;
2705 }
2706 len = buff + index - p;
2707 }
2708
2709 count = lynq_split(p, len, '\n', split_lines);
2710
2711 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002712 for(index=0; index < count; index++)
2713 {
you.chen35020192022-05-06 11:30:57 +08002714 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002715 if (p != NULL)
2716 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002717 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002718 if (*p == '\"')
2719 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002720 p++;
2721 }
you.chen35020192022-05-06 11:30:57 +08002722 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2724 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002725 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002726 if (*p == '\"')
2727 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002728 p++;
2729 }
2730 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 else
2732 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002733 continue;
you.chen35020192022-05-06 11:30:57 +08002734 }
2735
2736 strcpy(password, p);
2737
qs.xiong9fbf74e2023-03-28 13:38:22 +08002738 while(*password != '\0')
2739 {
2740 if (*password == '\"')
2741 {
you.chen35020192022-05-06 11:30:57 +08002742 *password = '\0';
2743 break;
2744 }
2745 password++;
2746 }
2747 ret = 0;
2748 break;
2749 } //end for(index=0; index < count; index++)
2750
2751 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002752}
2753
you.chen35020192022-05-06 11:30:57 +08002754static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2755 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002756 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002757
qs.xiong9fbf74e2023-03-28 13:38:22 +08002758 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2759 {
you.chen35020192022-05-06 11:30:57 +08002760 return -1;
2761 }
2762
2763 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002764
qs.xiong9fbf74e2023-03-28 13:38:22 +08002765 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2766 {
2767 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002768 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002769 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002770 {
you.chena6cd55a2022-05-08 12:20:18 +08002771 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002772 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002773 }
you.chen70f377f2023-04-14 18:17:09 +08002774 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2775 {
2776 return 0;
2777 }
you.chena6cd55a2022-05-08 12:20:18 +08002778 }
2779 }
you.chen70f377f2023-04-14 18:17:09 +08002780 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2781 {
2782 return 0;
2783 }
2784
qs.xiong9fbf74e2023-03-28 13:38:22 +08002785 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2786 {
you.chen70f377f2023-04-14 18:17:09 +08002787 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002788 return -1;
2789 }
2790 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2791 {
2792
you.chen70f377f2023-04-14 18:17:09 +08002793 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2794 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002795 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2796 {
2797
2798 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002799 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002800 }else
2801 {
you.chen70f377f2023-04-14 18:17:09 +08002802 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002803 *auth = -1;
2804 return -1;
2805 }
you.chen35020192022-05-06 11:30:57 +08002806 return 0;
2807}
2808
2809int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002810{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002811 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002812 int pass_len, net_no, count, index;
2813 char lynq_tmp_cmd[300]={0};
2814 int net_no_list[128];
2815 lynq_wifi_auth_s net_auth;
2816 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002817 if(pass_len < 8 || pass_len >= 64)
2818 {
2819 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002820 return -1;
2821 }
2822
2823 CHECK_IDX(idx, CTRL_STA);
2824
2825 net_no = -1;
2826 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2827
qs.xiong9fbf74e2023-03-28 13:38:22 +08002828 for (index=0; index < count; index++)
2829 {
you.chen35020192022-05-06 11:30:57 +08002830 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002831 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2832 {
you.chen35020192022-05-06 11:30:57 +08002833 net_no = net_no_list[index];
2834 break;
2835 }
2836 }
2837
qs.xiong9fbf74e2023-03-28 13:38:22 +08002838 if (net_no < 0)
2839 {
you.chen35020192022-05-06 11:30:57 +08002840 return -1;
2841 }
2842
2843 CHECK_WPA_CTRL(CTRL_STA);
2844
2845 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2846
2847 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2848 DO_OK_FAIL_REQUEST(cmd_save_config);
2849
2850 return 0;
2851}
2852
2853int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2854
2855 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002856 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002857 int count, index;
2858 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002859 char *buff, *p, *ssid, *ssid_end_flag;
2860 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002861 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002862
you.chen755332b2022-08-06 16:59:10 +08002863 network_len = 0;
2864 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002865 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002866
you.chen35020192022-05-06 11:30:57 +08002867 CHECK_IDX(idx, CTRL_STA);
2868
qs.xiong9fbf74e2023-03-28 13:38:22 +08002869 if (NULL == password)
2870 {
2871 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002872 return -1;
2873 }
2874
you.chen35020192022-05-06 11:30:57 +08002875 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002876 if (NULL == fp)
2877 {
2878 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002879 return -1;
2880 }
2881
you.chen6d247052023-06-01 16:39:54 +08002882 fseek(fp, 0, SEEK_END);
2883 len = ftell(fp);
2884 buff = malloc(len + 1);
2885
2886 if (buff == NULL)
2887 {
2888 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2889 return -1;
2890 }
2891
you.chen35020192022-05-06 11:30:57 +08002892 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002893 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002894 fclose(fp);
2895
qs.xiong9fbf74e2023-03-28 13:38:22 +08002896 for(index=0; index < len; index ++)
2897 {
2898 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002899 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002901 {
you.chen35020192022-05-06 11:30:57 +08002902 continue;
2903 }
you.chen6d247052023-06-01 16:39:54 +08002904 p = buff + index + 9;
2905 for (; index < len; index ++ )
2906 {
2907 if (buff[index] != '}')
2908 {
2909 continue;
2910 }
2911 buff[index] = '\0';
2912 break;
2913 }
2914 network_len = buff + index - p;
2915 break;
you.chen35020192022-05-06 11:30:57 +08002916 }
2917
qs.xiongb3f26af2023-02-17 18:41:07 +08002918 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002919 {
2920 if (buff != NULL)
2921 {
2922 free(buff);
2923 }
2924
qs.xiongb3f26af2023-02-17 18:41:07 +08002925 return -1;
you.chen6d247052023-06-01 16:39:54 +08002926 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002927
you.chend2fef3f2023-02-13 10:50:35 +08002928 ssid = strstr(p, "ssid=");
2929 if (ssid != NULL) {
2930 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002931 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002932 {
you.chend2fef3f2023-02-13 10:50:35 +08002933 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2934 break;
2935 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002936 else
you.chen6d247052023-06-01 16:39:54 +08002937 {
you.chend2fef3f2023-02-13 10:50:35 +08002938 ssid_end_flag = strstr(ssid, "\n");
2939 if (ssid_end_flag != NULL)
2940 {
2941 ssid_len = (ssid_end_flag - ssid) / 2;
2942 for(i=0; i<ssid_len; i++)
2943 {
2944 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2945 }
2946 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2947 break;
2948 }
2949 }
you.chen35020192022-05-06 11:30:57 +08002950 }
you.chend2fef3f2023-02-13 10:50:35 +08002951
you.chen35020192022-05-06 11:30:57 +08002952 }
2953
qs.xiong9fbf74e2023-03-28 13:38:22 +08002954 if (index >= len || NULL == p || network_len <= 0)
2955 {
you.chen6d247052023-06-01 16:39:54 +08002956 if (buff != NULL)
2957 {
2958 free(buff);
2959 }
you.chen35020192022-05-06 11:30:57 +08002960 return -1;
2961 }
2962
you.chen755332b2022-08-06 16:59:10 +08002963 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002964
2965 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002966 for(index=0; index < count; index++)
2967 {
you.chen35020192022-05-06 11:30:57 +08002968 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002969 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002970 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002971 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002972 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002973 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002974 p++;
2975 }
you.chen35020192022-05-06 11:30:57 +08002976 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002977 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002978 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002979 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002980 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002981 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002982 p++;
2983 }
2984 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002985 else
you.chen6d247052023-06-01 16:39:54 +08002986 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002987 continue;
you.chen35020192022-05-06 11:30:57 +08002988 }
2989
qs.xiong13673462023-02-21 19:12:54 +08002990 if (*p == '\"')
2991 p++;
2992 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002993
qs.xiong13673462023-02-21 19:12:54 +08002994 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002995 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002996 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002997 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002998 {
you.chen35020192022-05-06 11:30:57 +08002999 *password = '\0';
3000 break;
3001 }
3002 password++;
3003 }
3004 ret = 0;
3005 break;
3006 } //end for(index=0; index < count; index++)
3007
you.chen6d247052023-06-01 16:39:54 +08003008 if (buff != NULL)
3009 {
3010 free(buff);
3011 }
3012
you.chen35020192022-05-06 11:30:57 +08003013 return ret;
3014}
3015
3016static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3017{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003018 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003019
qs.xiong9fbf74e2023-03-28 13:38:22 +08003020 if (sta_ssid == NULL)
3021 {
3022 RLOGE("sta_ssid is null\n");
3023 return -1;
you.chen35020192022-05-06 11:30:57 +08003024 }
3025
qs.xiong9fbf74e2023-03-28 13:38:22 +08003026 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003027
3028 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3029
3030 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3031// DO_OK_FAIL_REQUEST(cmd_save_config);
3032
qs.xiong9fbf74e2023-03-28 13:38:22 +08003033 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003034
3035}
3036
you.chen35020192022-05-06 11:30:57 +08003037static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003038{
you.chen35020192022-05-06 11:30:57 +08003039 char lynq_disable_cmd[128]={0};
3040 char lynq_select_cmd[128]={0};
3041
3042 CHECK_WPA_CTRL(CTRL_STA);
3043
qs.xiong9fbf74e2023-03-28 13:38:22 +08003044 if (save != 0)
3045 {
you.chenc29444e2022-06-07 18:01:16 +08003046 if (start_flag != 0)
3047 {
3048 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3049 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3050 }
3051 else
3052 {
3053 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3054 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3055 }
you.chen35020192022-05-06 11:30:57 +08003056 DO_OK_FAIL_REQUEST(cmd_save_config);
3057 }
3058
qs.xiong9fbf74e2023-03-28 13:38:22 +08003059 if (start_flag == 0)
3060 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003061 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003062 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3063 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003064 else
3065 {
you.chen35020192022-05-06 11:30:57 +08003066 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3067 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3068 }
3069
3070 return 0;
3071}
3072
3073int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3074{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003075 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003076 CHECK_IDX(idx, CTRL_STA);
3077
you.chen6c2dd9c2022-05-16 17:55:28 +08003078 curr_status_info curr_state;
3079 ap_info_s ap_info;
3080 curr_state.ap = &ap_info;
3081 curr_state.state = NULL;
3082
qs.xiong9fbf74e2023-03-28 13:38:22 +08003083 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3084 {
you.chend2fef3f2023-02-13 10:50:35 +08003085 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003086 return 0;
3087 }
3088
3089 return -1;
you.chen35020192022-05-06 11:30:57 +08003090}
3091
3092int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3093{
you.chen9ac66392022-08-06 17:01:16 +08003094 scan_info_s *scan_list = NULL;
3095 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003096 int scan_len=0;
3097 int save_len=0;
3098 int best_index = -1;
3099 int best_scan_index = -1;
3100 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003101 int i, j, ret;
3102
3103 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003104
3105 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003106 if (info == NULL)
3107 {
you.chen35020192022-05-06 11:30:57 +08003108 return -1;
3109 }
3110
3111 curr_status_info curr_state;
3112 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003113 char status[64];
you.chen35020192022-05-06 11:30:57 +08003114
you.chen9ac66392022-08-06 17:01:16 +08003115 memset(&ap_info, 0, sizeof (ap_info));
3116 memset(status, 0, sizeof (status));
3117
3118 curr_state.ap = &ap_info;
3119 curr_state.state = status;
3120
qs.xiong9fbf74e2023-03-28 13:38:22 +08003121 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3122 {
you.chen35020192022-05-06 11:30:57 +08003123 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003124 if (strcmp(status, STATE_COMPLETED) == 0)
3125 {
3126 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3127 }
3128 else
3129 {
3130 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3131 }
you.chen593621d2023-04-27 17:52:44 +08003132 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003133 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003134 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003135 return 0;
3136 }
3137
you.chen9ac66392022-08-06 17:01:16 +08003138 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003139 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003140 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3141 {
you.chen9ac66392022-08-06 17:01:16 +08003142 if (NULL != scan_list)
3143 {
3144 free(scan_list);
3145 }
you.chen35020192022-05-06 11:30:57 +08003146 return -1;
3147 }
3148
qs.xiong9fbf74e2023-03-28 13:38:22 +08003149 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3150 {
you.chen9ac66392022-08-06 17:01:16 +08003151 if (NULL != scan_list)
3152 {
3153 free(scan_list);
3154 }
3155 if (NULL != save_list)
3156 {
3157 free(save_list);
3158 }
you.chen35020192022-05-06 11:30:57 +08003159 return -1;
3160 }
3161
qs.xiong9fbf74e2023-03-28 13:38:22 +08003162 for (i=0; i < save_len; i++)
3163 {
3164 for (j=0; j < scan_len; j++)
3165 {
you.chen35020192022-05-06 11:30:57 +08003166 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003167 && save_list[i].base_info.auth == scan_list[j].auth)
3168 {
3169 if (best_rssi == 0)
3170 {
you.chen9ac66392022-08-06 17:01:16 +08003171 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003172 best_rssi = scan_list[j].rssi;
3173 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003174 else if (best_rssi > scan_list[j].rssi)
3175 {
you.chen35020192022-05-06 11:30:57 +08003176 best_index = i;
3177 best_scan_index = j;
3178 best_rssi = scan_list[j].rssi;
3179 }
you.chend2fef3f2023-02-13 10:50:35 +08003180 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 +08003181 break;
3182 }
3183 }
3184 }
3185
qs.xiong9fbf74e2023-03-28 13:38:22 +08003186 if (best_index >= 0)
3187 {
you.chen35020192022-05-06 11:30:57 +08003188 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003189 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 +08003190 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3191 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003192 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003193 }
3194
you.chen9ac66392022-08-06 17:01:16 +08003195 if (NULL != scan_list)
3196 {
3197 free(scan_list);
3198 }
3199 if (NULL != save_list)
3200 {
3201 free(save_list);
3202 }
3203
3204 return ret;
you.chen35020192022-05-06 11:30:57 +08003205}
3206
3207static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3208{
qs.xiongc8d92a62023-03-29 17:36:14 +08003209 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003210 char lynq_ket_mgmt_cmd[64]={0};
3211 char lynq_pairwise_cmd[64]={0};
3212 char lynq_psk_cmd[64]={0};
3213
3214 CHECK_WPA_CTRL(CTRL_STA);
3215
qs.xiong9fbf74e2023-03-28 13:38:22 +08003216 switch(auth)
3217 {
3218 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003219 {
3220 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003221
you.chen35020192022-05-06 11:30:57 +08003222 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003223// DO_OK_FAIL_REQUEST(cmd_save_config);
3224 break;
3225 }
3226 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003227 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003228 {
3229 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3230 {
you.chen35020192022-05-06 11:30:57 +08003231 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3232 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003233 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3234 {
you.chena6cd55a2022-05-08 12:20:18 +08003235 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003236 }
3237 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3238 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003239
you.chen35020192022-05-06 11:30:57 +08003240 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3241 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3242 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003243
qs.xiong9fbf74e2023-03-28 13:38:22 +08003244 if (password != NULL)
3245 {
you.chen35020192022-05-06 11:30:57 +08003246 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3247 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003248 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003249 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003250
you.chen35020192022-05-06 11:30:57 +08003251// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003252 break;
3253 }
3254 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3255 {
qs.xiong3e506812023-04-06 11:08:48 +08003256 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003257 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003258 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3259 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3260
qs.xiong3e506812023-04-06 11:08:48 +08003261 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003262 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3263 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3264 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3265
3266 break;
3267 }
3268 case LYNQ_WIFI_AUTH_WPA3_PSK:
3269 {
qs.xiong3e506812023-04-06 11:08:48 +08003270 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003271 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003272 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3273 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3274
qs.xiong3e506812023-04-06 11:08:48 +08003275 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003276 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3277 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3278 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3279
3280 break;
3281 }
3282 default:
3283 return -1;
you.chen35020192022-05-06 11:30:57 +08003284 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003285
qs.xiong9fbf74e2023-03-28 13:38:22 +08003286 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003287}
qs.xiong7a105ce2022-03-02 09:43:11 -05003288
you.chen35020192022-05-06 11:30:57 +08003289static int inner_get_curr_net_no(int interface) {
3290 curr_status_info curr_state;
3291 curr_state.ap = NULL;
3292 curr_state.state = NULL;
3293
qs.xiong9fbf74e2023-03-28 13:38:22 +08003294 if (0 != inner_get_status_info(interface, &curr_state))
3295 {
you.chen35020192022-05-06 11:30:57 +08003296 return -1;
3297 }
3298
3299 return curr_state.net_no;
3300}
3301
3302int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003303{
you.chen35020192022-05-06 11:30:57 +08003304 int net_no;
3305 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003306
you.chen35020192022-05-06 11:30:57 +08003307 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003308
qs.xiong9fbf74e2023-03-28 13:38:22 +08003309 if (net_no < 0)
3310 {
you.chen35020192022-05-06 11:30:57 +08003311 return -1;
3312 }
3313
3314 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003315}
3316
you.chenb95401e2023-05-12 19:39:06 +08003317int 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 -05003318{
you.chen35020192022-05-06 11:30:57 +08003319 int count, net_no, index;
3320 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003321 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003322 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003323 curr_status_info curr_state;
3324 ap_info_s ap_info;
3325 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003326
qs.xiong9fbf74e2023-03-28 13:38:22 +08003327 if (ssid == NULL || *ssid == '\0')
3328 {
3329 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003330 return -1;
3331 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003332
qs.xiong9fbf74e2023-03-28 13:38:22 +08003333 if (LYNQ_WIFI_AUTH_OPEN != auth)
3334 {
3335 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003336 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003337 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003338 return -1;
3339 }
3340 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003341
you.chen70f377f2023-04-14 18:17:09 +08003342
3343 pthread_mutex_lock(&s_global_check_mutex);
3344 if (s_sta_status != INNER_STA_STATUS_INIT)
3345 {
3346 s_sta_status = INNER_STA_STATUS_CANCEL;
3347 pthread_cond_signal(&s_global_check_cond);
3348 }
3349 pthread_mutex_unlock(&s_global_check_mutex);
3350
you.chen35020192022-05-06 11:30:57 +08003351 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003352 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003353
3354 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003355 memset(&ap_info, 0, sizeof (ap_info));
3356 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003357
you.chen70f377f2023-04-14 18:17:09 +08003358 curr_state.ap = &ap_info;
3359 curr_state.state = status;
3360
3361 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003362 {
you.chen70f377f2023-04-14 18:17:09 +08003363 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3364 {
3365 net_no = curr_state.net_no;
3366 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3367 && strcmp(ap_info.psw, psw) == 0)
3368 {
3369 RLOGD("already connected\n");
3370
3371 pthread_mutex_lock(&s_global_check_mutex);
3372 s_sta_status = INNER_STA_STATUS_CONNECTED;
3373 pthread_cond_signal(&s_global_check_cond);
3374 pthread_mutex_unlock(&s_global_check_mutex);
3375 return 0;
3376 }
you.chen35020192022-05-06 11:30:57 +08003377 }
3378 }
3379
you.chen70f377f2023-04-14 18:17:09 +08003380 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003381 {
you.chen70f377f2023-04-14 18:17:09 +08003382 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3383
3384 for (index=0; index < count; index++)
3385 {
3386 net_auth = -1;
3387 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3388 {
3389 net_no = net_no_list[index];
3390 break;
3391 }
you.chen35020192022-05-06 11:30:57 +08003392 }
3393
you.chen70f377f2023-04-14 18:17:09 +08003394 if (net_no < 0)
3395 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003396 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3397 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3398 {
3399 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3400 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3401 DO_OK_FAIL_REQUEST(rm_net_cmd);
3402 }
you.chen70f377f2023-04-14 18:17:09 +08003403 net_no = lynq_add_network(CTRL_STA);
3404 if (net_no == -1)
3405 {
3406 return -1;
3407 }
3408
3409 RLOGD("net no is %d\n", net_no);
3410 if (0 != inner_set_sta_ssid(net_no, ssid))
3411 {
3412 return -1;
3413 }
you.chen35020192022-05-06 11:30:57 +08003414 }
3415 }
3416
qs.xiong9fbf74e2023-03-28 13:38:22 +08003417 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3418 {
you.chen35020192022-05-06 11:30:57 +08003419 return -1;
3420 }
3421
you.chen70f377f2023-04-14 18:17:09 +08003422
3423 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003424 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003425 usleep(200*1000);
3426
3427 ret = inner_sta_start_stop(net_no, 1, 1);
3428
3429 pthread_mutex_lock(&s_global_check_mutex);
3430 s_sta_status = INNER_STA_STATUS_CONNECTING;
3431 strcpy(s_sta_current_connecting_ssid, ssid);
3432 struct timeval now;
3433 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003434 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003435 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3436 pthread_cond_signal(&s_global_check_cond);
3437 pthread_mutex_unlock(&s_global_check_mutex);
3438 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003439}
3440
you.chenb95401e2023-05-12 19:39:06 +08003441int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3442{
3443 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3444}
3445
you.chen35020192022-05-06 11:30:57 +08003446int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003447{
you.chen35020192022-05-06 11:30:57 +08003448 ap_info_s ap;
3449 curr_status_info curr_state;
3450 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003451
qs.xiong9fbf74e2023-03-28 13:38:22 +08003452 if (ssid == NULL || *ssid == '\0')
3453 {
3454 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003455 return -1;
3456 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003457
you.chen35020192022-05-06 11:30:57 +08003458 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003459
you.chen35020192022-05-06 11:30:57 +08003460 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003461 curr_state.state = NULL;
3462
qs.xiong9fbf74e2023-03-28 13:38:22 +08003463 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3464 {
you.chen35020192022-05-06 11:30:57 +08003465 return 0;
3466 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003467
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 if (strcmp(ap.ap_ssid, ssid) != 0)
3469 {
you.chen35020192022-05-06 11:30:57 +08003470 return 0;
3471 }
3472
you.chen70f377f2023-04-14 18:17:09 +08003473 pthread_mutex_lock(&s_global_check_mutex);
3474 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3475 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003476 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003477}
qs.xiong97fa59b2022-04-07 05:41:29 -04003478
qs.xiongc93bf2b2023-08-25 10:22:08 +08003479int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3480{
3481 ap_info_s ap;
3482 curr_status_info curr_state;
3483 ap.ap_ssid[0] = '\0';
3484
3485 if (ssid == NULL || *ssid == '\0')
3486 {
3487 RLOGE("input ssid is NULL\n");
3488 return -1;
3489 }
3490
3491 CHECK_IDX(idx, CTRL_STA);
3492
3493
3494 curr_state.ap = &ap;
3495 curr_state.state = NULL;
3496
3497 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3498 {
3499 return 0;
3500 }
3501
3502 if (strcmp(ap.ap_ssid, ssid) != 0)
3503 {
3504 return 0;
3505 }
3506
3507 pthread_mutex_lock(&s_global_check_mutex);
3508 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3509 pthread_mutex_unlock(&s_global_check_mutex);
3510 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3511
3512}
3513
3514
you.chena6cd55a2022-05-08 12:20:18 +08003515int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3516{
qs.xiongad2f89d2023-01-18 13:17:41 +08003517// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3518// const char *lynq_reconnect_cmd = "RECONNECT";
3519 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3520 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3521// const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all";
qs.xiong7a105ce2022-03-02 09:43:11 -05003522
you.chen35020192022-05-06 11:30:57 +08003523 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003524 CHECK_WPA_CTRL(CTRL_STA);
3525
you.chenc9928582023-04-24 15:39:37 +08003526 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3527 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003528 {
you.chenc9928582023-04-24 15:39:37 +08003529 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003530 return -1;
3531 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003532
qs.xiongad2f89d2023-01-18 13:17:41 +08003533 system(lynq_enable_sta_cmd);
3534 system(lynq_reconnect_cmd);
3535// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003536 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003537}
3538
you.chen6d247052023-06-01 16:39:54 +08003539static int inner_get_status_info_state (int interface, char *state) {
3540 curr_status_info curr_state;
3541 curr_state.ap = NULL;
3542 curr_state.state = state;
3543 return inner_get_status_info(interface, &curr_state);
3544}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003545
3546int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3547{
3548
3549 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3550
3551 CHECK_IDX(idx, CTRL_STA);
3552 CHECK_WPA_CTRL(CTRL_STA);
3553
3554 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3555 if (ret != 0)
3556 {
3557 RLOGE("lynq_wifi_ap_start excute script fail");
3558 return -1;
3559 }
3560
3561// system(lynq_enable_sta_cmd);
3562 system(lynq_reconnect_cmd);
3563
3564 return 0;
3565}
3566
3567
you.chen35020192022-05-06 11:30:57 +08003568int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003569{
qs.xiongad2f89d2023-01-18 13:17:41 +08003570// char lynq_disable_network_cmd[MAX_CMD];
3571// curr_status_info curr_state;
3572// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003573 int i=0;
3574 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003575
you.chen6d247052023-06-01 16:39:54 +08003576// const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08003577 CHECK_IDX(idx, CTRL_STA);
3578 CHECK_WPA_CTRL(CTRL_STA);
3579
you.chen6d247052023-06-01 16:39:54 +08003580// system(lynq_disable_sta_cmd);
3581 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003582 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003583
3584 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3585 if (ret != 0)
3586 {
3587 RLOGE("lynq_wifi_ap_start excute script fail");
3588 return -1;
3589 }
3590
you.chen6d247052023-06-01 16:39:54 +08003591 for (i=0; i < 30; i++) // to check if sta is realy stoped
3592 {
3593 if (inner_get_status_info_state(idx, state) != 0)
3594 {
3595 break;
3596 }
3597
3598 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3599 {
3600 break;
3601 }
3602 RLOGD("lynq_wifi_ap_start curr state %s", state);
3603 usleep(SLEEP_TIME_ON_IDLE);
3604 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003605
you.chena6cd55a2022-05-08 12:20:18 +08003606 return 0;
3607// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003608}
qs.xiongfcc914b2023-07-06 21:16:20 +08003609int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3610{
3611 char LYNQ_DISABLE_CMD[128]={0};
3612 CHECK_IDX(idx, CTRL_STA);
3613 CHECK_WPA_CTRL(CTRL_STA);
3614 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3615 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3616 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3617 return 0;
3618}
qs.xiong7a105ce2022-03-02 09:43:11 -05003619
you.chen35020192022-05-06 11:30:57 +08003620//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3621// int i, count;
3622// char *p;
3623// const char * FLAG_SSID = "ssid=";
3624// const char * FLAG_SBSID = "bssid=";
3625// const char * FLAG_KEY_MGMT = "key_mgmt=";
3626// const char * FLAG_FREQ = "freq=";
3627// char lynq_sta_cmd[MAX_CMD];
3628// char *split_lines[128] = {0};
3629
3630// CHECK_WPA_CTRL(CTRL_AP);
3631
3632// sprintf(lynq_sta_cmd, "STA %s", bssid);
3633
3634// DO_REQUEST(lynq_sta_cmd);
3635
3636// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3637
3638// for(i=0; i < count; i++) {
3639// p = strstr(split_lines[i], FLAG_SSID);
3640// if (p != NULL) {
3641// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3642// continue;
3643// }
3644// }
3645
3646// lynq_get_interface_ip(idx, ap->ap_ip);
3647// lynq_ap_password_set(idx, ap->psw);
3648
3649// return 0;
3650//}
3651
3652static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3653 curr_status_info curr_state;
3654 curr_state.ap = ap;
3655 curr_state.state = NULL;
3656 return inner_get_status_info(interface, &curr_state);
3657}
3658
3659int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003660{
you.chend2fef3f2023-02-13 10:50:35 +08003661 int index, line_count;
3662 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003663 const char *lynq_first_sta_cmd = "STA-FIRST";
3664 char lynq_next_sta_cmd[MAX_CMD];
3665 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003666 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003667
you.chen35020192022-05-06 11:30:57 +08003668 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003669
you.chen35020192022-05-06 11:30:57 +08003670 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003671
you.chenb95401e2023-05-12 19:39:06 +08003672 // ap_info_s * tmp_ap;
3673 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003674 if (ap == NULL || list == NULL || len == NULL)
3675 {
3676 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003677 return -1;
3678 }
3679
you.chenb95401e2023-05-12 19:39:06 +08003680 // ap = &tmp_ap;
3681 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003682 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003683 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003684
you.chenb95401e2023-05-12 19:39:06 +08003685 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003686 {
you.chenb95401e2023-05-12 19:39:06 +08003687 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003688 return -1;
3689 }
3690
3691 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3692 lynq_ap_password_get(idx, (*ap)->psw);
3693
you.chen35020192022-05-06 11:30:57 +08003694 DO_REQUEST(lynq_first_sta_cmd);
3695
3696 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003697 while (reply_len > 0)
3698 {
3699 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003700 {
you.chen35020192022-05-06 11:30:57 +08003701 break;
3702 }
3703 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3704 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3705 strcpy(bssid[index], split_lines[0]);
3706 index++;
3707 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3708 reply_len = MAX_RET;
3709 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003710 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 +08003711 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003712 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003713 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003714 break;
3715 }
3716 }
3717
3718 *len = index;
3719
3720 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003721 for (index=0; index < *len; index++)
3722 {
you.chend2fef3f2023-02-13 10:50:35 +08003723 dev_info = &(*list)[index];
3724 memset(dev_info, 0, sizeof(device_info_s));
3725 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3726 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3727 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3728 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003729 free(bssid[index]);
3730 }
3731
3732 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003733}
3734
you.chen35020192022-05-06 11:30:57 +08003735int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003736{
you.chen35020192022-05-06 11:30:57 +08003737 int i, count, index, count_words;
3738 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3739 char *split_lines[128] = {0};
3740 char *split_words[128] = {0};
3741 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003742
qs.xiong9fbf74e2023-03-28 13:38:22 +08003743 if (list == NULL || len == NULL)
3744 {
you.chen35020192022-05-06 11:30:57 +08003745 return -1;
3746 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003747
you.chen9ac66392022-08-06 17:01:16 +08003748 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3749 {
3750 usleep(100 * 1000);
3751 }
3752
you.chen35020192022-05-06 11:30:57 +08003753 CHECK_IDX(idx, CTRL_STA);
3754
3755 CHECK_WPA_CTRL(CTRL_STA);
3756
3757 DO_REQUEST(lynq_scan_result_cmd);
3758
3759 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3760 *len = count - 1;
3761 *list = malloc(sizeof (scan_info_s) * *len);
3762
3763 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003764 for (index=0; index <count_words; index++)
3765 {
3766 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003767 }
3768
qs.xiong9fbf74e2023-03-28 13:38:22 +08003769 for(index = 1;index < count; index++)
3770 {
3771 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003772 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003773 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3774 if (count_words < 4)
3775 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003776 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003777 //bssid / frequency / signal level / flags / ssid
3778 p = (*list) + index - 1;
3779 strcpy(p->mac, split_words[0]);
3780 p->band = convert_band_from_freq(atoi(split_words[1]));
3781 p->rssi = -1 * atoi( split_words[2]);
3782 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003783 if (count_words == 4) // ssid hided
3784 {
3785 p->ssid[0] = '\0';
3786 }
3787 else
3788 {
3789 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3790 }
you.chen35020192022-05-06 11:30:57 +08003791 }
3792
3793 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003794}
qs.xiong97fa59b2022-04-07 05:41:29 -04003795
you.chen35020192022-05-06 11:30:57 +08003796int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3797{
3798 int count, net_no, index;
3799 int net_no_list[128];
3800 lynq_wifi_auth_s net_auth;
3801 char lynq_remove_cmd[MAX_CMD];
3802
qs.xiong9fbf74e2023-03-28 13:38:22 +08003803 if (ssid == NULL || *ssid == '\0')
3804 {
3805 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003806 return -1;
3807 }
3808
3809 CHECK_IDX(idx, CTRL_STA);
3810
3811 CHECK_WPA_CTRL(CTRL_STA);
3812
3813 net_no = -1;
3814 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3815
qs.xiong9fbf74e2023-03-28 13:38:22 +08003816 for (index=0; index < count; index++)
3817 {
you.chen35020192022-05-06 11:30:57 +08003818 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003819 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3820 {
you.chen35020192022-05-06 11:30:57 +08003821 net_no = net_no_list[index];
3822 break;
3823 }
3824 }
3825
qs.xiong9fbf74e2023-03-28 13:38:22 +08003826 if (net_no < 0)
3827 {
you.chen35020192022-05-06 11:30:57 +08003828 return 0;
3829 }
3830
3831 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3832
3833 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3834 DO_OK_FAIL_REQUEST(cmd_save_config);
3835
3836 return 0;
3837}
3838
3839int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003840{
you.chend2fef3f2023-02-13 10:50:35 +08003841 int count, index;
you.chen35020192022-05-06 11:30:57 +08003842 int net_no_list[128];
3843 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003844 RLOGD("enter lynq_get_sta_saved_ap api\n");
3845 if (list == NULL || len == NULL)
3846 {
3847 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003848 return -1;
3849 }
3850
3851 CHECK_IDX(idx, CTRL_STA);
3852
3853// CHECK_WPA_CTRL(CTRL_STA);
3854
3855 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003856 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003857
you.chen057aac42023-04-13 14:06:58 +08003858 if (count < 0)
3859 {
3860 RLOGE("list network fail");
3861 return count;
3862 }
3863 else if (count == 0)
3864 {
3865 *list = NULL;
3866 *len = 0;
3867 return 0;
3868 }
3869
you.chen35020192022-05-06 11:30:57 +08003870 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003871 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003872 *len = count;
3873
qs.xiong9fbf74e2023-03-28 13:38:22 +08003874 for (index=0; index < count; index++)
3875 {
you.chen35020192022-05-06 11:30:57 +08003876 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003877 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003878 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003879 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003880 {
you.chen35020192022-05-06 11:30:57 +08003881 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3882 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003883 else
you.chen057aac42023-04-13 14:06:58 +08003884 {
you.chen35020192022-05-06 11:30:57 +08003885 (*list)[index].base_info.band = -1;
3886 }
you.chen057aac42023-04-13 14:06:58 +08003887 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003888 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003889 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003890 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003891 return 0;
3892}
3893
3894int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3895{
qs.xiongc8d92a62023-03-29 17:36:14 +08003896 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003897 const char *lynq_scan_cmd = "SCAN";
3898
3899 CHECK_IDX(idx, CTRL_STA);
3900
3901 CHECK_WPA_CTRL(CTRL_STA);
3902
you.chen0df3e7e2023-05-10 15:56:26 +08003903 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3904 {
3905 RLOGD("tmp clear scanlist");
3906 system(clean_last_re);
3907 }
you.chen9ac66392022-08-06 17:01:16 +08003908 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003909 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003910 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3911 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003912 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003913 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3914 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003915 g_sta_scan_finish_flag = 1;
3916 return -1;
3917 }
you.chen35020192022-05-06 11:30:57 +08003918
3919 return 0;
3920}
3921
3922int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003923 if (cb == NULL)
3924 {
3925 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003926 return -1;
3927 }
3928
you.chen6d247052023-06-01 16:39:54 +08003929 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003930 g_ap_callback_priv = priv;
3931 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003932 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003933
you.chen6d247052023-06-01 16:39:54 +08003934 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003935 if (g_ap_watcher_pid == 0 )
3936 {
3937 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3938 {
3939 g_ap_watcher_pid = 0;
3940 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3941 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3942 return -1;
3943 }
3944 }
3945
3946 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3947 RLOGD("creat APWatcherTheradProc susccs");
3948
you.chen35020192022-05-06 11:30:57 +08003949 return 0;
3950}
3951
3952int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003953 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003954 if (g_ap_callback_priv == priv)
3955 {
you.chen35020192022-05-06 11:30:57 +08003956 g_ap_callback_func = NULL;
3957 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003958 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003959 return 0;
3960 }
you.chen6d247052023-06-01 16:39:54 +08003961 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003962 return -1;
3963}
3964
3965int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003966 if (cb == NULL)
3967 {
3968 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003969 return -1;
3970 }
3971
you.chen6d247052023-06-01 16:39:54 +08003972 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003973 g_sta_callback_priv = priv;
3974 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003975 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003976
you.chen6d247052023-06-01 16:39:54 +08003977 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003978 if (g_sta_watcher_pid == 0 ) {
3979 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3980 {
3981 g_sta_watcher_pid = 0;
3982 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3983 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3984 return -1;
3985 }
3986 }
3987
3988 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3989 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003990 return 0;
3991}
3992
3993int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003994 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003995 if (g_sta_callback_priv == priv)
3996 {
you.chen35020192022-05-06 11:30:57 +08003997 g_sta_callback_func = NULL;
3998 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003999 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004000 return 0;
4001 }
you.chen6d247052023-06-01 16:39:54 +08004002 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08004003 return -1;
4004}
4005
qs.xiongfcc914b2023-07-06 21:16:20 +08004006int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
4007 if (cb == NULL)
4008 {
4009 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
4010 return -1;
4011 }
4012 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4013 g_sta_auto_callback_priv = priv;
4014 g_sta_auto_callback_func = cb;
4015 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4016 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4017 if (g_sta_auto_watcher_pid == 0 ) {
4018 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4019 {
4020 g_sta_auto_watcher_pid = 0;
4021 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4022 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4023 return -1;
4024 }
4025 }
4026 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4027 RLOGD("creat STAWatcherTheradProc susccs");
4028 return 0;
4029}
4030int lynq_unreg_sta_auto_event_callback(void * priv) {
4031 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4032 if (g_sta_auto_callback_priv == priv)
4033 {
4034 g_sta_auto_watcher_stop_flag = 1;
4035 if (g_sta_auto_watcher_pid != 0)
4036 {
4037 pthread_join(g_sta_auto_watcher_pid, NULL);
4038 }
4039 g_sta_auto_watcher_pid = 0;
4040 g_sta_auto_callback_func = NULL;
4041 g_sta_auto_callback_priv = NULL;
4042 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4043 return 0;
4044 }
4045 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4046 return -1;
4047}
you.chen35020192022-05-06 11:30:57 +08004048int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4049{
4050 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004051 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004052 CHECK_IDX(idx, CTRL_AP);
4053
qs.xiong9fbf74e2023-03-28 13:38:22 +08004054 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4055 {
you.chen35020192022-05-06 11:30:57 +08004056 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4057 return 0;
4058 }
4059
qs.xiong9fbf74e2023-03-28 13:38:22 +08004060 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4061 {
you.chen35020192022-05-06 11:30:57 +08004062 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4063 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004064 else
4065 {
you.chen35020192022-05-06 11:30:57 +08004066 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4067 }
4068
4069 return 0;
4070}
4071
4072int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4073 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004074 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004075 CHECK_IDX(idx, CTRL_STA);
4076
qs.xiong9fbf74e2023-03-28 13:38:22 +08004077 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4078 {
you.chen35020192022-05-06 11:30:57 +08004079 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4080 return 0;
4081 }
4082
qs.xiong9fbf74e2023-03-28 13:38:22 +08004083 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4084 {
you.chen35020192022-05-06 11:30:57 +08004085 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4086 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004087 else
4088 {
you.chen35020192022-05-06 11:30:57 +08004089 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4090 }
4091
4092 return 0;
4093}
4094
4095int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4096// CHECK_IDX(idx, CTRL_AP);
4097// int ret = 0;
4098// size_t reply_len = MAX_RET;
4099// char cmd_reply[MAX_RET]={0};
4100// const char * cmd_str = "GET country";
4101// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4102// do{
4103// if (NULL == s_lynq_wpa_ctrl) {
4104// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4105// if (NULL == s_lynq_wpa_ctrl ) {
4106// printf("wpa_ctrl_open fail\n");
4107// return -1;
4108// }
4109// }
4110// }while(0);
4111
4112// do {
4113// reply_len = MAX_RET;
4114// cmd_reply[0] = '\0';
4115// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004116// 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 +08004117// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004118// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004119// return ret;
4120// }
4121// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004122// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004123// }while(0);
4124
4125 FILE *fp;
4126 size_t i = 0;
4127 char lynq_cmd_ret[MAX_RET]={0};
4128
4129// CHECK_IDX(idx, CTRL_AP);
4130
4131 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004132 {
4133 perror("popen error!");
4134 return -1;
4135 }
you.chen35020192022-05-06 11:30:57 +08004136 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4137 {
4138 perror("fread fail!");
4139 return -1;
4140 }
4141
qs.xiong9fbf74e2023-03-28 13:38:22 +08004142 for(i=0; i < strlen(lynq_cmd_ret); i++)
4143 {
4144 if (lynq_cmd_ret[i] == ' ')
4145 {
you.chen35020192022-05-06 11:30:57 +08004146 lynq_cmd_ret[i] = '\0';
4147 break;
4148 }
4149 }
4150
4151 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004152 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004153
4154 int ret=pclose(fp);
4155 if(ret==-1)
4156 {
4157 perror("close file faild");
4158 }
4159
4160 return 0;
4161}
4162
qs.xiong44fac672023-08-29 16:15:55 +08004163
you.chen705a7ef2023-06-01 22:06:45 +08004164static int check_and_init_uci_config(char * country_code)
4165{
4166 FILE * fp;
4167 int is_different = 0;
4168 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4169 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4170 const char * commit_uci_cmd ="uci commit";
4171 char set_country_cmd[MAX_CMD];
4172 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004173
you.chen705a7ef2023-06-01 22:06:45 +08004174 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004175
you.chen705a7ef2023-06-01 22:06:45 +08004176 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004177 {
you.chen705a7ef2023-06-01 22:06:45 +08004178 if (0 != system(create_uci_cmd))
4179 {
4180 RLOGE("creat_uci_cmd fail");
4181 return -1;
4182 }
4183 is_different = 1;
4184 }
4185
4186 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4187 {
4188 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004189 return -1;
4190 }
4191
you.chen705a7ef2023-06-01 22:06:45 +08004192 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004193 {
you.chen705a7ef2023-06-01 22:06:45 +08004194 RLOGE("fread fail!");
4195 fclose(fp);
4196 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004197 }
4198
you.chen705a7ef2023-06-01 22:06:45 +08004199 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4200 {
qs.xiong44fac672023-08-29 16:15:55 +08004201 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 +08004202 is_different = 1;
4203 }
4204
4205 fclose(fp);
4206
4207 if (is_different)
4208 {
4209 if ( 0 != system(set_country_cmd))
4210 {
4211 RLOGE("set_country_cmd fail");
4212 return -1;
4213 }
4214 if (0 != system(commit_uci_cmd))
4215 {
4216 RLOGE("commmit fail");
4217 }
4218 }
4219
4220 return is_different;
4221}
4222
4223int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4224 char check_current_code[10];
4225 const char * support_country[] = {"CN", "EU"};
4226
4227 int ret,is_different, i, cc_count;
4228
4229 if (country_code == NULL || country_code[0] == '\0')
4230 {
4231 RLOGE("bad country code\n");
4232 return -1;
4233 }
4234
4235 cc_count = sizeof (support_country) / sizeof (char*);
4236 for(i=0; i < cc_count; i++)
4237 {
4238 if (strcmp(support_country[i], country_code) == 0)
4239 {
4240 break;
4241 }
4242 }
4243
4244 if (i >= cc_count)
4245 {
4246 RLOGE("unspported country code %s\n", country_code);
4247 return -1;
4248 }
4249
4250 is_different = check_and_init_uci_config(country_code);
4251 if( is_different < 0 )
4252 {
4253 RLOGE("init set uci fail\n");
4254 return -1;
4255 }
4256
4257 ret = lynq_get_country_code(idx,check_current_code);
4258 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4259 {
4260 ret = lynq_wifi_disable();
4261 if(ret != 0 )
4262 {
4263 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4264 return -1;
4265 }
4266 }
4267
4268 return 0;
you.chen35020192022-05-06 11:30:57 +08004269}
4270
4271int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4272{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004273 RLOGD("enter lynq_get_connect_ap_mac\n");
4274 if (mac == NULL)
4275 {
4276 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004277 return -1;
4278 }
4279
4280 CHECK_IDX(idx, CTRL_STA);
4281 ap_info_s ap;
4282 ap.ap_mac[0] = '\0';
4283
qs.xiong9fbf74e2023-03-28 13:38:22 +08004284 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4285 {
you.chen35020192022-05-06 11:30:57 +08004286 return -1;
4287 }
4288 strcpy(mac, ap.ap_mac);
4289
4290 return 0;
4291}
4292
4293int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4294{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004295 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004296 struct ifaddrs *ifaddr_header, *ifaddr;
4297 struct in_addr * ifa;
4298 const char * ifaName = "wlan0";
4299 if (ip == NULL)
4300 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004301 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004302 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004303 }
you.chenf58b3c92022-06-21 16:53:48 +08004304
qs.xiong9fbf74e2023-03-28 13:38:22 +08004305 if (idx == 1)
4306 {
you.chen0df3e7e2023-05-10 15:56:26 +08004307 ifaName = inner_get_ap_interface_name();
4308 if (ifaName == NULL)
4309 {
4310 RLOGE("[lynq_get_interface_ip] ap name get fail");
4311 return -1;
4312 }
you.chen9ac66392022-08-06 17:01:16 +08004313 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004314 else if (idx != 0)
4315 {
you.chen35020192022-05-06 11:30:57 +08004316 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004317 }
you.chen35020192022-05-06 11:30:57 +08004318
you.chen9ac66392022-08-06 17:01:16 +08004319 if (getifaddrs(&ifaddr_header) == -1)
4320 {
you.chen35020192022-05-06 11:30:57 +08004321 perror("getifaddrs");
4322 return -1;
4323 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004324 }
you.chen35020192022-05-06 11:30:57 +08004325
4326
you.chen9ac66392022-08-06 17:01:16 +08004327 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4328 {
4329 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004330 continue;
you.chen9ac66392022-08-06 17:01:16 +08004331 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4332 {
4333 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4334 {
4335 // is a valid IP4 Address
4336 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4337 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004338 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004339 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004340 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004341 return 0;
4342 }
4343 }
4344 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004345 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004346 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004347 return -1;
you.chen35020192022-05-06 11:30:57 +08004348}
4349
4350int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4351{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004352 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004353 int count;
4354 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004355 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004356 char *split_words[128] = {0};
4357 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4358
4359 CHECK_WPA_CTRL(idx);
4360
4361 DO_REQUEST(lynq_get_mac_cmd);
4362
qs.xiong9fbf74e2023-03-28 13:38:22 +08004363 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4364 {
4365 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004366 return -1;
4367 }
4368
4369 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4370
qs.xiong9fbf74e2023-03-28 13:38:22 +08004371 if (count < 2)
4372 {
you.chen35020192022-05-06 11:30:57 +08004373 return -1;
4374 }
4375
qs.xiong9fbf74e2023-03-28 13:38:22 +08004376 for (i=0; i < strlen(split_words[1]); i++ )
4377 {
4378 if (split_words[1][i] != ' ')
4379 {
you.chen35020192022-05-06 11:30:57 +08004380 break;
4381 }
4382 }
4383
qs.xiongdd6e44c2023-08-08 15:02:53 +08004384 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004385
4386 return 0;
4387}
4388
4389int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4390{
4391// int count;
4392// char *split_words[128] = {0};
4393// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4394
4395// if (rssi == NULL) {
4396// return -1;
4397// }
4398
4399// CHECK_IDX(idx, CTRL_STA);
4400
4401// CHECK_WPA_CTRL(CTRL_STA);
4402
4403// DO_REQUEST(lynq_get_rssi_cmd);
4404
4405// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4406// return -1;
4407// }
4408
4409// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4410
4411// if (count < 2) {
4412// return -1;
4413// }
4414
4415// *rssi = atoi(split_words[1]) * -1;
4416
you.chen35020192022-05-06 11:30:57 +08004417 char lynq_cmd_ret[MAX_RET]={0};
4418
qs.xiongff0ae0f2022-10-11 15:47:14 +08004419/*******change other cmd to get rssi*******
4420 *
4421 *wl rssi ---> wl -i wlan0 rssi
4422 *
4423 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004424 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004425 {
you.chen23c4a5f2023-04-12 16:46:00 +08004426 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004427 return -1;
4428 }
you.chen9f17e4d2022-06-06 17:18:18 +08004429 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004430/****** if got rssi is 0,means sta didn't connected any device****/
4431 if(*rssi == 0)
4432 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004433 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004434 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004435 }
you.chen35020192022-05-06 11:30:57 +08004436
4437 return 0;
4438}
4439
4440int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4441{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004442 RLOGD("enter lynq_get_connect_ap_band\n");
4443 if (band == NULL)
4444 {
you.chen35020192022-05-06 11:30:57 +08004445 return -1;
4446 }
4447
4448 CHECK_IDX(idx, CTRL_STA);
4449 ap_info_s ap;
4450 ap.band = -1;
4451
qs.xiong9fbf74e2023-03-28 13:38:22 +08004452 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4453 {
you.chen35020192022-05-06 11:30:57 +08004454 return -1;
4455 }
4456 *band = ap.band;
4457
4458 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004459}
you.chenf58b3c92022-06-21 16:53:48 +08004460
4461int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4462{
you.chenb95401e2023-05-12 19:39:06 +08004463 int ret;
4464 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004465 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004466
4467 if (ip == NULL)
4468 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004469 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004470 return -1;
4471 }
4472
4473 CHECK_IDX(idx, CTRL_STA);
4474
qs.xionge4cbf1c2023-02-28 18:22:49 +08004475 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004476 {
4477 return -1;
4478 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004479
you.chenb95401e2023-05-12 19:39:06 +08004480 ip[0] = '\0';
4481 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4482 if (ret != 0)
4483 {
4484 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4485 return -1;
4486 }
4487
4488 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4489 {
4490 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004491 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004492 if (ret != 0)
4493 {
4494 ip[0] = '\0';
4495 return 0;
4496 }
4497 else
4498 {
4499 p = strchr(ip, '\n');
4500 if (p != NULL)
4501 {
4502 *p = '\0';
4503 }
4504 }
4505 }
4506 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004507}
4508
qs.xiong026c5c72022-10-17 11:15:45 +08004509int lynq_ap_connect_num(int sta_number)
4510{
4511 char lynq_limit_cmd[32]={0};
4512 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004513 if((sta_number < 1 ) && (sta_number > 15))
4514 {
4515 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004516 return -1;
4517 }
4518 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4519 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004520 if(ret != 0)
4521 {
4522 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004523 }
4524 return 0;
4525}
you.chenf58b3c92022-06-21 16:53:48 +08004526
qs.xiong77905552022-10-17 11:19:57 +08004527int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4528{
4529
4530 char lynq_wifi_acs_cmd[128]={0};
4531 char lynq_cmd_mode[128]={0};
4532 char lynq_cmd_slect[128]={0};
4533
qs.xiong9fbf74e2023-03-28 13:38:22 +08004534 if((acs_mode != 2) && (acs_mode != 5))
4535 {
qs.xiong77905552022-10-17 11:19:57 +08004536 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4537 }
4538
qs.xiong9fbf74e2023-03-28 13:38:22 +08004539 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4540 {
qs.xiong77905552022-10-17 11:19:57 +08004541 return -1;
4542 }
4543
4544 CHECK_IDX(idx, CTRL_AP);
4545
4546 CHECK_WPA_CTRL(CTRL_AP);
4547
4548 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4549 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4550 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4551
4552 DO_OK_FAIL_REQUEST(cmd_disconnect);
4553 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4554 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4555 DO_OK_FAIL_REQUEST(cmd_save_config);
4556 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4557
4558 return 0;
4559}
you.chen0f5c6432022-11-07 18:31:14 +08004560//you.chen add for tv-box start
4561static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4562 FILE *fp;
4563 //printf("to exec cmd:%s\n", str_cmd);
4564 if((fp=popen(str_cmd,"r"))==NULL)
4565 {
4566 perror("popen error!");
4567 return -1;
4568 }
4569 if((fread(str_cmd_ret,max_len,1,fp))<0)
4570 {
4571 perror("fread fail!");
4572 fclose(fp);
4573 return -1;
4574 }
4575 fclose(fp);
4576 return 0;
4577}
4578
4579static int get_netmask_length(const char* mask)
4580{
4581 int masklen=0, i=0;
4582 int netmask=0;
4583
4584 if(mask == NULL)
4585 {
4586 return 0;
4587 }
4588
4589 struct in_addr ip_addr;
4590 if( inet_aton(mask, &ip_addr) )
4591 {
4592 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004593 }else
4594 {
you.chen0f5c6432022-11-07 18:31:14 +08004595 netmask = 0;
4596 return 0;
4597 }
4598
4599 while(0 == (netmask & 0x01) && i<32)
4600 {
4601 i++;
4602 netmask = netmask>>1;
4603 }
4604 masklen = 32-i;
4605 return masklen;
4606}
4607
4608static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4609 int mask_len;
4610 char *p;
4611 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004612 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4613 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004614 return -1;
4615 p = strstr(str_cmd_ret, "Mask:");
4616 if (p == NULL)
4617 return -1;
4618 mask_len = get_netmask_length(p + 5);
4619 if (mask_len == 0)
4620 return -1;
4621 p = strstr(str_cmd_ret, "inet addr:");
4622 if (p == NULL)
4623 return -1;
4624 strcpy(tmp, p + 10);
4625 p = strstr(tmp, " ");
4626 if (p != NULL)
4627 *p = '\0';
4628 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4629 return 0;
4630}
4631
4632static void GBWWatchThreadProc() {
4633 int i,n, nloop, nmax, ncheckcount, nidlecount;
4634 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4635 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4636 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4637 char *results[16] = {0};
4638 char str_cmd[256] = {0};
4639 char str_cmd_ret[128] = {0};
4640 char dest_ip[32] = {0};
4641 lastAP1Bytes = lastAP2Bytes = 0;
4642 lastAP1Drop = lastAP2Drop = 0;
4643 lastAP1Speed = lastAP2Speed = 0;
4644 setAP1Speed = 50;
4645 setAP2Speed = 80;
4646 nloop = 0;
4647 nmax = 6;
4648 ncheckcount = nidlecount = 0;
4649
you.chen0df3e7e2023-05-10 15:56:26 +08004650 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004651 {
4652 RLOGE("------gbw thread run\n");
4653 return;
4654 }
4655
qs.xiong9fbf74e2023-03-28 13:38:22 +08004656 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004657 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4658 while (dest_ip[0] == '\0') {
4659 sleep(1);
4660 str_cmd_ret[0] = '\0';
4661 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4662 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4663 if (str_cmd_ret[n] == '\n'){
4664 str_cmd_ret[n] = '\0';
4665 break;
4666 }
4667 }
4668 if (str_cmd_ret[0] != '\0')
4669 {
4670 strcpy(dest_ip, str_cmd_ret);
4671 }
4672 }
4673
you.chenc9928582023-04-24 15:39:37 +08004674 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4675 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4676 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 +08004677 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4678 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004679 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004680 return;
4681 }
you.chenc9928582023-04-24 15:39:37 +08004682 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);
4683 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);
4684 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 +08004685
4686 while (1) {
4687 sleep(1);
4688 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004689 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4690 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4691 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004692 continue;
4693 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004694 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004695 if (n > 9) {
4696 if (strcmp(results[1], "Sent") == 0) {
4697 currAP1Bytes = atoll(results[2]);
4698 }
4699 if (strcmp(results[6], "(dropped") == 0) {
4700 currAP1Drop = atoi(results[7]);
4701 }
4702 }
4703
4704 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004705 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4706 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4707 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004708 continue;
4709 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004710 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004711 if (n > 9) {
4712 if (strcmp(results[1], "Sent") == 0) {
4713 currAP2Bytes = atoll(results[2]);
4714 }
4715 if (strcmp(results[6], "(dropped") == 0) {
4716 currAP2Drop = atoi(results[7]);
4717 }
4718 }
4719
4720 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4721 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4722 lastAP1Bytes = currAP1Bytes;
4723 lastAP2Bytes = currAP2Bytes;
4724 continue;
4725 }
4726
4727 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4728 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4729 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4730 lastAP1Speed = currAP1Speed;
4731 lastAP2Speed = currAP2Speed;
4732 lastAP1Bytes = currAP1Bytes;
4733 lastAP2Bytes = currAP2Bytes;
4734
4735 currSetAP1Speed = setAP1Speed;
4736 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4737 ncheckcount++;
4738 if (ncheckcount > 3) {
4739 ncheckcount = 0;
4740 currSetAP1Speed = 5;
4741 }
4742 }
4743 else {
4744 ncheckcount = 0;
4745 if (currAP1Speed < 5)
4746 nidlecount++;
4747 else
4748 nidlecount = 0;
4749
4750 }
4751
4752 if (nidlecount > 60 ){
4753 currSetAP1Speed = 50;
4754 }
4755
4756 if (currSetAP1Speed != setAP1Speed) {
4757 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004758 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4759 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004760 }
4761 }
4762}
4763
4764int enableGBW(const char* mac) {
4765 int i,len;
4766 char get_ipaddr_cmd[128]={0};
4767 ap_info_s *ap;
4768 device_info_s * list;
4769
4770 if (mac == NULL || g_gbw_enabled == 1)
4771 return -1;
4772 len = strlen(mac);
4773 g_gbw_mac = malloc(len + 1);
4774 for(i=0;i<len;i++) {
4775 if (mac[i] >= 'A' && mac[i] <= 'Z')
4776 {
4777 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4778 }
4779 else
4780 g_gbw_mac[i] = mac[i];
4781 }
4782 g_gbw_mac[i] = '\0';
4783 g_gbw_enabled = 1;
4784
4785 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4786 if (system(get_ipaddr_cmd) == 0) {
4787 //startGBW();
4788 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4789 for (i=0;i<len;i++) {
4790 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4791 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4792 startGBW();
4793 }
4794 free(ap);
4795 free(list);
4796 }
4797 }
4798 return 0;
4799}
4800
4801int disableGBW() {
4802 stopGBW();
4803 free(g_gbw_mac);
4804 g_gbw_mac = NULL;
4805 g_gbw_enabled = 1;
4806 return 0;
4807}
4808
4809static int startGBW() {
4810 if (g_gbw_watcher_pid != 0) {
4811 stopGBW();
4812 }
4813 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4814}
4815
4816static int stopGBW() {
4817 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004818 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004819 pthread_cancel(g_gbw_watcher_pid);
4820 pthread_join(g_gbw_watcher_pid, &retval);
4821 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004822 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4823 if (s_ap_iterface_name[0] != '\0')
4824 {
4825 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4826 system(cmd);
4827 }
you.chen0f5c6432022-11-07 18:31:14 +08004828}
4829//you.chen add for tv-box end