blob: 26e030deabd69118227e33158a703756eede2ef8 [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
656static void APTmpWatcherThreadProc() {
657
658 int i = 0;
659 int delytime = 300;
660 g_ap_tmp_watcher_stop_flag = 0;
661
662 RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
663 while(1)
664 {
665 sleep(1);
666 i++;
667 if ( i == delytime )
668 {
669 if( lynq_connected_ap_sta_status() == 0 ) //o --->no sta device connect this ap
670 {
671 if(check_current_fw_status() == 1) //1 --->current fw status not 0x0096
672 {
673 system("wl down");
674 system("wl up");
675 }
676
677 }
678 i = 0;
679 }
680 if( g_ap_tmp_watcher_stop_flag == 1 ) //quit proc
681 {
682 RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
683 return;
684 }
685 }
686
687}
688
qs.xiongf0128b12023-06-29 17:29:39 +0800689static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
690{
691 char LYNQ_DISABLE_CMD[128]={0};
692
693 CHECK_IDX(idx, CTRL_STA);
694 CHECK_WPA_CTRL(CTRL_STA);
695
696 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
697 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
698 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800699 DO_OK_FAIL_REQUEST(cmd_save_config);
700
qs.xiongf0128b12023-06-29 17:29:39 +0800701 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800702
qs.xiongf0128b12023-06-29 17:29:39 +0800703}
704
705
qs.xiong455c30b2023-04-12 11:40:02 +0800706void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
707{
708 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800709 char *wpanetid;
710 char destid[3] = {0};
711 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800712 *error = LYNQ_WAIT_CONNECT_ACTIVE;
713 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
714 {
715 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
716 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
717 return;
718 }
719
720 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
721 {
722 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
723 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
724 return;
725 }
726
727 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
728 {
qs.xiongf0128b12023-06-29 17:29:39 +0800729 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
730 wpanetid = strstr(modify,"id=");
731 if ( wpanetid != NULL )
732 {
733 wpanetid +=strlen("id=");
734 memcpy(destid,wpanetid,2);
735 tmpdisid = atoi(destid);
736
737 }
qs.xiong455c30b2023-04-12 11:40:02 +0800738 pReason = strstr(modify, "reason=");
739 if (pReason != NULL)
740 {
741 pReason += strlen("reason=");
742 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
743 {
744 *error = LYNQ_TIME_OUT;
745 }
746 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
747 {
748 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800749 // tmp fix sta autoconnect connect and disconnect
750 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
751 {
752 RLOGE("stop wlan0 network %d fail",tmpdisid);
753 }
qs.xiong455c30b2023-04-12 11:40:02 +0800754 }
755 else
756 {
757 *error = LYNQ_UNSPECIFIED_REASON;
758 }
qs.xiong455c30b2023-04-12 11:40:02 +0800759 }
760 else
761 {
762 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800763 }
qs.xiongf0128b12023-06-29 17:29:39 +0800764 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
765 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800766
767 }
768
769 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
770 {
771 *error = LYNQ_NOT_FIND_AP;
772 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
773 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
774 return;
775 }
776
777
778 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
779 {
780 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
781 pReason = strstr(modify, "status_code=");
782 if (pReason != NULL)
783 {
784 pReason += strlen("status_code=");
785 if (memcmp(pReason, "17", 2) == 0)
786 {
787 *error = LYNQ_AP_UNABLE_HANDLE;
788 }
789 else if (memcmp(pReason, "1",1) == 0)
790 {
791 *error = LYNQ_UNSPECIFIED_REASON;
792 }
793 else
794 {
795 *error = LYNQ_UNSPECIFIED_REASON;
796 }
797
798 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
799 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
800 return;
801 }
802 else
803 {
804 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
805 *error = LYNQ_UNSPECIFIED_REASON;
806 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
807 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
808 return;
809 }
810 }
811
812 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
813 {
814 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
815 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
816 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
817 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
818 return;
819 }
820
821 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
822 {
823 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
824 *error = LYNQ_WAIT_CONNECT_ACTIVE;
825 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
826 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
827 return;
828 }
829
you.chen32cb31e2023-04-13 14:05:45 +0800830 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800831 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800832 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800833 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
834 return;
835
836}
837
qs.xiongfcc914b2023-07-06 21:16:20 +0800838void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
839{
840 char *pReason;
841 char *wpanetid;
842 char destid[3];
843 *error = LYNQ_WAIT_CONNECT_ACTIVE;
844 *networkid = -1;
845 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
846 {
847 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
848 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
849 return;
850 }
851 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
852 {
853 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
854 wpanetid = strstr(modify,"id=");
855 if ( wpanetid != NULL )
856 {
857 wpanetid +=strlen("id=");
858 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
859 if (memcpy(destid,wpanetid,2) != NULL)
860 {
861 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
862 *networkid = atoi(destid);
863 RLOGD("get networkid is %d\n",*networkid);
864 }
865 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
866 }
867 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
868 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
869 return;
870 }
871 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
872 {
873 wpanetid = strstr(modify,"id=");
874 if ( wpanetid != NULL )
875 {
876 wpanetid +=strlen("id=");
877 if (memcpy(destid,wpanetid,2) != NULL)
878 {
879 *networkid = atoi(destid);
880 RLOGD("get networkid is %d\n",*networkid);
881 }
882 }
883 pReason = strstr(modify, "reason=");
884 if (pReason != NULL)
885 {
886 pReason += strlen("reason=");
887 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
888 {
889 *error = LYNQ_TIME_OUT;
890 }
891 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
892 {
893 *error = LYNQ_PSW_ERROR;
894 }
895 else
896 {
897 *error = LYNQ_UNSPECIFIED_REASON;
898 }
899 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
900 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
901 return;
902 }
903 else
904 {
905 *error = LYNQ_UNSPECIFIED_REASON;
906 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
907 return;
908 }
909 }
910 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
911 {
912 wpanetid = strstr(modify,"id=");
913 if ( wpanetid != NULL )
914 {
915 wpanetid +=strlen("id=");
916 if (memcpy(destid,wpanetid,2) != NULL)
917 {
918 *networkid = atoi(destid);
919 RLOGD("get networkid is %d\n",*networkid);
920 }
921 }
922 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
923 pReason = strstr(modify, "status_code=");
924 if (pReason != NULL)
925 {
926 pReason += strlen("status_code=");
927 if (memcmp(pReason, "17", 2) == 0)
928 {
929 *error = LYNQ_AP_UNABLE_HANDLE;
930 }
931 else if (memcmp(pReason, "1",1) == 0)
932 {
933 *error = LYNQ_UNSPECIFIED_REASON;
934 }
935 else
936 {
937 *error = LYNQ_UNSPECIFIED_REASON;
938 }
939 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
940 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
941 return;
942 }
943 else
944 {
945 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
946 *error = LYNQ_UNSPECIFIED_REASON;
947 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
qs.xiong44fac672023-08-29 16:15:55 +0800948 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
qs.xiongfcc914b2023-07-06 21:16:20 +0800949 return;
950 }
951 }
952 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
953 {
954 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
955 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
956 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
957 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
958 return;
959 }
960 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
961 {
962 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
963 *error = LYNQ_WAIT_CONNECT_ACTIVE;
964 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
965 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
966 return;
967 }
968 RLOGD("EVENT : %s\n", modify);
969 *error = LYNQ_UNSPECIFIED_REASON;
970 *state = LYNQ_WIFI_STATUS_EGNORE;
971 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
972 return;
973}
you.chen70f377f2023-04-14 18:17:09 +0800974static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
975{
you.chen6d247052023-06-01 16:39:54 +0800976 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800977 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
978 {
979 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
980 g_sta_callback_func(g_sta_callback_priv, state, error);
981 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
982 }
you.chen6d247052023-06-01 16:39:54 +0800983 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800984}
qs.xiongfcc914b2023-07-06 21:16:20 +0800985static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
986{
987 pthread_mutex_lock(&s_sta_callback_mutex);
988 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
989 {
990 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
991 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
992 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
993 }
994 pthread_mutex_unlock(&s_sta_callback_mutex);
995}
you.chen70f377f2023-04-14 18:17:09 +0800996
you.chen35020192022-05-06 11:30:57 +0800997static void STAWatcherThreadProc() {
998 size_t len = MAX_RET;
999 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +08001000 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +08001001 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +08001002 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001003
you.chen6c2dd9c2022-05-16 17:55:28 +08001004 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +08001005 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +08001006
you.chen70f377f2023-04-14 18:17:09 +08001007 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001008 while (g_sta_watcher_stop_flag == 0)
1009 {
you.chenf711c8a2023-04-13 13:49:45 +08001010 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +08001011 {
you.chen35020192022-05-06 11:30:57 +08001012 continue;
1013 }
you.chenf711c8a2023-04-13 13:49:45 +08001014
you.chen6c2dd9c2022-05-16 17:55:28 +08001015 memset(msg_notify, 0, MAX_RET);
1016 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001017 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +08001018 {
you.chen35020192022-05-06 11:30:57 +08001019 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001020 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
1021 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001022 {
you.chen35020192022-05-06 11:30:57 +08001023 g_sta_scan_finish_flag = 1;
1024 }
1025
qs.xiong9fbf74e2023-03-28 13:38:22 +08001026 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +08001027 {
you.chen35020192022-05-06 11:30:57 +08001028 continue;
1029 }
qs.xiong455c30b2023-04-12 11:40:02 +08001030 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +08001031 notify_connect_status(state, error);
1032
1033 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +08001034 {
you.chen70f377f2023-04-14 18:17:09 +08001035 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +08001036 if (last_state != state)
1037 {
1038 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1039 {
1040 system_call_v("%s %s", sta_status_change_script, "connect");
1041 }
1042 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1043 {
1044 system_call_v("%s %s", sta_status_change_script, "disconnect");
1045 }
1046 }
1047
1048 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +08001049 }
you.chen35020192022-05-06 11:30:57 +08001050 }
1051 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001052 if (lynq_wpa_ctrl != NULL)
1053 {
you.chen92fd5d32022-05-25 10:09:47 +08001054 wpa_ctrl_detach(lynq_wpa_ctrl);
1055 wpa_ctrl_close(lynq_wpa_ctrl);
1056 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001057}
qs.xiongfcc914b2023-07-06 21:16:20 +08001058static void STAAutoWatcherThreadProc() {
1059 size_t len = MAX_RET;
1060 char msg_notify[MAX_RET];
1061 error_number_s error;
1062 lynq_wifi_sta_status_s state, last_state = -1;
1063 int idle_count = 0;
1064 int networkid;
1065 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
1066 g_sta_auto_watcher_stop_flag = 0;
1067 RLOGD("STAAutoWatcherThreadProc thread started ------");
1068 while (g_sta_auto_watcher_stop_flag == 0)
1069 {
1070 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
1071 {
1072 continue;
1073 }
1074 memset(msg_notify, 0, MAX_RET);
1075 len = MAX_RET;
1076 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
1077 {
1078 msg_notify[len+1] = '\0';
1079 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
1080 if (strstr(msg_notify, state_scan_result) != NULL)
1081 {
1082 g_sta_auto_scan_finish_flag = 1;
1083 }
1084 if (g_sta_auto_callback_func == NULL)
1085 {
1086 continue;
1087 }
1088 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
1089 notify_auto_connect_status(state, error,networkid);
1090 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
1091 {
1092 inner_check_connect_error(msg_notify, state, error);
1093 if (last_state != state)
1094 {
1095 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
1096 {
1097 system_call_v("%s %s", sta_status_change_script, "connect");
1098 }
1099 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
1100 {
1101 system_call_v("%s %s", sta_status_change_script, "disconnect");
1102 }
1103 }
1104 last_state = state;
1105 }
1106 }
1107 }
1108 if (lynq_wpa_ctrl != NULL)
1109 {
1110 wpa_ctrl_detach(lynq_wpa_ctrl);
1111 wpa_ctrl_close(lynq_wpa_ctrl);
1112 }
1113}
qs.xiongf1b525b2022-03-31 00:58:23 -04001114
you.chen70f377f2023-04-14 18:17:09 +08001115// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1116static void GlobalWatcherThreadProc()
1117{
1118 int ret, connect_timeout, service_abnormal;
1119 error_number_s error_num = -1;
1120 inner_sta_status_s sta_status;
1121 scan_info_s *scan_list = NULL;
1122 int i, scan_len=0;
1123 char connecting_ssid[64];
1124 struct timeval now;
1125
1126 RLOGD("GlobalWatcherThreadProc start to run");
1127
1128 while (1)
1129 {
1130 pthread_mutex_lock(&s_global_check_mutex);
1131 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
1132 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
1133 {
1134 pthread_mutex_unlock(&s_global_check_mutex);
1135 usleep(50*1000);
1136 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1137 continue;
1138 }
1139
1140 connect_timeout = 0;
1141 service_abnormal = 0;
1142 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
1143 {
1144 while (1)
1145 {
1146 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
1147 if (ret == ETIME)
1148 {
1149 connect_timeout = 1;
1150 }
1151 else if (ret != 0)
1152 {
1153 gettimeofday(&now,NULL);
1154 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1155 {
1156 usleep(SLEEP_TIME_ON_IDLE);
1157 continue;
1158 }
1159 connect_timeout = 1;
1160 }
1161 sta_status = s_sta_status;
1162 error_num = s_sta_error_number;
1163 s_sta_status = INNER_STA_STATUS_INIT;
1164 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1165 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1166 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1167 break;
1168 }
1169 }
1170 if (s_service_invoke_timeout_cnt > 10)
1171 {
1172 service_abnormal = 1;
1173 s_service_invoke_timeout_cnt = 0;
1174 }
1175 pthread_mutex_unlock(&s_global_check_mutex);
1176
1177 if (service_abnormal == 1)
1178 {
1179 sleep(1);
1180 RLOGE("wpa service is abnormal info app to exit");
1181 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001182
1183 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1184
you.chen70f377f2023-04-14 18:17:09 +08001185 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1186 }
1187
1188 if (sta_status == INNER_STA_STATUS_CANCEL)
1189 {
1190 continue;
1191 }
1192 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1193 {
1194 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1195 }
1196 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1197 {
1198 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1199 {
1200 for(i=0; i < scan_len;i++)
1201 {
1202 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1203 {
1204 error_num = LYNQ_AUTH_ERROR;
1205 break;
1206 }
1207 }
1208 free(scan_list);
1209 }
1210 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1211 }
1212 else if (connect_timeout == 0)
1213 {
1214 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1215 }
1216 else // wait timeout
1217 {
1218 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1219 {
1220 ; // wpa service abnormal
1221 }
1222 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1223 {
1224 RLOGD("GlobalWatcherThreadProc notify connected");
1225 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1226 }
1227 else
1228 {
1229 RLOGD("GlobalWatcherThreadProc notify timeout");
1230 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1231 }
1232 }
1233 } // while (1)
1234}
1235
qs.xiong1af5daf2022-03-14 09:12:12 -04001236int lynq_wifi_enable(void)
1237{
you.chen35020192022-05-06 11:30:57 +08001238 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001239 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001240 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001241 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1242
qs.xiong9fbf74e2023-03-28 13:38:22 +08001243 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1244 {
you.chend2fef3f2023-02-13 10:50:35 +08001245 goto out_enable;
1246 }
1247
you.chenc9928582023-04-24 15:39:37 +08001248 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001249 if (ret != 0)
1250 {
1251 //printf("service state %d\n", ret);
1252 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001253 ret = -1;
1254 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001255 }
lhfe8da902022-10-11 18:55:36 +08001256
you.chen70f377f2023-04-14 18:17:09 +08001257 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1258 {
1259 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1260 if(ret<0)
1261 {
1262 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1263 ret = -1;
1264 goto out_enable;
1265 }
1266 }
1267
you.chend2fef3f2023-02-13 10:50:35 +08001268 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1269 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1270 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1271 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1272out_enable:
1273 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001274 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001275}
1276
qs.xiong1af5daf2022-03-14 09:12:12 -04001277int lynq_wifi_disable(void)
1278{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001279 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001280 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001281 g_ap_watcher_stop_flag = 1;
1282 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001283 g_sta_auto_watcher_stop_flag = 1;
qs.xiong44fac672023-08-29 16:15:55 +08001284 g_ap_tmp_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001285 if (g_ap_watcher_pid != 0)
1286 pthread_join(g_ap_watcher_pid, NULL);
1287 if (g_sta_watcher_pid != 0)
1288 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001289 if (g_sta_auto_watcher_pid != 0)
1290 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001291 if (g_lynq_wpa_ctrl[0] != NULL)
1292 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1293 if (g_lynq_wpa_ctrl[1] != NULL)
1294 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
qs.xiong44fac672023-08-29 16:15:55 +08001295 if (g_ap_tmp_watcher_pid != 0)
1296 pthread_join(g_ap_tmp_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001297 g_ap_watcher_pid = 0;
1298 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001299 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001300 g_lynq_wpa_ctrl[0] = NULL;
1301 g_lynq_wpa_ctrl[1] = NULL;
1302 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001303 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1304 return 0;
1305}
1306
1307static inline char inner_convert_char(char in)
1308{
1309 if (in >= '0' && in <= '9')
1310 {
1311 return in - '0';
1312 }
1313 else if (in >= 'a' && in <= 'f')
1314 {
1315 return in - 'a' + 10;
1316 }
1317 else if (in >= 'A' && in <= 'F')
1318 {
1319 return in - 'A' + 10;
1320 }
1321 else
1322 {
1323 return '\xff';
1324 }
1325}
1326
1327static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1328{
1329 char *p;
1330 size_t pos = 0;
1331 if (NULL == out_ssid)
1332 return;
1333 //printf("input ssid=[%s]\n", ssid);
1334 memset(out_ssid, 0, out_ssid_len);
1335 if (NULL == ssid)
1336 return;
1337 p = strchr(ssid, '\\');
1338 if (NULL == p)
1339 {
1340 strncpy(out_ssid, ssid, out_ssid_len);
1341 //printf(" first %s\n", out_ssid);
1342 }
1343 else
1344 {
1345 pos = p - ssid;
1346 memcpy(out_ssid, ssid, pos);
1347 //printf("pos %lu -- %s\n", pos, out_ssid);
1348 for(; pos < out_ssid_len; pos ++)
1349 {
1350 if (p[0] == '\0')
1351 {
1352 //printf(" out %s\n", out_ssid);
1353 return;
1354 }
1355 else if (p[0] != '\\')
1356 {
1357 out_ssid[pos] = p[0];
1358 p += 1;
1359 }
1360 else if (p[1] == 'x' || p[1] == 'X')
1361 {
1362 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1363 p += 4;
1364 }
1365 else if (p[1] == '\\')
1366 {
1367 out_ssid[pos] = '\\';
1368 p += 2;
1369 }
1370 else if (p[1] == 't')
1371 {
1372 out_ssid[pos] = '\t';
1373 p += 2;
1374 }
1375 else if (p[1] == 'r')
1376 {
1377 out_ssid[pos] = '\r';
1378 p += 2;
1379 }
1380 else if (p[1] == 'n')
1381 {
1382 out_ssid[pos] = '\n';
1383 p += 2;
1384 }//todo find a better way to convert?
1385 }
1386 }
1387 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001388}
qs.xiong1af5daf2022-03-14 09:12:12 -04001389
you.chen35020192022-05-06 11:30:57 +08001390static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001391 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001392 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001393 RLOGD("enter inner_get_param");
1394 if (out_put == NULL)
1395 {
1396 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001397 return -1;
1398 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001399 if (param_name == NULL)
1400 {
1401 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001402 return -1;
1403 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001404 if (param_name[0] == '\0')
1405 {
1406 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001407 return -1;
1408 }
1409
1410 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1411
1412 CHECK_WPA_CTRL(interface);
1413
1414 DO_REQUEST(lynq_cmd_get);
1415
qs.xiong9fbf74e2023-03-28 13:38:22 +08001416 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1417 {
1418 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001419 return -1;
1420 }
1421
you.chena6fa5b22022-05-18 10:28:19 +08001422// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001423 if (strcmp(param_name, "ssid") == 0)
1424 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001425 if (cmd_reply[0] == '\"')
1426 {
you.chend2fef3f2023-02-13 10:50:35 +08001427 ssid_len = reply_len - 1;
1428 memcpy(out_put, cmd_reply + 1, ssid_len);
1429 if (out_put[ssid_len-1] == '\"')
1430 {
1431 out_put[ssid_len-1] = '\0';
1432 }
1433 else
1434 {
1435 out_put[ssid_len] = '\0';
1436 }
1437 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001438 else
1439 {
you.chend2fef3f2023-02-13 10:50:35 +08001440 ssid_len = reply_len / 2;
1441 for(i=0; i<ssid_len; i++)
1442 {
1443 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1444 }
1445 out_put[ssid_len] = '\0';
1446 }
1447 }
1448 else
1449 {
1450 memcpy(out_put, cmd_reply, reply_len + 1);
1451 }
you.chen35020192022-05-06 11:30:57 +08001452 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001453}
qs.xiong1af5daf2022-03-14 09:12:12 -04001454
you.chen35020192022-05-06 11:30:57 +08001455static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1456 int ret = 0;
1457 char * end = str + len - 1;
1458 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001459 while(str < end)
1460 {
1461 if (*str == delimiter)
1462 {
you.chen35020192022-05-06 11:30:57 +08001463 *str++ = '\0';
1464 results[ret++] = str;
1465 continue;
1466 }
1467 str++;
1468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001469 if (*str == delimiter)
1470 {
you.chen35020192022-05-06 11:30:57 +08001471 *str = '\0';
1472 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001473
you.chen6ed36a62023-04-27 17:51:56 +08001474 results[ret] = NULL;
1475
you.chen35020192022-05-06 11:30:57 +08001476 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001477}
1478
you.chend2fef3f2023-02-13 10:50:35 +08001479static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1480{
1481 char * p;
1482 int ret = 0;
1483 char cmd[256]={0};
1484 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001485 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001486 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001487 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001488 ret = exec_cmd(cmd, ip, ip_len);
1489 p = strchr(ip, '\n');
1490 if (NULL != p)
1491 {
1492 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001493 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001494 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001495 return ret;
1496}
1497
you.chend2fef3f2023-02-13 10:50:35 +08001498static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001499 struct in_addr addr ={0};
1500 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001501 char cmd[64] = {0};
1502 char * p;
1503 int ret;
you.chen35020192022-05-06 11:30:57 +08001504
qs.xiong9fbf74e2023-03-28 13:38:22 +08001505 if (ip == NULL || *ip == '\0' || hostname == NULL)
1506 {
1507 RLOGE("ip == NULL or hostname == NULL");
1508 return -1;
you.chen35020192022-05-06 11:30:57 +08001509 }
1510
you.chend2fef3f2023-02-13 10:50:35 +08001511 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001512 if (inet_aton(ip, &addr) == 0)
1513 {
you.chen35020192022-05-06 11:30:57 +08001514 printf("---inet_aton fail\n");
1515 return -1;
1516 }
1517
1518 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1519
qs.xiong9fbf74e2023-03-28 13:38:22 +08001520 if (ht == NULL)
1521 {
you.chen186d3c32023-05-18 14:19:46 +08001522 hostname[0] = '\0';
1523 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1524 ret = exec_cmd(cmd, hostname, 32);
1525 if (ret == 0)
1526 {
1527 p = strchr(hostname, '\n');
1528 if (p != NULL)
1529 {
1530 *p = '\0';
1531 }
1532 return 0;
qs.xiong6d64b542023-07-26 17:09:22 +08001533 }else{
1534 usleep( 10 * 1000);
1535 ret = exec_cmd(cmd, hostname, 32);
1536 if( ret == 0)
1537 {
1538 p= strchr(hostname, '\n');
1539 if(p != NULL )
1540 {
1541 *p = '\0';
1542 }
1543 return 0;
1544 }
you.chen186d3c32023-05-18 14:19:46 +08001545 }
1546 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001547 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001548 herror(NULL);
1549 return -1;
1550 }
1551
1552 strcpy(hostname, ht->h_name);
1553
1554 return 0;
1555}
1556
1557static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1558{
1559 int count, index, words_count;
1560 char * split_lines[128]= {0};
1561 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001562 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001563 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001564 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001565
1566 CHECK_WPA_CTRL(ap_sta);
1567
1568 DO_REQUEST(lynq_wifi_list_networks);
1569
1570 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1571
1572 //@todo check ssid field to compatible
1573
1574 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001575 for(index=1; index < count; index++)
1576 {
you.chen35020192022-05-06 11:30:57 +08001577 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001578 if (words_count > 2)
1579 {
you.chend2fef3f2023-02-13 10:50:35 +08001580 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001581 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1582 {
you.chen35020192022-05-06 11:30:57 +08001583 net_no_list[ret++] = atoi(split_words[0]);
1584 }
1585 }
1586 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001587 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001588 return ret;
1589}
1590
1591static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001592 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001593 CHECK_WPA_CTRL(ap_sta);
1594 const char *lynq_wifi_add_network = "ADD_NETWORK";
1595
qs.xiong9fbf74e2023-03-28 13:38:22 +08001596 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001597 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001598 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1599 {
1600 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001601 return -1;
1602 }
1603
qs.xiong9fbf74e2023-03-28 13:38:22 +08001604 for(i=0;i<reply_len;i++)
1605 {
1606 if(cmd_reply[i] == '\n')
1607 {
you.chen35020192022-05-06 11:30:57 +08001608 cmd_reply[i] = '\0';
1609 break;
1610 }
1611 }
1612 return atoi(cmd_reply);
1613}
you.chena6cd55a2022-05-08 12:20:18 +08001614
you.chen35020192022-05-06 11:30:57 +08001615static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1616{
1617 int count, index;
1618 int net_no_list[128];
1619
qs.xiong9fbf74e2023-03-28 13:38:22 +08001620 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001621 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001622 for (index=0; index < count; index++)
1623 {
1624 if (net_no_list[index] == net_no)
1625 {
you.chen35020192022-05-06 11:30:57 +08001626 return 0;
1627 }
1628 }
1629
1630 if (count >= 1)
1631 index = net_no_list[count - 1];
1632 else
1633 index = -1;
1634
qs.xiong9fbf74e2023-03-28 13:38:22 +08001635 while (index < net_no )
1636 {
you.chen35020192022-05-06 11:30:57 +08001637 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001638 if (index >= net_no)
1639 { // required network no created
1640 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001641 return 0;
1642 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001643 else if( index < 0)
1644 {
1645 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001646 return -1;
1647 }
you.chen35020192022-05-06 11:30:57 +08001648 }
1649
1650 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001651 {
1652 RLOGE("[lynq_check_network_number] network index < 0");
1653 return -1;
1654 }
1655 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001656 return 0;
1657}
1658
1659static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001660 if (freq > 5000 && freq < 6000)
1661 {
you.chen35020192022-05-06 11:30:57 +08001662 return LYNQ_WIFI_5G_band;
1663 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001664 else if (freq > 2000 && freq < 3000)
1665 {
you.chen35020192022-05-06 11:30:57 +08001666 return LYNQ_WIFI_2G_band;
1667 }
1668 return LYNQ_WIFI_2_and_5G_band;
1669}
1670
1671static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001672 if (key_mgmt != NULL)
1673 {
1674 if (memcmp( key_mgmt, "NONE", 4) == 0)
1675 {
you.chen35020192022-05-06 11:30:57 +08001676 return LYNQ_WIFI_AUTH_OPEN;
1677 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001678 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1679 {
you.chen35020192022-05-06 11:30:57 +08001680 return LYNQ_WIFI_AUTH_WEP;
1681 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001682 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1683 {
you.chen35020192022-05-06 11:30:57 +08001684 return LYNQ_WIFI_AUTH_WPA_PSK;
1685 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001686 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1687 {
you.chen35020192022-05-06 11:30:57 +08001688 return LYNQ_WIFI_AUTH_WPA2_PSK;
1689 }
1690 }
1691
1692 return -1;
1693}
1694
1695static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001696 if (flag != NULL)
1697 {
qs.xiongba01e1f2023-09-06 14:14:32 +08001698 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 +08001699 {
1700 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001701 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001702 {
1703 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1704 }else if (strstr( flag, "WPA2-PSK") != NULL)
1705 {
you.chen35020192022-05-06 11:30:57 +08001706 return LYNQ_WIFI_AUTH_WPA2_PSK;
1707 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001708 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001709 {
you.chen35020192022-05-06 11:30:57 +08001710 return LYNQ_WIFI_AUTH_WPA_PSK;
1711 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001712 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001713 {
you.chen35020192022-05-06 11:30:57 +08001714 return LYNQ_WIFI_AUTH_WEP;
1715 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001716 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001717 {
you.chen35020192022-05-06 11:30:57 +08001718 return LYNQ_WIFI_AUTH_OPEN;
1719 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001720 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001721 {
you.chend2fef3f2023-02-13 10:50:35 +08001722 return LYNQ_WIFI_AUTH_OPEN;
1723 }
qs.xiong46f41562023-07-11 21:06:47 +08001724 else
1725 {
1726 RLOGD("convert_max_auth_from_flag not-found auth mode");
1727 }
you.chen35020192022-05-06 11:30:57 +08001728 }
1729
1730 return -1;
1731}
1732
1733static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1734 switch (bw) {
1735 case 10:
1736 return LYNQ_WIFI_BANDWIDTH_HT10;
1737 break;
1738 case 20:
1739 return LYNQ_WIFI_BANDWIDTH_HT20;
1740 break;
1741 case 40:
1742 return LYNQ_WIFI_BANDWIDTH_HT40;
1743 break;
1744 case 80:
1745 return LYNQ_WIFI_BANDWIDTH_HT80;
1746 break;
1747 default:
1748 break;
1749 }
1750
1751 return -1;
1752}
1753
you.chen70f377f2023-04-14 18:17:09 +08001754static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001755static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1756 int i, count;
1757 char *p;
1758 const char *lynq_status_cmd = "STATUS";
1759 const char * FLAG_SSID = "ssid=";
1760 const char * FLAG_SBSID = "bssid=";
1761 const char * FLAG_KEY_MGMT = "key_mgmt=";
1762 const char * FLAG_FREQ = "freq=";
1763 const char * FLAG_STATE = "wpa_state=";
1764 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001765 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001766 char *split_lines[128] = {0};
1767
1768 CHECK_WPA_CTRL(interface);
1769
qs.xiong9fbf74e2023-03-28 13:38:22 +08001770 if (curr_state == NULL)
1771 {
1772 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001773 return -1;
1774 }
1775
1776 DO_REQUEST(lynq_status_cmd);
1777
1778 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1779
1780 curr_state->net_no = -1;
1781 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001782 for(i=0; i < count; i++)
1783 {
1784 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001785 {
you.chen35020192022-05-06 11:30:57 +08001786 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001788 {
you.chend2fef3f2023-02-13 10:50:35 +08001789 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001790 ret = 0;
1791 continue;
1792 }
you.chenf58b3c92022-06-21 16:53:48 +08001793 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001794 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001795 {
you.chend2fef3f2023-02-13 10:50:35 +08001796 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 +08001797 ret = 0;
1798 continue;
1799 }
you.chen35020192022-05-06 11:30:57 +08001800 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001801 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001802 {
you.chen450d0172022-07-15 17:56:48 +08001803 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001804 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001805 ret = 0;
1806 continue;
1807 }
1808 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001809 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001810 {
you.chen35020192022-05-06 11:30:57 +08001811 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1812 ret = 0;
1813 continue;
1814 }
you.chend2fef3f2023-02-13 10:50:35 +08001815 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001816 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001817 {
you.chend2fef3f2023-02-13 10:50:35 +08001818 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1819 ret = 0;
1820 continue;
1821 }
you.chen35020192022-05-06 11:30:57 +08001822 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001823 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001824 {
you.chen35020192022-05-06 11:30:57 +08001825 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001826 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001827 {
you.chen35020192022-05-06 11:30:57 +08001828 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1829 ret = 0;
1830 continue;
1831 }
1832
1833 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001834 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001835 {
you.chen35020192022-05-06 11:30:57 +08001836 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001837 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001838 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001839 }
1840 }
1841
you.chen70f377f2023-04-14 18:17:09 +08001842 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1843 {
1844 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1845 }
1846
you.chen35020192022-05-06 11:30:57 +08001847 return ret;
1848}
1849
qs.xiongf1b525b2022-03-31 00:58:23 -04001850int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001851{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001852 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001853 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001854
qs.xiong9fbf74e2023-03-28 13:38:22 +08001855 if (ap_ssid == NULL)
1856 {
1857 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001858 return -1;
1859 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001860 else
1861 {
1862 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001863 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001864
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1866 {
1867 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001868 return -1;
1869 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001870
you.chen35020192022-05-06 11:30:57 +08001871 CHECK_IDX(idx, CTRL_AP);
1872
1873 CHECK_WPA_CTRL(CTRL_AP);
1874
1875 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1876
1877 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1878 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001879 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1880 return 0;
you.chen35020192022-05-06 11:30:57 +08001881
qs.xiong7a105ce2022-03-02 09:43:11 -05001882}
1883
you.chen35020192022-05-06 11:30:57 +08001884int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001885{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001886 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001887 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001888 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001889}
1890
qs.xiongc9c79f72022-10-17 15:27:18 +08001891/*****
1892 *frequency <------>channel
1893 *
1894 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1895 *
1896 *
1897 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1898 *
1899 *
1900 * */
1901static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001902 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};
1903 int i;
1904 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1905
qs.xiong69a332b2022-12-02 09:58:57 +08001906 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001907 {
1908 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001909 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001910 }
qs.xiongc00b6032022-11-29 16:28:03 +08001911
1912 if(i == arr_len)
1913 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001914 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001915 return -1;
1916 }
qs.xiongc00b6032022-11-29 16:28:03 +08001917
qs.xiongc9c79f72022-10-17 15:27:18 +08001918 return 0;
1919}
qs.xiong13673462023-02-21 19:12:54 +08001920
1921static int lynq_check_frequencyby_country_code(int input_frequency)
1922{
1923 char str_cnc[]="CN";
1924 char str_dest[20]="";
1925
1926 if( lynq_get_country_code(1,str_dest) != 0 )
1927 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001928 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001929 return -1;
1930 }
1931 if( strncmp(str_dest,str_cnc,2) != 0 )
1932 {
1933 return 0;
1934 }else if( 2473 < input_frequency && input_frequency < 5744)
1935 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001936 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001937 return -1;
1938 }
1939 return 0;
1940}
qs.xiongf1b525b2022-03-31 00:58:23 -04001941int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001942{
qs.xiongc00b6032022-11-29 16:28:03 +08001943 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001944 char lynq_wifi_frequency_cmd[128]={0};
1945 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001946 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001947 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001948 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001949 check = lynq_check_set_frequency(lynq_wifi_frequency);
1950 if(check != 0)
1951 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001952 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001953 return -1;
you.chen35020192022-05-06 11:30:57 +08001954 }
qs.xiong13673462023-02-21 19:12:54 +08001955 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1956 if(check != 0)
1957 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001958 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001959 return -1;
1960 }
1961
qs.xiongc00b6032022-11-29 16:28:03 +08001962 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1963 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001964 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001965 return -1;
1966 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001967
you.chen35020192022-05-06 11:30:57 +08001968 CHECK_IDX(idx, CTRL_AP);
1969
1970 CHECK_WPA_CTRL(CTRL_AP);
1971
1972 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1973 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1974 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1975
you.chen6c2dd9c2022-05-16 17:55:28 +08001976 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001977 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1978 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1979 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001980
qs.xiong9fbf74e2023-03-28 13:38:22 +08001981 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001982}
1983
qs.xiongf1b525b2022-03-31 00:58:23 -04001984int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001985{
you.chen35020192022-05-06 11:30:57 +08001986 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001987 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001988 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001989
qs.xiong9fbf74e2023-03-28 13:38:22 +08001990 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1991 {
1992 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001993 return -1;
1994 }
1995 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001996
qs.xiong9fbf74e2023-03-28 13:38:22 +08001997 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001998}
1999
qs.xiongf1b525b2022-03-31 00:58:23 -04002000int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
2001{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002002 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08002003 CHECK_IDX(idx, CTRL_AP);
2004 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002005 case LYNQ_WIFI_BANDWIDTH_HT10:
2006 {
2007 RLOGE("bandwith [%d] not support now\n", bandwidth);
2008 return -1;
2009 }
2010 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08002011 {
2012 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
2013 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002014 if (system(lynq_cmd_bandwith) != 0 )
2015 {
2016 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002017 return -1;
2018 }
2019 system("wl up");
2020 break;
2021 }
2022 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002023 {
qs.xiong10379192023-02-21 13:19:42 +08002024 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08002025 sprintf(lynq_cmd_bandwith, "wl chanspec ");
2026 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002027 if (system(lynq_cmd_bandwith) != 0 )
2028 {
2029 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002030 return -1;
2031 }
2032 system("wl up");
2033 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002034 }
you.chen35020192022-05-06 11:30:57 +08002035 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002036 {
qs.xiong10379192023-02-21 13:19:42 +08002037 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08002038 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 if (system(lynq_cmd_bandwith) != 0 )
2040 {
2041 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08002042 return -1;
2043 }
2044 system("wl up");
2045 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002046 }
2047 default:
you.chen35020192022-05-06 11:30:57 +08002048 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002049 RLOGE("auth type [%d] not support now\n", bandwidth);
2050 return -1;
you.chen35020192022-05-06 11:30:57 +08002051 }
2052 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002053
2054
you.chen35020192022-05-06 11:30:57 +08002055 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002056}
you.chen35020192022-05-06 11:30:57 +08002057
qs.xiongf1b525b2022-03-31 00:58:23 -04002058int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
2059{
you.chen35020192022-05-06 11:30:57 +08002060 int count = 0;
2061 int index = 0;
2062 char *split_words[128] = {0};
2063 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002064 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002065 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002066
you.chen35020192022-05-06 11:30:57 +08002067 CHECK_WPA_CTRL(CTRL_AP);
2068
2069 DO_REQUEST(lynq_chanspec_cmd);
2070
2071 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2072 for(;index < count; index++) {
2073 if (strncmp(split_words[index], "bw", 2) != 0) {
2074 continue;
2075 }
2076
2077 index++;
2078 if (index >= count) {
2079 return -1;
2080 }
2081
qs.xiong9fbf74e2023-03-28 13:38:22 +08002082 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002083 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
2084 return 0;
2085 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002086 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08002087 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002088}
qs.xiong0fb469a2022-04-14 03:50:45 -04002089
qs.xiongf1b525b2022-03-31 00:58:23 -04002090int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002091{
you.chen35020192022-05-06 11:30:57 +08002092 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002093 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08002094 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04002095
you.chen35020192022-05-06 11:30:57 +08002096 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04002097
qs.xiong9fbf74e2023-03-28 13:38:22 +08002098 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
2099 {
you.chen35020192022-05-06 11:30:57 +08002100 return -1;
2101 }
2102
2103 system("wl down");
2104 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08002105 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08002106 return -1;
2107 }
2108 system("wl up");
2109 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002110}
qs.xiong0fb469a2022-04-14 03:50:45 -04002111
qs.xiongf1b525b2022-03-31 00:58:23 -04002112int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05002113{
you.chen35020192022-05-06 11:30:57 +08002114 int count = 0;
2115 int index = 0;
2116 char *split_words[128] = {0};
2117 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002119 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04002120
you.chen35020192022-05-06 11:30:57 +08002121 CHECK_WPA_CTRL(CTRL_AP);
2122
2123 DO_REQUEST(lynq_chanspec_cmd);
2124
2125 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002126 for(;index < count; index++)
2127 {
2128 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08002129 if (strncmp(split_words[index], "channel", 2) != 0) {
2130 continue;
2131 }
2132
2133 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002134 if (index >= count)
2135 {
you.chen35020192022-05-06 11:30:57 +08002136 return -1;
2137 }
2138
2139 *channel = atoi(split_words[index]);
2140 return 0;
2141 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002142 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08002143 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002144}
2145
2146
you.chen35020192022-05-06 11:30:57 +08002147int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002148{
you.chen6c2dd9c2022-05-16 17:55:28 +08002149 char ssid[MAX_CMD] = {0};
2150 int freq = 0;
2151 char lynq_auth_cmd[64]={0};
2152 char lynq_auth_alg_cmd[64]={0};
2153 char lynq_psk_cmd[64]={0};
2154 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002155 char lynq_ieee80211_cmd[64]={0};
2156 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002157 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002158 CHECK_IDX(idx, CTRL_AP);
2159
you.chen6c2dd9c2022-05-16 17:55:28 +08002160 CHECK_WPA_CTRL(CTRL_AP);
2161
qs.xiong9fbf74e2023-03-28 13:38:22 +08002162 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2163 {
2164 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002165 return -1;
2166 }
2167
you.chen92fd5d32022-05-25 10:09:47 +08002168 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002169 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002170 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002171 return 0;
2172 }
2173 else {
2174 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2175 ssid[0] = '\0';
2176 }
2177 lynq_wifi_ap_frequency_get(idx, &freq);
2178
2179 DO_OK_FAIL_REQUEST(cmd_disconnect);
2180 DO_OK_FAIL_REQUEST(cmd_remove_all);
2181 if (ssid[0] != '\0') {
2182 lynq_wifi_ap_ssid_set(idx, ssid);
2183 }
2184 if (freq != 0) {
2185 lynq_wifi_ap_frequency_set(idx, freq);
2186 }
2187 }
2188 }
you.chen35020192022-05-06 11:30:57 +08002189
qs.xiong9fbf74e2023-03-28 13:38:22 +08002190 switch(auth){
2191 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002192 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002193 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002194 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002195 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002196 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002197 break;
2198 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002199 case LYNQ_WIFI_AUTH_WEP:
2200 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002201 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002202 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002203 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002204 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2205
2206 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2207 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2208 break;
2209 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002210 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002211 {
2212 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2213 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2214 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2215
2216 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2217 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2218 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2219 break;
2220
2221 }
you.chen35020192022-05-06 11:30:57 +08002222 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002223 {
2224 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2225 {
you.chen35020192022-05-06 11:30:57 +08002226 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2227 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2228 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002229 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2230 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002231 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002232 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002233 }
2234// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2235// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2236 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002237
you.chen35020192022-05-06 11:30:57 +08002238 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2239 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2240 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002241 break;
2242 }
2243 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2244 {
2245 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2246 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2247 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2248 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2249
2250 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2251 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2252 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2253 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2254 break;
2255 }
2256 case LYNQ_WIFI_AUTH_WPA3_PSK:
2257 {
2258 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2259 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002260 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002261 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2262
2263 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2264 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2265 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2266 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2267 break;
2268 }
2269 default:
you.chen35020192022-05-06 11:30:57 +08002270 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002271 RLOGE("auth type [%d] not support now\n", auth);
2272 return -1;
you.chen35020192022-05-06 11:30:57 +08002273 }
2274 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002275 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002276
qs.xiong9fbf74e2023-03-28 13:38:22 +08002277 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002278}
2279
you.chen35020192022-05-06 11:30:57 +08002280int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002281{
you.chen35020192022-05-06 11:30:57 +08002282 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002283 char lynq_auth_alg_str[MAX_RET] = {0};
2284 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002285 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002286 CHECK_IDX(idx, CTRL_AP);
2287
qs.xiong9fbf74e2023-03-28 13:38:22 +08002288 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2289 {
2290 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002291 return -1;
2292 }
2293
qs.xiong9fbf74e2023-03-28 13:38:22 +08002294 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2295 {
2296 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2297 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002298 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002299 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002300 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002301 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002302 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2303 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002304 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002305 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002306 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002307 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002308 else
2309 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002310 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002311 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002312 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002313 }
you.chen35020192022-05-06 11:30:57 +08002314 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2316 {
2317 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2318 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002319 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002320 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002321 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002322 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2323 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002324 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002325 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002326 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002327 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002328 else
2329 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002330 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002331 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002332 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002333 }
you.chen35020192022-05-06 11:30:57 +08002334 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335
2336 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2337 {
2338 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2339 return -1;
2340 }
2341
2342 if (memcmp(lynq_auth_str,"1",1) == 0 )
2343 {
2344 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2345 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002346 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002347 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2348 {
2349 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2350 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002351 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002352 }
2353 else
2354 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002355 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002356 *auth = -1;
2357 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002358
you.chen6c2dd9c2022-05-16 17:55:28 +08002359 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002360}
qs.xiong1af5daf2022-03-14 09:12:12 -04002361
you.chenb95401e2023-05-12 19:39:06 +08002362static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2363{
2364 char status[64];
you.chencba13492023-05-19 13:53:43 +08002365 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002366 curr_status_info curr_state;
2367
2368 CHECK_WPA_CTRL(CTRL_AP);
2369
2370 memset(status, 0, sizeof (status));
2371
2372 curr_state.ap = NULL;
2373 curr_state.state = status;
2374
2375 printf("inner_check_ap_connected %d\n", retry_count);
2376 usleep(500*1000);
2377 if (0 == inner_get_status_info(idx, &curr_state))
2378 {
2379 if (strcmp(status, STATE_COMPLETED) == 0)
2380 {
2381 return 0;
2382 }
2383 else if (retry_count == 4) //not ok in 2s, do reconnect
2384 {
2385 DO_REQUEST("RECONNECT");
2386 return inner_check_ap_connected(idx, retry_count+1);
2387 }
you.chencba13492023-05-19 13:53:43 +08002388 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002389 {
2390 printf("retry 10 time\n");
2391 return -1;
2392 }
2393 else
2394 {
you.chen6d247052023-06-01 16:39:54 +08002395 if (strcmp(status, STATE_DISCONNECTED) == 0)
2396 {
2397 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2398 DO_REQUEST(LYNQ_WIFI_CMD);
2399 }
you.chenb95401e2023-05-12 19:39:06 +08002400 return inner_check_ap_connected(idx, retry_count+1);
2401 }
2402 }
2403 return -1;
2404}
qs.xiong1af5daf2022-03-14 09:12:12 -04002405
qs.xiongf1b525b2022-03-31 00:58:23 -04002406int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002407{
you.chen35020192022-05-06 11:30:57 +08002408 char LYNQ_WIFI_CMD[128]={0};
2409 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2410 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002411 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002412 CHECK_IDX(idx, CTRL_AP);
2413
2414 CHECK_WPA_CTRL(CTRL_AP);
2415
you.chen0df3e7e2023-05-10 15:56:26 +08002416 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002417 {
you.chen0df3e7e2023-05-10 15:56:26 +08002418 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002419 return -1;
2420 }
you.chen35020192022-05-06 11:30:57 +08002421
2422 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2423 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2424
2425 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2426 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2427
you.chenc9928582023-04-24 15:39:37 +08002428 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2429 if (ret != 0)
2430 {
2431 RLOGE("lynq_wifi_ap_start excute script fail");
2432 return -1;
2433 }
2434
you.chenb95401e2023-05-12 19:39:06 +08002435 if (inner_check_ap_connected(idx, 0) != 0)
2436 {
2437 return -1;
2438 }
2439
you.chen0df3e7e2023-05-10 15:56:26 +08002440 check_tether_and_notify();
qs.xiong44fac672023-08-29 16:15:55 +08002441 if (g_ap_tmp_watcher_pid == 0)
2442 {
2443 if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
2444 {
2445 g_ap_tmp_watcher_pid = 0;
2446 RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
2447 return -1;
2448 }
2449 RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
2450 }
you.chen0df3e7e2023-05-10 15:56:26 +08002451
qs.xiong9fbf74e2023-03-28 13:38:22 +08002452 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002453}
2454
qs.xiongf1b525b2022-03-31 00:58:23 -04002455int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002456{
you.chen35020192022-05-06 11:30:57 +08002457 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002458}
2459
qs.xiongf1b525b2022-03-31 00:58:23 -04002460int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002461{
you.chen35020192022-05-06 11:30:57 +08002462 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002463
you.chen35020192022-05-06 11:30:57 +08002464 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002465
you.chen35020192022-05-06 11:30:57 +08002466 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002467
you.chen35020192022-05-06 11:30:57 +08002468 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2469
2470 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2471
you.chenb4b121c2022-05-06 17:50:16 +08002472// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002473
2474 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2475 if (ret != 0)
2476 {
2477 RLOGE("lynq_wifi_ap_start excute script fail");
2478 return -1;
2479 }
qs.xiong44fac672023-08-29 16:15:55 +08002480 g_ap_tmp_watcher_stop_flag = 1;
2481 if (g_ap_tmp_watcher_pid != 0)
2482 pthread_join(g_ap_tmp_watcher_pid, NULL);
2483 g_ap_tmp_watcher_pid = 0;
qs.xiongae96e1f2023-08-23 17:08:36 +08002484
qs.xiong9fbf74e2023-03-28 13:38:22 +08002485 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002486}
qs.xiong1af5daf2022-03-14 09:12:12 -04002487
qs.xiongf1b525b2022-03-31 00:58:23 -04002488int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002489{
you.chen35020192022-05-06 11:30:57 +08002490 char lynq_disable_cmd[128] = {0};
2491 char lynq_select_cmd[128] = {0};
2492 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002493 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002494 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002495
you.chen35020192022-05-06 11:30:57 +08002496 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002497 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2498 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2499
2500 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2501 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2502 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002503
qs.xiong9fbf74e2023-03-28 13:38:22 +08002504 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002505}
2506
qs.xiongf1b525b2022-03-31 00:58:23 -04002507int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002508{
you.chen35020192022-05-06 11:30:57 +08002509 char lynq_disable_cmd[128] = {0};
2510 char lynq_select_cmd[128] = {0};
2511 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002512 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002513 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002514
you.chen35020192022-05-06 11:30:57 +08002515 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002516
you.chen35020192022-05-06 11:30:57 +08002517 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2518 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2519
2520 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2521 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2522 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002523
qs.xiong9fbf74e2023-03-28 13:38:22 +08002524 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002525}
qs.xiongf1b525b2022-03-31 00:58:23 -04002526
you.chen35020192022-05-06 11:30:57 +08002527int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002528{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002529 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002530 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002531 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002532 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002533 RLOGD("enter lynq_ap_password_set");
2534 if( password == NULL )
2535 {
2536 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002537 return -1;
2538 }
2539 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002540 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002541 if(pass_len < 8 || pass_len >= 64)
2542 {
2543 RLOGE("[lynq_ap_password_set]input password len not in rage");
2544 return -1;
you.chen35020192022-05-06 11:30:57 +08002545 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002546
you.chen35020192022-05-06 11:30:57 +08002547 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002548
qs.xiong9fbf74e2023-03-28 13:38:22 +08002549 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2550 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002551 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002552 return -1;
2553 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002554 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2555 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002556 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2557 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002558 }
2559
you.chen35020192022-05-06 11:30:57 +08002560 CHECK_WPA_CTRL(CTRL_AP);
2561
qs.xiong9fbf74e2023-03-28 13:38:22 +08002562 if (auth == LYNQ_WIFI_AUTH_WEP)
2563 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002564 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002565 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2566 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2567 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2568 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2569 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002570 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2571 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002572 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 +08002573 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2574 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2575 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002576 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2577 {
2578
qs.xiongc8d92a62023-03-29 17:36:14 +08002579 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 +08002580 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002581 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002582 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2583 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2584
2585 }
2586 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002587 {
2588 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002589 return -1;
2590 }
you.chen35020192022-05-06 11:30:57 +08002591
you.chen35020192022-05-06 11:30:57 +08002592 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002593
qs.xiong9fbf74e2023-03-28 13:38:22 +08002594 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002595}
2596
you.chen35020192022-05-06 11:30:57 +08002597int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002598{
you.chen35020192022-05-06 11:30:57 +08002599 FILE * fp;
2600 int len, ret;
2601 int count, index;
2602 char *split_lines[128] = {0};
2603 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002604 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002605
you.chen35020192022-05-06 11:30:57 +08002606 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002607
you.chen35020192022-05-06 11:30:57 +08002608 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2609// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002610 if (NULL == fp)
2611 {
2612 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002613 return -1;
2614 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002615
you.chen35020192022-05-06 11:30:57 +08002616 buff = alloca(MAX_RET);
2617 fseek(fp, 0, SEEK_SET);
2618 len = fread(buff, 1, MAX_RET, fp);
2619 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002620
qs.xiong9fbf74e2023-03-28 13:38:22 +08002621 for(index=0; index < len; index ++)
2622 {
2623 if (memcmp(buff + index, "network={", 9) != 0)
2624 {
you.chen35020192022-05-06 11:30:57 +08002625 continue;
2626 }
2627 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002628 for (; index < len; index ++ )
2629 {
2630 if (buff[index] != '}')
2631 {
you.chen35020192022-05-06 11:30:57 +08002632 continue;
2633 }
2634 buff[index] = '\0';
2635 break;
2636 }
2637 len = buff + index - p;
2638 }
2639
2640 count = lynq_split(p, len, '\n', split_lines);
2641
2642 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002643 for(index=0; index < count; index++)
2644 {
you.chen35020192022-05-06 11:30:57 +08002645 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002646 if (p != NULL)
2647 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002648 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002649 if (*p == '\"')
2650 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002651 p++;
2652 }
you.chen35020192022-05-06 11:30:57 +08002653 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002654 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2655 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002656 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002657 if (*p == '\"')
2658 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002659 p++;
2660 }
2661 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002662 else
2663 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002664 continue;
you.chen35020192022-05-06 11:30:57 +08002665 }
2666
2667 strcpy(password, p);
2668
qs.xiong9fbf74e2023-03-28 13:38:22 +08002669 while(*password != '\0')
2670 {
2671 if (*password == '\"')
2672 {
you.chen35020192022-05-06 11:30:57 +08002673 *password = '\0';
2674 break;
2675 }
2676 password++;
2677 }
2678 ret = 0;
2679 break;
2680 } //end for(index=0; index < count; index++)
2681
2682 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002683}
2684
you.chen35020192022-05-06 11:30:57 +08002685static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2686 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002687 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002688
qs.xiong9fbf74e2023-03-28 13:38:22 +08002689 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2690 {
you.chen35020192022-05-06 11:30:57 +08002691 return -1;
2692 }
2693
2694 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002695
qs.xiong9fbf74e2023-03-28 13:38:22 +08002696 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2697 {
2698 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002699 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002700 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002701 {
you.chena6cd55a2022-05-08 12:20:18 +08002702 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002703 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002704 }
you.chen70f377f2023-04-14 18:17:09 +08002705 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2706 {
2707 return 0;
2708 }
you.chena6cd55a2022-05-08 12:20:18 +08002709 }
2710 }
you.chen70f377f2023-04-14 18:17:09 +08002711 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2712 {
2713 return 0;
2714 }
2715
qs.xiong9fbf74e2023-03-28 13:38:22 +08002716 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2717 {
you.chen70f377f2023-04-14 18:17:09 +08002718 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002719 return -1;
2720 }
2721 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2722 {
2723
you.chen70f377f2023-04-14 18:17:09 +08002724 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2725 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002726 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2727 {
2728
2729 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002730 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 }else
2732 {
you.chen70f377f2023-04-14 18:17:09 +08002733 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002734 *auth = -1;
2735 return -1;
2736 }
you.chen35020192022-05-06 11:30:57 +08002737 return 0;
2738}
2739
2740int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002741{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002742 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002743 int pass_len, net_no, count, index;
2744 char lynq_tmp_cmd[300]={0};
2745 int net_no_list[128];
2746 lynq_wifi_auth_s net_auth;
2747 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002748 if(pass_len < 8 || pass_len >= 64)
2749 {
2750 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002751 return -1;
2752 }
2753
2754 CHECK_IDX(idx, CTRL_STA);
2755
2756 net_no = -1;
2757 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2758
qs.xiong9fbf74e2023-03-28 13:38:22 +08002759 for (index=0; index < count; index++)
2760 {
you.chen35020192022-05-06 11:30:57 +08002761 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002762 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2763 {
you.chen35020192022-05-06 11:30:57 +08002764 net_no = net_no_list[index];
2765 break;
2766 }
2767 }
2768
qs.xiong9fbf74e2023-03-28 13:38:22 +08002769 if (net_no < 0)
2770 {
you.chen35020192022-05-06 11:30:57 +08002771 return -1;
2772 }
2773
2774 CHECK_WPA_CTRL(CTRL_STA);
2775
2776 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2777
2778 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2779 DO_OK_FAIL_REQUEST(cmd_save_config);
2780
2781 return 0;
2782}
2783
2784int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2785
2786 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002787 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002788 int count, index;
2789 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002790 char *buff, *p, *ssid, *ssid_end_flag;
2791 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002792 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002793
you.chen755332b2022-08-06 16:59:10 +08002794 network_len = 0;
2795 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002796 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002797
you.chen35020192022-05-06 11:30:57 +08002798 CHECK_IDX(idx, CTRL_STA);
2799
qs.xiong9fbf74e2023-03-28 13:38:22 +08002800 if (NULL == password)
2801 {
2802 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002803 return -1;
2804 }
2805
you.chen35020192022-05-06 11:30:57 +08002806 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002807 if (NULL == fp)
2808 {
2809 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002810 return -1;
2811 }
2812
you.chen6d247052023-06-01 16:39:54 +08002813 fseek(fp, 0, SEEK_END);
2814 len = ftell(fp);
2815 buff = malloc(len + 1);
2816
2817 if (buff == NULL)
2818 {
2819 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2820 return -1;
2821 }
2822
you.chen35020192022-05-06 11:30:57 +08002823 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002824 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002825 fclose(fp);
2826
qs.xiong9fbf74e2023-03-28 13:38:22 +08002827 for(index=0; index < len; index ++)
2828 {
2829 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002830 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002831 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002832 {
you.chen35020192022-05-06 11:30:57 +08002833 continue;
2834 }
you.chen6d247052023-06-01 16:39:54 +08002835 p = buff + index + 9;
2836 for (; index < len; index ++ )
2837 {
2838 if (buff[index] != '}')
2839 {
2840 continue;
2841 }
2842 buff[index] = '\0';
2843 break;
2844 }
2845 network_len = buff + index - p;
2846 break;
you.chen35020192022-05-06 11:30:57 +08002847 }
2848
qs.xiongb3f26af2023-02-17 18:41:07 +08002849 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002850 {
2851 if (buff != NULL)
2852 {
2853 free(buff);
2854 }
2855
qs.xiongb3f26af2023-02-17 18:41:07 +08002856 return -1;
you.chen6d247052023-06-01 16:39:54 +08002857 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002858
you.chend2fef3f2023-02-13 10:50:35 +08002859 ssid = strstr(p, "ssid=");
2860 if (ssid != NULL) {
2861 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002862 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002863 {
you.chend2fef3f2023-02-13 10:50:35 +08002864 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2865 break;
2866 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002867 else
you.chen6d247052023-06-01 16:39:54 +08002868 {
you.chend2fef3f2023-02-13 10:50:35 +08002869 ssid_end_flag = strstr(ssid, "\n");
2870 if (ssid_end_flag != NULL)
2871 {
2872 ssid_len = (ssid_end_flag - ssid) / 2;
2873 for(i=0; i<ssid_len; i++)
2874 {
2875 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2876 }
2877 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2878 break;
2879 }
2880 }
you.chen35020192022-05-06 11:30:57 +08002881 }
you.chend2fef3f2023-02-13 10:50:35 +08002882
you.chen35020192022-05-06 11:30:57 +08002883 }
2884
qs.xiong9fbf74e2023-03-28 13:38:22 +08002885 if (index >= len || NULL == p || network_len <= 0)
2886 {
you.chen6d247052023-06-01 16:39:54 +08002887 if (buff != NULL)
2888 {
2889 free(buff);
2890 }
you.chen35020192022-05-06 11:30:57 +08002891 return -1;
2892 }
2893
you.chen755332b2022-08-06 16:59:10 +08002894 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002895
2896 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002897 for(index=0; index < count; index++)
2898 {
you.chen35020192022-05-06 11:30:57 +08002899 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002901 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002902 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002903 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002904 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002905 p++;
2906 }
you.chen35020192022-05-06 11:30:57 +08002907 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002908 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002909 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002910 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002911 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002912 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002913 p++;
2914 }
2915 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002916 else
you.chen6d247052023-06-01 16:39:54 +08002917 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002918 continue;
you.chen35020192022-05-06 11:30:57 +08002919 }
2920
qs.xiong13673462023-02-21 19:12:54 +08002921 if (*p == '\"')
2922 p++;
2923 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002924
qs.xiong13673462023-02-21 19:12:54 +08002925 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002926 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002927 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002928 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002929 {
you.chen35020192022-05-06 11:30:57 +08002930 *password = '\0';
2931 break;
2932 }
2933 password++;
2934 }
2935 ret = 0;
2936 break;
2937 } //end for(index=0; index < count; index++)
2938
you.chen6d247052023-06-01 16:39:54 +08002939 if (buff != NULL)
2940 {
2941 free(buff);
2942 }
2943
you.chen35020192022-05-06 11:30:57 +08002944 return ret;
2945}
2946
2947static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2948{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002949 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002950
qs.xiong9fbf74e2023-03-28 13:38:22 +08002951 if (sta_ssid == NULL)
2952 {
2953 RLOGE("sta_ssid is null\n");
2954 return -1;
you.chen35020192022-05-06 11:30:57 +08002955 }
2956
qs.xiong9fbf74e2023-03-28 13:38:22 +08002957 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002958
2959 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2960
2961 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2962// DO_OK_FAIL_REQUEST(cmd_save_config);
2963
qs.xiong9fbf74e2023-03-28 13:38:22 +08002964 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002965
2966}
2967
you.chen35020192022-05-06 11:30:57 +08002968static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002969{
you.chen35020192022-05-06 11:30:57 +08002970 char lynq_disable_cmd[128]={0};
2971 char lynq_select_cmd[128]={0};
2972
2973 CHECK_WPA_CTRL(CTRL_STA);
2974
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 if (save != 0)
2976 {
you.chenc29444e2022-06-07 18:01:16 +08002977 if (start_flag != 0)
2978 {
2979 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2980 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2981 }
2982 else
2983 {
2984 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2985 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2986 }
you.chen35020192022-05-06 11:30:57 +08002987 DO_OK_FAIL_REQUEST(cmd_save_config);
2988 }
2989
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 if (start_flag == 0)
2991 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002992 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002993 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2994 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002995 else
2996 {
you.chen35020192022-05-06 11:30:57 +08002997 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2998 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2999 }
3000
3001 return 0;
3002}
3003
3004int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
3005{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003006 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08003007 CHECK_IDX(idx, CTRL_STA);
3008
you.chen6c2dd9c2022-05-16 17:55:28 +08003009 curr_status_info curr_state;
3010 ap_info_s ap_info;
3011 curr_state.ap = &ap_info;
3012 curr_state.state = NULL;
3013
qs.xiong9fbf74e2023-03-28 13:38:22 +08003014 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
3015 {
you.chend2fef3f2023-02-13 10:50:35 +08003016 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08003017 return 0;
3018 }
3019
3020 return -1;
you.chen35020192022-05-06 11:30:57 +08003021}
3022
3023int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
3024{
you.chen9ac66392022-08-06 17:01:16 +08003025 scan_info_s *scan_list = NULL;
3026 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08003027 int scan_len=0;
3028 int save_len=0;
3029 int best_index = -1;
3030 int best_scan_index = -1;
3031 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08003032 int i, j, ret;
3033
3034 ret = -1;
you.chen35020192022-05-06 11:30:57 +08003035
3036 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003037 if (info == NULL)
3038 {
you.chen35020192022-05-06 11:30:57 +08003039 return -1;
3040 }
3041
3042 curr_status_info curr_state;
3043 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08003044 char status[64];
you.chen35020192022-05-06 11:30:57 +08003045
you.chen9ac66392022-08-06 17:01:16 +08003046 memset(&ap_info, 0, sizeof (ap_info));
3047 memset(status, 0, sizeof (status));
3048
3049 curr_state.ap = &ap_info;
3050 curr_state.state = status;
3051
qs.xiong9fbf74e2023-03-28 13:38:22 +08003052 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
3053 {
you.chen35020192022-05-06 11:30:57 +08003054 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08003055 if (strcmp(status, STATE_COMPLETED) == 0)
3056 {
3057 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
3058 }
3059 else
3060 {
3061 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3062 }
you.chen593621d2023-04-27 17:52:44 +08003063 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08003064 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08003065 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003066 return 0;
3067 }
3068
you.chen9ac66392022-08-06 17:01:16 +08003069 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08003070 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003071 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
3072 {
you.chen9ac66392022-08-06 17:01:16 +08003073 if (NULL != scan_list)
3074 {
3075 free(scan_list);
3076 }
you.chen35020192022-05-06 11:30:57 +08003077 return -1;
3078 }
3079
qs.xiong9fbf74e2023-03-28 13:38:22 +08003080 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
3081 {
you.chen9ac66392022-08-06 17:01:16 +08003082 if (NULL != scan_list)
3083 {
3084 free(scan_list);
3085 }
3086 if (NULL != save_list)
3087 {
3088 free(save_list);
3089 }
you.chen35020192022-05-06 11:30:57 +08003090 return -1;
3091 }
3092
qs.xiong9fbf74e2023-03-28 13:38:22 +08003093 for (i=0; i < save_len; i++)
3094 {
3095 for (j=0; j < scan_len; j++)
3096 {
you.chen35020192022-05-06 11:30:57 +08003097 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08003098 && save_list[i].base_info.auth == scan_list[j].auth)
3099 {
3100 if (best_rssi == 0)
3101 {
you.chen9ac66392022-08-06 17:01:16 +08003102 best_index = i;
you.chen35020192022-05-06 11:30:57 +08003103 best_rssi = scan_list[j].rssi;
3104 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003105 else if (best_rssi > scan_list[j].rssi)
3106 {
you.chen35020192022-05-06 11:30:57 +08003107 best_index = i;
3108 best_scan_index = j;
3109 best_rssi = scan_list[j].rssi;
3110 }
you.chend2fef3f2023-02-13 10:50:35 +08003111 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 +08003112 break;
3113 }
3114 }
3115 }
3116
qs.xiong9fbf74e2023-03-28 13:38:22 +08003117 if (best_index >= 0)
3118 {
you.chen35020192022-05-06 11:30:57 +08003119 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08003120 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 +08003121 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
3122 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08003123 ret = 0;
you.chen35020192022-05-06 11:30:57 +08003124 }
3125
you.chen9ac66392022-08-06 17:01:16 +08003126 if (NULL != scan_list)
3127 {
3128 free(scan_list);
3129 }
3130 if (NULL != save_list)
3131 {
3132 free(save_list);
3133 }
3134
3135 return ret;
you.chen35020192022-05-06 11:30:57 +08003136}
3137
3138static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
3139{
qs.xiongc8d92a62023-03-29 17:36:14 +08003140 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08003141 char lynq_ket_mgmt_cmd[64]={0};
3142 char lynq_pairwise_cmd[64]={0};
3143 char lynq_psk_cmd[64]={0};
3144
3145 CHECK_WPA_CTRL(CTRL_STA);
3146
qs.xiong9fbf74e2023-03-28 13:38:22 +08003147 switch(auth)
3148 {
3149 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08003150 {
3151 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003152
you.chen35020192022-05-06 11:30:57 +08003153 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003154// DO_OK_FAIL_REQUEST(cmd_save_config);
3155 break;
3156 }
3157 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08003158 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08003159 {
3160 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
3161 {
you.chen35020192022-05-06 11:30:57 +08003162 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
3163 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003164 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
3165 {
you.chena6cd55a2022-05-08 12:20:18 +08003166 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08003167 }
3168 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
3169 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04003170
you.chen35020192022-05-06 11:30:57 +08003171 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3172 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3173 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003174
qs.xiong9fbf74e2023-03-28 13:38:22 +08003175 if (password != NULL)
3176 {
you.chen35020192022-05-06 11:30:57 +08003177 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3178 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003179 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003180 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003181
you.chen35020192022-05-06 11:30:57 +08003182// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003183 break;
3184 }
3185 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3186 {
qs.xiong3e506812023-04-06 11:08:48 +08003187 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003188 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003189 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3190 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3191
qs.xiong3e506812023-04-06 11:08:48 +08003192 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003193 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3194 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3195 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3196
3197 break;
3198 }
3199 case LYNQ_WIFI_AUTH_WPA3_PSK:
3200 {
qs.xiong3e506812023-04-06 11:08:48 +08003201 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003202 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003203 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3204 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3205
qs.xiong3e506812023-04-06 11:08:48 +08003206 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003207 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3208 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3209 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3210
3211 break;
3212 }
3213 default:
3214 return -1;
you.chen35020192022-05-06 11:30:57 +08003215 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003216
qs.xiong9fbf74e2023-03-28 13:38:22 +08003217 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003218}
qs.xiong7a105ce2022-03-02 09:43:11 -05003219
you.chen35020192022-05-06 11:30:57 +08003220static int inner_get_curr_net_no(int interface) {
3221 curr_status_info curr_state;
3222 curr_state.ap = NULL;
3223 curr_state.state = NULL;
3224
qs.xiong9fbf74e2023-03-28 13:38:22 +08003225 if (0 != inner_get_status_info(interface, &curr_state))
3226 {
you.chen35020192022-05-06 11:30:57 +08003227 return -1;
3228 }
3229
3230 return curr_state.net_no;
3231}
3232
3233int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003234{
you.chen35020192022-05-06 11:30:57 +08003235 int net_no;
3236 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003237
you.chen35020192022-05-06 11:30:57 +08003238 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003239
qs.xiong9fbf74e2023-03-28 13:38:22 +08003240 if (net_no < 0)
3241 {
you.chen35020192022-05-06 11:30:57 +08003242 return -1;
3243 }
3244
3245 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003246}
3247
you.chenb95401e2023-05-12 19:39:06 +08003248int 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 -05003249{
you.chen35020192022-05-06 11:30:57 +08003250 int count, net_no, index;
3251 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003252 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003253 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003254 curr_status_info curr_state;
3255 ap_info_s ap_info;
3256 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003257
qs.xiong9fbf74e2023-03-28 13:38:22 +08003258 if (ssid == NULL || *ssid == '\0')
3259 {
3260 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003261 return -1;
3262 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003263
qs.xiong9fbf74e2023-03-28 13:38:22 +08003264 if (LYNQ_WIFI_AUTH_OPEN != auth)
3265 {
3266 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003267 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003268 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003269 return -1;
3270 }
3271 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003272
you.chen70f377f2023-04-14 18:17:09 +08003273
3274 pthread_mutex_lock(&s_global_check_mutex);
3275 if (s_sta_status != INNER_STA_STATUS_INIT)
3276 {
3277 s_sta_status = INNER_STA_STATUS_CANCEL;
3278 pthread_cond_signal(&s_global_check_cond);
3279 }
3280 pthread_mutex_unlock(&s_global_check_mutex);
3281
you.chen35020192022-05-06 11:30:57 +08003282 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003283 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003284
3285 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003286 memset(&ap_info, 0, sizeof (ap_info));
3287 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003288
you.chen70f377f2023-04-14 18:17:09 +08003289 curr_state.ap = &ap_info;
3290 curr_state.state = status;
3291
3292 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003293 {
you.chen70f377f2023-04-14 18:17:09 +08003294 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3295 {
3296 net_no = curr_state.net_no;
3297 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3298 && strcmp(ap_info.psw, psw) == 0)
3299 {
3300 RLOGD("already connected\n");
3301
3302 pthread_mutex_lock(&s_global_check_mutex);
3303 s_sta_status = INNER_STA_STATUS_CONNECTED;
3304 pthread_cond_signal(&s_global_check_cond);
3305 pthread_mutex_unlock(&s_global_check_mutex);
3306 return 0;
3307 }
you.chen35020192022-05-06 11:30:57 +08003308 }
3309 }
3310
you.chen70f377f2023-04-14 18:17:09 +08003311 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003312 {
you.chen70f377f2023-04-14 18:17:09 +08003313 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3314
3315 for (index=0; index < count; index++)
3316 {
3317 net_auth = -1;
3318 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3319 {
3320 net_no = net_no_list[index];
3321 break;
3322 }
you.chen35020192022-05-06 11:30:57 +08003323 }
3324
you.chen70f377f2023-04-14 18:17:09 +08003325 if (net_no < 0)
3326 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003327 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3328 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3329 {
3330 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3331 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3332 DO_OK_FAIL_REQUEST(rm_net_cmd);
3333 }
you.chen70f377f2023-04-14 18:17:09 +08003334 net_no = lynq_add_network(CTRL_STA);
3335 if (net_no == -1)
3336 {
3337 return -1;
3338 }
3339
3340 RLOGD("net no is %d\n", net_no);
3341 if (0 != inner_set_sta_ssid(net_no, ssid))
3342 {
3343 return -1;
3344 }
you.chen35020192022-05-06 11:30:57 +08003345 }
3346 }
3347
qs.xiong9fbf74e2023-03-28 13:38:22 +08003348 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3349 {
you.chen35020192022-05-06 11:30:57 +08003350 return -1;
3351 }
3352
you.chen70f377f2023-04-14 18:17:09 +08003353
3354 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003355 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003356 usleep(200*1000);
3357
3358 ret = inner_sta_start_stop(net_no, 1, 1);
3359
3360 pthread_mutex_lock(&s_global_check_mutex);
3361 s_sta_status = INNER_STA_STATUS_CONNECTING;
3362 strcpy(s_sta_current_connecting_ssid, ssid);
3363 struct timeval now;
3364 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003365 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003366 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3367 pthread_cond_signal(&s_global_check_cond);
3368 pthread_mutex_unlock(&s_global_check_mutex);
3369 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003370}
3371
you.chenb95401e2023-05-12 19:39:06 +08003372int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3373{
3374 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3375}
3376
you.chen35020192022-05-06 11:30:57 +08003377int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003378{
you.chen35020192022-05-06 11:30:57 +08003379 ap_info_s ap;
3380 curr_status_info curr_state;
3381 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003382
qs.xiong9fbf74e2023-03-28 13:38:22 +08003383 if (ssid == NULL || *ssid == '\0')
3384 {
3385 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003386 return -1;
3387 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003388
you.chen35020192022-05-06 11:30:57 +08003389 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003390
you.chen35020192022-05-06 11:30:57 +08003391 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003392 curr_state.state = NULL;
3393
qs.xiong9fbf74e2023-03-28 13:38:22 +08003394 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3395 {
you.chen35020192022-05-06 11:30:57 +08003396 return 0;
3397 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003398
qs.xiong9fbf74e2023-03-28 13:38:22 +08003399 if (strcmp(ap.ap_ssid, ssid) != 0)
3400 {
you.chen35020192022-05-06 11:30:57 +08003401 return 0;
3402 }
3403
you.chen70f377f2023-04-14 18:17:09 +08003404 pthread_mutex_lock(&s_global_check_mutex);
3405 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3406 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003407 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003408}
qs.xiong97fa59b2022-04-07 05:41:29 -04003409
qs.xiongc93bf2b2023-08-25 10:22:08 +08003410int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3411{
3412 ap_info_s ap;
3413 curr_status_info curr_state;
3414 ap.ap_ssid[0] = '\0';
3415
3416 if (ssid == NULL || *ssid == '\0')
3417 {
3418 RLOGE("input ssid is NULL\n");
3419 return -1;
3420 }
3421
3422 CHECK_IDX(idx, CTRL_STA);
3423
3424
3425 curr_state.ap = &ap;
3426 curr_state.state = NULL;
3427
3428 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3429 {
3430 return 0;
3431 }
3432
3433 if (strcmp(ap.ap_ssid, ssid) != 0)
3434 {
3435 return 0;
3436 }
3437
3438 pthread_mutex_lock(&s_global_check_mutex);
3439 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3440 pthread_mutex_unlock(&s_global_check_mutex);
3441 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3442
3443}
3444
3445
you.chena6cd55a2022-05-08 12:20:18 +08003446int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3447{
qs.xiongad2f89d2023-01-18 13:17:41 +08003448// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3449// const char *lynq_reconnect_cmd = "RECONNECT";
3450 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3451 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3452// 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 -05003453
you.chen35020192022-05-06 11:30:57 +08003454 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003455 CHECK_WPA_CTRL(CTRL_STA);
3456
you.chenc9928582023-04-24 15:39:37 +08003457 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3458 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003459 {
you.chenc9928582023-04-24 15:39:37 +08003460 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003461 return -1;
3462 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003463
qs.xiongad2f89d2023-01-18 13:17:41 +08003464 system(lynq_enable_sta_cmd);
3465 system(lynq_reconnect_cmd);
3466// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003467 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003468}
3469
you.chen6d247052023-06-01 16:39:54 +08003470static int inner_get_status_info_state (int interface, char *state) {
3471 curr_status_info curr_state;
3472 curr_state.ap = NULL;
3473 curr_state.state = state;
3474 return inner_get_status_info(interface, &curr_state);
3475}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003476
3477int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3478{
3479
3480 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3481
3482 CHECK_IDX(idx, CTRL_STA);
3483 CHECK_WPA_CTRL(CTRL_STA);
3484
3485 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3486 if (ret != 0)
3487 {
3488 RLOGE("lynq_wifi_ap_start excute script fail");
3489 return -1;
3490 }
3491
3492// system(lynq_enable_sta_cmd);
3493 system(lynq_reconnect_cmd);
3494
3495 return 0;
3496}
3497
3498
you.chen35020192022-05-06 11:30:57 +08003499int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003500{
qs.xiongad2f89d2023-01-18 13:17:41 +08003501// char lynq_disable_network_cmd[MAX_CMD];
3502// curr_status_info curr_state;
3503// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003504 int i=0;
3505 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003506
you.chen6d247052023-06-01 16:39:54 +08003507// 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 +08003508 CHECK_IDX(idx, CTRL_STA);
3509 CHECK_WPA_CTRL(CTRL_STA);
3510
you.chen6d247052023-06-01 16:39:54 +08003511// system(lynq_disable_sta_cmd);
3512 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003513 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003514
3515 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3516 if (ret != 0)
3517 {
3518 RLOGE("lynq_wifi_ap_start excute script fail");
3519 return -1;
3520 }
3521
you.chen6d247052023-06-01 16:39:54 +08003522 for (i=0; i < 30; i++) // to check if sta is realy stoped
3523 {
3524 if (inner_get_status_info_state(idx, state) != 0)
3525 {
3526 break;
3527 }
3528
3529 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3530 {
3531 break;
3532 }
3533 RLOGD("lynq_wifi_ap_start curr state %s", state);
3534 usleep(SLEEP_TIME_ON_IDLE);
3535 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003536
you.chena6cd55a2022-05-08 12:20:18 +08003537 return 0;
3538// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003539}
qs.xiongfcc914b2023-07-06 21:16:20 +08003540int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3541{
3542 char LYNQ_DISABLE_CMD[128]={0};
3543 CHECK_IDX(idx, CTRL_STA);
3544 CHECK_WPA_CTRL(CTRL_STA);
3545 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3546 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3547 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3548 return 0;
3549}
qs.xiong7a105ce2022-03-02 09:43:11 -05003550
you.chen35020192022-05-06 11:30:57 +08003551//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3552// int i, count;
3553// char *p;
3554// const char * FLAG_SSID = "ssid=";
3555// const char * FLAG_SBSID = "bssid=";
3556// const char * FLAG_KEY_MGMT = "key_mgmt=";
3557// const char * FLAG_FREQ = "freq=";
3558// char lynq_sta_cmd[MAX_CMD];
3559// char *split_lines[128] = {0};
3560
3561// CHECK_WPA_CTRL(CTRL_AP);
3562
3563// sprintf(lynq_sta_cmd, "STA %s", bssid);
3564
3565// DO_REQUEST(lynq_sta_cmd);
3566
3567// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3568
3569// for(i=0; i < count; i++) {
3570// p = strstr(split_lines[i], FLAG_SSID);
3571// if (p != NULL) {
3572// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3573// continue;
3574// }
3575// }
3576
3577// lynq_get_interface_ip(idx, ap->ap_ip);
3578// lynq_ap_password_set(idx, ap->psw);
3579
3580// return 0;
3581//}
3582
3583static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3584 curr_status_info curr_state;
3585 curr_state.ap = ap;
3586 curr_state.state = NULL;
3587 return inner_get_status_info(interface, &curr_state);
3588}
3589
3590int 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 -04003591{
you.chend2fef3f2023-02-13 10:50:35 +08003592 int index, line_count;
3593 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003594 const char *lynq_first_sta_cmd = "STA-FIRST";
3595 char lynq_next_sta_cmd[MAX_CMD];
3596 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003597 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003598
you.chen35020192022-05-06 11:30:57 +08003599 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003600
you.chen35020192022-05-06 11:30:57 +08003601 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003602
you.chenb95401e2023-05-12 19:39:06 +08003603 // ap_info_s * tmp_ap;
3604 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003605 if (ap == NULL || list == NULL || len == NULL)
3606 {
3607 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003608 return -1;
3609 }
3610
you.chenb95401e2023-05-12 19:39:06 +08003611 // ap = &tmp_ap;
3612 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003613 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003614 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003615
you.chenb95401e2023-05-12 19:39:06 +08003616 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003617 {
you.chenb95401e2023-05-12 19:39:06 +08003618 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003619 return -1;
3620 }
3621
3622 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3623 lynq_ap_password_get(idx, (*ap)->psw);
3624
you.chen35020192022-05-06 11:30:57 +08003625 DO_REQUEST(lynq_first_sta_cmd);
3626
3627 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003628 while (reply_len > 0)
3629 {
3630 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003631 {
you.chen35020192022-05-06 11:30:57 +08003632 break;
3633 }
3634 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3635 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3636 strcpy(bssid[index], split_lines[0]);
3637 index++;
3638 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3639 reply_len = MAX_RET;
3640 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003641 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 +08003642 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003643 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003644 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003645 break;
3646 }
3647 }
3648
3649 *len = index;
3650
3651 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003652 for (index=0; index < *len; index++)
3653 {
you.chend2fef3f2023-02-13 10:50:35 +08003654 dev_info = &(*list)[index];
3655 memset(dev_info, 0, sizeof(device_info_s));
3656 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3657 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3658 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3659 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003660 free(bssid[index]);
3661 }
3662
3663 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003664}
3665
you.chen35020192022-05-06 11:30:57 +08003666int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003667{
you.chen35020192022-05-06 11:30:57 +08003668 int i, count, index, count_words;
3669 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3670 char *split_lines[128] = {0};
3671 char *split_words[128] = {0};
3672 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003673
qs.xiong9fbf74e2023-03-28 13:38:22 +08003674 if (list == NULL || len == NULL)
3675 {
you.chen35020192022-05-06 11:30:57 +08003676 return -1;
3677 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003678
you.chen9ac66392022-08-06 17:01:16 +08003679 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3680 {
3681 usleep(100 * 1000);
3682 }
3683
you.chen35020192022-05-06 11:30:57 +08003684 CHECK_IDX(idx, CTRL_STA);
3685
3686 CHECK_WPA_CTRL(CTRL_STA);
3687
3688 DO_REQUEST(lynq_scan_result_cmd);
3689
3690 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3691 *len = count - 1;
3692 *list = malloc(sizeof (scan_info_s) * *len);
3693
3694 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003695 for (index=0; index <count_words; index++)
3696 {
3697 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003698 }
3699
qs.xiong9fbf74e2023-03-28 13:38:22 +08003700 for(index = 1;index < count; index++)
3701 {
3702 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003703 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003704 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3705 if (count_words < 4)
3706 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003707 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003708 //bssid / frequency / signal level / flags / ssid
3709 p = (*list) + index - 1;
3710 strcpy(p->mac, split_words[0]);
3711 p->band = convert_band_from_freq(atoi(split_words[1]));
3712 p->rssi = -1 * atoi( split_words[2]);
3713 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003714 if (count_words == 4) // ssid hided
3715 {
3716 p->ssid[0] = '\0';
3717 }
3718 else
3719 {
3720 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3721 }
you.chen35020192022-05-06 11:30:57 +08003722 }
3723
3724 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003725}
qs.xiong97fa59b2022-04-07 05:41:29 -04003726
you.chen35020192022-05-06 11:30:57 +08003727int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3728{
3729 int count, net_no, index;
3730 int net_no_list[128];
3731 lynq_wifi_auth_s net_auth;
3732 char lynq_remove_cmd[MAX_CMD];
3733
qs.xiong9fbf74e2023-03-28 13:38:22 +08003734 if (ssid == NULL || *ssid == '\0')
3735 {
3736 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003737 return -1;
3738 }
3739
3740 CHECK_IDX(idx, CTRL_STA);
3741
3742 CHECK_WPA_CTRL(CTRL_STA);
3743
3744 net_no = -1;
3745 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3746
qs.xiong9fbf74e2023-03-28 13:38:22 +08003747 for (index=0; index < count; index++)
3748 {
you.chen35020192022-05-06 11:30:57 +08003749 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003750 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3751 {
you.chen35020192022-05-06 11:30:57 +08003752 net_no = net_no_list[index];
3753 break;
3754 }
3755 }
3756
qs.xiong9fbf74e2023-03-28 13:38:22 +08003757 if (net_no < 0)
3758 {
you.chen35020192022-05-06 11:30:57 +08003759 return 0;
3760 }
3761
3762 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3763
3764 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3765 DO_OK_FAIL_REQUEST(cmd_save_config);
3766
3767 return 0;
3768}
3769
3770int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003771{
you.chend2fef3f2023-02-13 10:50:35 +08003772 int count, index;
you.chen35020192022-05-06 11:30:57 +08003773 int net_no_list[128];
3774 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003775 RLOGD("enter lynq_get_sta_saved_ap api\n");
3776 if (list == NULL || len == NULL)
3777 {
3778 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003779 return -1;
3780 }
3781
3782 CHECK_IDX(idx, CTRL_STA);
3783
3784// CHECK_WPA_CTRL(CTRL_STA);
3785
3786 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003787 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003788
you.chen057aac42023-04-13 14:06:58 +08003789 if (count < 0)
3790 {
3791 RLOGE("list network fail");
3792 return count;
3793 }
3794 else if (count == 0)
3795 {
3796 *list = NULL;
3797 *len = 0;
3798 return 0;
3799 }
3800
you.chen35020192022-05-06 11:30:57 +08003801 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003802 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003803 *len = count;
3804
qs.xiong9fbf74e2023-03-28 13:38:22 +08003805 for (index=0; index < count; index++)
3806 {
you.chen35020192022-05-06 11:30:57 +08003807 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003808 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003809 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003810 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003811 {
you.chen35020192022-05-06 11:30:57 +08003812 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3813 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003814 else
you.chen057aac42023-04-13 14:06:58 +08003815 {
you.chen35020192022-05-06 11:30:57 +08003816 (*list)[index].base_info.band = -1;
3817 }
you.chen057aac42023-04-13 14:06:58 +08003818 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003819 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003820 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003821 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003822 return 0;
3823}
3824
3825int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3826{
qs.xiongc8d92a62023-03-29 17:36:14 +08003827 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003828 const char *lynq_scan_cmd = "SCAN";
3829
3830 CHECK_IDX(idx, CTRL_STA);
3831
3832 CHECK_WPA_CTRL(CTRL_STA);
3833
you.chen0df3e7e2023-05-10 15:56:26 +08003834 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3835 {
3836 RLOGD("tmp clear scanlist");
3837 system(clean_last_re);
3838 }
you.chen9ac66392022-08-06 17:01:16 +08003839 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003840 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003841 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3842 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003843 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003844 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3845 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003846 g_sta_scan_finish_flag = 1;
3847 return -1;
3848 }
you.chen35020192022-05-06 11:30:57 +08003849
3850 return 0;
3851}
3852
3853int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003854 if (cb == NULL)
3855 {
3856 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003857 return -1;
3858 }
3859
you.chen6d247052023-06-01 16:39:54 +08003860 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003861 g_ap_callback_priv = priv;
3862 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003863 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003864
you.chen6d247052023-06-01 16:39:54 +08003865 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003866 if (g_ap_watcher_pid == 0 )
3867 {
3868 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3869 {
3870 g_ap_watcher_pid = 0;
3871 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3872 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3873 return -1;
3874 }
3875 }
3876
3877 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3878 RLOGD("creat APWatcherTheradProc susccs");
3879
you.chen35020192022-05-06 11:30:57 +08003880 return 0;
3881}
3882
3883int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003884 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003885 if (g_ap_callback_priv == priv)
3886 {
you.chen35020192022-05-06 11:30:57 +08003887 g_ap_callback_func = NULL;
3888 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003889 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003890 return 0;
3891 }
you.chen6d247052023-06-01 16:39:54 +08003892 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003893 return -1;
3894}
3895
3896int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003897 if (cb == NULL)
3898 {
3899 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003900 return -1;
3901 }
3902
you.chen6d247052023-06-01 16:39:54 +08003903 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003904 g_sta_callback_priv = priv;
3905 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003906 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003907
you.chen6d247052023-06-01 16:39:54 +08003908 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003909 if (g_sta_watcher_pid == 0 ) {
3910 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3911 {
3912 g_sta_watcher_pid = 0;
3913 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3914 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3915 return -1;
3916 }
3917 }
3918
3919 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3920 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003921 return 0;
3922}
3923
3924int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003925 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003926 if (g_sta_callback_priv == priv)
3927 {
you.chen35020192022-05-06 11:30:57 +08003928 g_sta_callback_func = NULL;
3929 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003930 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003931 return 0;
3932 }
you.chen6d247052023-06-01 16:39:54 +08003933 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003934 return -1;
3935}
3936
qs.xiongfcc914b2023-07-06 21:16:20 +08003937int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3938 if (cb == NULL)
3939 {
3940 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3941 return -1;
3942 }
3943 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3944 g_sta_auto_callback_priv = priv;
3945 g_sta_auto_callback_func = cb;
3946 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3947 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3948 if (g_sta_auto_watcher_pid == 0 ) {
3949 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3950 {
3951 g_sta_auto_watcher_pid = 0;
3952 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3953 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3954 return -1;
3955 }
3956 }
3957 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3958 RLOGD("creat STAWatcherTheradProc susccs");
3959 return 0;
3960}
3961int lynq_unreg_sta_auto_event_callback(void * priv) {
3962 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3963 if (g_sta_auto_callback_priv == priv)
3964 {
3965 g_sta_auto_watcher_stop_flag = 1;
3966 if (g_sta_auto_watcher_pid != 0)
3967 {
3968 pthread_join(g_sta_auto_watcher_pid, NULL);
3969 }
3970 g_sta_auto_watcher_pid = 0;
3971 g_sta_auto_callback_func = NULL;
3972 g_sta_auto_callback_priv = NULL;
3973 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3974 return 0;
3975 }
3976 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3977 return -1;
3978}
you.chen35020192022-05-06 11:30:57 +08003979int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3980{
3981 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003982 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003983 CHECK_IDX(idx, CTRL_AP);
3984
qs.xiong9fbf74e2023-03-28 13:38:22 +08003985 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3986 {
you.chen35020192022-05-06 11:30:57 +08003987 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3988 return 0;
3989 }
3990
qs.xiong9fbf74e2023-03-28 13:38:22 +08003991 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3992 {
you.chen35020192022-05-06 11:30:57 +08003993 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3994 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003995 else
3996 {
you.chen35020192022-05-06 11:30:57 +08003997 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3998 }
3999
4000 return 0;
4001}
4002
4003int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
4004 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08004005 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08004006 CHECK_IDX(idx, CTRL_STA);
4007
qs.xiong9fbf74e2023-03-28 13:38:22 +08004008 if (inner_get_status_info_state(CTRL_STA, state) != 0)
4009 {
you.chen35020192022-05-06 11:30:57 +08004010 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4011 return 0;
4012 }
4013
qs.xiong9fbf74e2023-03-28 13:38:22 +08004014 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
4015 {
you.chen35020192022-05-06 11:30:57 +08004016 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
4017 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004018 else
4019 {
you.chen35020192022-05-06 11:30:57 +08004020 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
4021 }
4022
4023 return 0;
4024}
4025
4026int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
4027// CHECK_IDX(idx, CTRL_AP);
4028// int ret = 0;
4029// size_t reply_len = MAX_RET;
4030// char cmd_reply[MAX_RET]={0};
4031// const char * cmd_str = "GET country";
4032// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
4033// do{
4034// if (NULL == s_lynq_wpa_ctrl) {
4035// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
4036// if (NULL == s_lynq_wpa_ctrl ) {
4037// printf("wpa_ctrl_open fail\n");
4038// return -1;
4039// }
4040// }
4041// }while(0);
4042
4043// do {
4044// reply_len = MAX_RET;
4045// cmd_reply[0] = '\0';
4046// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08004047// 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 +08004048// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004049// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08004050// return ret;
4051// }
4052// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08004053// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08004054// }while(0);
4055
4056 FILE *fp;
4057 size_t i = 0;
4058 char lynq_cmd_ret[MAX_RET]={0};
4059
4060// CHECK_IDX(idx, CTRL_AP);
4061
4062 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08004063 {
4064 perror("popen error!");
4065 return -1;
4066 }
you.chen35020192022-05-06 11:30:57 +08004067 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
4068 {
4069 perror("fread fail!");
4070 return -1;
4071 }
4072
qs.xiong9fbf74e2023-03-28 13:38:22 +08004073 for(i=0; i < strlen(lynq_cmd_ret); i++)
4074 {
4075 if (lynq_cmd_ret[i] == ' ')
4076 {
you.chen35020192022-05-06 11:30:57 +08004077 lynq_cmd_ret[i] = '\0';
4078 break;
4079 }
4080 }
4081
4082 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004083 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08004084
4085 int ret=pclose(fp);
4086 if(ret==-1)
4087 {
4088 perror("close file faild");
4089 }
4090
4091 return 0;
4092}
4093
qs.xiong44fac672023-08-29 16:15:55 +08004094
you.chen705a7ef2023-06-01 22:06:45 +08004095static int check_and_init_uci_config(char * country_code)
4096{
4097 FILE * fp;
4098 int is_different = 0;
4099 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
4100 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
4101 const char * commit_uci_cmd ="uci commit";
4102 char set_country_cmd[MAX_CMD];
4103 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08004104
you.chen705a7ef2023-06-01 22:06:45 +08004105 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08004106
you.chen705a7ef2023-06-01 22:06:45 +08004107 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08004108 {
you.chen705a7ef2023-06-01 22:06:45 +08004109 if (0 != system(create_uci_cmd))
4110 {
4111 RLOGE("creat_uci_cmd fail");
4112 return -1;
4113 }
4114 is_different = 1;
4115 }
4116
4117 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
4118 {
4119 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08004120 return -1;
4121 }
4122
you.chen705a7ef2023-06-01 22:06:45 +08004123 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08004124 {
you.chen705a7ef2023-06-01 22:06:45 +08004125 RLOGE("fread fail!");
4126 fclose(fp);
4127 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08004128 }
4129
you.chen705a7ef2023-06-01 22:06:45 +08004130 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
4131 {
qs.xiong44fac672023-08-29 16:15:55 +08004132 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 +08004133 is_different = 1;
4134 }
4135
4136 fclose(fp);
4137
4138 if (is_different)
4139 {
4140 if ( 0 != system(set_country_cmd))
4141 {
4142 RLOGE("set_country_cmd fail");
4143 return -1;
4144 }
4145 if (0 != system(commit_uci_cmd))
4146 {
4147 RLOGE("commmit fail");
4148 }
4149 }
4150
4151 return is_different;
4152}
4153
4154int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
4155 char check_current_code[10];
4156 const char * support_country[] = {"CN", "EU"};
4157
4158 int ret,is_different, i, cc_count;
4159
4160 if (country_code == NULL || country_code[0] == '\0')
4161 {
4162 RLOGE("bad country code\n");
4163 return -1;
4164 }
4165
4166 cc_count = sizeof (support_country) / sizeof (char*);
4167 for(i=0; i < cc_count; i++)
4168 {
4169 if (strcmp(support_country[i], country_code) == 0)
4170 {
4171 break;
4172 }
4173 }
4174
4175 if (i >= cc_count)
4176 {
4177 RLOGE("unspported country code %s\n", country_code);
4178 return -1;
4179 }
4180
4181 is_different = check_and_init_uci_config(country_code);
4182 if( is_different < 0 )
4183 {
4184 RLOGE("init set uci fail\n");
4185 return -1;
4186 }
4187
4188 ret = lynq_get_country_code(idx,check_current_code);
4189 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4190 {
4191 ret = lynq_wifi_disable();
4192 if(ret != 0 )
4193 {
4194 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4195 return -1;
4196 }
4197 }
4198
4199 return 0;
you.chen35020192022-05-06 11:30:57 +08004200}
4201
4202int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4203{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004204 RLOGD("enter lynq_get_connect_ap_mac\n");
4205 if (mac == NULL)
4206 {
4207 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004208 return -1;
4209 }
4210
4211 CHECK_IDX(idx, CTRL_STA);
4212 ap_info_s ap;
4213 ap.ap_mac[0] = '\0';
4214
qs.xiong9fbf74e2023-03-28 13:38:22 +08004215 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4216 {
you.chen35020192022-05-06 11:30:57 +08004217 return -1;
4218 }
4219 strcpy(mac, ap.ap_mac);
4220
4221 return 0;
4222}
4223
4224int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4225{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004226 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004227 struct ifaddrs *ifaddr_header, *ifaddr;
4228 struct in_addr * ifa;
4229 const char * ifaName = "wlan0";
4230 if (ip == NULL)
4231 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004232 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004233 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004234 }
you.chenf58b3c92022-06-21 16:53:48 +08004235
qs.xiong9fbf74e2023-03-28 13:38:22 +08004236 if (idx == 1)
4237 {
you.chen0df3e7e2023-05-10 15:56:26 +08004238 ifaName = inner_get_ap_interface_name();
4239 if (ifaName == NULL)
4240 {
4241 RLOGE("[lynq_get_interface_ip] ap name get fail");
4242 return -1;
4243 }
you.chen9ac66392022-08-06 17:01:16 +08004244 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004245 else if (idx != 0)
4246 {
you.chen35020192022-05-06 11:30:57 +08004247 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004248 }
you.chen35020192022-05-06 11:30:57 +08004249
you.chen9ac66392022-08-06 17:01:16 +08004250 if (getifaddrs(&ifaddr_header) == -1)
4251 {
you.chen35020192022-05-06 11:30:57 +08004252 perror("getifaddrs");
4253 return -1;
4254 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004255 }
you.chen35020192022-05-06 11:30:57 +08004256
4257
you.chen9ac66392022-08-06 17:01:16 +08004258 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4259 {
4260 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004261 continue;
you.chen9ac66392022-08-06 17:01:16 +08004262 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4263 {
4264 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4265 {
4266 // is a valid IP4 Address
4267 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4268 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004269 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004270 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004271 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004272 return 0;
4273 }
4274 }
4275 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004276 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004277 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004278 return -1;
you.chen35020192022-05-06 11:30:57 +08004279}
4280
4281int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4282{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004283 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004284 int count;
4285 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004286 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004287 char *split_words[128] = {0};
4288 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4289
4290 CHECK_WPA_CTRL(idx);
4291
4292 DO_REQUEST(lynq_get_mac_cmd);
4293
qs.xiong9fbf74e2023-03-28 13:38:22 +08004294 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4295 {
4296 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004297 return -1;
4298 }
4299
4300 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4301
qs.xiong9fbf74e2023-03-28 13:38:22 +08004302 if (count < 2)
4303 {
you.chen35020192022-05-06 11:30:57 +08004304 return -1;
4305 }
4306
qs.xiong9fbf74e2023-03-28 13:38:22 +08004307 for (i=0; i < strlen(split_words[1]); i++ )
4308 {
4309 if (split_words[1][i] != ' ')
4310 {
you.chen35020192022-05-06 11:30:57 +08004311 break;
4312 }
4313 }
4314
qs.xiongdd6e44c2023-08-08 15:02:53 +08004315 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004316
4317 return 0;
4318}
4319
4320int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4321{
4322// int count;
4323// char *split_words[128] = {0};
4324// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4325
4326// if (rssi == NULL) {
4327// return -1;
4328// }
4329
4330// CHECK_IDX(idx, CTRL_STA);
4331
4332// CHECK_WPA_CTRL(CTRL_STA);
4333
4334// DO_REQUEST(lynq_get_rssi_cmd);
4335
4336// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4337// return -1;
4338// }
4339
4340// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4341
4342// if (count < 2) {
4343// return -1;
4344// }
4345
4346// *rssi = atoi(split_words[1]) * -1;
4347
you.chen35020192022-05-06 11:30:57 +08004348 char lynq_cmd_ret[MAX_RET]={0};
4349
qs.xiongff0ae0f2022-10-11 15:47:14 +08004350/*******change other cmd to get rssi*******
4351 *
4352 *wl rssi ---> wl -i wlan0 rssi
4353 *
4354 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004355 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004356 {
you.chen23c4a5f2023-04-12 16:46:00 +08004357 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004358 return -1;
4359 }
you.chen9f17e4d2022-06-06 17:18:18 +08004360 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004361/****** if got rssi is 0,means sta didn't connected any device****/
4362 if(*rssi == 0)
4363 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004364 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004365 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004366 }
you.chen35020192022-05-06 11:30:57 +08004367
4368 return 0;
4369}
4370
4371int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4372{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004373 RLOGD("enter lynq_get_connect_ap_band\n");
4374 if (band == NULL)
4375 {
you.chen35020192022-05-06 11:30:57 +08004376 return -1;
4377 }
4378
4379 CHECK_IDX(idx, CTRL_STA);
4380 ap_info_s ap;
4381 ap.band = -1;
4382
qs.xiong9fbf74e2023-03-28 13:38:22 +08004383 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4384 {
you.chen35020192022-05-06 11:30:57 +08004385 return -1;
4386 }
4387 *band = ap.band;
4388
4389 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004390}
you.chenf58b3c92022-06-21 16:53:48 +08004391
4392int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4393{
you.chenb95401e2023-05-12 19:39:06 +08004394 int ret;
4395 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004396 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004397
4398 if (ip == NULL)
4399 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004400 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004401 return -1;
4402 }
4403
4404 CHECK_IDX(idx, CTRL_STA);
4405
qs.xionge4cbf1c2023-02-28 18:22:49 +08004406 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004407 {
4408 return -1;
4409 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004410
you.chenb95401e2023-05-12 19:39:06 +08004411 ip[0] = '\0';
4412 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4413 if (ret != 0)
4414 {
4415 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4416 return -1;
4417 }
4418
4419 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4420 {
4421 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004422 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004423 if (ret != 0)
4424 {
4425 ip[0] = '\0';
4426 return 0;
4427 }
4428 else
4429 {
4430 p = strchr(ip, '\n');
4431 if (p != NULL)
4432 {
4433 *p = '\0';
4434 }
4435 }
4436 }
4437 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004438}
4439
qs.xiong026c5c72022-10-17 11:15:45 +08004440int lynq_ap_connect_num(int sta_number)
4441{
4442 char lynq_limit_cmd[32]={0};
4443 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004444 if((sta_number < 1 ) && (sta_number > 15))
4445 {
4446 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004447 return -1;
4448 }
4449 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4450 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004451 if(ret != 0)
4452 {
4453 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004454 }
4455 return 0;
4456}
you.chenf58b3c92022-06-21 16:53:48 +08004457
qs.xiong77905552022-10-17 11:19:57 +08004458int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4459{
4460
4461 char lynq_wifi_acs_cmd[128]={0};
4462 char lynq_cmd_mode[128]={0};
4463 char lynq_cmd_slect[128]={0};
4464
qs.xiong9fbf74e2023-03-28 13:38:22 +08004465 if((acs_mode != 2) && (acs_mode != 5))
4466 {
qs.xiong77905552022-10-17 11:19:57 +08004467 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4468 }
4469
qs.xiong9fbf74e2023-03-28 13:38:22 +08004470 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4471 {
qs.xiong77905552022-10-17 11:19:57 +08004472 return -1;
4473 }
4474
4475 CHECK_IDX(idx, CTRL_AP);
4476
4477 CHECK_WPA_CTRL(CTRL_AP);
4478
4479 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4480 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4481 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4482
4483 DO_OK_FAIL_REQUEST(cmd_disconnect);
4484 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4485 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4486 DO_OK_FAIL_REQUEST(cmd_save_config);
4487 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4488
4489 return 0;
4490}
you.chen0f5c6432022-11-07 18:31:14 +08004491//you.chen add for tv-box start
4492static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4493 FILE *fp;
4494 //printf("to exec cmd:%s\n", str_cmd);
4495 if((fp=popen(str_cmd,"r"))==NULL)
4496 {
4497 perror("popen error!");
4498 return -1;
4499 }
4500 if((fread(str_cmd_ret,max_len,1,fp))<0)
4501 {
4502 perror("fread fail!");
4503 fclose(fp);
4504 return -1;
4505 }
4506 fclose(fp);
4507 return 0;
4508}
4509
4510static int get_netmask_length(const char* mask)
4511{
4512 int masklen=0, i=0;
4513 int netmask=0;
4514
4515 if(mask == NULL)
4516 {
4517 return 0;
4518 }
4519
4520 struct in_addr ip_addr;
4521 if( inet_aton(mask, &ip_addr) )
4522 {
4523 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004524 }else
4525 {
you.chen0f5c6432022-11-07 18:31:14 +08004526 netmask = 0;
4527 return 0;
4528 }
4529
4530 while(0 == (netmask & 0x01) && i<32)
4531 {
4532 i++;
4533 netmask = netmask>>1;
4534 }
4535 masklen = 32-i;
4536 return masklen;
4537}
4538
4539static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4540 int mask_len;
4541 char *p;
4542 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004543 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4544 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004545 return -1;
4546 p = strstr(str_cmd_ret, "Mask:");
4547 if (p == NULL)
4548 return -1;
4549 mask_len = get_netmask_length(p + 5);
4550 if (mask_len == 0)
4551 return -1;
4552 p = strstr(str_cmd_ret, "inet addr:");
4553 if (p == NULL)
4554 return -1;
4555 strcpy(tmp, p + 10);
4556 p = strstr(tmp, " ");
4557 if (p != NULL)
4558 *p = '\0';
4559 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4560 return 0;
4561}
4562
4563static void GBWWatchThreadProc() {
4564 int i,n, nloop, nmax, ncheckcount, nidlecount;
4565 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4566 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4567 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4568 char *results[16] = {0};
4569 char str_cmd[256] = {0};
4570 char str_cmd_ret[128] = {0};
4571 char dest_ip[32] = {0};
4572 lastAP1Bytes = lastAP2Bytes = 0;
4573 lastAP1Drop = lastAP2Drop = 0;
4574 lastAP1Speed = lastAP2Speed = 0;
4575 setAP1Speed = 50;
4576 setAP2Speed = 80;
4577 nloop = 0;
4578 nmax = 6;
4579 ncheckcount = nidlecount = 0;
4580
you.chen0df3e7e2023-05-10 15:56:26 +08004581 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004582 {
4583 RLOGE("------gbw thread run\n");
4584 return;
4585 }
4586
qs.xiong9fbf74e2023-03-28 13:38:22 +08004587 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004588 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4589 while (dest_ip[0] == '\0') {
4590 sleep(1);
4591 str_cmd_ret[0] = '\0';
4592 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4593 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4594 if (str_cmd_ret[n] == '\n'){
4595 str_cmd_ret[n] = '\0';
4596 break;
4597 }
4598 }
4599 if (str_cmd_ret[0] != '\0')
4600 {
4601 strcpy(dest_ip, str_cmd_ret);
4602 }
4603 }
4604
you.chenc9928582023-04-24 15:39:37 +08004605 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4606 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4607 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 +08004608 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4609 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004610 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004611 return;
4612 }
you.chenc9928582023-04-24 15:39:37 +08004613 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);
4614 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);
4615 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 +08004616
4617 while (1) {
4618 sleep(1);
4619 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004620 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4621 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4622 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004623 continue;
4624 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004625 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004626 if (n > 9) {
4627 if (strcmp(results[1], "Sent") == 0) {
4628 currAP1Bytes = atoll(results[2]);
4629 }
4630 if (strcmp(results[6], "(dropped") == 0) {
4631 currAP1Drop = atoi(results[7]);
4632 }
4633 }
4634
4635 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004636 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4637 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4638 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004639 continue;
4640 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004641 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004642 if (n > 9) {
4643 if (strcmp(results[1], "Sent") == 0) {
4644 currAP2Bytes = atoll(results[2]);
4645 }
4646 if (strcmp(results[6], "(dropped") == 0) {
4647 currAP2Drop = atoi(results[7]);
4648 }
4649 }
4650
4651 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4652 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4653 lastAP1Bytes = currAP1Bytes;
4654 lastAP2Bytes = currAP2Bytes;
4655 continue;
4656 }
4657
4658 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4659 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4660 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4661 lastAP1Speed = currAP1Speed;
4662 lastAP2Speed = currAP2Speed;
4663 lastAP1Bytes = currAP1Bytes;
4664 lastAP2Bytes = currAP2Bytes;
4665
4666 currSetAP1Speed = setAP1Speed;
4667 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4668 ncheckcount++;
4669 if (ncheckcount > 3) {
4670 ncheckcount = 0;
4671 currSetAP1Speed = 5;
4672 }
4673 }
4674 else {
4675 ncheckcount = 0;
4676 if (currAP1Speed < 5)
4677 nidlecount++;
4678 else
4679 nidlecount = 0;
4680
4681 }
4682
4683 if (nidlecount > 60 ){
4684 currSetAP1Speed = 50;
4685 }
4686
4687 if (currSetAP1Speed != setAP1Speed) {
4688 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004689 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4690 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004691 }
4692 }
4693}
4694
4695int enableGBW(const char* mac) {
4696 int i,len;
4697 char get_ipaddr_cmd[128]={0};
4698 ap_info_s *ap;
4699 device_info_s * list;
4700
4701 if (mac == NULL || g_gbw_enabled == 1)
4702 return -1;
4703 len = strlen(mac);
4704 g_gbw_mac = malloc(len + 1);
4705 for(i=0;i<len;i++) {
4706 if (mac[i] >= 'A' && mac[i] <= 'Z')
4707 {
4708 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4709 }
4710 else
4711 g_gbw_mac[i] = mac[i];
4712 }
4713 g_gbw_mac[i] = '\0';
4714 g_gbw_enabled = 1;
4715
4716 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4717 if (system(get_ipaddr_cmd) == 0) {
4718 //startGBW();
4719 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4720 for (i=0;i<len;i++) {
4721 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4722 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4723 startGBW();
4724 }
4725 free(ap);
4726 free(list);
4727 }
4728 }
4729 return 0;
4730}
4731
4732int disableGBW() {
4733 stopGBW();
4734 free(g_gbw_mac);
4735 g_gbw_mac = NULL;
4736 g_gbw_enabled = 1;
4737 return 0;
4738}
4739
4740static int startGBW() {
4741 if (g_gbw_watcher_pid != 0) {
4742 stopGBW();
4743 }
4744 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4745}
4746
4747static int stopGBW() {
4748 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004749 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004750 pthread_cancel(g_gbw_watcher_pid);
4751 pthread_join(g_gbw_watcher_pid, &retval);
4752 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004753 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4754 if (s_ap_iterface_name[0] != '\0')
4755 {
4756 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4757 system(cmd);
4758 }
you.chen0f5c6432022-11-07 18:31:14 +08004759}
4760//you.chen add for tv-box end