blob: 9d8812b99fb477011f72983a55715291bdac8cce [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;
1602 }
1603 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001604 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001605 herror(NULL);
1606 return -1;
1607 }
1608
1609 strcpy(hostname, ht->h_name);
1610
1611 return 0;
1612}
1613
1614static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1615{
1616 int count, index, words_count;
1617 char * split_lines[128]= {0};
1618 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001619 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001620 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001621 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001622
1623 CHECK_WPA_CTRL(ap_sta);
1624
1625 DO_REQUEST(lynq_wifi_list_networks);
1626
1627 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1628
1629 //@todo check ssid field to compatible
1630
1631 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001632 for(index=1; index < count; index++)
1633 {
you.chen35020192022-05-06 11:30:57 +08001634 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001635 if (words_count > 2)
1636 {
you.chend2fef3f2023-02-13 10:50:35 +08001637 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001638 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1639 {
you.chen35020192022-05-06 11:30:57 +08001640 net_no_list[ret++] = atoi(split_words[0]);
1641 }
1642 }
1643 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001644 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001645 return ret;
1646}
1647
1648static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001649 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001650 CHECK_WPA_CTRL(ap_sta);
1651 const char *lynq_wifi_add_network = "ADD_NETWORK";
1652
qs.xiong9fbf74e2023-03-28 13:38:22 +08001653 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001654 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001655 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1656 {
1657 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001658 return -1;
1659 }
1660
qs.xiong9fbf74e2023-03-28 13:38:22 +08001661 for(i=0;i<reply_len;i++)
1662 {
1663 if(cmd_reply[i] == '\n')
1664 {
you.chen35020192022-05-06 11:30:57 +08001665 cmd_reply[i] = '\0';
1666 break;
1667 }
1668 }
1669 return atoi(cmd_reply);
1670}
you.chena6cd55a2022-05-08 12:20:18 +08001671
you.chen35020192022-05-06 11:30:57 +08001672static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1673{
1674 int count, index;
1675 int net_no_list[128];
1676
qs.xiong9fbf74e2023-03-28 13:38:22 +08001677 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001678 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001679 for (index=0; index < count; index++)
1680 {
1681 if (net_no_list[index] == net_no)
1682 {
you.chen35020192022-05-06 11:30:57 +08001683 return 0;
1684 }
1685 }
1686
1687 if (count >= 1)
1688 index = net_no_list[count - 1];
1689 else
1690 index = -1;
1691
qs.xiong9fbf74e2023-03-28 13:38:22 +08001692 while (index < net_no )
1693 {
you.chen35020192022-05-06 11:30:57 +08001694 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001695 if (index >= net_no)
1696 { // required network no created
1697 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001698 return 0;
1699 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001700 else if( index < 0)
1701 {
1702 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001703 return -1;
1704 }
you.chen35020192022-05-06 11:30:57 +08001705 }
1706
1707 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001708 {
1709 RLOGE("[lynq_check_network_number] network index < 0");
1710 return -1;
1711 }
1712 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001713 return 0;
1714}
1715
1716static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001717 if (freq > 5000 && freq < 6000)
1718 {
you.chen35020192022-05-06 11:30:57 +08001719 return LYNQ_WIFI_5G_band;
1720 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001721 else if (freq > 2000 && freq < 3000)
1722 {
you.chen35020192022-05-06 11:30:57 +08001723 return LYNQ_WIFI_2G_band;
1724 }
1725 return LYNQ_WIFI_2_and_5G_band;
1726}
1727
1728static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001729 if (key_mgmt != NULL)
1730 {
1731 if (memcmp( key_mgmt, "NONE", 4) == 0)
1732 {
you.chen35020192022-05-06 11:30:57 +08001733 return LYNQ_WIFI_AUTH_OPEN;
1734 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001735 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1736 {
you.chen35020192022-05-06 11:30:57 +08001737 return LYNQ_WIFI_AUTH_WEP;
1738 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001739 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1740 {
you.chen35020192022-05-06 11:30:57 +08001741 return LYNQ_WIFI_AUTH_WPA_PSK;
1742 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001743 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1744 {
you.chen35020192022-05-06 11:30:57 +08001745 return LYNQ_WIFI_AUTH_WPA2_PSK;
1746 }
1747 }
1748
1749 return -1;
1750}
1751
1752static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001753 if (flag != NULL)
1754 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001755 if ( strstr(flag,"WPA2-PSK+SAE-CCMP") != NULL || strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || ( strstr(flag,"SAE-H2E") != NULL && strstr(flag,"WPS") == NULL ) )
qs.xiong3e506812023-04-06 11:08:48 +08001756 {
1757 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001758 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001759 {
1760 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1761 }else if (strstr( flag, "WPA2-PSK") != NULL)
1762 {
you.chen35020192022-05-06 11:30:57 +08001763 return LYNQ_WIFI_AUTH_WPA2_PSK;
1764 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001766 {
you.chen35020192022-05-06 11:30:57 +08001767 return LYNQ_WIFI_AUTH_WPA_PSK;
1768 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001769 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001770 {
you.chen35020192022-05-06 11:30:57 +08001771 return LYNQ_WIFI_AUTH_WEP;
1772 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001773 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001774 {
you.chen35020192022-05-06 11:30:57 +08001775 return LYNQ_WIFI_AUTH_OPEN;
1776 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001778 {
you.chend2fef3f2023-02-13 10:50:35 +08001779 return LYNQ_WIFI_AUTH_OPEN;
1780 }
qs.xiong46f41562023-07-11 21:06:47 +08001781 else
1782 {
1783 RLOGD("convert_max_auth_from_flag not-found auth mode");
1784 }
you.chen35020192022-05-06 11:30:57 +08001785 }
1786
1787 return -1;
1788}
1789
1790static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1791 switch (bw) {
1792 case 10:
1793 return LYNQ_WIFI_BANDWIDTH_HT10;
1794 break;
1795 case 20:
1796 return LYNQ_WIFI_BANDWIDTH_HT20;
1797 break;
1798 case 40:
1799 return LYNQ_WIFI_BANDWIDTH_HT40;
1800 break;
1801 case 80:
1802 return LYNQ_WIFI_BANDWIDTH_HT80;
1803 break;
1804 default:
1805 break;
1806 }
1807
1808 return -1;
1809}
1810
you.chen70f377f2023-04-14 18:17:09 +08001811static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001812static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1813 int i, count;
1814 char *p;
1815 const char *lynq_status_cmd = "STATUS";
1816 const char * FLAG_SSID = "ssid=";
1817 const char * FLAG_SBSID = "bssid=";
1818 const char * FLAG_KEY_MGMT = "key_mgmt=";
1819 const char * FLAG_FREQ = "freq=";
1820 const char * FLAG_STATE = "wpa_state=";
1821 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001822 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001823 char *split_lines[128] = {0};
1824
1825 CHECK_WPA_CTRL(interface);
1826
qs.xiong9fbf74e2023-03-28 13:38:22 +08001827 if (curr_state == NULL)
1828 {
1829 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001830 return -1;
1831 }
1832
1833 DO_REQUEST(lynq_status_cmd);
1834
1835 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1836
1837 curr_state->net_no = -1;
1838 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 for(i=0; i < count; i++)
1840 {
1841 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001842 {
you.chen35020192022-05-06 11:30:57 +08001843 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001844 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001845 {
you.chend2fef3f2023-02-13 10:50:35 +08001846 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001847 ret = 0;
1848 continue;
1849 }
you.chenf58b3c92022-06-21 16:53:48 +08001850 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001851 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001852 {
you.chend2fef3f2023-02-13 10:50:35 +08001853 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 +08001854 ret = 0;
1855 continue;
1856 }
you.chen35020192022-05-06 11:30:57 +08001857 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001858 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001859 {
you.chen450d0172022-07-15 17:56:48 +08001860 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001861 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001862 ret = 0;
1863 continue;
1864 }
1865 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001866 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001867 {
you.chen35020192022-05-06 11:30:57 +08001868 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1869 ret = 0;
1870 continue;
1871 }
you.chend2fef3f2023-02-13 10:50:35 +08001872 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001873 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001874 {
you.chend2fef3f2023-02-13 10:50:35 +08001875 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1876 ret = 0;
1877 continue;
1878 }
you.chen35020192022-05-06 11:30:57 +08001879 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001880 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001881 {
you.chen35020192022-05-06 11:30:57 +08001882 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001883 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001884 {
you.chen35020192022-05-06 11:30:57 +08001885 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1886 ret = 0;
1887 continue;
1888 }
1889
1890 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001891 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001892 {
you.chen35020192022-05-06 11:30:57 +08001893 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001894 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001895 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001896 }
1897 }
1898
you.chen70f377f2023-04-14 18:17:09 +08001899 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1900 {
1901 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1902 }
1903
you.chen35020192022-05-06 11:30:57 +08001904 return ret;
1905}
1906
qs.xiongf1b525b2022-03-31 00:58:23 -04001907int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001908{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001909 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001910 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001911
qs.xiong9fbf74e2023-03-28 13:38:22 +08001912 if (ap_ssid == NULL)
1913 {
1914 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001915 return -1;
1916 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001917 else
1918 {
1919 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001920 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001921
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1923 {
1924 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001925 return -1;
1926 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001927
you.chen35020192022-05-06 11:30:57 +08001928 CHECK_IDX(idx, CTRL_AP);
1929
1930 CHECK_WPA_CTRL(CTRL_AP);
1931
1932 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1933
1934 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1935 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001936 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1937 return 0;
you.chen35020192022-05-06 11:30:57 +08001938
qs.xiong7a105ce2022-03-02 09:43:11 -05001939}
1940
you.chen35020192022-05-06 11:30:57 +08001941int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001942{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001943 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001944 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001945 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001946}
1947
qs.xiongc9c79f72022-10-17 15:27:18 +08001948/*****
1949 *frequency <------>channel
1950 *
1951 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1952 *
1953 *
1954 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1955 *
1956 *
1957 * */
1958static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001959 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};
1960 int i;
1961 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1962
qs.xiong69a332b2022-12-02 09:58:57 +08001963 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001964 {
1965 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001966 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001967 }
qs.xiongc00b6032022-11-29 16:28:03 +08001968
1969 if(i == arr_len)
1970 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001971 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001972 return -1;
1973 }
qs.xiongc00b6032022-11-29 16:28:03 +08001974
qs.xiongc9c79f72022-10-17 15:27:18 +08001975 return 0;
1976}
qs.xiong13673462023-02-21 19:12:54 +08001977
1978static int lynq_check_frequencyby_country_code(int input_frequency)
1979{
1980 char str_cnc[]="CN";
1981 char str_dest[20]="";
1982
1983 if( lynq_get_country_code(1,str_dest) != 0 )
1984 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001985 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001986 return -1;
1987 }
1988 if( strncmp(str_dest,str_cnc,2) != 0 )
1989 {
1990 return 0;
1991 }else if( 2473 < input_frequency && input_frequency < 5744)
1992 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001993 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001994 return -1;
1995 }
1996 return 0;
1997}
qs.xiongf1b525b2022-03-31 00:58:23 -04001998int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001999{
qs.xiongc00b6032022-11-29 16:28:03 +08002000 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08002001 char lynq_wifi_frequency_cmd[128]={0};
2002 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08002003 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08002005 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08002006 check = lynq_check_set_frequency(lynq_wifi_frequency);
2007 if(check != 0)
2008 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002009 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08002010 return -1;
you.chen35020192022-05-06 11:30:57 +08002011 }
qs.xiong13673462023-02-21 19:12:54 +08002012 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
2013 if(check != 0)
2014 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08002016 return -1;
2017 }
2018
qs.xiongc00b6032022-11-29 16:28:03 +08002019 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2020 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002021 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08002022 return -1;
2023 }
qs.xiong1af5daf2022-03-14 09:12:12 -04002024
you.chen35020192022-05-06 11:30:57 +08002025 CHECK_IDX(idx, CTRL_AP);
2026
2027 CHECK_WPA_CTRL(CTRL_AP);
2028
2029 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
2030 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2031 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2032
you.chen6c2dd9c2022-05-16 17:55:28 +08002033 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08002034 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
2035 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2036 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002037
qs.xiong9fbf74e2023-03-28 13:38:22 +08002038 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002039}
2040
qs.xiongf1b525b2022-03-31 00:58:23 -04002041int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05002042{
you.chen35020192022-05-06 11:30:57 +08002043 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002044 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08002045 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04002046
qs.xiong9fbf74e2023-03-28 13:38:22 +08002047 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
2048 {
2049 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08002050 return -1;
2051 }
2052 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04002053
qs.xiong9fbf74e2023-03-28 13:38:22 +08002054 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002055}
2056
qs.xiongf1b525b2022-03-31 00:58:23 -04002057int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2058{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002059 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002060 CHECK_IDX(idx, CTRL_AP);
2061 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002062 case LYNQ_WIFI_BANDWIDTH_HT10:
2063 {
2064 RLOGE("bandwith [%d] not support now\n", bandwidth);
2065 return -1;
2066 }
2067 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002068 {
2069 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2070 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002071 if (system(lynq_cmd_bandwith) != 0 )
2072 {
2073 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002074 return -1;
2075 }
2076 system("wl up");
2077 break;
2078 }
2079 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002080 {
qs.xiong10379192023-02-21 13:19:42 +08002081 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002082 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2083 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002084 if (system(lynq_cmd_bandwith) != 0 )
2085 {
2086 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002087 return -1;
2088 }
2089 system("wl up");
2090 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002091 }
you.chen35020192022-05-06 11:30:57 +08002092 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002093 {
qs.xiong10379192023-02-21 13:19:42 +08002094 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002095 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 }
2104 default:
you.chen35020192022-05-06 11:30:57 +08002105 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002106 RLOGE("auth type [%d] not support now\n", bandwidth);
2107 return -1;
you.chen35020192022-05-06 11:30:57 +08002108 }
2109 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002110
2111
you.chen35020192022-05-06 11:30:57 +08002112 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002113}
you.chen35020192022-05-06 11:30:57 +08002114
qs.xiongf1b525b2022-03-31 00:58:23 -04002115int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2116{
you.chen35020192022-05-06 11:30:57 +08002117 int count = 0;
2118 int index = 0;
2119 char *split_words[128] = {0};
2120 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002121 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002122 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002123
you.chen35020192022-05-06 11:30:57 +08002124 CHECK_WPA_CTRL(CTRL_AP);
2125
2126 DO_REQUEST(lynq_chanspec_cmd);
2127
2128 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2129 for(;index < count; index++) {
2130 if (strncmp(split_words[index], "bw", 2) != 0) {
2131 continue;
2132 }
2133
2134 index++;
2135 if (index >= count) {
2136 return -1;
2137 }
2138
qs.xiong9fbf74e2023-03-28 13:38:22 +08002139 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002140 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2141 return 0;
2142 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002143 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002144 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002145}
qs.xiong0fb469a2022-04-14 03:50:45 -04002146
qs.xiongf1b525b2022-03-31 00:58:23 -04002147int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002148{
you.chen35020192022-05-06 11:30:57 +08002149 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002150 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002151 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002152
you.chen35020192022-05-06 11:30:57 +08002153 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002154
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2156 {
you.chen35020192022-05-06 11:30:57 +08002157 return -1;
2158 }
2159
2160 system("wl down");
2161 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002162 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002163 return -1;
2164 }
2165 system("wl up");
2166 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002167}
qs.xiong0fb469a2022-04-14 03:50:45 -04002168
qs.xiongf1b525b2022-03-31 00:58:23 -04002169int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002170{
you.chen35020192022-05-06 11:30:57 +08002171 int count = 0;
2172 int index = 0;
2173 char *split_words[128] = {0};
2174 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002175 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002176 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002177
you.chen35020192022-05-06 11:30:57 +08002178 CHECK_WPA_CTRL(CTRL_AP);
2179
2180 DO_REQUEST(lynq_chanspec_cmd);
2181
2182 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002183 for(;index < count; index++)
2184 {
2185 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002186 if (strncmp(split_words[index], "channel", 2) != 0) {
2187 continue;
2188 }
2189
2190 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002191 if (index >= count)
2192 {
you.chen35020192022-05-06 11:30:57 +08002193 return -1;
2194 }
2195
2196 *channel = atoi(split_words[index]);
2197 return 0;
2198 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002199 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002200 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002201}
2202
2203
you.chen35020192022-05-06 11:30:57 +08002204int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002205{
you.chen6c2dd9c2022-05-16 17:55:28 +08002206 char ssid[MAX_CMD] = {0};
2207 int freq = 0;
2208 char lynq_auth_cmd[64]={0};
2209 char lynq_auth_alg_cmd[64]={0};
2210 char lynq_psk_cmd[64]={0};
2211 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002212 char lynq_ieee80211_cmd[64]={0};
2213 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002214 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002215 CHECK_IDX(idx, CTRL_AP);
2216
you.chen6c2dd9c2022-05-16 17:55:28 +08002217 CHECK_WPA_CTRL(CTRL_AP);
2218
qs.xiong9fbf74e2023-03-28 13:38:22 +08002219 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2220 {
2221 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002222 return -1;
2223 }
2224
you.chen92fd5d32022-05-25 10:09:47 +08002225 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002226 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002227 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002228 return 0;
2229 }
2230 else {
2231 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2232 ssid[0] = '\0';
2233 }
2234 lynq_wifi_ap_frequency_get(idx, &freq);
2235
2236 DO_OK_FAIL_REQUEST(cmd_disconnect);
2237 DO_OK_FAIL_REQUEST(cmd_remove_all);
2238 if (ssid[0] != '\0') {
2239 lynq_wifi_ap_ssid_set(idx, ssid);
2240 }
2241 if (freq != 0) {
2242 lynq_wifi_ap_frequency_set(idx, freq);
2243 }
2244 }
2245 }
you.chen35020192022-05-06 11:30:57 +08002246
qs.xiong9fbf74e2023-03-28 13:38:22 +08002247 switch(auth){
2248 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002249 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002250 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002251 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002252 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002253 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002254 break;
2255 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002256 case LYNQ_WIFI_AUTH_WEP:
2257 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002258 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002259 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002260 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002261 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2262
2263 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2264 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2265 break;
2266 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002267 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002268 {
2269 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2270 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2271 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2272
2273 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2274 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2275 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2276 break;
2277
2278 }
you.chen35020192022-05-06 11:30:57 +08002279 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002280 {
2281 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2282 {
you.chen35020192022-05-06 11:30:57 +08002283 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2284 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2285 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002286 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2287 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002288 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002289 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002290 }
2291// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2292// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2293 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002294
you.chen35020192022-05-06 11:30:57 +08002295 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2296 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2297 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002298 break;
2299 }
2300 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2301 {
2302 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2303 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2304 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2305 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2306
2307 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2308 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2309 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2310 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2311 break;
2312 }
2313 case LYNQ_WIFI_AUTH_WPA3_PSK:
2314 {
2315 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2316 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002317 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002318 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2319
2320 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2321 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2322 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2323 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2324 break;
2325 }
2326 default:
you.chen35020192022-05-06 11:30:57 +08002327 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002328 RLOGE("auth type [%d] not support now\n", auth);
2329 return -1;
you.chen35020192022-05-06 11:30:57 +08002330 }
2331 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002332 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002333
qs.xiong9fbf74e2023-03-28 13:38:22 +08002334 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002335}
2336
you.chen35020192022-05-06 11:30:57 +08002337int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002338{
you.chen35020192022-05-06 11:30:57 +08002339 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002340 char lynq_auth_alg_str[MAX_RET] = {0};
2341 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002342 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002343 CHECK_IDX(idx, CTRL_AP);
2344
qs.xiong9fbf74e2023-03-28 13:38:22 +08002345 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2346 {
2347 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002348 return -1;
2349 }
2350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2352 {
2353 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2354 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002355 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002356 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002357 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002358 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002359 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2360 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002361 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002362 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002363 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002364 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002365 else
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 }
you.chen35020192022-05-06 11:30:57 +08002371 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2373 {
2374 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2375 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002376 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002377 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002378 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002379 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2380 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002381 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002382 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002383 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002384 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002385 else
2386 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002387 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002388 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002389 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002390 }
you.chen35020192022-05-06 11:30:57 +08002391 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002392
2393 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2394 {
2395 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2396 return -1;
2397 }
2398
2399 if (memcmp(lynq_auth_str,"1",1) == 0 )
2400 {
2401 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2402 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002403 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002404 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2405 {
2406 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2407 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002408 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409 }
2410 else
2411 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002412 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002413 *auth = -1;
2414 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002415
you.chen6c2dd9c2022-05-16 17:55:28 +08002416 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002417}
qs.xiong1af5daf2022-03-14 09:12:12 -04002418
you.chenb95401e2023-05-12 19:39:06 +08002419static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2420{
2421 char status[64];
you.chencba13492023-05-19 13:53:43 +08002422 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002423 curr_status_info curr_state;
2424
2425 CHECK_WPA_CTRL(CTRL_AP);
2426
2427 memset(status, 0, sizeof (status));
2428
2429 curr_state.ap = NULL;
2430 curr_state.state = status;
2431
2432 printf("inner_check_ap_connected %d\n", retry_count);
2433 usleep(500*1000);
2434 if (0 == inner_get_status_info(idx, &curr_state))
2435 {
2436 if (strcmp(status, STATE_COMPLETED) == 0)
2437 {
2438 return 0;
2439 }
2440 else if (retry_count == 4) //not ok in 2s, do reconnect
2441 {
2442 DO_REQUEST("RECONNECT");
2443 return inner_check_ap_connected(idx, retry_count+1);
2444 }
you.chencba13492023-05-19 13:53:43 +08002445 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002446 {
2447 printf("retry 10 time\n");
2448 return -1;
2449 }
2450 else
2451 {
you.chen6d247052023-06-01 16:39:54 +08002452 if (strcmp(status, STATE_DISCONNECTED) == 0)
2453 {
2454 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2455 DO_REQUEST(LYNQ_WIFI_CMD);
2456 }
you.chenb95401e2023-05-12 19:39:06 +08002457 return inner_check_ap_connected(idx, retry_count+1);
2458 }
2459 }
2460 return -1;
2461}
qs.xiong1af5daf2022-03-14 09:12:12 -04002462
qs.xiongf1b525b2022-03-31 00:58:23 -04002463int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002464{
you.chen35020192022-05-06 11:30:57 +08002465 char LYNQ_WIFI_CMD[128]={0};
2466 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2467 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002468 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002469 CHECK_IDX(idx, CTRL_AP);
2470
2471 CHECK_WPA_CTRL(CTRL_AP);
2472
you.chen0df3e7e2023-05-10 15:56:26 +08002473 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002474 {
you.chen0df3e7e2023-05-10 15:56:26 +08002475 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002476 return -1;
2477 }
you.chen35020192022-05-06 11:30:57 +08002478
2479 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2480 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2481
2482 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2483 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2484
you.chenc9928582023-04-24 15:39:37 +08002485 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2486 if (ret != 0)
2487 {
2488 RLOGE("lynq_wifi_ap_start excute script fail");
2489 return -1;
2490 }
2491
you.chenb95401e2023-05-12 19:39:06 +08002492 if (inner_check_ap_connected(idx, 0) != 0)
2493 {
2494 return -1;
2495 }
2496
you.chen0df3e7e2023-05-10 15:56:26 +08002497 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002498 if (g_ap_tmp_watcher_pid == 0)
2499 {
2500 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2501 {
2502 g_ap_tmp_watcher_pid = 0;
2503 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2504 return -1;
2505 }
2506 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2507 }
you.chen0df3e7e2023-05-10 15:56:26 +08002508
qs.xiong9fbf74e2023-03-28 13:38:22 +08002509 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002510}
2511
qs.xiongf1b525b2022-03-31 00:58:23 -04002512int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002513{
you.chen35020192022-05-06 11:30:57 +08002514 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002515}
2516
qs.xiongf1b525b2022-03-31 00:58:23 -04002517int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002518{
you.chen35020192022-05-06 11:30:57 +08002519 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002520
you.chen35020192022-05-06 11:30:57 +08002521 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002522
you.chen35020192022-05-06 11:30:57 +08002523 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002524
you.chen35020192022-05-06 11:30:57 +08002525 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2526
2527 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2528
you.chenb4b121c2022-05-06 17:50:16 +08002529// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002530
2531 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2532 if (ret != 0)
2533 {
2534 RLOGE("lynq_wifi_ap_start excute script fail");
2535 return -1;
2536 }
qs.xiong44fac672023-08-29 16:15:55 +08002537 g_ap_tmp_watcher_stop_flag = 1;
2538 if (g_ap_tmp_watcher_pid != 0)
2539 pthread_join(g_ap_tmp_watcher_pid, NULL);
2540 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002541
qs.xiong9fbf74e2023-03-28 13:38:22 +08002542 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002543}
qs.xiong1af5daf2022-03-14 09:12:12 -04002544
qs.xiongf1b525b2022-03-31 00:58:23 -04002545int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002546{
you.chen35020192022-05-06 11:30:57 +08002547 char lynq_disable_cmd[128] = {0};
2548 char lynq_select_cmd[128] = {0};
2549 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002550 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002551 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002552
you.chen35020192022-05-06 11:30:57 +08002553 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002554 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2555 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2556
2557 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2558 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2559 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002560
qs.xiong9fbf74e2023-03-28 13:38:22 +08002561 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002562}
2563
qs.xiongf1b525b2022-03-31 00:58:23 -04002564int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002565{
you.chen35020192022-05-06 11:30:57 +08002566 char lynq_disable_cmd[128] = {0};
2567 char lynq_select_cmd[128] = {0};
2568 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002569 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002570 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002571
you.chen35020192022-05-06 11:30:57 +08002572 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002573
you.chen35020192022-05-06 11:30:57 +08002574 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2575 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2576
2577 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2578 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2579 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002580
qs.xiong9fbf74e2023-03-28 13:38:22 +08002581 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002582}
qs.xiongf1b525b2022-03-31 00:58:23 -04002583
you.chen35020192022-05-06 11:30:57 +08002584int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002585{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002586 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002587 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002588 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002589 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002590 RLOGD("enter lynq_ap_password_set");
2591 if( password == NULL )
2592 {
2593 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002594 return -1;
2595 }
2596 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002597 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002598 if(pass_len < 8 || pass_len >= 64)
2599 {
2600 RLOGE("[lynq_ap_password_set]input password len not in rage");
2601 return -1;
you.chen35020192022-05-06 11:30:57 +08002602 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002603
you.chen35020192022-05-06 11:30:57 +08002604 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002605
qs.xiong9fbf74e2023-03-28 13:38:22 +08002606 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2607 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002608 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002609 return -1;
2610 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002611 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2612 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002613 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2614 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002615 }
2616
you.chen35020192022-05-06 11:30:57 +08002617 CHECK_WPA_CTRL(CTRL_AP);
2618
qs.xiong9fbf74e2023-03-28 13:38:22 +08002619 if (auth == LYNQ_WIFI_AUTH_WEP)
2620 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002621 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002622 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2623 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2624 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2625 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2626 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002627 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2628 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002629 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 +08002630 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2631 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2632 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002633 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2634 {
2635
qs.xiongc8d92a62023-03-29 17:36:14 +08002636 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 +08002637 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002638 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002639 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2640 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2641
2642 }
2643 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002644 {
2645 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002646 return -1;
2647 }
you.chen35020192022-05-06 11:30:57 +08002648
you.chen35020192022-05-06 11:30:57 +08002649 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002650
qs.xiong9fbf74e2023-03-28 13:38:22 +08002651 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002652}
2653
you.chen35020192022-05-06 11:30:57 +08002654int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002655{
you.chen35020192022-05-06 11:30:57 +08002656 FILE * fp;
2657 int len, ret;
2658 int count, index;
2659 char *split_lines[128] = {0};
2660 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002661 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002662
you.chen35020192022-05-06 11:30:57 +08002663 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002664
you.chen35020192022-05-06 11:30:57 +08002665 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2666// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002667 if (NULL == fp)
2668 {
2669 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002670 return -1;
2671 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002672
you.chen35020192022-05-06 11:30:57 +08002673 buff = alloca(MAX_RET);
2674 fseek(fp, 0, SEEK_SET);
2675 len = fread(buff, 1, MAX_RET, fp);
2676 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002677
qs.xiong9fbf74e2023-03-28 13:38:22 +08002678 for(index=0; index < len; index ++)
2679 {
2680 if (memcmp(buff + index, "network={", 9) != 0)
2681 {
you.chen35020192022-05-06 11:30:57 +08002682 continue;
2683 }
2684 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002685 for (; index < len; index ++ )
2686 {
2687 if (buff[index] != '}')
2688 {
you.chen35020192022-05-06 11:30:57 +08002689 continue;
2690 }
2691 buff[index] = '\0';
2692 break;
2693 }
2694 len = buff + index - p;
2695 }
2696
2697 count = lynq_split(p, len, '\n', split_lines);
2698
2699 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002700 for(index=0; index < count; index++)
2701 {
you.chen35020192022-05-06 11:30:57 +08002702 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002703 if (p != NULL)
2704 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002705 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002706 if (*p == '\"')
2707 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002708 p++;
2709 }
you.chen35020192022-05-06 11:30:57 +08002710 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002711 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2712 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002713 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002714 if (*p == '\"')
2715 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002716 p++;
2717 }
2718 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002719 else
2720 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002721 continue;
you.chen35020192022-05-06 11:30:57 +08002722 }
2723
2724 strcpy(password, p);
2725
qs.xiong9fbf74e2023-03-28 13:38:22 +08002726 while(*password != '\0')
2727 {
2728 if (*password == '\"')
2729 {
you.chen35020192022-05-06 11:30:57 +08002730 *password = '\0';
2731 break;
2732 }
2733 password++;
2734 }
2735 ret = 0;
2736 break;
2737 } //end for(index=0; index < count; index++)
2738
2739 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002740}
2741
you.chen35020192022-05-06 11:30:57 +08002742static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2743 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002744 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002745
qs.xiong9fbf74e2023-03-28 13:38:22 +08002746 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2747 {
you.chen35020192022-05-06 11:30:57 +08002748 return -1;
2749 }
2750
2751 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002752
qs.xiong9fbf74e2023-03-28 13:38:22 +08002753 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2754 {
2755 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002756 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002757 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002758 {
you.chena6cd55a2022-05-08 12:20:18 +08002759 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002760 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002761 }
you.chen70f377f2023-04-14 18:17:09 +08002762 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2763 {
2764 return 0;
2765 }
you.chena6cd55a2022-05-08 12:20:18 +08002766 }
2767 }
you.chen70f377f2023-04-14 18:17:09 +08002768 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2769 {
2770 return 0;
2771 }
2772
qs.xiong9fbf74e2023-03-28 13:38:22 +08002773 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2774 {
you.chen70f377f2023-04-14 18:17:09 +08002775 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002776 return -1;
2777 }
2778 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2779 {
2780
you.chen70f377f2023-04-14 18:17:09 +08002781 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2782 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002783 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2784 {
2785
2786 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002787 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002788 }else
2789 {
you.chen70f377f2023-04-14 18:17:09 +08002790 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002791 *auth = -1;
2792 return -1;
2793 }
you.chen35020192022-05-06 11:30:57 +08002794 return 0;
2795}
2796
2797int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002798{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002799 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002800 int pass_len, net_no, count, index;
2801 char lynq_tmp_cmd[300]={0};
2802 int net_no_list[128];
2803 lynq_wifi_auth_s net_auth;
2804 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002805 if(pass_len < 8 || pass_len >= 64)
2806 {
2807 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002808 return -1;
2809 }
2810
2811 CHECK_IDX(idx, CTRL_STA);
2812
2813 net_no = -1;
2814 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2815
qs.xiong9fbf74e2023-03-28 13:38:22 +08002816 for (index=0; index < count; index++)
2817 {
you.chen35020192022-05-06 11:30:57 +08002818 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002819 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2820 {
you.chen35020192022-05-06 11:30:57 +08002821 net_no = net_no_list[index];
2822 break;
2823 }
2824 }
2825
qs.xiong9fbf74e2023-03-28 13:38:22 +08002826 if (net_no < 0)
2827 {
you.chen35020192022-05-06 11:30:57 +08002828 return -1;
2829 }
2830
2831 CHECK_WPA_CTRL(CTRL_STA);
2832
2833 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2834
2835 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2836 DO_OK_FAIL_REQUEST(cmd_save_config);
2837
2838 return 0;
2839}
2840
2841int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2842
2843 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002844 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002845 int count, index;
2846 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002847 char *buff, *p, *ssid, *ssid_end_flag;
2848 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002849 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002850
you.chen755332b2022-08-06 16:59:10 +08002851 network_len = 0;
2852 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002853 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002854
you.chen35020192022-05-06 11:30:57 +08002855 CHECK_IDX(idx, CTRL_STA);
2856
qs.xiong9fbf74e2023-03-28 13:38:22 +08002857 if (NULL == password)
2858 {
2859 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002860 return -1;
2861 }
2862
you.chen35020192022-05-06 11:30:57 +08002863 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002864 if (NULL == fp)
2865 {
2866 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002867 return -1;
2868 }
2869
you.chen6d247052023-06-01 16:39:54 +08002870 fseek(fp, 0, SEEK_END);
2871 len = ftell(fp);
2872 buff = malloc(len + 1);
2873
2874 if (buff == NULL)
2875 {
2876 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2877 return -1;
2878 }
2879
you.chen35020192022-05-06 11:30:57 +08002880 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002881 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002882 fclose(fp);
2883
qs.xiong9fbf74e2023-03-28 13:38:22 +08002884 for(index=0; index < len; index ++)
2885 {
2886 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002887 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002888 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002889 {
you.chen35020192022-05-06 11:30:57 +08002890 continue;
2891 }
you.chen6d247052023-06-01 16:39:54 +08002892 p = buff + index + 9;
2893 for (; index < len; index ++ )
2894 {
2895 if (buff[index] != '}')
2896 {
2897 continue;
2898 }
2899 buff[index] = '\0';
2900 break;
2901 }
2902 network_len = buff + index - p;
2903 break;
you.chen35020192022-05-06 11:30:57 +08002904 }
2905
qs.xiongb3f26af2023-02-17 18:41:07 +08002906 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002907 {
2908 if (buff != NULL)
2909 {
2910 free(buff);
2911 }
2912
qs.xiongb3f26af2023-02-17 18:41:07 +08002913 return -1;
you.chen6d247052023-06-01 16:39:54 +08002914 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002915
you.chend2fef3f2023-02-13 10:50:35 +08002916 ssid = strstr(p, "ssid=");
2917 if (ssid != NULL) {
2918 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002919 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002920 {
you.chend2fef3f2023-02-13 10:50:35 +08002921 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2922 break;
2923 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002924 else
you.chen6d247052023-06-01 16:39:54 +08002925 {
you.chend2fef3f2023-02-13 10:50:35 +08002926 ssid_end_flag = strstr(ssid, "\n");
2927 if (ssid_end_flag != NULL)
2928 {
2929 ssid_len = (ssid_end_flag - ssid) / 2;
2930 for(i=0; i<ssid_len; i++)
2931 {
2932 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2933 }
2934 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2935 break;
2936 }
2937 }
you.chen35020192022-05-06 11:30:57 +08002938 }
you.chend2fef3f2023-02-13 10:50:35 +08002939
you.chen35020192022-05-06 11:30:57 +08002940 }
2941
qs.xiong9fbf74e2023-03-28 13:38:22 +08002942 if (index >= len || NULL == p || network_len <= 0)
2943 {
you.chen6d247052023-06-01 16:39:54 +08002944 if (buff != NULL)
2945 {
2946 free(buff);
2947 }
you.chen35020192022-05-06 11:30:57 +08002948 return -1;
2949 }
2950
you.chen755332b2022-08-06 16:59:10 +08002951 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002952
2953 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002954 for(index=0; index < count; index++)
2955 {
you.chen35020192022-05-06 11:30:57 +08002956 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002957 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002958 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002959 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002960 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002961 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002962 p++;
2963 }
you.chen35020192022-05-06 11:30:57 +08002964 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002965 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002966 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002967 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002968 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002969 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002970 p++;
2971 }
2972 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002973 else
you.chen6d247052023-06-01 16:39:54 +08002974 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002975 continue;
you.chen35020192022-05-06 11:30:57 +08002976 }
2977
qs.xiong13673462023-02-21 19:12:54 +08002978 if (*p == '\"')
2979 p++;
2980 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002981
qs.xiong13673462023-02-21 19:12:54 +08002982 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002983 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002984 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002985 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002986 {
you.chen35020192022-05-06 11:30:57 +08002987 *password = '\0';
2988 break;
2989 }
2990 password++;
2991 }
2992 ret = 0;
2993 break;
2994 } //end for(index=0; index < count; index++)
2995
you.chen6d247052023-06-01 16:39:54 +08002996 if (buff != NULL)
2997 {
2998 free(buff);
2999 }
3000
you.chen35020192022-05-06 11:30:57 +08003001 return ret;
3002}
3003
3004static int inner_set_sta_ssid(int net_no, char *sta_ssid)
3005{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003006 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04003007
qs.xiong9fbf74e2023-03-28 13:38:22 +08003008 if (sta_ssid == NULL)
3009 {
3010 RLOGE("sta_ssid is null\n");
3011 return -1;
you.chen35020192022-05-06 11:30:57 +08003012 }
3013
qs.xiong9fbf74e2023-03-28 13:38:22 +08003014 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003015
3016 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
3017
3018 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
3019// DO_OK_FAIL_REQUEST(cmd_save_config);
3020
qs.xiong9fbf74e2023-03-28 13:38:22 +08003021 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003022
3023}
3024
you.chen35020192022-05-06 11:30:57 +08003025static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05003026{
you.chen35020192022-05-06 11:30:57 +08003027 char lynq_disable_cmd[128]={0};
3028 char lynq_select_cmd[128]={0};
3029
3030 CHECK_WPA_CTRL(CTRL_STA);
3031
qs.xiong9fbf74e2023-03-28 13:38:22 +08003032 if (save != 0)
3033 {
you.chenc29444e2022-06-07 18:01:16 +08003034 if (start_flag != 0)
3035 {
3036 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
3037 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3038 }
3039 else
3040 {
3041 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
3042 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3043 }
you.chen35020192022-05-06 11:30:57 +08003044 DO_OK_FAIL_REQUEST(cmd_save_config);
3045 }
3046
qs.xiong9fbf74e2023-03-28 13:38:22 +08003047 if (start_flag == 0)
3048 {
you.chen6c2dd9c2022-05-16 17:55:28 +08003049 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08003050 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
3051 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003052 else
3053 {
you.chen35020192022-05-06 11:30:57 +08003054 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
3055 DO_OK_FAIL_REQUEST(lynq_select_cmd);
3056 }
3057
3058 return 0;
3059}
3060
3061int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3062{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003063 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003064 CHECK_IDX(idx, CTRL_STA);
3065
you.chen6c2dd9c2022-05-16 17:55:28 +08003066 curr_status_info curr_state;
3067 ap_info_s ap_info;
3068 curr_state.ap = &ap_info;
3069 curr_state.state = NULL;
3070
qs.xiong9fbf74e2023-03-28 13:38:22 +08003071 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3072 {
you.chend2fef3f2023-02-13 10:50:35 +08003073 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003074 return 0;
3075 }
3076
3077 return -1;
you.chen35020192022-05-06 11:30:57 +08003078}
3079
3080int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3081{
you.chen9ac66392022-08-06 17:01:16 +08003082 scan_info_s *scan_list = NULL;
3083 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003084 int scan_len=0;
3085 int save_len=0;
3086 int best_index = -1;
3087 int best_scan_index = -1;
3088 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003089 int i, j, ret;
3090
3091 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003092
3093 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003094 if (info == NULL)
3095 {
you.chen35020192022-05-06 11:30:57 +08003096 return -1;
3097 }
3098
3099 curr_status_info curr_state;
3100 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003101 char status[64];
you.chen35020192022-05-06 11:30:57 +08003102
you.chen9ac66392022-08-06 17:01:16 +08003103 memset(&ap_info, 0, sizeof (ap_info));
3104 memset(status, 0, sizeof (status));
3105
3106 curr_state.ap = &ap_info;
3107 curr_state.state = status;
3108
qs.xiong9fbf74e2023-03-28 13:38:22 +08003109 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3110 {
you.chen35020192022-05-06 11:30:57 +08003111 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003112 if (strcmp(status, STATE_COMPLETED) == 0)
3113 {
3114 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3115 }
3116 else
3117 {
3118 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3119 }
you.chen593621d2023-04-27 17:52:44 +08003120 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003121 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003122 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003123 return 0;
3124 }
3125
you.chen9ac66392022-08-06 17:01:16 +08003126 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003127 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003128 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3129 {
you.chen9ac66392022-08-06 17:01:16 +08003130 if (NULL != scan_list)
3131 {
3132 free(scan_list);
3133 }
you.chen35020192022-05-06 11:30:57 +08003134 return -1;
3135 }
3136
qs.xiong9fbf74e2023-03-28 13:38:22 +08003137 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3138 {
you.chen9ac66392022-08-06 17:01:16 +08003139 if (NULL != scan_list)
3140 {
3141 free(scan_list);
3142 }
3143 if (NULL != save_list)
3144 {
3145 free(save_list);
3146 }
you.chen35020192022-05-06 11:30:57 +08003147 return -1;
3148 }
3149
qs.xiong9fbf74e2023-03-28 13:38:22 +08003150 for (i=0; i < save_len; i++)
3151 {
3152 for (j=0; j < scan_len; j++)
3153 {
you.chen35020192022-05-06 11:30:57 +08003154 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003155 && save_list[i].base_info.auth == scan_list[j].auth)
3156 {
3157 if (best_rssi == 0)
3158 {
you.chen9ac66392022-08-06 17:01:16 +08003159 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003160 best_rssi = scan_list[j].rssi;
3161 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003162 else if (best_rssi > scan_list[j].rssi)
3163 {
you.chen35020192022-05-06 11:30:57 +08003164 best_index = i;
3165 best_scan_index = j;
3166 best_rssi = scan_list[j].rssi;
3167 }
you.chend2fef3f2023-02-13 10:50:35 +08003168 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 +08003169 break;
3170 }
3171 }
3172 }
3173
qs.xiong9fbf74e2023-03-28 13:38:22 +08003174 if (best_index >= 0)
3175 {
you.chen35020192022-05-06 11:30:57 +08003176 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003177 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 +08003178 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3179 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003180 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003181 }
3182
you.chen9ac66392022-08-06 17:01:16 +08003183 if (NULL != scan_list)
3184 {
3185 free(scan_list);
3186 }
3187 if (NULL != save_list)
3188 {
3189 free(save_list);
3190 }
3191
3192 return ret;
you.chen35020192022-05-06 11:30:57 +08003193}
3194
3195static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3196{
qs.xiongc8d92a62023-03-29 17:36:14 +08003197 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003198 char lynq_ket_mgmt_cmd[64]={0};
3199 char lynq_pairwise_cmd[64]={0};
3200 char lynq_psk_cmd[64]={0};
3201
3202 CHECK_WPA_CTRL(CTRL_STA);
3203
qs.xiong9fbf74e2023-03-28 13:38:22 +08003204 switch(auth)
3205 {
3206 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003207 {
3208 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003209
you.chen35020192022-05-06 11:30:57 +08003210 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003211// DO_OK_FAIL_REQUEST(cmd_save_config);
3212 break;
3213 }
3214 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003215 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003216 {
3217 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3218 {
you.chen35020192022-05-06 11:30:57 +08003219 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3220 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003221 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3222 {
you.chena6cd55a2022-05-08 12:20:18 +08003223 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003224 }
3225 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3226 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003227
you.chen35020192022-05-06 11:30:57 +08003228 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3229 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3230 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003231
qs.xiong9fbf74e2023-03-28 13:38:22 +08003232 if (password != NULL)
3233 {
you.chen35020192022-05-06 11:30:57 +08003234 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3235 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003236 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003237 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003238
you.chen35020192022-05-06 11:30:57 +08003239// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003240 break;
3241 }
3242 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3243 {
qs.xiong3e506812023-04-06 11:08:48 +08003244 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003245 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003246 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3247 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3248
qs.xiong3e506812023-04-06 11:08:48 +08003249 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003250 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3251 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3252 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3253
3254 break;
3255 }
3256 case LYNQ_WIFI_AUTH_WPA3_PSK:
3257 {
qs.xiong3e506812023-04-06 11:08:48 +08003258 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003259 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003260 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3261 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3262
qs.xiong3e506812023-04-06 11:08:48 +08003263 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003264 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3265 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3266 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3267
3268 break;
3269 }
3270 default:
3271 return -1;
you.chen35020192022-05-06 11:30:57 +08003272 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003273
qs.xiong9fbf74e2023-03-28 13:38:22 +08003274 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003275}
qs.xiong7a105ce2022-03-02 09:43:11 -05003276
you.chen35020192022-05-06 11:30:57 +08003277static int inner_get_curr_net_no(int interface) {
3278 curr_status_info curr_state;
3279 curr_state.ap = NULL;
3280 curr_state.state = NULL;
3281
qs.xiong9fbf74e2023-03-28 13:38:22 +08003282 if (0 != inner_get_status_info(interface, &curr_state))
3283 {
you.chen35020192022-05-06 11:30:57 +08003284 return -1;
3285 }
3286
3287 return curr_state.net_no;
3288}
3289
3290int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003291{
you.chen35020192022-05-06 11:30:57 +08003292 int net_no;
3293 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003294
you.chen35020192022-05-06 11:30:57 +08003295 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003296
qs.xiong9fbf74e2023-03-28 13:38:22 +08003297 if (net_no < 0)
3298 {
you.chen35020192022-05-06 11:30:57 +08003299 return -1;
3300 }
3301
3302 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003303}
3304
you.chenb95401e2023-05-12 19:39:06 +08003305int 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 -05003306{
you.chen35020192022-05-06 11:30:57 +08003307 int count, net_no, index;
3308 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003309 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003310 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003311 curr_status_info curr_state;
3312 ap_info_s ap_info;
3313 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003314
qs.xiong9fbf74e2023-03-28 13:38:22 +08003315 if (ssid == NULL || *ssid == '\0')
3316 {
3317 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003318 return -1;
3319 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003320
qs.xiong9fbf74e2023-03-28 13:38:22 +08003321 if (LYNQ_WIFI_AUTH_OPEN != auth)
3322 {
3323 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003324 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003325 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003326 return -1;
3327 }
3328 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003329
you.chen70f377f2023-04-14 18:17:09 +08003330
3331 pthread_mutex_lock(&s_global_check_mutex);
3332 if (s_sta_status != INNER_STA_STATUS_INIT)
3333 {
3334 s_sta_status = INNER_STA_STATUS_CANCEL;
3335 pthread_cond_signal(&s_global_check_cond);
3336 }
3337 pthread_mutex_unlock(&s_global_check_mutex);
3338
you.chen35020192022-05-06 11:30:57 +08003339 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003340 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003341
3342 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003343 memset(&ap_info, 0, sizeof (ap_info));
3344 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003345
you.chen70f377f2023-04-14 18:17:09 +08003346 curr_state.ap = &ap_info;
3347 curr_state.state = status;
3348
3349 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003350 {
you.chen70f377f2023-04-14 18:17:09 +08003351 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3352 {
3353 net_no = curr_state.net_no;
3354 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3355 && strcmp(ap_info.psw, psw) == 0)
3356 {
3357 RLOGD("already connected\n");
3358
3359 pthread_mutex_lock(&s_global_check_mutex);
3360 s_sta_status = INNER_STA_STATUS_CONNECTED;
3361 pthread_cond_signal(&s_global_check_cond);
3362 pthread_mutex_unlock(&s_global_check_mutex);
3363 return 0;
3364 }
you.chen35020192022-05-06 11:30:57 +08003365 }
3366 }
3367
you.chen70f377f2023-04-14 18:17:09 +08003368 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003369 {
you.chen70f377f2023-04-14 18:17:09 +08003370 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3371
3372 for (index=0; index < count; index++)
3373 {
3374 net_auth = -1;
3375 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3376 {
3377 net_no = net_no_list[index];
3378 break;
3379 }
you.chen35020192022-05-06 11:30:57 +08003380 }
3381
you.chen70f377f2023-04-14 18:17:09 +08003382 if (net_no < 0)
3383 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003384 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3385 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3386 {
3387 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3388 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3389 DO_OK_FAIL_REQUEST(rm_net_cmd);
3390 }
you.chen70f377f2023-04-14 18:17:09 +08003391 net_no = lynq_add_network(CTRL_STA);
3392 if (net_no == -1)
3393 {
3394 return -1;
3395 }
3396
3397 RLOGD("net no is %d\n", net_no);
3398 if (0 != inner_set_sta_ssid(net_no, ssid))
3399 {
3400 return -1;
3401 }
you.chen35020192022-05-06 11:30:57 +08003402 }
3403 }
3404
qs.xiong9fbf74e2023-03-28 13:38:22 +08003405 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3406 {
you.chen35020192022-05-06 11:30:57 +08003407 return -1;
3408 }
3409
you.chen70f377f2023-04-14 18:17:09 +08003410
3411 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003412 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003413 usleep(200*1000);
3414
3415 ret = inner_sta_start_stop(net_no, 1, 1);
3416
3417 pthread_mutex_lock(&s_global_check_mutex);
3418 s_sta_status = INNER_STA_STATUS_CONNECTING;
3419 strcpy(s_sta_current_connecting_ssid, ssid);
3420 struct timeval now;
3421 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003422 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003423 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3424 pthread_cond_signal(&s_global_check_cond);
3425 pthread_mutex_unlock(&s_global_check_mutex);
3426 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003427}
3428
you.chenb95401e2023-05-12 19:39:06 +08003429int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3430{
3431 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3432}
3433
you.chen35020192022-05-06 11:30:57 +08003434int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003435{
you.chen35020192022-05-06 11:30:57 +08003436 ap_info_s ap;
3437 curr_status_info curr_state;
3438 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003439
qs.xiong9fbf74e2023-03-28 13:38:22 +08003440 if (ssid == NULL || *ssid == '\0')
3441 {
3442 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003443 return -1;
3444 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003445
you.chen35020192022-05-06 11:30:57 +08003446 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003447
you.chen35020192022-05-06 11:30:57 +08003448 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003449 curr_state.state = NULL;
3450
qs.xiong9fbf74e2023-03-28 13:38:22 +08003451 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3452 {
you.chen35020192022-05-06 11:30:57 +08003453 return 0;
3454 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003455
qs.xiong9fbf74e2023-03-28 13:38:22 +08003456 if (strcmp(ap.ap_ssid, ssid) != 0)
3457 {
you.chen35020192022-05-06 11:30:57 +08003458 return 0;
3459 }
3460
you.chen70f377f2023-04-14 18:17:09 +08003461 pthread_mutex_lock(&s_global_check_mutex);
3462 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3463 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003464 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003465}
qs.xiong97fa59b2022-04-07 05:41:29 -04003466
qs.xiongc93bf2b2023-08-25 10:22:08 +08003467int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3468{
3469 ap_info_s ap;
3470 curr_status_info curr_state;
3471 ap.ap_ssid[0] = '\0';
3472
3473 if (ssid == NULL || *ssid == '\0')
3474 {
3475 RLOGE("input ssid is NULL\n");
3476 return -1;
3477 }
3478
3479 CHECK_IDX(idx, CTRL_STA);
3480
3481
3482 curr_state.ap = &ap;
3483 curr_state.state = NULL;
3484
3485 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3486 {
3487 return 0;
3488 }
3489
3490 if (strcmp(ap.ap_ssid, ssid) != 0)
3491 {
3492 return 0;
3493 }
3494
3495 pthread_mutex_lock(&s_global_check_mutex);
3496 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3497 pthread_mutex_unlock(&s_global_check_mutex);
3498 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3499
3500}
3501
3502
you.chena6cd55a2022-05-08 12:20:18 +08003503int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3504{
qs.xiongad2f89d2023-01-18 13:17:41 +08003505// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3506// const char *lynq_reconnect_cmd = "RECONNECT";
3507 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3508 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3509// 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 -05003510
you.chen35020192022-05-06 11:30:57 +08003511 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003512 CHECK_WPA_CTRL(CTRL_STA);
3513
you.chenc9928582023-04-24 15:39:37 +08003514 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3515 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003516 {
you.chenc9928582023-04-24 15:39:37 +08003517 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003518 return -1;
3519 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003520
qs.xiongad2f89d2023-01-18 13:17:41 +08003521 system(lynq_enable_sta_cmd);
3522 system(lynq_reconnect_cmd);
3523// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003524 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003525}
3526
you.chen6d247052023-06-01 16:39:54 +08003527static int inner_get_status_info_state (int interface, char *state) {
3528 curr_status_info curr_state;
3529 curr_state.ap = NULL;
3530 curr_state.state = state;
3531 return inner_get_status_info(interface, &curr_state);
3532}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003533
3534int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3535{
3536
3537 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3538
3539 CHECK_IDX(idx, CTRL_STA);
3540 CHECK_WPA_CTRL(CTRL_STA);
3541
3542 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3543 if (ret != 0)
3544 {
3545 RLOGE("lynq_wifi_ap_start excute script fail");
3546 return -1;
3547 }
3548
3549// system(lynq_enable_sta_cmd);
3550 system(lynq_reconnect_cmd);
3551
3552 return 0;
3553}
3554
3555
you.chen35020192022-05-06 11:30:57 +08003556int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003557{
qs.xiongad2f89d2023-01-18 13:17:41 +08003558// char lynq_disable_network_cmd[MAX_CMD];
3559// curr_status_info curr_state;
3560// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003561 int i=0;
3562 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003563
you.chen6d247052023-06-01 16:39:54 +08003564// 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 +08003565 CHECK_IDX(idx, CTRL_STA);
3566 CHECK_WPA_CTRL(CTRL_STA);
3567
you.chen6d247052023-06-01 16:39:54 +08003568// system(lynq_disable_sta_cmd);
3569 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003570 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003571
3572 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3573 if (ret != 0)
3574 {
3575 RLOGE("lynq_wifi_ap_start excute script fail");
3576 return -1;
3577 }
3578
you.chen6d247052023-06-01 16:39:54 +08003579 for (i=0; i < 30; i++) // to check if sta is realy stoped
3580 {
3581 if (inner_get_status_info_state(idx, state) != 0)
3582 {
3583 break;
3584 }
3585
3586 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3587 {
3588 break;
3589 }
3590 RLOGD("lynq_wifi_ap_start curr state %s", state);
3591 usleep(SLEEP_TIME_ON_IDLE);
3592 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003593
you.chena6cd55a2022-05-08 12:20:18 +08003594 return 0;
3595// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003596}
qs.xiongfcc914b2023-07-06 21:16:20 +08003597int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3598{
3599 char LYNQ_DISABLE_CMD[128]={0};
3600 CHECK_IDX(idx, CTRL_STA);
3601 CHECK_WPA_CTRL(CTRL_STA);
3602 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3603 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3604 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3605 return 0;
3606}
qs.xiong7a105ce2022-03-02 09:43:11 -05003607
you.chen35020192022-05-06 11:30:57 +08003608//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3609// int i, count;
3610// char *p;
3611// const char * FLAG_SSID = "ssid=";
3612// const char * FLAG_SBSID = "bssid=";
3613// const char * FLAG_KEY_MGMT = "key_mgmt=";
3614// const char * FLAG_FREQ = "freq=";
3615// char lynq_sta_cmd[MAX_CMD];
3616// char *split_lines[128] = {0};
3617
3618// CHECK_WPA_CTRL(CTRL_AP);
3619
3620// sprintf(lynq_sta_cmd, "STA %s", bssid);
3621
3622// DO_REQUEST(lynq_sta_cmd);
3623
3624// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3625
3626// for(i=0; i < count; i++) {
3627// p = strstr(split_lines[i], FLAG_SSID);
3628// if (p != NULL) {
3629// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3630// continue;
3631// }
3632// }
3633
3634// lynq_get_interface_ip(idx, ap->ap_ip);
3635// lynq_ap_password_set(idx, ap->psw);
3636
3637// return 0;
3638//}
3639
3640static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3641 curr_status_info curr_state;
3642 curr_state.ap = ap;
3643 curr_state.state = NULL;
3644 return inner_get_status_info(interface, &curr_state);
3645}
3646
3647int 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 -04003648{
qs.xiong5071c802023-09-06 14:04:15 +08003649 RLOGD("[wifi]-----enter lynq_get_ap_device_list");
you.chend2fef3f2023-02-13 10:50:35 +08003650 int index, line_count;
3651 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003652 const char *lynq_first_sta_cmd = "STA-FIRST";
3653 char lynq_next_sta_cmd[MAX_CMD];
3654 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003655 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003656
you.chen35020192022-05-06 11:30:57 +08003657 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003658
you.chen35020192022-05-06 11:30:57 +08003659 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003660
you.chenb95401e2023-05-12 19:39:06 +08003661 // ap_info_s * tmp_ap;
3662 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003663 if (ap == NULL || list == NULL || len == NULL)
3664 {
3665 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003666 return -1;
3667 }
3668
you.chenb95401e2023-05-12 19:39:06 +08003669 // ap = &tmp_ap;
3670 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003671 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003672 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003673
you.chenb95401e2023-05-12 19:39:06 +08003674 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003675 {
you.chenb95401e2023-05-12 19:39:06 +08003676 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003677 return -1;
3678 }
3679
3680 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3681 lynq_ap_password_get(idx, (*ap)->psw);
3682
you.chen35020192022-05-06 11:30:57 +08003683 DO_REQUEST(lynq_first_sta_cmd);
3684
3685 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003686 while (reply_len > 0)
3687 {
3688 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003689 {
you.chen35020192022-05-06 11:30:57 +08003690 break;
3691 }
3692 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3693 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3694 strcpy(bssid[index], split_lines[0]);
3695 index++;
3696 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3697 reply_len = MAX_RET;
3698 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003699 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 +08003700 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003701 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003702 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003703 break;
3704 }
3705 }
3706
3707 *len = index;
3708
3709 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003710 for (index=0; index < *len; index++)
3711 {
you.chend2fef3f2023-02-13 10:50:35 +08003712 dev_info = &(*list)[index];
3713 memset(dev_info, 0, sizeof(device_info_s));
3714 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3715 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3716 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3717 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003718 free(bssid[index]);
3719 }
qs.xiong5071c802023-09-06 14:04:15 +08003720 RLOGD("[wifi]-----end lynq_get_ap_device_list");
you.chen35020192022-05-06 11:30:57 +08003721 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003722}
3723
you.chen35020192022-05-06 11:30:57 +08003724int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003725{
you.chen35020192022-05-06 11:30:57 +08003726 int i, count, index, count_words;
3727 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3728 char *split_lines[128] = {0};
3729 char *split_words[128] = {0};
3730 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003731
qs.xiong9fbf74e2023-03-28 13:38:22 +08003732 if (list == NULL || len == NULL)
3733 {
you.chen35020192022-05-06 11:30:57 +08003734 return -1;
3735 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003736
you.chen9ac66392022-08-06 17:01:16 +08003737 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3738 {
3739 usleep(100 * 1000);
3740 }
3741
you.chen35020192022-05-06 11:30:57 +08003742 CHECK_IDX(idx, CTRL_STA);
3743
3744 CHECK_WPA_CTRL(CTRL_STA);
3745
3746 DO_REQUEST(lynq_scan_result_cmd);
3747
3748 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3749 *len = count - 1;
3750 *list = malloc(sizeof (scan_info_s) * *len);
3751
3752 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003753 for (index=0; index <count_words; index++)
3754 {
3755 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003756 }
3757
qs.xiong9fbf74e2023-03-28 13:38:22 +08003758 for(index = 1;index < count; index++)
3759 {
3760 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003761 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003762 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3763 if (count_words < 4)
3764 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003765 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003766 //bssid / frequency / signal level / flags / ssid
3767 p = (*list) + index - 1;
3768 strcpy(p->mac, split_words[0]);
3769 p->band = convert_band_from_freq(atoi(split_words[1]));
3770 p->rssi = -1 * atoi( split_words[2]);
3771 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003772 if (count_words == 4) // ssid hided
3773 {
3774 p->ssid[0] = '\0';
3775 }
3776 else
3777 {
3778 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3779 }
you.chen35020192022-05-06 11:30:57 +08003780 }
3781
3782 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003783}
qs.xiong97fa59b2022-04-07 05:41:29 -04003784
you.chen35020192022-05-06 11:30:57 +08003785int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3786{
3787 int count, net_no, index;
3788 int net_no_list[128];
3789 lynq_wifi_auth_s net_auth;
3790 char lynq_remove_cmd[MAX_CMD];
3791
qs.xiong9fbf74e2023-03-28 13:38:22 +08003792 if (ssid == NULL || *ssid == '\0')
3793 {
3794 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003795 return -1;
3796 }
3797
3798 CHECK_IDX(idx, CTRL_STA);
3799
3800 CHECK_WPA_CTRL(CTRL_STA);
3801
3802 net_no = -1;
3803 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3804
qs.xiong9fbf74e2023-03-28 13:38:22 +08003805 for (index=0; index < count; index++)
3806 {
you.chen35020192022-05-06 11:30:57 +08003807 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003808 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3809 {
you.chen35020192022-05-06 11:30:57 +08003810 net_no = net_no_list[index];
3811 break;
3812 }
3813 }
3814
qs.xiong9fbf74e2023-03-28 13:38:22 +08003815 if (net_no < 0)
3816 {
you.chen35020192022-05-06 11:30:57 +08003817 return 0;
3818 }
3819
3820 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3821
3822 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3823 DO_OK_FAIL_REQUEST(cmd_save_config);
3824
3825 return 0;
3826}
3827
3828int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003829{
you.chend2fef3f2023-02-13 10:50:35 +08003830 int count, index;
you.chen35020192022-05-06 11:30:57 +08003831 int net_no_list[128];
3832 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003833 RLOGD("enter lynq_get_sta_saved_ap api\n");
3834 if (list == NULL || len == NULL)
3835 {
3836 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003837 return -1;
3838 }
3839
3840 CHECK_IDX(idx, CTRL_STA);
3841
3842// CHECK_WPA_CTRL(CTRL_STA);
3843
3844 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003845 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003846
you.chen057aac42023-04-13 14:06:58 +08003847 if (count < 0)
3848 {
3849 RLOGE("list network fail");
3850 return count;
3851 }
3852 else if (count == 0)
3853 {
3854 *list = NULL;
3855 *len = 0;
3856 return 0;
3857 }
3858
you.chen35020192022-05-06 11:30:57 +08003859 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003860 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003861 *len = count;
3862
qs.xiong9fbf74e2023-03-28 13:38:22 +08003863 for (index=0; index < count; index++)
3864 {
you.chen35020192022-05-06 11:30:57 +08003865 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003866 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003867 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003868 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003869 {
you.chen35020192022-05-06 11:30:57 +08003870 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3871 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003872 else
you.chen057aac42023-04-13 14:06:58 +08003873 {
you.chen35020192022-05-06 11:30:57 +08003874 (*list)[index].base_info.band = -1;
3875 }
you.chen057aac42023-04-13 14:06:58 +08003876 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003877 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003878 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003879 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003880 return 0;
3881}
3882
3883int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3884{
qs.xiongc8d92a62023-03-29 17:36:14 +08003885 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003886 const char *lynq_scan_cmd = "SCAN";
3887
3888 CHECK_IDX(idx, CTRL_STA);
3889
3890 CHECK_WPA_CTRL(CTRL_STA);
3891
you.chen0df3e7e2023-05-10 15:56:26 +08003892 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3893 {
3894 RLOGD("tmp clear scanlist");
3895 system(clean_last_re);
3896 }
you.chen9ac66392022-08-06 17:01:16 +08003897 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003898 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003899 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3900 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003901 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003902 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3903 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003904 g_sta_scan_finish_flag = 1;
3905 return -1;
3906 }
you.chen35020192022-05-06 11:30:57 +08003907
3908 return 0;
3909}
3910
3911int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003912 if (cb == NULL)
3913 {
3914 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003915 return -1;
3916 }
3917
you.chen6d247052023-06-01 16:39:54 +08003918 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003919 g_ap_callback_priv = priv;
3920 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003921 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003922
you.chen6d247052023-06-01 16:39:54 +08003923 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003924 if (g_ap_watcher_pid == 0 )
3925 {
3926 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3927 {
3928 g_ap_watcher_pid = 0;
3929 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3930 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3931 return -1;
3932 }
3933 }
3934
3935 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3936 RLOGD("creat APWatcherTheradProc susccs");
3937
you.chen35020192022-05-06 11:30:57 +08003938 return 0;
3939}
3940
3941int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003942 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003943 if (g_ap_callback_priv == priv)
3944 {
you.chen35020192022-05-06 11:30:57 +08003945 g_ap_callback_func = NULL;
3946 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003947 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003948 return 0;
3949 }
you.chen6d247052023-06-01 16:39:54 +08003950 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003951 return -1;
3952}
3953
3954int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003955 if (cb == NULL)
3956 {
3957 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003958 return -1;
3959 }
3960
you.chen6d247052023-06-01 16:39:54 +08003961 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003962 g_sta_callback_priv = priv;
3963 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003964 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003965
you.chen6d247052023-06-01 16:39:54 +08003966 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003967 if (g_sta_watcher_pid == 0 ) {
3968 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3969 {
3970 g_sta_watcher_pid = 0;
3971 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3972 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3973 return -1;
3974 }
3975 }
3976
3977 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3978 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003979 return 0;
3980}
3981
3982int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003983 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003984 if (g_sta_callback_priv == priv)
3985 {
you.chen35020192022-05-06 11:30:57 +08003986 g_sta_callback_func = NULL;
3987 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003988 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003989 return 0;
3990 }
you.chen6d247052023-06-01 16:39:54 +08003991 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003992 return -1;
3993}
3994
qs.xiongfcc914b2023-07-06 21:16:20 +08003995int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3996 if (cb == NULL)
3997 {
3998 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3999 return -1;
4000 }
4001 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4002 g_sta_auto_callback_priv = priv;
4003 g_sta_auto_callback_func = cb;
4004 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4005 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
4006 if (g_sta_auto_watcher_pid == 0 ) {
4007 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
4008 {
4009 g_sta_auto_watcher_pid = 0;
4010 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4011 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
4012 return -1;
4013 }
4014 }
4015 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
4016 RLOGD("creat STAWatcherTheradProc susccs");
4017 return 0;
4018}
4019int lynq_unreg_sta_auto_event_callback(void * priv) {
4020 pthread_mutex_lock(&s_sta_auto_callback_mutex);
4021 if (g_sta_auto_callback_priv == priv)
4022 {
4023 g_sta_auto_watcher_stop_flag = 1;
4024 if (g_sta_auto_watcher_pid != 0)
4025 {
4026 pthread_join(g_sta_auto_watcher_pid, NULL);
4027 }
4028 g_sta_auto_watcher_pid = 0;
4029 g_sta_auto_callback_func = NULL;
4030 g_sta_auto_callback_priv = NULL;
4031 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4032 return 0;
4033 }
4034 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
4035 return -1;
4036}
you.chen35020192022-05-06 11:30:57 +08004037int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
4038{
4039 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004040 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08004041 CHECK_IDX(idx, CTRL_AP);
4042
qs.xiong9fbf74e2023-03-28 13:38:22 +08004043 if (inner_get_status_info_state(CTRL_AP, state) != 0)
4044 {
you.chen35020192022-05-06 11:30:57 +08004045 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4046 return 0;
4047 }
4048
qs.xiong9fbf74e2023-03-28 13:38:22 +08004049 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4050 {
you.chen35020192022-05-06 11:30:57 +08004051 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
4052 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004053 else
4054 {
you.chen35020192022-05-06 11:30:57 +08004055 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
4056 }
4057
4058 return 0;
4059}
4060
4061int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4062 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004063 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004064 CHECK_IDX(idx, CTRL_STA);
4065
qs.xiong9fbf74e2023-03-28 13:38:22 +08004066 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4067 {
you.chen35020192022-05-06 11:30:57 +08004068 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4069 return 0;
4070 }
4071
qs.xiong9fbf74e2023-03-28 13:38:22 +08004072 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4073 {
you.chen35020192022-05-06 11:30:57 +08004074 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4075 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004076 else
4077 {
you.chen35020192022-05-06 11:30:57 +08004078 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4079 }
4080
4081 return 0;
4082}
4083
4084int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4085// CHECK_IDX(idx, CTRL_AP);
4086// int ret = 0;
4087// size_t reply_len = MAX_RET;
4088// char cmd_reply[MAX_RET]={0};
4089// const char * cmd_str = "GET country";
4090// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4091// do{
4092// if (NULL == s_lynq_wpa_ctrl) {
4093// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4094// if (NULL == s_lynq_wpa_ctrl ) {
4095// printf("wpa_ctrl_open fail\n");
4096// return -1;
4097// }
4098// }
4099// }while(0);
4100
4101// do {
4102// reply_len = MAX_RET;
4103// cmd_reply[0] = '\0';
4104// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004105// 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 +08004106// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004107// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004108// return ret;
4109// }
4110// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004111// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004112// }while(0);
4113
4114 FILE *fp;
4115 size_t i = 0;
4116 char lynq_cmd_ret[MAX_RET]={0};
4117
4118// CHECK_IDX(idx, CTRL_AP);
4119
4120 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004121 {
4122 perror("popen error!");
4123 return -1;
4124 }
you.chen35020192022-05-06 11:30:57 +08004125 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4126 {
4127 perror("fread fail!");
4128 return -1;
4129 }
4130
qs.xiong9fbf74e2023-03-28 13:38:22 +08004131 for(i=0; i < strlen(lynq_cmd_ret); i++)
4132 {
4133 if (lynq_cmd_ret[i] == ' ')
4134 {
you.chen35020192022-05-06 11:30:57 +08004135 lynq_cmd_ret[i] = '\0';
4136 break;
4137 }
4138 }
4139
4140 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004141 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004142
4143 int ret=pclose(fp);
4144 if(ret==-1)
4145 {
4146 perror("close file faild");
4147 }
4148
4149 return 0;
4150}
4151
qs.xiong44fac672023-08-29 16:15:55 +08004152
you.chen705a7ef2023-06-01 22:06:45 +08004153static int check_and_init_uci_config(char * country_code)
4154{
4155 FILE * fp;
4156 int is_different = 0;
4157 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4158 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4159 const char * commit_uci_cmd ="uci commit";
4160 char set_country_cmd[MAX_CMD];
4161 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004162
you.chen705a7ef2023-06-01 22:06:45 +08004163 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004164
you.chen705a7ef2023-06-01 22:06:45 +08004165 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004166 {
you.chen705a7ef2023-06-01 22:06:45 +08004167 if (0 != system(create_uci_cmd))
4168 {
4169 RLOGE("creat_uci_cmd fail");
4170 return -1;
4171 }
4172 is_different = 1;
4173 }
4174
4175 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4176 {
4177 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004178 return -1;
4179 }
4180
you.chen705a7ef2023-06-01 22:06:45 +08004181 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004182 {
you.chen705a7ef2023-06-01 22:06:45 +08004183 RLOGE("fread fail!");
4184 fclose(fp);
4185 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004186 }
4187
you.chen705a7ef2023-06-01 22:06:45 +08004188 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4189 {
qs.xiong44fac672023-08-29 16:15:55 +08004190 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 +08004191 is_different = 1;
4192 }
4193
4194 fclose(fp);
4195
4196 if (is_different)
4197 {
4198 if ( 0 != system(set_country_cmd))
4199 {
4200 RLOGE("set_country_cmd fail");
4201 return -1;
4202 }
4203 if (0 != system(commit_uci_cmd))
4204 {
4205 RLOGE("commmit fail");
4206 }
4207 }
4208
4209 return is_different;
4210}
4211
4212int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4213 char check_current_code[10];
4214 const char * support_country[] = {"CN", "EU"};
4215
4216 int ret,is_different, i, cc_count;
4217
4218 if (country_code == NULL || country_code[0] == '\0')
4219 {
4220 RLOGE("bad country code\n");
4221 return -1;
4222 }
4223
4224 cc_count = sizeof (support_country) / sizeof (char*);
4225 for(i=0; i < cc_count; i++)
4226 {
4227 if (strcmp(support_country[i], country_code) == 0)
4228 {
4229 break;
4230 }
4231 }
4232
4233 if (i >= cc_count)
4234 {
4235 RLOGE("unspported country code %s\n", country_code);
4236 return -1;
4237 }
4238
4239 is_different = check_and_init_uci_config(country_code);
4240 if( is_different < 0 )
4241 {
4242 RLOGE("init set uci fail\n");
4243 return -1;
4244 }
4245
4246 ret = lynq_get_country_code(idx,check_current_code);
4247 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4248 {
4249 ret = lynq_wifi_disable();
4250 if(ret != 0 )
4251 {
4252 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4253 return -1;
4254 }
4255 }
4256
4257 return 0;
you.chen35020192022-05-06 11:30:57 +08004258}
4259
4260int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4261{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004262 RLOGD("enter lynq_get_connect_ap_mac\n");
4263 if (mac == NULL)
4264 {
4265 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004266 return -1;
4267 }
4268
4269 CHECK_IDX(idx, CTRL_STA);
4270 ap_info_s ap;
4271 ap.ap_mac[0] = '\0';
4272
qs.xiong9fbf74e2023-03-28 13:38:22 +08004273 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4274 {
you.chen35020192022-05-06 11:30:57 +08004275 return -1;
4276 }
4277 strcpy(mac, ap.ap_mac);
4278
4279 return 0;
4280}
4281
4282int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4283{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004284 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004285 struct ifaddrs *ifaddr_header, *ifaddr;
4286 struct in_addr * ifa;
4287 const char * ifaName = "wlan0";
4288 if (ip == NULL)
4289 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004291 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004292 }
you.chenf58b3c92022-06-21 16:53:48 +08004293
qs.xiong9fbf74e2023-03-28 13:38:22 +08004294 if (idx == 1)
4295 {
you.chen0df3e7e2023-05-10 15:56:26 +08004296 ifaName = inner_get_ap_interface_name();
4297 if (ifaName == NULL)
4298 {
4299 RLOGE("[lynq_get_interface_ip] ap name get fail");
4300 return -1;
4301 }
you.chen9ac66392022-08-06 17:01:16 +08004302 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004303 else if (idx != 0)
4304 {
you.chen35020192022-05-06 11:30:57 +08004305 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004306 }
you.chen35020192022-05-06 11:30:57 +08004307
you.chen9ac66392022-08-06 17:01:16 +08004308 if (getifaddrs(&ifaddr_header) == -1)
4309 {
you.chen35020192022-05-06 11:30:57 +08004310 perror("getifaddrs");
4311 return -1;
4312 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004313 }
you.chen35020192022-05-06 11:30:57 +08004314
4315
you.chen9ac66392022-08-06 17:01:16 +08004316 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4317 {
4318 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004319 continue;
you.chen9ac66392022-08-06 17:01:16 +08004320 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4321 {
4322 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4323 {
4324 // is a valid IP4 Address
4325 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4326 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004327 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004328 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004329 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004330 return 0;
4331 }
4332 }
4333 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004334 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004335 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004336 return -1;
you.chen35020192022-05-06 11:30:57 +08004337}
4338
4339int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4340{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004341 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004342 int count;
4343 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004344 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004345 char *split_words[128] = {0};
4346 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4347
4348 CHECK_WPA_CTRL(idx);
4349
4350 DO_REQUEST(lynq_get_mac_cmd);
4351
qs.xiong9fbf74e2023-03-28 13:38:22 +08004352 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4353 {
4354 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004355 return -1;
4356 }
4357
4358 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4359
qs.xiong9fbf74e2023-03-28 13:38:22 +08004360 if (count < 2)
4361 {
you.chen35020192022-05-06 11:30:57 +08004362 return -1;
4363 }
4364
qs.xiong9fbf74e2023-03-28 13:38:22 +08004365 for (i=0; i < strlen(split_words[1]); i++ )
4366 {
4367 if (split_words[1][i] != ' ')
4368 {
you.chen35020192022-05-06 11:30:57 +08004369 break;
4370 }
4371 }
4372
qs.xiongdd6e44c2023-08-08 15:02:53 +08004373 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004374
4375 return 0;
4376}
4377
4378int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4379{
4380// int count;
4381// char *split_words[128] = {0};
4382// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4383
4384// if (rssi == NULL) {
4385// return -1;
4386// }
4387
4388// CHECK_IDX(idx, CTRL_STA);
4389
4390// CHECK_WPA_CTRL(CTRL_STA);
4391
4392// DO_REQUEST(lynq_get_rssi_cmd);
4393
4394// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4395// return -1;
4396// }
4397
4398// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4399
4400// if (count < 2) {
4401// return -1;
4402// }
4403
4404// *rssi = atoi(split_words[1]) * -1;
4405
you.chen35020192022-05-06 11:30:57 +08004406 char lynq_cmd_ret[MAX_RET]={0};
4407
qs.xiongff0ae0f2022-10-11 15:47:14 +08004408/*******change other cmd to get rssi*******
4409 *
4410 *wl rssi ---> wl -i wlan0 rssi
4411 *
4412 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004413 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004414 {
you.chen23c4a5f2023-04-12 16:46:00 +08004415 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004416 return -1;
4417 }
you.chen9f17e4d2022-06-06 17:18:18 +08004418 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004419/****** if got rssi is 0,means sta didn't connected any device****/
4420 if(*rssi == 0)
4421 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004422 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004423 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004424 }
you.chen35020192022-05-06 11:30:57 +08004425
4426 return 0;
4427}
4428
4429int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4430{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004431 RLOGD("enter lynq_get_connect_ap_band\n");
4432 if (band == NULL)
4433 {
you.chen35020192022-05-06 11:30:57 +08004434 return -1;
4435 }
4436
4437 CHECK_IDX(idx, CTRL_STA);
4438 ap_info_s ap;
4439 ap.band = -1;
4440
qs.xiong9fbf74e2023-03-28 13:38:22 +08004441 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4442 {
you.chen35020192022-05-06 11:30:57 +08004443 return -1;
4444 }
4445 *band = ap.band;
4446
4447 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004448}
you.chenf58b3c92022-06-21 16:53:48 +08004449
4450int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4451{
you.chenb95401e2023-05-12 19:39:06 +08004452 int ret;
4453 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004454 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004455
4456 if (ip == NULL)
4457 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004458 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004459 return -1;
4460 }
4461
4462 CHECK_IDX(idx, CTRL_STA);
4463
qs.xionge4cbf1c2023-02-28 18:22:49 +08004464 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004465 {
4466 return -1;
4467 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004468
you.chenb95401e2023-05-12 19:39:06 +08004469 ip[0] = '\0';
4470 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4471 if (ret != 0)
4472 {
4473 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4474 return -1;
4475 }
4476
4477 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4478 {
4479 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004480 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004481 if (ret != 0)
4482 {
4483 ip[0] = '\0';
4484 return 0;
4485 }
4486 else
4487 {
4488 p = strchr(ip, '\n');
4489 if (p != NULL)
4490 {
4491 *p = '\0';
4492 }
4493 }
4494 }
4495 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004496}
4497
qs.xiong026c5c72022-10-17 11:15:45 +08004498int lynq_ap_connect_num(int sta_number)
4499{
4500 char lynq_limit_cmd[32]={0};
4501 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004502 if((sta_number < 1 ) && (sta_number > 15))
4503 {
4504 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004505 return -1;
4506 }
4507 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4508 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004509 if(ret != 0)
4510 {
4511 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004512 }
4513 return 0;
4514}
you.chenf58b3c92022-06-21 16:53:48 +08004515
qs.xiong77905552022-10-17 11:19:57 +08004516int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4517{
4518
4519 char lynq_wifi_acs_cmd[128]={0};
4520 char lynq_cmd_mode[128]={0};
4521 char lynq_cmd_slect[128]={0};
4522
qs.xiong9fbf74e2023-03-28 13:38:22 +08004523 if((acs_mode != 2) && (acs_mode != 5))
4524 {
qs.xiong77905552022-10-17 11:19:57 +08004525 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4526 }
4527
qs.xiong9fbf74e2023-03-28 13:38:22 +08004528 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4529 {
qs.xiong77905552022-10-17 11:19:57 +08004530 return -1;
4531 }
4532
4533 CHECK_IDX(idx, CTRL_AP);
4534
4535 CHECK_WPA_CTRL(CTRL_AP);
4536
4537 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4538 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4539 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4540
4541 DO_OK_FAIL_REQUEST(cmd_disconnect);
4542 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4543 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4544 DO_OK_FAIL_REQUEST(cmd_save_config);
4545 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4546
4547 return 0;
4548}
you.chen0f5c6432022-11-07 18:31:14 +08004549//you.chen add for tv-box start
4550static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4551 FILE *fp;
4552 //printf("to exec cmd:%s\n", str_cmd);
4553 if((fp=popen(str_cmd,"r"))==NULL)
4554 {
4555 perror("popen error!");
4556 return -1;
4557 }
4558 if((fread(str_cmd_ret,max_len,1,fp))<0)
4559 {
4560 perror("fread fail!");
4561 fclose(fp);
4562 return -1;
4563 }
4564 fclose(fp);
4565 return 0;
4566}
4567
4568static int get_netmask_length(const char* mask)
4569{
4570 int masklen=0, i=0;
4571 int netmask=0;
4572
4573 if(mask == NULL)
4574 {
4575 return 0;
4576 }
4577
4578 struct in_addr ip_addr;
4579 if( inet_aton(mask, &ip_addr) )
4580 {
4581 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004582 }else
4583 {
you.chen0f5c6432022-11-07 18:31:14 +08004584 netmask = 0;
4585 return 0;
4586 }
4587
4588 while(0 == (netmask & 0x01) && i<32)
4589 {
4590 i++;
4591 netmask = netmask>>1;
4592 }
4593 masklen = 32-i;
4594 return masklen;
4595}
4596
4597static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4598 int mask_len;
4599 char *p;
4600 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004601 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4602 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004603 return -1;
4604 p = strstr(str_cmd_ret, "Mask:");
4605 if (p == NULL)
4606 return -1;
4607 mask_len = get_netmask_length(p + 5);
4608 if (mask_len == 0)
4609 return -1;
4610 p = strstr(str_cmd_ret, "inet addr:");
4611 if (p == NULL)
4612 return -1;
4613 strcpy(tmp, p + 10);
4614 p = strstr(tmp, " ");
4615 if (p != NULL)
4616 *p = '\0';
4617 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4618 return 0;
4619}
4620
4621static void GBWWatchThreadProc() {
4622 int i,n, nloop, nmax, ncheckcount, nidlecount;
4623 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4624 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4625 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4626 char *results[16] = {0};
4627 char str_cmd[256] = {0};
4628 char str_cmd_ret[128] = {0};
4629 char dest_ip[32] = {0};
4630 lastAP1Bytes = lastAP2Bytes = 0;
4631 lastAP1Drop = lastAP2Drop = 0;
4632 lastAP1Speed = lastAP2Speed = 0;
4633 setAP1Speed = 50;
4634 setAP2Speed = 80;
4635 nloop = 0;
4636 nmax = 6;
4637 ncheckcount = nidlecount = 0;
4638
you.chen0df3e7e2023-05-10 15:56:26 +08004639 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004640 {
4641 RLOGE("------gbw thread run\n");
4642 return;
4643 }
4644
qs.xiong9fbf74e2023-03-28 13:38:22 +08004645 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004646 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4647 while (dest_ip[0] == '\0') {
4648 sleep(1);
4649 str_cmd_ret[0] = '\0';
4650 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4651 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4652 if (str_cmd_ret[n] == '\n'){
4653 str_cmd_ret[n] = '\0';
4654 break;
4655 }
4656 }
4657 if (str_cmd_ret[0] != '\0')
4658 {
4659 strcpy(dest_ip, str_cmd_ret);
4660 }
4661 }
4662
you.chenc9928582023-04-24 15:39:37 +08004663 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4664 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4665 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 +08004666 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4667 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004668 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004669 return;
4670 }
you.chenc9928582023-04-24 15:39:37 +08004671 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);
4672 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);
4673 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 +08004674
4675 while (1) {
4676 sleep(1);
4677 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004678 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4679 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4680 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004681 continue;
4682 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004683 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004684 if (n > 9) {
4685 if (strcmp(results[1], "Sent") == 0) {
4686 currAP1Bytes = atoll(results[2]);
4687 }
4688 if (strcmp(results[6], "(dropped") == 0) {
4689 currAP1Drop = atoi(results[7]);
4690 }
4691 }
4692
4693 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004694 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4695 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4696 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004697 continue;
4698 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004699 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004700 if (n > 9) {
4701 if (strcmp(results[1], "Sent") == 0) {
4702 currAP2Bytes = atoll(results[2]);
4703 }
4704 if (strcmp(results[6], "(dropped") == 0) {
4705 currAP2Drop = atoi(results[7]);
4706 }
4707 }
4708
4709 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4710 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4711 lastAP1Bytes = currAP1Bytes;
4712 lastAP2Bytes = currAP2Bytes;
4713 continue;
4714 }
4715
4716 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4717 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4718 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4719 lastAP1Speed = currAP1Speed;
4720 lastAP2Speed = currAP2Speed;
4721 lastAP1Bytes = currAP1Bytes;
4722 lastAP2Bytes = currAP2Bytes;
4723
4724 currSetAP1Speed = setAP1Speed;
4725 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4726 ncheckcount++;
4727 if (ncheckcount > 3) {
4728 ncheckcount = 0;
4729 currSetAP1Speed = 5;
4730 }
4731 }
4732 else {
4733 ncheckcount = 0;
4734 if (currAP1Speed < 5)
4735 nidlecount++;
4736 else
4737 nidlecount = 0;
4738
4739 }
4740
4741 if (nidlecount > 60 ){
4742 currSetAP1Speed = 50;
4743 }
4744
4745 if (currSetAP1Speed != setAP1Speed) {
4746 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004747 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4748 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004749 }
4750 }
4751}
4752
4753int enableGBW(const char* mac) {
4754 int i,len;
4755 char get_ipaddr_cmd[128]={0};
4756 ap_info_s *ap;
4757 device_info_s * list;
4758
4759 if (mac == NULL || g_gbw_enabled == 1)
4760 return -1;
4761 len = strlen(mac);
4762 g_gbw_mac = malloc(len + 1);
4763 for(i=0;i<len;i++) {
4764 if (mac[i] >= 'A' && mac[i] <= 'Z')
4765 {
4766 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4767 }
4768 else
4769 g_gbw_mac[i] = mac[i];
4770 }
4771 g_gbw_mac[i] = '\0';
4772 g_gbw_enabled = 1;
4773
4774 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4775 if (system(get_ipaddr_cmd) == 0) {
4776 //startGBW();
4777 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4778 for (i=0;i<len;i++) {
4779 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4780 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4781 startGBW();
4782 }
4783 free(ap);
4784 free(list);
4785 }
4786 }
4787 return 0;
4788}
4789
4790int disableGBW() {
4791 stopGBW();
4792 free(g_gbw_mac);
4793 g_gbw_mac = NULL;
4794 g_gbw_enabled = 1;
4795 return 0;
4796}
4797
4798static int startGBW() {
4799 if (g_gbw_watcher_pid != 0) {
4800 stopGBW();
4801 }
4802 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4803}
4804
4805static int stopGBW() {
4806 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004807 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004808 pthread_cancel(g_gbw_watcher_pid);
4809 pthread_join(g_gbw_watcher_pid, &retval);
4810 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004811 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4812 if (s_ap_iterface_name[0] != '\0')
4813 {
4814 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4815 system(cmd);
4816 }
you.chen0f5c6432022-11-07 18:31:14 +08004817}
4818//you.chen add for tv-box end