blob: f56cc2d79df32e52adce74cc03c19b99909a2bf2 [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
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
49pthread_t g_sta_watcher_pid = 0;
50volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080051volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080052volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080053
qs.xiongfcc914b2023-07-06 21:16:20 +080054pthread_t g_sta_auto_watcher_pid = 0;
55volatile int g_sta_auto_watcher_stop_flag = 0;
56volatile int g_sta_auto_scan_finish_flag = 1;
57volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058void * g_ap_callback_priv = NULL;
59AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
60void * g_sta_callback_priv = NULL;
61STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080062void * g_sta_auto_callback_priv = NULL;
63STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080064
65//const char * CTRL_PATH="/var/run/wpa_supplicant";
66const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
67//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
68const char * cmd_list_networks = "LIST_NETWORKS";
69const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080070const char * cmd_disconnect = "DISCONNECT";
71const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080072const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080073const char * STATE_COMPLETED = "COMPLETED";
you.chen6d247052023-06-01 16:39:54 +080074const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080075
you.chenf711c8a2023-04-13 13:49:45 +080076const char * cmd_ping = "PING";
77const char * rsp_pong = "PONG";
78const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
79const int MAX_IDLE_COUNT = 600; // 60s
80
you.chenc9928582023-04-24 15:39:37 +080081const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
82const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
83const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
84const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
85const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
86
87static char s_ap_iterface_name[64] = {0};
88
you.chend2fef3f2023-02-13 10:50:35 +080089struct local_wpa_ctrl{
90 struct wpa_ctrl *ctrl;
91 pthread_mutex_t mutex;
92};
93
you.chen70f377f2023-04-14 18:17:09 +080094
you.chend2fef3f2023-02-13 10:50:35 +080095static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +080096static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
97static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +080098// add for auto connect
99static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800100
101static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800102
you.chen0f5c6432022-11-07 18:31:14 +0800103//you.chen add for tv-box start
104volatile int g_gbw_enabled = 0;
105char * g_gbw_mac = NULL;
106pthread_t g_gbw_watcher_pid = 0;
107static int startGBW();
108static int stopGBW();
109//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800110
111typedef struct __curr_status_info {
112 ap_info_s *ap;
113 char * state;
114 int net_no;
115}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400116
you.chen70f377f2023-04-14 18:17:09 +0800117typedef enum {
118 INNER_STA_STATUS_INIT = 0,
119 INNER_STA_STATUS_CONNECTING,
120 INNER_STA_STATUS_ASSOCIATING,
121 INNER_STA_STATUS_ASSOCIATED,
122 INNER_STA_STATUS_CONNECTED,
123 INNER_STA_STATUS_DISCONNECTING,
124 INNER_STA_STATUS_DISCONNECTED,
125 INNER_STA_STATUS_CANCEL,
126}inner_sta_status_s;
127
128static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
129static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
130static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
131static error_number_s s_sta_error_number = -1;
132static char s_sta_current_connecting_ssid[64] = {0};
133static struct timespec s_sta_connect_timeout;
134const int MAX_CONNNECT_TIME = 15; // second
135pthread_t g_global_watcher_pid = 0;
136static int s_service_invoke_timeout_cnt=0;
137const int FAKE_MAX_INT_VALUE = 99999;
138
139static void notify_service_invoke_fail(int error)
140{
141 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
142 pthread_mutex_lock(&s_global_check_mutex);
143 if (error == -2) //timeout
144 {
145 s_service_invoke_timeout_cnt++;
146 if (s_service_invoke_timeout_cnt > 10)
147 {
148 pthread_cond_signal(&s_global_check_cond);
149 }
150 }
151 else if (error == -1)
152 {
153 // check if can connect wpa service
154 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
155 if (lynq_wpa_ctrl == NULL)
156 {
157 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
158 pthread_cond_signal(&s_global_check_cond);
159 }
160 wpa_ctrl_close(lynq_wpa_ctrl);
161 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
162 if (lynq_wpa_ctrl == NULL)
163 {
164 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
165 pthread_cond_signal(&s_global_check_cond);
166 }
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 }
169
170 pthread_mutex_unlock(&s_global_check_mutex);
171}
172
you.chenc9928582023-04-24 15:39:37 +0800173static int system_call_v(const char * fmt, ...)
174{
175 char str_cmd[256] = {0};
176 va_list args;
177 va_start(args, fmt);
178 vsprintf(str_cmd, fmt, args);
179 va_end(args);
180 printf("system call----------%s\n", str_cmd);
181 return system(str_cmd);
182}
183
you.chen0df3e7e2023-05-10 15:56:26 +0800184static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
185
186static const char * inner_get_ap_interface_name()
187{
188 char * p;
189 char cmd[128]={0};
190
191 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
192 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
193 {
194 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
195 return NULL;
196 }
197 p = strchr(s_ap_iterface_name, ' ');
198 if (NULL != p)
199 {
200 *p = '\0';
201 }
202 p = strchr(s_ap_iterface_name, '\n');
203 if (NULL != p)
204 {
205 *p = '\0';
206 }
207 if (s_ap_iterface_name[0] == '\0')
208 {
209 return NULL;
210 }
211
212 return s_ap_iterface_name;
213}
214
you.chen70f377f2023-04-14 18:17:09 +0800215static void check_tether_and_notify()
216{
217 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800218 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 +0800219 {
220 return;
221 }
222 pthread_mutex_lock(&s_global_check_mutex);
223 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
224 pthread_cond_signal(&s_global_check_cond);
225 pthread_mutex_unlock(&s_global_check_mutex);
226}
227
you.chend2fef3f2023-02-13 10:50:35 +0800228static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
229 char *reply, size_t *reply_len,
230 void (*msg_cb)(char *msg, size_t len))
231{
232 int ret;
233 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800234 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800235 return -1;
236 }
237 pthread_mutex_lock(&ctrl->mutex);
238 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
239 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800240 if (ret != 0)
241 {
242 notify_service_invoke_fail(ret);
243 }
you.chend2fef3f2023-02-13 10:50:35 +0800244 return ret;
245}
246
247static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
248 int repeat_cnt;
249 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
250 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800251 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800252 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
253 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
254// printf("wait enable finish\n");
255 usleep(500 * 1000);
256 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
257 }
258 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800259 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800260 goto out_addr;
261 }
262 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
263 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
264 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800265 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800266 goto out_addr;
267 }
268 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
269 }
270 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
271out_addr:
272 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
273 return lynq_wpa_ctrl;
274}
275
qs.xiong97fa59b2022-04-07 05:41:29 -0400276#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400277{\
you.chen35020192022-05-06 11:30:57 +0800278 perror((str));\
279 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400280}
281
you.chen35020192022-05-06 11:30:57 +0800282#define CHECK_IDX(idx, type) do { \
283 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
284 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800285 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800286 return -1; \
287 } \
288 }while (0)
289
290#define CHECK_WPA_CTRL(index) int ret = 0;\
291 size_t reply_len = MAX_RET; \
292 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800293 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800294 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800295 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
296 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800297 }while(0)
298
299#define DO_REQUEST(cmd_str) do { \
300 reply_len = MAX_RET;\
301 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800302 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800303 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 +0800304 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800305 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800306 return ret; \
307 } \
308 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800310 }while(0)
311
312#define DO_OK_FAIL_REQUEST(cmd_str) do { \
313 DO_REQUEST(cmd_str); \
314 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800315 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800316 return -1; \
317 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800318 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800319 return -1; \
320 } \
321 }while (0)
322
323
you.chenf711c8a2023-04-13 13:49:45 +0800324static int check_connection(struct wpa_ctrl * wpa_ctrl)
325{
326 size_t reply_len = MAX_RET;
327 char cmd_reply[MAX_RET]={0};
328 int ret;
329
330 RLOGD("check_connection [%p]", wpa_ctrl);
331 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
332
333 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
334 {
335 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 +0800336 if (ret != 0)
337 {
338 notify_service_invoke_fail(ret);
339 }
you.chenf711c8a2023-04-13 13:49:45 +0800340 return -1;
341 }
342
343 return 0;
344}
345
346/**
347 * @brief check_pending_msg
348 * @param lynq_wpa_ctrl
349 * @return 1 has msg, 0 no msg, -1 error
350 */
351static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
352{
353 int ret;
354
355 if (*pp_lynq_wpa_ctrl == NULL) // need connect
356 {
357 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
358 if (*pp_lynq_wpa_ctrl == NULL)
359 {
360 usleep(SLEEP_TIME_ON_IDLE);
361 return -1;
362 }
363
364 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
365 if (ret == 0) // attach success
366 {
367 *started_flag = 1;
368 }
369 else
370 {
you.chen70f377f2023-04-14 18:17:09 +0800371 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800372 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
373 *pp_lynq_wpa_ctrl = NULL;
374 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800375 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800376 usleep(SLEEP_TIME_ON_IDLE);
377 return -1;
378 }
379 }
380
381 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
382 if ( ret == 0) // no pending messages
383 {
384 usleep(SLEEP_TIME_ON_IDLE);
385 *idle_count += 1;
386 if (*idle_count > MAX_IDLE_COUNT)
387 {
388 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
389 {
390 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
391 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
392 *pp_lynq_wpa_ctrl = NULL;
393 *idle_count = 0;
394 return -1;
395 }
396 *idle_count = 0;
397 }
398 return 0;
399 }
400 else if ( ret == -1) // on error
401 {
402 RLOGE("[wifi error]sta wpa_ctrl_pending");
403 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
404 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
405 *pp_lynq_wpa_ctrl = NULL;
406 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800407 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800408 return -1;
409 }
410
411 *idle_count = 0;
412 return 1;
413}
414
you.chen6d247052023-06-01 16:39:54 +0800415static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
416{
417 pthread_mutex_lock(&s_ap_callback_mutex);
418 if (g_ap_callback_func != NULL)
419 g_ap_callback_func(g_ap_callback_priv, status);
420 pthread_mutex_unlock(&s_ap_callback_mutex);
421}
422
you.chen35020192022-05-06 11:30:57 +0800423static void APWatcherThreadProc() {
424 size_t len = MAX_RET;
425 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800426 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800427
you.chen6c2dd9c2022-05-16 17:55:28 +0800428 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800429 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800430
qs.xiong9fbf74e2023-03-28 13:38:22 +0800431 while (g_ap_watcher_stop_flag == 0)
432 {
you.chenf711c8a2023-04-13 13:49:45 +0800433 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
434 {
you.chen70f377f2023-04-14 18:17:09 +0800435 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
436 {
437 check_tether_and_notify();
438 }
439
you.chen35020192022-05-06 11:30:57 +0800440 continue;
441 }
you.chenf711c8a2023-04-13 13:49:45 +0800442
you.chen6c2dd9c2022-05-16 17:55:28 +0800443 memset(msg_notify, 0, MAX_RET);
444 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800445 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800446 {
you.chen35020192022-05-06 11:30:57 +0800447 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800448 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800449 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800450 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800451 {
you.chen6d247052023-06-01 16:39:54 +0800452 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800453 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800454 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800455 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
456 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800457 {
you.chen0f5c6432022-11-07 18:31:14 +0800458 stopGBW();
459 }
460 }
you.chen35020192022-05-06 11:30:57 +0800461 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800462 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800463 {
you.chen6d247052023-06-01 16:39:54 +0800464 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800465 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800466 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800467 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
468 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800469 {
you.chen0f5c6432022-11-07 18:31:14 +0800470 startGBW();
471 }
472 }
you.chen35020192022-05-06 11:30:57 +0800473 }
qs.xiong31163d62023-07-11 18:54:40 +0800474 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 )
475 {
476 RLOGD("APWatcherThreadProc ap------> service error");
477 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
478 }
479 else
480 {
481 RLOGD("APWatcherThreadProc ap------> going on check next msg");
482 }
you.chenf711c8a2023-04-13 13:49:45 +0800483 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800484 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
485 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800486 if (lynq_wpa_ctrl != NULL)
487 {
you.chen92fd5d32022-05-25 10:09:47 +0800488 wpa_ctrl_detach(lynq_wpa_ctrl);
489 wpa_ctrl_close(lynq_wpa_ctrl);
490 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400491}
492
you.chen70f377f2023-04-14 18:17:09 +0800493static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
494{
495 char * p;
496 const char * try_associat_flag = "Trying to associate";
497 const char * associated_flag = "Associated with ";
498
499 pthread_mutex_lock(&s_global_check_mutex);
500 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
501 {
502 pthread_mutex_unlock(&s_global_check_mutex);
503 return;
504 }
505
506 if (state == LYNQ_WIFI_STATUS_EGNORE)
507 {
508 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
509 {
510 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
511 }
512 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
513 {
514 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
515 }
516 }
517 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
518 {
519 s_sta_error_number = error_num;
520 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
521 {
522 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
523 pthread_cond_signal(&s_global_check_cond);
524 }
525 }
526 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
527 {
528 s_sta_status = INNER_STA_STATUS_CONNECTED;
529 pthread_cond_signal(&s_global_check_cond);
530 }
531 pthread_mutex_unlock(&s_global_check_mutex);
532}
533
qs.xiongf0128b12023-06-29 17:29:39 +0800534static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
535{
536 char LYNQ_DISABLE_CMD[128]={0};
537
538 CHECK_IDX(idx, CTRL_STA);
539 CHECK_WPA_CTRL(CTRL_STA);
540
541 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
542 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
543 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
qs.xiongc93bf2b2023-08-25 10:22:08 +0800544 DO_OK_FAIL_REQUEST(cmd_save_config);
545
qs.xiongf0128b12023-06-29 17:29:39 +0800546 return 0;
qs.xiongc93bf2b2023-08-25 10:22:08 +0800547
qs.xiongf0128b12023-06-29 17:29:39 +0800548}
549
550
qs.xiong455c30b2023-04-12 11:40:02 +0800551void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
552{
553 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800554 char *wpanetid;
555 char destid[3] = {0};
556 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800557 *error = LYNQ_WAIT_CONNECT_ACTIVE;
558 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
559 {
560 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
561 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
562 return;
563 }
564
565 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
566 {
567 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
568 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
569 return;
570 }
571
572 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
573 {
qs.xiongf0128b12023-06-29 17:29:39 +0800574 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
575 wpanetid = strstr(modify,"id=");
576 if ( wpanetid != NULL )
577 {
578 wpanetid +=strlen("id=");
579 memcpy(destid,wpanetid,2);
580 tmpdisid = atoi(destid);
581
582 }
qs.xiong455c30b2023-04-12 11:40:02 +0800583 pReason = strstr(modify, "reason=");
584 if (pReason != NULL)
585 {
586 pReason += strlen("reason=");
587 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
588 {
589 *error = LYNQ_TIME_OUT;
590 }
591 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
592 {
593 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800594 // tmp fix sta autoconnect connect and disconnect
595 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
596 {
597 RLOGE("stop wlan0 network %d fail",tmpdisid);
598 }
qs.xiong455c30b2023-04-12 11:40:02 +0800599 }
600 else
601 {
602 *error = LYNQ_UNSPECIFIED_REASON;
603 }
qs.xiong455c30b2023-04-12 11:40:02 +0800604 }
605 else
606 {
607 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800608 }
qs.xiongf0128b12023-06-29 17:29:39 +0800609 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
610 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800611
612 }
613
614 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
615 {
616 *error = LYNQ_NOT_FIND_AP;
617 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
618 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
619 return;
620 }
621
622
623 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
624 {
625 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
626 pReason = strstr(modify, "status_code=");
627 if (pReason != NULL)
628 {
629 pReason += strlen("status_code=");
630 if (memcmp(pReason, "17", 2) == 0)
631 {
632 *error = LYNQ_AP_UNABLE_HANDLE;
633 }
634 else if (memcmp(pReason, "1",1) == 0)
635 {
636 *error = LYNQ_UNSPECIFIED_REASON;
637 }
638 else
639 {
640 *error = LYNQ_UNSPECIFIED_REASON;
641 }
642
643 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
644 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
645 return;
646 }
647 else
648 {
649 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
650 *error = LYNQ_UNSPECIFIED_REASON;
651 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
652 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
653 return;
654 }
655 }
656
657 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
658 {
659 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
660 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
661 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
662 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
663 return;
664 }
665
666 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
667 {
668 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
669 *error = LYNQ_WAIT_CONNECT_ACTIVE;
670 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
671 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
672 return;
673 }
674
you.chen32cb31e2023-04-13 14:05:45 +0800675 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800676 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800677 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800678 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
679 return;
680
681}
682
qs.xiongfcc914b2023-07-06 21:16:20 +0800683void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
684{
685 char *pReason;
686 char *wpanetid;
687 char destid[3];
688 *error = LYNQ_WAIT_CONNECT_ACTIVE;
689 *networkid = -1;
690 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
691 {
692 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
693 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
694 return;
695 }
696 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
697 {
698 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
699 wpanetid = strstr(modify,"id=");
700 if ( wpanetid != NULL )
701 {
702 wpanetid +=strlen("id=");
703 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
704 if (memcpy(destid,wpanetid,2) != NULL)
705 {
706 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
707 *networkid = atoi(destid);
708 RLOGD("get networkid is %d\n",*networkid);
709 }
710 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
711 }
712 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
713 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
714 return;
715 }
716 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
717 {
718 wpanetid = strstr(modify,"id=");
719 if ( wpanetid != NULL )
720 {
721 wpanetid +=strlen("id=");
722 if (memcpy(destid,wpanetid,2) != NULL)
723 {
724 *networkid = atoi(destid);
725 RLOGD("get networkid is %d\n",*networkid);
726 }
727 }
728 pReason = strstr(modify, "reason=");
729 if (pReason != NULL)
730 {
731 pReason += strlen("reason=");
732 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
733 {
734 *error = LYNQ_TIME_OUT;
735 }
736 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
737 {
738 *error = LYNQ_PSW_ERROR;
739 }
740 else
741 {
742 *error = LYNQ_UNSPECIFIED_REASON;
743 }
744 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
745 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
746 return;
747 }
748 else
749 {
750 *error = LYNQ_UNSPECIFIED_REASON;
751 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
752 return;
753 }
754 }
755 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
756 {
757 wpanetid = strstr(modify,"id=");
758 if ( wpanetid != NULL )
759 {
760 wpanetid +=strlen("id=");
761 if (memcpy(destid,wpanetid,2) != NULL)
762 {
763 *networkid = atoi(destid);
764 RLOGD("get networkid is %d\n",*networkid);
765 }
766 }
767 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
768 pReason = strstr(modify, "status_code=");
769 if (pReason != NULL)
770 {
771 pReason += strlen("status_code=");
772 if (memcmp(pReason, "17", 2) == 0)
773 {
774 *error = LYNQ_AP_UNABLE_HANDLE;
775 }
776 else if (memcmp(pReason, "1",1) == 0)
777 {
778 *error = LYNQ_UNSPECIFIED_REASON;
779 }
780 else
781 {
782 *error = LYNQ_UNSPECIFIED_REASON;
783 }
784 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
785 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
786 return;
787 }
788 else
789 {
790 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
791 *error = LYNQ_UNSPECIFIED_REASON;
792 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
793 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d£¬networkid:%d\n",*state,*error,*networkid);
794 return;
795 }
796 }
797 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
798 {
799 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
800 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
801 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
802 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
803 return;
804 }
805 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
806 {
807 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
808 *error = LYNQ_WAIT_CONNECT_ACTIVE;
809 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
810 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
811 return;
812 }
813 RLOGD("EVENT : %s\n", modify);
814 *error = LYNQ_UNSPECIFIED_REASON;
815 *state = LYNQ_WIFI_STATUS_EGNORE;
816 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
817 return;
818}
you.chen70f377f2023-04-14 18:17:09 +0800819static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
820{
you.chen6d247052023-06-01 16:39:54 +0800821 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800822 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
823 {
824 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
825 g_sta_callback_func(g_sta_callback_priv, state, error);
826 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
827 }
you.chen6d247052023-06-01 16:39:54 +0800828 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800829}
qs.xiongfcc914b2023-07-06 21:16:20 +0800830static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
831{
832 pthread_mutex_lock(&s_sta_callback_mutex);
833 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
834 {
835 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
836 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
837 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
838 }
839 pthread_mutex_unlock(&s_sta_callback_mutex);
840}
you.chen70f377f2023-04-14 18:17:09 +0800841
you.chen35020192022-05-06 11:30:57 +0800842static void STAWatcherThreadProc() {
843 size_t len = MAX_RET;
844 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +0800845 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +0800846 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +0800847 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400848
you.chen6c2dd9c2022-05-16 17:55:28 +0800849 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800850 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800851
you.chen70f377f2023-04-14 18:17:09 +0800852 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +0800853 while (g_sta_watcher_stop_flag == 0)
854 {
you.chenf711c8a2023-04-13 13:49:45 +0800855 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +0800856 {
you.chen35020192022-05-06 11:30:57 +0800857 continue;
858 }
you.chenf711c8a2023-04-13 13:49:45 +0800859
you.chen6c2dd9c2022-05-16 17:55:28 +0800860 memset(msg_notify, 0, MAX_RET);
861 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800862 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +0800863 {
you.chen35020192022-05-06 11:30:57 +0800864 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +0800865 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
866 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800867 {
you.chen35020192022-05-06 11:30:57 +0800868 g_sta_scan_finish_flag = 1;
869 }
870
qs.xiong9fbf74e2023-03-28 13:38:22 +0800871 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800872 {
you.chen35020192022-05-06 11:30:57 +0800873 continue;
874 }
qs.xiong455c30b2023-04-12 11:40:02 +0800875 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +0800876 notify_connect_status(state, error);
877
878 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +0800879 {
you.chen70f377f2023-04-14 18:17:09 +0800880 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +0800881 if (last_state != state)
882 {
883 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
884 {
885 system_call_v("%s %s", sta_status_change_script, "connect");
886 }
887 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
888 {
889 system_call_v("%s %s", sta_status_change_script, "disconnect");
890 }
891 }
892
893 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +0800894 }
you.chen35020192022-05-06 11:30:57 +0800895 }
896 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800897 if (lynq_wpa_ctrl != NULL)
898 {
you.chen92fd5d32022-05-25 10:09:47 +0800899 wpa_ctrl_detach(lynq_wpa_ctrl);
900 wpa_ctrl_close(lynq_wpa_ctrl);
901 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400902}
qs.xiongfcc914b2023-07-06 21:16:20 +0800903static void STAAutoWatcherThreadProc() {
904 size_t len = MAX_RET;
905 char msg_notify[MAX_RET];
906 error_number_s error;
907 lynq_wifi_sta_status_s state, last_state = -1;
908 int idle_count = 0;
909 int networkid;
910 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
911 g_sta_auto_watcher_stop_flag = 0;
912 RLOGD("STAAutoWatcherThreadProc thread started ------");
913 while (g_sta_auto_watcher_stop_flag == 0)
914 {
915 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
916 {
917 continue;
918 }
919 memset(msg_notify, 0, MAX_RET);
920 len = MAX_RET;
921 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
922 {
923 msg_notify[len+1] = '\0';
924 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
925 if (strstr(msg_notify, state_scan_result) != NULL)
926 {
927 g_sta_auto_scan_finish_flag = 1;
928 }
929 if (g_sta_auto_callback_func == NULL)
930 {
931 continue;
932 }
933 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
934 notify_auto_connect_status(state, error,networkid);
935 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
936 {
937 inner_check_connect_error(msg_notify, state, error);
938 if (last_state != state)
939 {
940 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
941 {
942 system_call_v("%s %s", sta_status_change_script, "connect");
943 }
944 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
945 {
946 system_call_v("%s %s", sta_status_change_script, "disconnect");
947 }
948 }
949 last_state = state;
950 }
951 }
952 }
953 if (lynq_wpa_ctrl != NULL)
954 {
955 wpa_ctrl_detach(lynq_wpa_ctrl);
956 wpa_ctrl_close(lynq_wpa_ctrl);
957 }
958}
qs.xiongf1b525b2022-03-31 00:58:23 -0400959
you.chen70f377f2023-04-14 18:17:09 +0800960// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
961static void GlobalWatcherThreadProc()
962{
963 int ret, connect_timeout, service_abnormal;
964 error_number_s error_num = -1;
965 inner_sta_status_s sta_status;
966 scan_info_s *scan_list = NULL;
967 int i, scan_len=0;
968 char connecting_ssid[64];
969 struct timeval now;
970
971 RLOGD("GlobalWatcherThreadProc start to run");
972
973 while (1)
974 {
975 pthread_mutex_lock(&s_global_check_mutex);
976 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
977 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
978 {
979 pthread_mutex_unlock(&s_global_check_mutex);
980 usleep(50*1000);
981 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
982 continue;
983 }
984
985 connect_timeout = 0;
986 service_abnormal = 0;
987 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
988 {
989 while (1)
990 {
991 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
992 if (ret == ETIME)
993 {
994 connect_timeout = 1;
995 }
996 else if (ret != 0)
997 {
998 gettimeofday(&now,NULL);
999 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
1000 {
1001 usleep(SLEEP_TIME_ON_IDLE);
1002 continue;
1003 }
1004 connect_timeout = 1;
1005 }
1006 sta_status = s_sta_status;
1007 error_num = s_sta_error_number;
1008 s_sta_status = INNER_STA_STATUS_INIT;
1009 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1010 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1011 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1012 break;
1013 }
1014 }
1015 if (s_service_invoke_timeout_cnt > 10)
1016 {
1017 service_abnormal = 1;
1018 s_service_invoke_timeout_cnt = 0;
1019 }
1020 pthread_mutex_unlock(&s_global_check_mutex);
1021
1022 if (service_abnormal == 1)
1023 {
1024 sleep(1);
1025 RLOGE("wpa service is abnormal info app to exit");
1026 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001027
1028 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1029
you.chen70f377f2023-04-14 18:17:09 +08001030 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1031 }
1032
1033 if (sta_status == INNER_STA_STATUS_CANCEL)
1034 {
1035 continue;
1036 }
1037 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1038 {
1039 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1040 }
1041 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1042 {
1043 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1044 {
1045 for(i=0; i < scan_len;i++)
1046 {
1047 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1048 {
1049 error_num = LYNQ_AUTH_ERROR;
1050 break;
1051 }
1052 }
1053 free(scan_list);
1054 }
1055 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1056 }
1057 else if (connect_timeout == 0)
1058 {
1059 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1060 }
1061 else // wait timeout
1062 {
1063 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1064 {
1065 ; // wpa service abnormal
1066 }
1067 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1068 {
1069 RLOGD("GlobalWatcherThreadProc notify connected");
1070 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1071 }
1072 else
1073 {
1074 RLOGD("GlobalWatcherThreadProc notify timeout");
1075 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1076 }
1077 }
1078 } // while (1)
1079}
1080
qs.xiong1af5daf2022-03-14 09:12:12 -04001081int lynq_wifi_enable(void)
1082{
you.chen35020192022-05-06 11:30:57 +08001083 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001084 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001085 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001086 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1087
qs.xiong9fbf74e2023-03-28 13:38:22 +08001088 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1089 {
you.chend2fef3f2023-02-13 10:50:35 +08001090 goto out_enable;
1091 }
1092
you.chenc9928582023-04-24 15:39:37 +08001093 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001094 if (ret != 0)
1095 {
1096 //printf("service state %d\n", ret);
1097 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001098 ret = -1;
1099 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001100 }
lhfe8da902022-10-11 18:55:36 +08001101
you.chen70f377f2023-04-14 18:17:09 +08001102 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1103 {
1104 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1105 if(ret<0)
1106 {
1107 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1108 ret = -1;
1109 goto out_enable;
1110 }
1111 }
1112
you.chend2fef3f2023-02-13 10:50:35 +08001113 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1114 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1115 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1116 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1117out_enable:
1118 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001119 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001120}
1121
qs.xiong1af5daf2022-03-14 09:12:12 -04001122int lynq_wifi_disable(void)
1123{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001124 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001125 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001126 g_ap_watcher_stop_flag = 1;
1127 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001128 g_sta_auto_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001129 if (g_ap_watcher_pid != 0)
1130 pthread_join(g_ap_watcher_pid, NULL);
1131 if (g_sta_watcher_pid != 0)
1132 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001133 if (g_sta_auto_watcher_pid != 0)
1134 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001135 if (g_lynq_wpa_ctrl[0] != NULL)
1136 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1137 if (g_lynq_wpa_ctrl[1] != NULL)
1138 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
1139 g_ap_watcher_pid = 0;
1140 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001141 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001142 g_lynq_wpa_ctrl[0] = NULL;
1143 g_lynq_wpa_ctrl[1] = NULL;
1144 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001145 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1146 return 0;
1147}
1148
1149static inline char inner_convert_char(char in)
1150{
1151 if (in >= '0' && in <= '9')
1152 {
1153 return in - '0';
1154 }
1155 else if (in >= 'a' && in <= 'f')
1156 {
1157 return in - 'a' + 10;
1158 }
1159 else if (in >= 'A' && in <= 'F')
1160 {
1161 return in - 'A' + 10;
1162 }
1163 else
1164 {
1165 return '\xff';
1166 }
1167}
1168
1169static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1170{
1171 char *p;
1172 size_t pos = 0;
1173 if (NULL == out_ssid)
1174 return;
1175 //printf("input ssid=[%s]\n", ssid);
1176 memset(out_ssid, 0, out_ssid_len);
1177 if (NULL == ssid)
1178 return;
1179 p = strchr(ssid, '\\');
1180 if (NULL == p)
1181 {
1182 strncpy(out_ssid, ssid, out_ssid_len);
1183 //printf(" first %s\n", out_ssid);
1184 }
1185 else
1186 {
1187 pos = p - ssid;
1188 memcpy(out_ssid, ssid, pos);
1189 //printf("pos %lu -- %s\n", pos, out_ssid);
1190 for(; pos < out_ssid_len; pos ++)
1191 {
1192 if (p[0] == '\0')
1193 {
1194 //printf(" out %s\n", out_ssid);
1195 return;
1196 }
1197 else if (p[0] != '\\')
1198 {
1199 out_ssid[pos] = p[0];
1200 p += 1;
1201 }
1202 else if (p[1] == 'x' || p[1] == 'X')
1203 {
1204 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1205 p += 4;
1206 }
1207 else if (p[1] == '\\')
1208 {
1209 out_ssid[pos] = '\\';
1210 p += 2;
1211 }
1212 else if (p[1] == 't')
1213 {
1214 out_ssid[pos] = '\t';
1215 p += 2;
1216 }
1217 else if (p[1] == 'r')
1218 {
1219 out_ssid[pos] = '\r';
1220 p += 2;
1221 }
1222 else if (p[1] == 'n')
1223 {
1224 out_ssid[pos] = '\n';
1225 p += 2;
1226 }//todo find a better way to convert?
1227 }
1228 }
1229 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001230}
qs.xiong1af5daf2022-03-14 09:12:12 -04001231
you.chen35020192022-05-06 11:30:57 +08001232static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001233 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001234 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001235 RLOGD("enter inner_get_param");
1236 if (out_put == NULL)
1237 {
1238 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001239 return -1;
1240 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001241 if (param_name == NULL)
1242 {
1243 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001244 return -1;
1245 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001246 if (param_name[0] == '\0')
1247 {
1248 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001249 return -1;
1250 }
1251
1252 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1253
1254 CHECK_WPA_CTRL(interface);
1255
1256 DO_REQUEST(lynq_cmd_get);
1257
qs.xiong9fbf74e2023-03-28 13:38:22 +08001258 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1259 {
1260 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001261 return -1;
1262 }
1263
you.chena6fa5b22022-05-18 10:28:19 +08001264// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001265 if (strcmp(param_name, "ssid") == 0)
1266 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001267 if (cmd_reply[0] == '\"')
1268 {
you.chend2fef3f2023-02-13 10:50:35 +08001269 ssid_len = reply_len - 1;
1270 memcpy(out_put, cmd_reply + 1, ssid_len);
1271 if (out_put[ssid_len-1] == '\"')
1272 {
1273 out_put[ssid_len-1] = '\0';
1274 }
1275 else
1276 {
1277 out_put[ssid_len] = '\0';
1278 }
1279 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001280 else
1281 {
you.chend2fef3f2023-02-13 10:50:35 +08001282 ssid_len = reply_len / 2;
1283 for(i=0; i<ssid_len; i++)
1284 {
1285 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1286 }
1287 out_put[ssid_len] = '\0';
1288 }
1289 }
1290 else
1291 {
1292 memcpy(out_put, cmd_reply, reply_len + 1);
1293 }
you.chen35020192022-05-06 11:30:57 +08001294 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001295}
qs.xiong1af5daf2022-03-14 09:12:12 -04001296
you.chen35020192022-05-06 11:30:57 +08001297static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1298 int ret = 0;
1299 char * end = str + len - 1;
1300 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001301 while(str < end)
1302 {
1303 if (*str == delimiter)
1304 {
you.chen35020192022-05-06 11:30:57 +08001305 *str++ = '\0';
1306 results[ret++] = str;
1307 continue;
1308 }
1309 str++;
1310 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001311 if (*str == delimiter)
1312 {
you.chen35020192022-05-06 11:30:57 +08001313 *str = '\0';
1314 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001315
you.chen6ed36a62023-04-27 17:51:56 +08001316 results[ret] = NULL;
1317
you.chen35020192022-05-06 11:30:57 +08001318 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001319}
1320
you.chend2fef3f2023-02-13 10:50:35 +08001321static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1322{
1323 char * p;
1324 int ret = 0;
1325 char cmd[256]={0};
1326 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001327 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001328 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001329 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001330 ret = exec_cmd(cmd, ip, ip_len);
1331 p = strchr(ip, '\n');
1332 if (NULL != p)
1333 {
1334 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001335 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001336 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001337 return ret;
1338}
1339
you.chend2fef3f2023-02-13 10:50:35 +08001340static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001341 struct in_addr addr ={0};
1342 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001343 char cmd[64] = {0};
1344 char * p;
1345 int ret;
you.chen35020192022-05-06 11:30:57 +08001346
qs.xiong9fbf74e2023-03-28 13:38:22 +08001347 if (ip == NULL || *ip == '\0' || hostname == NULL)
1348 {
1349 RLOGE("ip == NULL or hostname == NULL");
1350 return -1;
you.chen35020192022-05-06 11:30:57 +08001351 }
1352
you.chend2fef3f2023-02-13 10:50:35 +08001353 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001354 if (inet_aton(ip, &addr) == 0)
1355 {
you.chen35020192022-05-06 11:30:57 +08001356 printf("---inet_aton fail\n");
1357 return -1;
1358 }
1359
1360 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1361
qs.xiong9fbf74e2023-03-28 13:38:22 +08001362 if (ht == NULL)
1363 {
you.chen186d3c32023-05-18 14:19:46 +08001364 hostname[0] = '\0';
1365 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1366 ret = exec_cmd(cmd, hostname, 32);
1367 if (ret == 0)
1368 {
1369 p = strchr(hostname, '\n');
1370 if (p != NULL)
1371 {
1372 *p = '\0';
1373 }
1374 return 0;
qs.xiong6d64b542023-07-26 17:09:22 +08001375 }else{
1376 usleep( 10 * 1000);
1377 ret = exec_cmd(cmd, hostname, 32);
1378 if( ret == 0)
1379 {
1380 p= strchr(hostname, '\n');
1381 if(p != NULL )
1382 {
1383 *p = '\0';
1384 }
1385 return 0;
1386 }
you.chen186d3c32023-05-18 14:19:46 +08001387 }
1388 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001389 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001390 herror(NULL);
1391 return -1;
1392 }
1393
1394 strcpy(hostname, ht->h_name);
1395
1396 return 0;
1397}
1398
1399static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1400{
1401 int count, index, words_count;
1402 char * split_lines[128]= {0};
1403 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001404 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001405 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001406 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001407
1408 CHECK_WPA_CTRL(ap_sta);
1409
1410 DO_REQUEST(lynq_wifi_list_networks);
1411
1412 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1413
1414 //@todo check ssid field to compatible
1415
1416 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001417 for(index=1; index < count; index++)
1418 {
you.chen35020192022-05-06 11:30:57 +08001419 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001420 if (words_count > 2)
1421 {
you.chend2fef3f2023-02-13 10:50:35 +08001422 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001423 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1424 {
you.chen35020192022-05-06 11:30:57 +08001425 net_no_list[ret++] = atoi(split_words[0]);
1426 }
1427 }
1428 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001429 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001430 return ret;
1431}
1432
1433static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001434 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001435 CHECK_WPA_CTRL(ap_sta);
1436 const char *lynq_wifi_add_network = "ADD_NETWORK";
1437
qs.xiong9fbf74e2023-03-28 13:38:22 +08001438 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001439 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001440 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1441 {
1442 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001443 return -1;
1444 }
1445
qs.xiong9fbf74e2023-03-28 13:38:22 +08001446 for(i=0;i<reply_len;i++)
1447 {
1448 if(cmd_reply[i] == '\n')
1449 {
you.chen35020192022-05-06 11:30:57 +08001450 cmd_reply[i] = '\0';
1451 break;
1452 }
1453 }
1454 return atoi(cmd_reply);
1455}
you.chena6cd55a2022-05-08 12:20:18 +08001456
you.chen35020192022-05-06 11:30:57 +08001457static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1458{
1459 int count, index;
1460 int net_no_list[128];
1461
qs.xiong9fbf74e2023-03-28 13:38:22 +08001462 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001463 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001464 for (index=0; index < count; index++)
1465 {
1466 if (net_no_list[index] == net_no)
1467 {
you.chen35020192022-05-06 11:30:57 +08001468 return 0;
1469 }
1470 }
1471
1472 if (count >= 1)
1473 index = net_no_list[count - 1];
1474 else
1475 index = -1;
1476
qs.xiong9fbf74e2023-03-28 13:38:22 +08001477 while (index < net_no )
1478 {
you.chen35020192022-05-06 11:30:57 +08001479 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001480 if (index >= net_no)
1481 { // required network no created
1482 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001483 return 0;
1484 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001485 else if( index < 0)
1486 {
1487 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001488 return -1;
1489 }
you.chen35020192022-05-06 11:30:57 +08001490 }
1491
1492 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001493 {
1494 RLOGE("[lynq_check_network_number] network index < 0");
1495 return -1;
1496 }
1497 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001498 return 0;
1499}
1500
1501static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001502 if (freq > 5000 && freq < 6000)
1503 {
you.chen35020192022-05-06 11:30:57 +08001504 return LYNQ_WIFI_5G_band;
1505 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001506 else if (freq > 2000 && freq < 3000)
1507 {
you.chen35020192022-05-06 11:30:57 +08001508 return LYNQ_WIFI_2G_band;
1509 }
1510 return LYNQ_WIFI_2_and_5G_band;
1511}
1512
1513static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001514 if (key_mgmt != NULL)
1515 {
1516 if (memcmp( key_mgmt, "NONE", 4) == 0)
1517 {
you.chen35020192022-05-06 11:30:57 +08001518 return LYNQ_WIFI_AUTH_OPEN;
1519 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001520 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1521 {
you.chen35020192022-05-06 11:30:57 +08001522 return LYNQ_WIFI_AUTH_WEP;
1523 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001524 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1525 {
you.chen35020192022-05-06 11:30:57 +08001526 return LYNQ_WIFI_AUTH_WPA_PSK;
1527 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001528 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1529 {
you.chen35020192022-05-06 11:30:57 +08001530 return LYNQ_WIFI_AUTH_WPA2_PSK;
1531 }
1532 }
1533
1534 return -1;
1535}
1536
1537static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001538 if (flag != NULL)
1539 {
qs.xiong46f41562023-07-11 21:06:47 +08001540 if ( strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || ( strstr(flag,"SAE-H2E") != NULL && strstr(flag,"WPS") == NULL ) )
qs.xiong3e506812023-04-06 11:08:48 +08001541 {
1542 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001543 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001544 {
1545 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1546 }else if (strstr( flag, "WPA2-PSK") != NULL)
1547 {
you.chen35020192022-05-06 11:30:57 +08001548 return LYNQ_WIFI_AUTH_WPA2_PSK;
1549 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001550 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001551 {
you.chen35020192022-05-06 11:30:57 +08001552 return LYNQ_WIFI_AUTH_WPA_PSK;
1553 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001554 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001555 {
you.chen35020192022-05-06 11:30:57 +08001556 return LYNQ_WIFI_AUTH_WEP;
1557 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001558 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001559 {
you.chen35020192022-05-06 11:30:57 +08001560 return LYNQ_WIFI_AUTH_OPEN;
1561 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001562 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001563 {
you.chend2fef3f2023-02-13 10:50:35 +08001564 return LYNQ_WIFI_AUTH_OPEN;
1565 }
qs.xiong46f41562023-07-11 21:06:47 +08001566 else
1567 {
1568 RLOGD("convert_max_auth_from_flag not-found auth mode");
1569 }
you.chen35020192022-05-06 11:30:57 +08001570 }
1571
1572 return -1;
1573}
1574
1575static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1576 switch (bw) {
1577 case 10:
1578 return LYNQ_WIFI_BANDWIDTH_HT10;
1579 break;
1580 case 20:
1581 return LYNQ_WIFI_BANDWIDTH_HT20;
1582 break;
1583 case 40:
1584 return LYNQ_WIFI_BANDWIDTH_HT40;
1585 break;
1586 case 80:
1587 return LYNQ_WIFI_BANDWIDTH_HT80;
1588 break;
1589 default:
1590 break;
1591 }
1592
1593 return -1;
1594}
1595
you.chen70f377f2023-04-14 18:17:09 +08001596static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001597static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1598 int i, count;
1599 char *p;
1600 const char *lynq_status_cmd = "STATUS";
1601 const char * FLAG_SSID = "ssid=";
1602 const char * FLAG_SBSID = "bssid=";
1603 const char * FLAG_KEY_MGMT = "key_mgmt=";
1604 const char * FLAG_FREQ = "freq=";
1605 const char * FLAG_STATE = "wpa_state=";
1606 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001607 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001608 char *split_lines[128] = {0};
1609
1610 CHECK_WPA_CTRL(interface);
1611
qs.xiong9fbf74e2023-03-28 13:38:22 +08001612 if (curr_state == NULL)
1613 {
1614 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001615 return -1;
1616 }
1617
1618 DO_REQUEST(lynq_status_cmd);
1619
1620 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1621
1622 curr_state->net_no = -1;
1623 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001624 for(i=0; i < count; i++)
1625 {
1626 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001627 {
you.chen35020192022-05-06 11:30:57 +08001628 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001629 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001630 {
you.chend2fef3f2023-02-13 10:50:35 +08001631 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001632 ret = 0;
1633 continue;
1634 }
you.chenf58b3c92022-06-21 16:53:48 +08001635 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001636 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001637 {
you.chend2fef3f2023-02-13 10:50:35 +08001638 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 +08001639 ret = 0;
1640 continue;
1641 }
you.chen35020192022-05-06 11:30:57 +08001642 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001643 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001644 {
you.chen450d0172022-07-15 17:56:48 +08001645 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001646 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001647 ret = 0;
1648 continue;
1649 }
1650 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001651 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001652 {
you.chen35020192022-05-06 11:30:57 +08001653 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1654 ret = 0;
1655 continue;
1656 }
you.chend2fef3f2023-02-13 10:50:35 +08001657 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001658 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001659 {
you.chend2fef3f2023-02-13 10:50:35 +08001660 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1661 ret = 0;
1662 continue;
1663 }
you.chen35020192022-05-06 11:30:57 +08001664 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001665 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001666 {
you.chen35020192022-05-06 11:30:57 +08001667 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001668 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001669 {
you.chen35020192022-05-06 11:30:57 +08001670 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1671 ret = 0;
1672 continue;
1673 }
1674
1675 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001676 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001677 {
you.chen35020192022-05-06 11:30:57 +08001678 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001679 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001680 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001681 }
1682 }
1683
you.chen70f377f2023-04-14 18:17:09 +08001684 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1685 {
1686 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1687 }
1688
you.chen35020192022-05-06 11:30:57 +08001689 return ret;
1690}
1691
qs.xiongf1b525b2022-03-31 00:58:23 -04001692int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001693{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001694 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001695 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001696
qs.xiong9fbf74e2023-03-28 13:38:22 +08001697 if (ap_ssid == NULL)
1698 {
1699 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001700 return -1;
1701 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001702 else
1703 {
1704 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001705 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001706
qs.xiong9fbf74e2023-03-28 13:38:22 +08001707 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1708 {
1709 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001710 return -1;
1711 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001712
you.chen35020192022-05-06 11:30:57 +08001713 CHECK_IDX(idx, CTRL_AP);
1714
1715 CHECK_WPA_CTRL(CTRL_AP);
1716
1717 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1718
1719 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1720 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001721 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1722 return 0;
you.chen35020192022-05-06 11:30:57 +08001723
qs.xiong7a105ce2022-03-02 09:43:11 -05001724}
1725
you.chen35020192022-05-06 11:30:57 +08001726int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001727{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001728 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001729 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001730 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001731}
1732
qs.xiongc9c79f72022-10-17 15:27:18 +08001733/*****
1734 *frequency <------>channel
1735 *
1736 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1737 *
1738 *
1739 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1740 *
1741 *
1742 * */
1743static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001744 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};
1745 int i;
1746 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1747
qs.xiong69a332b2022-12-02 09:58:57 +08001748 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001749 {
1750 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001751 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001752 }
qs.xiongc00b6032022-11-29 16:28:03 +08001753
1754 if(i == arr_len)
1755 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001756 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001757 return -1;
1758 }
qs.xiongc00b6032022-11-29 16:28:03 +08001759
qs.xiongc9c79f72022-10-17 15:27:18 +08001760 return 0;
1761}
qs.xiong13673462023-02-21 19:12:54 +08001762
1763static int lynq_check_frequencyby_country_code(int input_frequency)
1764{
1765 char str_cnc[]="CN";
1766 char str_dest[20]="";
1767
1768 if( lynq_get_country_code(1,str_dest) != 0 )
1769 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001770 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001771 return -1;
1772 }
1773 if( strncmp(str_dest,str_cnc,2) != 0 )
1774 {
1775 return 0;
1776 }else if( 2473 < input_frequency && input_frequency < 5744)
1777 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001778 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001779 return -1;
1780 }
1781 return 0;
1782}
qs.xiongf1b525b2022-03-31 00:58:23 -04001783int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001784{
qs.xiongc00b6032022-11-29 16:28:03 +08001785 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001786 char lynq_wifi_frequency_cmd[128]={0};
1787 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001788 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001789 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001790 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001791 check = lynq_check_set_frequency(lynq_wifi_frequency);
1792 if(check != 0)
1793 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001794 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001795 return -1;
you.chen35020192022-05-06 11:30:57 +08001796 }
qs.xiong13673462023-02-21 19:12:54 +08001797 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1798 if(check != 0)
1799 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001800 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001801 return -1;
1802 }
1803
qs.xiongc00b6032022-11-29 16:28:03 +08001804 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1805 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001806 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001807 return -1;
1808 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001809
you.chen35020192022-05-06 11:30:57 +08001810 CHECK_IDX(idx, CTRL_AP);
1811
1812 CHECK_WPA_CTRL(CTRL_AP);
1813
1814 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1815 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1816 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1817
you.chen6c2dd9c2022-05-16 17:55:28 +08001818 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001819 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1820 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1821 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001822
qs.xiong9fbf74e2023-03-28 13:38:22 +08001823 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001824}
1825
qs.xiongf1b525b2022-03-31 00:58:23 -04001826int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001827{
you.chen35020192022-05-06 11:30:57 +08001828 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001829 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001830 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001831
qs.xiong9fbf74e2023-03-28 13:38:22 +08001832 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1833 {
1834 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001835 return -1;
1836 }
1837 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001838
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001840}
1841
qs.xiongf1b525b2022-03-31 00:58:23 -04001842int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1843{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001844 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08001845 CHECK_IDX(idx, CTRL_AP);
1846 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001847 case LYNQ_WIFI_BANDWIDTH_HT10:
1848 {
1849 RLOGE("bandwith [%d] not support now\n", bandwidth);
1850 return -1;
1851 }
1852 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08001853 {
1854 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1855 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 if (system(lynq_cmd_bandwith) != 0 )
1857 {
1858 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001859 return -1;
1860 }
1861 system("wl up");
1862 break;
1863 }
1864 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 {
qs.xiong10379192023-02-21 13:19:42 +08001866 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001867 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1868 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001869 if (system(lynq_cmd_bandwith) != 0 )
1870 {
1871 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001872 return -1;
1873 }
1874 system("wl up");
1875 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001876 }
you.chen35020192022-05-06 11:30:57 +08001877 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 {
qs.xiong10379192023-02-21 13:19:42 +08001879 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001880 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001881 if (system(lynq_cmd_bandwith) != 0 )
1882 {
1883 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001884 return -1;
1885 }
1886 system("wl up");
1887 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001888 }
1889 default:
you.chen35020192022-05-06 11:30:57 +08001890 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001891 RLOGE("auth type [%d] not support now\n", bandwidth);
1892 return -1;
you.chen35020192022-05-06 11:30:57 +08001893 }
1894 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001895
1896
you.chen35020192022-05-06 11:30:57 +08001897 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001898}
you.chen35020192022-05-06 11:30:57 +08001899
qs.xiongf1b525b2022-03-31 00:58:23 -04001900int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1901{
you.chen35020192022-05-06 11:30:57 +08001902 int count = 0;
1903 int index = 0;
1904 char *split_words[128] = {0};
1905 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001906 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001907 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001908
you.chen35020192022-05-06 11:30:57 +08001909 CHECK_WPA_CTRL(CTRL_AP);
1910
1911 DO_REQUEST(lynq_chanspec_cmd);
1912
1913 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1914 for(;index < count; index++) {
1915 if (strncmp(split_words[index], "bw", 2) != 0) {
1916 continue;
1917 }
1918
1919 index++;
1920 if (index >= count) {
1921 return -1;
1922 }
1923
qs.xiong9fbf74e2023-03-28 13:38:22 +08001924 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001925 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1926 return 0;
1927 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001928 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001929 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001930}
qs.xiong0fb469a2022-04-14 03:50:45 -04001931
qs.xiongf1b525b2022-03-31 00:58:23 -04001932int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001933{
you.chen35020192022-05-06 11:30:57 +08001934 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001935 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08001936 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001937
you.chen35020192022-05-06 11:30:57 +08001938 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001939
qs.xiong9fbf74e2023-03-28 13:38:22 +08001940 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1941 {
you.chen35020192022-05-06 11:30:57 +08001942 return -1;
1943 }
1944
1945 system("wl down");
1946 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001947 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08001948 return -1;
1949 }
1950 system("wl up");
1951 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001952}
qs.xiong0fb469a2022-04-14 03:50:45 -04001953
qs.xiongf1b525b2022-03-31 00:58:23 -04001954int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001955{
you.chen35020192022-05-06 11:30:57 +08001956 int count = 0;
1957 int index = 0;
1958 char *split_words[128] = {0};
1959 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001960 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08001961 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001962
you.chen35020192022-05-06 11:30:57 +08001963 CHECK_WPA_CTRL(CTRL_AP);
1964
1965 DO_REQUEST(lynq_chanspec_cmd);
1966
1967 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001968 for(;index < count; index++)
1969 {
1970 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001971 if (strncmp(split_words[index], "channel", 2) != 0) {
1972 continue;
1973 }
1974
1975 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001976 if (index >= count)
1977 {
you.chen35020192022-05-06 11:30:57 +08001978 return -1;
1979 }
1980
1981 *channel = atoi(split_words[index]);
1982 return 0;
1983 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001984 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08001985 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001986}
1987
1988
you.chen35020192022-05-06 11:30:57 +08001989int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001990{
you.chen6c2dd9c2022-05-16 17:55:28 +08001991 char ssid[MAX_CMD] = {0};
1992 int freq = 0;
1993 char lynq_auth_cmd[64]={0};
1994 char lynq_auth_alg_cmd[64]={0};
1995 char lynq_psk_cmd[64]={0};
1996 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001997 char lynq_ieee80211_cmd[64]={0};
1998 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001999 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08002000 CHECK_IDX(idx, CTRL_AP);
2001
you.chen6c2dd9c2022-05-16 17:55:28 +08002002 CHECK_WPA_CTRL(CTRL_AP);
2003
qs.xiong9fbf74e2023-03-28 13:38:22 +08002004 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2005 {
2006 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002007 return -1;
2008 }
2009
you.chen92fd5d32022-05-25 10:09:47 +08002010 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002011 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002012 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002013 return 0;
2014 }
2015 else {
2016 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2017 ssid[0] = '\0';
2018 }
2019 lynq_wifi_ap_frequency_get(idx, &freq);
2020
2021 DO_OK_FAIL_REQUEST(cmd_disconnect);
2022 DO_OK_FAIL_REQUEST(cmd_remove_all);
2023 if (ssid[0] != '\0') {
2024 lynq_wifi_ap_ssid_set(idx, ssid);
2025 }
2026 if (freq != 0) {
2027 lynq_wifi_ap_frequency_set(idx, freq);
2028 }
2029 }
2030 }
you.chen35020192022-05-06 11:30:57 +08002031
qs.xiong9fbf74e2023-03-28 13:38:22 +08002032 switch(auth){
2033 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002034 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002035 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002036 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002037 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002038 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 break;
2040 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002041 case LYNQ_WIFI_AUTH_WEP:
2042 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002043 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002044 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002045 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002046 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2047
2048 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2049 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2050 break;
2051 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002052 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002053 {
2054 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2055 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2056 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2057
2058 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2059 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2060 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2061 break;
2062
2063 }
you.chen35020192022-05-06 11:30:57 +08002064 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002065 {
2066 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2067 {
you.chen35020192022-05-06 11:30:57 +08002068 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2069 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2070 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002071 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2072 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002073 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002074 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002075 }
2076// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2077// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2078 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002079
you.chen35020192022-05-06 11:30:57 +08002080 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2081 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2082 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002083 break;
2084 }
2085 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2086 {
2087 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2088 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2089 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2090 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2091
2092 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2093 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2094 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2095 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2096 break;
2097 }
2098 case LYNQ_WIFI_AUTH_WPA3_PSK:
2099 {
2100 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2101 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002102 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002103 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2104
2105 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2106 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2107 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2108 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2109 break;
2110 }
2111 default:
you.chen35020192022-05-06 11:30:57 +08002112 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002113 RLOGE("auth type [%d] not support now\n", auth);
2114 return -1;
you.chen35020192022-05-06 11:30:57 +08002115 }
2116 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002117 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002118
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002120}
2121
you.chen35020192022-05-06 11:30:57 +08002122int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002123{
you.chen35020192022-05-06 11:30:57 +08002124 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002125 char lynq_auth_alg_str[MAX_RET] = {0};
2126 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002127 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002128 CHECK_IDX(idx, CTRL_AP);
2129
qs.xiong9fbf74e2023-03-28 13:38:22 +08002130 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2131 {
2132 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002133 return -1;
2134 }
2135
qs.xiong9fbf74e2023-03-28 13:38:22 +08002136 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2137 {
2138 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2139 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002140 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002141 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002142 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002143 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2145 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002146 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002147 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002148 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002149 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002150 else
2151 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002152 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002153 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002154 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002155 }
you.chen35020192022-05-06 11:30:57 +08002156 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2158 {
2159 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2160 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002161 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002162 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002163 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002164 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2165 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002166 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002167 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002168 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002169 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002170 else
2171 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002172 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002173 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002174 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002175 }
you.chen35020192022-05-06 11:30:57 +08002176 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002177
2178 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2179 {
2180 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2181 return -1;
2182 }
2183
2184 if (memcmp(lynq_auth_str,"1",1) == 0 )
2185 {
2186 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2187 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002188 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002189 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2190 {
2191 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2192 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002193 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002194 }
2195 else
2196 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002197 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002198 *auth = -1;
2199 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002200
you.chen6c2dd9c2022-05-16 17:55:28 +08002201 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002202}
qs.xiong1af5daf2022-03-14 09:12:12 -04002203
you.chenb95401e2023-05-12 19:39:06 +08002204static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2205{
2206 char status[64];
you.chencba13492023-05-19 13:53:43 +08002207 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002208 curr_status_info curr_state;
2209
2210 CHECK_WPA_CTRL(CTRL_AP);
2211
2212 memset(status, 0, sizeof (status));
2213
2214 curr_state.ap = NULL;
2215 curr_state.state = status;
2216
2217 printf("inner_check_ap_connected %d\n", retry_count);
2218 usleep(500*1000);
2219 if (0 == inner_get_status_info(idx, &curr_state))
2220 {
2221 if (strcmp(status, STATE_COMPLETED) == 0)
2222 {
2223 return 0;
2224 }
2225 else if (retry_count == 4) //not ok in 2s, do reconnect
2226 {
2227 DO_REQUEST("RECONNECT");
2228 return inner_check_ap_connected(idx, retry_count+1);
2229 }
you.chencba13492023-05-19 13:53:43 +08002230 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002231 {
2232 printf("retry 10 time\n");
2233 return -1;
2234 }
2235 else
2236 {
you.chen6d247052023-06-01 16:39:54 +08002237 if (strcmp(status, STATE_DISCONNECTED) == 0)
2238 {
2239 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2240 DO_REQUEST(LYNQ_WIFI_CMD);
2241 }
you.chenb95401e2023-05-12 19:39:06 +08002242 return inner_check_ap_connected(idx, retry_count+1);
2243 }
2244 }
2245 return -1;
2246}
qs.xiong1af5daf2022-03-14 09:12:12 -04002247
qs.xiongf1b525b2022-03-31 00:58:23 -04002248int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002249{
you.chen35020192022-05-06 11:30:57 +08002250 char LYNQ_WIFI_CMD[128]={0};
2251 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2252 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002253 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002254 CHECK_IDX(idx, CTRL_AP);
2255
2256 CHECK_WPA_CTRL(CTRL_AP);
2257
you.chen0df3e7e2023-05-10 15:56:26 +08002258 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002259 {
you.chen0df3e7e2023-05-10 15:56:26 +08002260 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002261 return -1;
2262 }
you.chen35020192022-05-06 11:30:57 +08002263
2264 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2265 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2266
2267 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2268 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2269
you.chenc9928582023-04-24 15:39:37 +08002270 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2271 if (ret != 0)
2272 {
2273 RLOGE("lynq_wifi_ap_start excute script fail");
2274 return -1;
2275 }
2276
you.chenb95401e2023-05-12 19:39:06 +08002277 if (inner_check_ap_connected(idx, 0) != 0)
2278 {
2279 return -1;
2280 }
2281
you.chen0df3e7e2023-05-10 15:56:26 +08002282 check_tether_and_notify();
2283
qs.xiong9fbf74e2023-03-28 13:38:22 +08002284 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002285}
2286
qs.xiongf1b525b2022-03-31 00:58:23 -04002287int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002288{
you.chen35020192022-05-06 11:30:57 +08002289 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002290}
2291
qs.xiongf1b525b2022-03-31 00:58:23 -04002292int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002293{
you.chen35020192022-05-06 11:30:57 +08002294 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002295
you.chen35020192022-05-06 11:30:57 +08002296 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002297
you.chen35020192022-05-06 11:30:57 +08002298 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002299
you.chen35020192022-05-06 11:30:57 +08002300 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2301
2302 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2303
you.chenb4b121c2022-05-06 17:50:16 +08002304// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002305
2306 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2307 if (ret != 0)
2308 {
2309 RLOGE("lynq_wifi_ap_start excute script fail");
2310 return -1;
2311 }
qs.xiongae96e1f2023-08-23 17:08:36 +08002312
qs.xiong9fbf74e2023-03-28 13:38:22 +08002313 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002314}
qs.xiong1af5daf2022-03-14 09:12:12 -04002315
qs.xiongf1b525b2022-03-31 00:58:23 -04002316int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002317{
you.chen35020192022-05-06 11:30:57 +08002318 char lynq_disable_cmd[128] = {0};
2319 char lynq_select_cmd[128] = {0};
2320 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002321 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002322 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002323
you.chen35020192022-05-06 11:30:57 +08002324 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002325 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2326 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2327
2328 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2329 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2330 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002331
qs.xiong9fbf74e2023-03-28 13:38:22 +08002332 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002333}
2334
qs.xiongf1b525b2022-03-31 00:58:23 -04002335int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002336{
you.chen35020192022-05-06 11:30:57 +08002337 char lynq_disable_cmd[128] = {0};
2338 char lynq_select_cmd[128] = {0};
2339 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002340 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002341 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002342
you.chen35020192022-05-06 11:30:57 +08002343 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002344
you.chen35020192022-05-06 11:30:57 +08002345 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2346 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2347
2348 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2349 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2350 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002351
qs.xiong9fbf74e2023-03-28 13:38:22 +08002352 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002353}
qs.xiongf1b525b2022-03-31 00:58:23 -04002354
you.chen35020192022-05-06 11:30:57 +08002355int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002356{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002357 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002358 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002359 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002360 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002361 RLOGD("enter lynq_ap_password_set");
2362 if( password == NULL )
2363 {
2364 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002365 return -1;
2366 }
2367 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002368 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002369 if(pass_len < 8 || pass_len >= 64)
2370 {
2371 RLOGE("[lynq_ap_password_set]input password len not in rage");
2372 return -1;
you.chen35020192022-05-06 11:30:57 +08002373 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002374
you.chen35020192022-05-06 11:30:57 +08002375 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002376
qs.xiong9fbf74e2023-03-28 13:38:22 +08002377 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2378 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002379 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002380 return -1;
2381 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002382 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2383 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002384 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2385 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002386 }
2387
you.chen35020192022-05-06 11:30:57 +08002388 CHECK_WPA_CTRL(CTRL_AP);
2389
qs.xiong9fbf74e2023-03-28 13:38:22 +08002390 if (auth == LYNQ_WIFI_AUTH_WEP)
2391 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002392 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002393 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2394 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2395 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2396 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2397 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002398 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2399 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002400 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 +08002401 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2402 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2403 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002404 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2405 {
2406
qs.xiongc8d92a62023-03-29 17:36:14 +08002407 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 +08002408 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002409 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002410 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2411 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2412
2413 }
2414 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002415 {
2416 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002417 return -1;
2418 }
you.chen35020192022-05-06 11:30:57 +08002419
you.chen35020192022-05-06 11:30:57 +08002420 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002421
qs.xiong9fbf74e2023-03-28 13:38:22 +08002422 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002423}
2424
you.chen35020192022-05-06 11:30:57 +08002425int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002426{
you.chen35020192022-05-06 11:30:57 +08002427 FILE * fp;
2428 int len, ret;
2429 int count, index;
2430 char *split_lines[128] = {0};
2431 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002432 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002433
you.chen35020192022-05-06 11:30:57 +08002434 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002435
you.chen35020192022-05-06 11:30:57 +08002436 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2437// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002438 if (NULL == fp)
2439 {
2440 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002441 return -1;
2442 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002443
you.chen35020192022-05-06 11:30:57 +08002444 buff = alloca(MAX_RET);
2445 fseek(fp, 0, SEEK_SET);
2446 len = fread(buff, 1, MAX_RET, fp);
2447 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002448
qs.xiong9fbf74e2023-03-28 13:38:22 +08002449 for(index=0; index < len; index ++)
2450 {
2451 if (memcmp(buff + index, "network={", 9) != 0)
2452 {
you.chen35020192022-05-06 11:30:57 +08002453 continue;
2454 }
2455 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002456 for (; index < len; index ++ )
2457 {
2458 if (buff[index] != '}')
2459 {
you.chen35020192022-05-06 11:30:57 +08002460 continue;
2461 }
2462 buff[index] = '\0';
2463 break;
2464 }
2465 len = buff + index - p;
2466 }
2467
2468 count = lynq_split(p, len, '\n', split_lines);
2469
2470 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002471 for(index=0; index < count; index++)
2472 {
you.chen35020192022-05-06 11:30:57 +08002473 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002474 if (p != NULL)
2475 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002476 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002477 if (*p == '\"')
2478 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002479 p++;
2480 }
you.chen35020192022-05-06 11:30:57 +08002481 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002482 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2483 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002484 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002485 if (*p == '\"')
2486 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002487 p++;
2488 }
2489 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002490 else
2491 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002492 continue;
you.chen35020192022-05-06 11:30:57 +08002493 }
2494
2495 strcpy(password, p);
2496
qs.xiong9fbf74e2023-03-28 13:38:22 +08002497 while(*password != '\0')
2498 {
2499 if (*password == '\"')
2500 {
you.chen35020192022-05-06 11:30:57 +08002501 *password = '\0';
2502 break;
2503 }
2504 password++;
2505 }
2506 ret = 0;
2507 break;
2508 } //end for(index=0; index < count; index++)
2509
2510 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002511}
2512
you.chen35020192022-05-06 11:30:57 +08002513static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2514 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002515 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002516
qs.xiong9fbf74e2023-03-28 13:38:22 +08002517 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2518 {
you.chen35020192022-05-06 11:30:57 +08002519 return -1;
2520 }
2521
2522 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002523
qs.xiong9fbf74e2023-03-28 13:38:22 +08002524 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2525 {
2526 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002527 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002528 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002529 {
you.chena6cd55a2022-05-08 12:20:18 +08002530 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002531 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002532 }
you.chen70f377f2023-04-14 18:17:09 +08002533 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2534 {
2535 return 0;
2536 }
you.chena6cd55a2022-05-08 12:20:18 +08002537 }
2538 }
you.chen70f377f2023-04-14 18:17:09 +08002539 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2540 {
2541 return 0;
2542 }
2543
qs.xiong9fbf74e2023-03-28 13:38:22 +08002544 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2545 {
you.chen70f377f2023-04-14 18:17:09 +08002546 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002547 return -1;
2548 }
2549 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2550 {
2551
you.chen70f377f2023-04-14 18:17:09 +08002552 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2553 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002554 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2555 {
2556
2557 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002558 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002559 }else
2560 {
you.chen70f377f2023-04-14 18:17:09 +08002561 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002562 *auth = -1;
2563 return -1;
2564 }
you.chen35020192022-05-06 11:30:57 +08002565 return 0;
2566}
2567
2568int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002569{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002570 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002571 int pass_len, net_no, count, index;
2572 char lynq_tmp_cmd[300]={0};
2573 int net_no_list[128];
2574 lynq_wifi_auth_s net_auth;
2575 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002576 if(pass_len < 8 || pass_len >= 64)
2577 {
2578 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002579 return -1;
2580 }
2581
2582 CHECK_IDX(idx, CTRL_STA);
2583
2584 net_no = -1;
2585 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2586
qs.xiong9fbf74e2023-03-28 13:38:22 +08002587 for (index=0; index < count; index++)
2588 {
you.chen35020192022-05-06 11:30:57 +08002589 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002590 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2591 {
you.chen35020192022-05-06 11:30:57 +08002592 net_no = net_no_list[index];
2593 break;
2594 }
2595 }
2596
qs.xiong9fbf74e2023-03-28 13:38:22 +08002597 if (net_no < 0)
2598 {
you.chen35020192022-05-06 11:30:57 +08002599 return -1;
2600 }
2601
2602 CHECK_WPA_CTRL(CTRL_STA);
2603
2604 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2605
2606 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2607 DO_OK_FAIL_REQUEST(cmd_save_config);
2608
2609 return 0;
2610}
2611
2612int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2613
2614 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002615 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002616 int count, index;
2617 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002618 char *buff, *p, *ssid, *ssid_end_flag;
2619 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002620 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002621
you.chen755332b2022-08-06 16:59:10 +08002622 network_len = 0;
2623 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002624 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002625
you.chen35020192022-05-06 11:30:57 +08002626 CHECK_IDX(idx, CTRL_STA);
2627
qs.xiong9fbf74e2023-03-28 13:38:22 +08002628 if (NULL == password)
2629 {
2630 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002631 return -1;
2632 }
2633
you.chen35020192022-05-06 11:30:57 +08002634 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002635 if (NULL == fp)
2636 {
2637 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002638 return -1;
2639 }
2640
you.chen6d247052023-06-01 16:39:54 +08002641 fseek(fp, 0, SEEK_END);
2642 len = ftell(fp);
2643 buff = malloc(len + 1);
2644
2645 if (buff == NULL)
2646 {
2647 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2648 return -1;
2649 }
2650
you.chen35020192022-05-06 11:30:57 +08002651 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002652 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002653 fclose(fp);
2654
qs.xiong9fbf74e2023-03-28 13:38:22 +08002655 for(index=0; index < len; index ++)
2656 {
2657 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002658 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002659 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002660 {
you.chen35020192022-05-06 11:30:57 +08002661 continue;
2662 }
you.chen6d247052023-06-01 16:39:54 +08002663 p = buff + index + 9;
2664 for (; index < len; index ++ )
2665 {
2666 if (buff[index] != '}')
2667 {
2668 continue;
2669 }
2670 buff[index] = '\0';
2671 break;
2672 }
2673 network_len = buff + index - p;
2674 break;
you.chen35020192022-05-06 11:30:57 +08002675 }
2676
qs.xiongb3f26af2023-02-17 18:41:07 +08002677 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002678 {
2679 if (buff != NULL)
2680 {
2681 free(buff);
2682 }
2683
qs.xiongb3f26af2023-02-17 18:41:07 +08002684 return -1;
you.chen6d247052023-06-01 16:39:54 +08002685 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002686
you.chend2fef3f2023-02-13 10:50:35 +08002687 ssid = strstr(p, "ssid=");
2688 if (ssid != NULL) {
2689 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002690 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002691 {
you.chend2fef3f2023-02-13 10:50:35 +08002692 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2693 break;
2694 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002695 else
you.chen6d247052023-06-01 16:39:54 +08002696 {
you.chend2fef3f2023-02-13 10:50:35 +08002697 ssid_end_flag = strstr(ssid, "\n");
2698 if (ssid_end_flag != NULL)
2699 {
2700 ssid_len = (ssid_end_flag - ssid) / 2;
2701 for(i=0; i<ssid_len; i++)
2702 {
2703 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2704 }
2705 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2706 break;
2707 }
2708 }
you.chen35020192022-05-06 11:30:57 +08002709 }
you.chend2fef3f2023-02-13 10:50:35 +08002710
you.chen35020192022-05-06 11:30:57 +08002711 }
2712
qs.xiong9fbf74e2023-03-28 13:38:22 +08002713 if (index >= len || NULL == p || network_len <= 0)
2714 {
you.chen6d247052023-06-01 16:39:54 +08002715 if (buff != NULL)
2716 {
2717 free(buff);
2718 }
you.chen35020192022-05-06 11:30:57 +08002719 return -1;
2720 }
2721
you.chen755332b2022-08-06 16:59:10 +08002722 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002723
2724 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002725 for(index=0; index < count; index++)
2726 {
you.chen35020192022-05-06 11:30:57 +08002727 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002728 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002729 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002730 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002732 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002733 p++;
2734 }
you.chen35020192022-05-06 11:30:57 +08002735 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002736 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002737 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002738 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002739 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002740 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002741 p++;
2742 }
2743 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002744 else
you.chen6d247052023-06-01 16:39:54 +08002745 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002746 continue;
you.chen35020192022-05-06 11:30:57 +08002747 }
2748
qs.xiong13673462023-02-21 19:12:54 +08002749 if (*p == '\"')
2750 p++;
2751 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002752
qs.xiong13673462023-02-21 19:12:54 +08002753 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002754 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002755 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002756 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002757 {
you.chen35020192022-05-06 11:30:57 +08002758 *password = '\0';
2759 break;
2760 }
2761 password++;
2762 }
2763 ret = 0;
2764 break;
2765 } //end for(index=0; index < count; index++)
2766
you.chen6d247052023-06-01 16:39:54 +08002767 if (buff != NULL)
2768 {
2769 free(buff);
2770 }
2771
you.chen35020192022-05-06 11:30:57 +08002772 return ret;
2773}
2774
2775static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2776{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002777 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002778
qs.xiong9fbf74e2023-03-28 13:38:22 +08002779 if (sta_ssid == NULL)
2780 {
2781 RLOGE("sta_ssid is null\n");
2782 return -1;
you.chen35020192022-05-06 11:30:57 +08002783 }
2784
qs.xiong9fbf74e2023-03-28 13:38:22 +08002785 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002786
2787 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2788
2789 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2790// DO_OK_FAIL_REQUEST(cmd_save_config);
2791
qs.xiong9fbf74e2023-03-28 13:38:22 +08002792 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002793
2794}
2795
you.chen35020192022-05-06 11:30:57 +08002796static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002797{
you.chen35020192022-05-06 11:30:57 +08002798 char lynq_disable_cmd[128]={0};
2799 char lynq_select_cmd[128]={0};
2800
2801 CHECK_WPA_CTRL(CTRL_STA);
2802
qs.xiong9fbf74e2023-03-28 13:38:22 +08002803 if (save != 0)
2804 {
you.chenc29444e2022-06-07 18:01:16 +08002805 if (start_flag != 0)
2806 {
2807 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2808 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2809 }
2810 else
2811 {
2812 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2813 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2814 }
you.chen35020192022-05-06 11:30:57 +08002815 DO_OK_FAIL_REQUEST(cmd_save_config);
2816 }
2817
qs.xiong9fbf74e2023-03-28 13:38:22 +08002818 if (start_flag == 0)
2819 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002820 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002821 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2822 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002823 else
2824 {
you.chen35020192022-05-06 11:30:57 +08002825 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2826 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2827 }
2828
2829 return 0;
2830}
2831
2832int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2833{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002834 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002835 CHECK_IDX(idx, CTRL_STA);
2836
you.chen6c2dd9c2022-05-16 17:55:28 +08002837 curr_status_info curr_state;
2838 ap_info_s ap_info;
2839 curr_state.ap = &ap_info;
2840 curr_state.state = NULL;
2841
qs.xiong9fbf74e2023-03-28 13:38:22 +08002842 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2843 {
you.chend2fef3f2023-02-13 10:50:35 +08002844 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08002845 return 0;
2846 }
2847
2848 return -1;
you.chen35020192022-05-06 11:30:57 +08002849}
2850
2851int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2852{
you.chen9ac66392022-08-06 17:01:16 +08002853 scan_info_s *scan_list = NULL;
2854 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08002855 int scan_len=0;
2856 int save_len=0;
2857 int best_index = -1;
2858 int best_scan_index = -1;
2859 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08002860 int i, j, ret;
2861
2862 ret = -1;
you.chen35020192022-05-06 11:30:57 +08002863
2864 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002865 if (info == NULL)
2866 {
you.chen35020192022-05-06 11:30:57 +08002867 return -1;
2868 }
2869
2870 curr_status_info curr_state;
2871 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08002872 char status[64];
you.chen35020192022-05-06 11:30:57 +08002873
you.chen9ac66392022-08-06 17:01:16 +08002874 memset(&ap_info, 0, sizeof (ap_info));
2875 memset(status, 0, sizeof (status));
2876
2877 curr_state.ap = &ap_info;
2878 curr_state.state = status;
2879
qs.xiong9fbf74e2023-03-28 13:38:22 +08002880 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2881 {
you.chen35020192022-05-06 11:30:57 +08002882 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08002883 if (strcmp(status, STATE_COMPLETED) == 0)
2884 {
2885 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2886 }
2887 else
2888 {
2889 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2890 }
you.chen593621d2023-04-27 17:52:44 +08002891 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08002892 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08002893 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002894 return 0;
2895 }
2896
you.chen9ac66392022-08-06 17:01:16 +08002897 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08002898 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002899 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2900 {
you.chen9ac66392022-08-06 17:01:16 +08002901 if (NULL != scan_list)
2902 {
2903 free(scan_list);
2904 }
you.chen35020192022-05-06 11:30:57 +08002905 return -1;
2906 }
2907
qs.xiong9fbf74e2023-03-28 13:38:22 +08002908 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2909 {
you.chen9ac66392022-08-06 17:01:16 +08002910 if (NULL != scan_list)
2911 {
2912 free(scan_list);
2913 }
2914 if (NULL != save_list)
2915 {
2916 free(save_list);
2917 }
you.chen35020192022-05-06 11:30:57 +08002918 return -1;
2919 }
2920
qs.xiong9fbf74e2023-03-28 13:38:22 +08002921 for (i=0; i < save_len; i++)
2922 {
2923 for (j=0; j < scan_len; j++)
2924 {
you.chen35020192022-05-06 11:30:57 +08002925 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08002926 && save_list[i].base_info.auth == scan_list[j].auth)
2927 {
2928 if (best_rssi == 0)
2929 {
you.chen9ac66392022-08-06 17:01:16 +08002930 best_index = i;
you.chen35020192022-05-06 11:30:57 +08002931 best_rssi = scan_list[j].rssi;
2932 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002933 else if (best_rssi > scan_list[j].rssi)
2934 {
you.chen35020192022-05-06 11:30:57 +08002935 best_index = i;
2936 best_scan_index = j;
2937 best_rssi = scan_list[j].rssi;
2938 }
you.chend2fef3f2023-02-13 10:50:35 +08002939 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 +08002940 break;
2941 }
2942 }
2943 }
2944
qs.xiong9fbf74e2023-03-28 13:38:22 +08002945 if (best_index >= 0)
2946 {
you.chen35020192022-05-06 11:30:57 +08002947 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08002948 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 +08002949 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2950 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08002951 ret = 0;
you.chen35020192022-05-06 11:30:57 +08002952 }
2953
you.chen9ac66392022-08-06 17:01:16 +08002954 if (NULL != scan_list)
2955 {
2956 free(scan_list);
2957 }
2958 if (NULL != save_list)
2959 {
2960 free(save_list);
2961 }
2962
2963 return ret;
you.chen35020192022-05-06 11:30:57 +08002964}
2965
2966static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2967{
qs.xiongc8d92a62023-03-29 17:36:14 +08002968 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08002969 char lynq_ket_mgmt_cmd[64]={0};
2970 char lynq_pairwise_cmd[64]={0};
2971 char lynq_psk_cmd[64]={0};
2972
2973 CHECK_WPA_CTRL(CTRL_STA);
2974
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 switch(auth)
2976 {
2977 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08002978 {
2979 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002980
you.chen35020192022-05-06 11:30:57 +08002981 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002982// DO_OK_FAIL_REQUEST(cmd_save_config);
2983 break;
2984 }
2985 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08002986 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002987 {
2988 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2989 {
you.chen35020192022-05-06 11:30:57 +08002990 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2991 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002992 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2993 {
you.chena6cd55a2022-05-08 12:20:18 +08002994 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08002995 }
2996 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2997 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002998
you.chen35020192022-05-06 11:30:57 +08002999 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
3000 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3001 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003002
qs.xiong9fbf74e2023-03-28 13:38:22 +08003003 if (password != NULL)
3004 {
you.chen35020192022-05-06 11:30:57 +08003005 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3006 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003007 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003008 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003009
you.chen35020192022-05-06 11:30:57 +08003010// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003011 break;
3012 }
3013 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3014 {
qs.xiong3e506812023-04-06 11:08:48 +08003015 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003016 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003017 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3018 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3019
qs.xiong3e506812023-04-06 11:08:48 +08003020 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003021 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3022 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3023 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3024
3025 break;
3026 }
3027 case LYNQ_WIFI_AUTH_WPA3_PSK:
3028 {
qs.xiong3e506812023-04-06 11:08:48 +08003029 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003030 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003031 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3032 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3033
qs.xiong3e506812023-04-06 11:08:48 +08003034 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003035 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3036 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3037 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3038
3039 break;
3040 }
3041 default:
3042 return -1;
you.chen35020192022-05-06 11:30:57 +08003043 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003044
qs.xiong9fbf74e2023-03-28 13:38:22 +08003045 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003046}
qs.xiong7a105ce2022-03-02 09:43:11 -05003047
you.chen35020192022-05-06 11:30:57 +08003048static int inner_get_curr_net_no(int interface) {
3049 curr_status_info curr_state;
3050 curr_state.ap = NULL;
3051 curr_state.state = NULL;
3052
qs.xiong9fbf74e2023-03-28 13:38:22 +08003053 if (0 != inner_get_status_info(interface, &curr_state))
3054 {
you.chen35020192022-05-06 11:30:57 +08003055 return -1;
3056 }
3057
3058 return curr_state.net_no;
3059}
3060
3061int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003062{
you.chen35020192022-05-06 11:30:57 +08003063 int net_no;
3064 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003065
you.chen35020192022-05-06 11:30:57 +08003066 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003067
qs.xiong9fbf74e2023-03-28 13:38:22 +08003068 if (net_no < 0)
3069 {
you.chen35020192022-05-06 11:30:57 +08003070 return -1;
3071 }
3072
3073 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003074}
3075
you.chenb95401e2023-05-12 19:39:06 +08003076int 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 -05003077{
you.chen35020192022-05-06 11:30:57 +08003078 int count, net_no, index;
3079 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003080 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003081 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003082 curr_status_info curr_state;
3083 ap_info_s ap_info;
3084 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003085
qs.xiong9fbf74e2023-03-28 13:38:22 +08003086 if (ssid == NULL || *ssid == '\0')
3087 {
3088 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003089 return -1;
3090 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003091
qs.xiong9fbf74e2023-03-28 13:38:22 +08003092 if (LYNQ_WIFI_AUTH_OPEN != auth)
3093 {
3094 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003095 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003096 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003097 return -1;
3098 }
3099 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003100
you.chen70f377f2023-04-14 18:17:09 +08003101
3102 pthread_mutex_lock(&s_global_check_mutex);
3103 if (s_sta_status != INNER_STA_STATUS_INIT)
3104 {
3105 s_sta_status = INNER_STA_STATUS_CANCEL;
3106 pthread_cond_signal(&s_global_check_cond);
3107 }
3108 pthread_mutex_unlock(&s_global_check_mutex);
3109
you.chen35020192022-05-06 11:30:57 +08003110 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003111 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003112
3113 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003114 memset(&ap_info, 0, sizeof (ap_info));
3115 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003116
you.chen70f377f2023-04-14 18:17:09 +08003117 curr_state.ap = &ap_info;
3118 curr_state.state = status;
3119
3120 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003121 {
you.chen70f377f2023-04-14 18:17:09 +08003122 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3123 {
3124 net_no = curr_state.net_no;
3125 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3126 && strcmp(ap_info.psw, psw) == 0)
3127 {
3128 RLOGD("already connected\n");
3129
3130 pthread_mutex_lock(&s_global_check_mutex);
3131 s_sta_status = INNER_STA_STATUS_CONNECTED;
3132 pthread_cond_signal(&s_global_check_cond);
3133 pthread_mutex_unlock(&s_global_check_mutex);
3134 return 0;
3135 }
you.chen35020192022-05-06 11:30:57 +08003136 }
3137 }
3138
you.chen70f377f2023-04-14 18:17:09 +08003139 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003140 {
you.chen70f377f2023-04-14 18:17:09 +08003141 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3142
3143 for (index=0; index < count; index++)
3144 {
3145 net_auth = -1;
3146 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3147 {
3148 net_no = net_no_list[index];
3149 break;
3150 }
you.chen35020192022-05-06 11:30:57 +08003151 }
3152
you.chen70f377f2023-04-14 18:17:09 +08003153 if (net_no < 0)
3154 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003155 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3156 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3157 {
3158 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3159 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3160 DO_OK_FAIL_REQUEST(rm_net_cmd);
3161 }
you.chen70f377f2023-04-14 18:17:09 +08003162 net_no = lynq_add_network(CTRL_STA);
3163 if (net_no == -1)
3164 {
3165 return -1;
3166 }
3167
3168 RLOGD("net no is %d\n", net_no);
3169 if (0 != inner_set_sta_ssid(net_no, ssid))
3170 {
3171 return -1;
3172 }
you.chen35020192022-05-06 11:30:57 +08003173 }
3174 }
3175
qs.xiong9fbf74e2023-03-28 13:38:22 +08003176 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3177 {
you.chen35020192022-05-06 11:30:57 +08003178 return -1;
3179 }
3180
you.chen70f377f2023-04-14 18:17:09 +08003181
3182 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003183 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003184 usleep(200*1000);
3185
3186 ret = inner_sta_start_stop(net_no, 1, 1);
3187
3188 pthread_mutex_lock(&s_global_check_mutex);
3189 s_sta_status = INNER_STA_STATUS_CONNECTING;
3190 strcpy(s_sta_current_connecting_ssid, ssid);
3191 struct timeval now;
3192 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003193 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003194 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3195 pthread_cond_signal(&s_global_check_cond);
3196 pthread_mutex_unlock(&s_global_check_mutex);
3197 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003198}
3199
you.chenb95401e2023-05-12 19:39:06 +08003200int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3201{
3202 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3203}
3204
you.chen35020192022-05-06 11:30:57 +08003205int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003206{
you.chen35020192022-05-06 11:30:57 +08003207 ap_info_s ap;
3208 curr_status_info curr_state;
3209 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003210
qs.xiong9fbf74e2023-03-28 13:38:22 +08003211 if (ssid == NULL || *ssid == '\0')
3212 {
3213 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003214 return -1;
3215 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003216
you.chen35020192022-05-06 11:30:57 +08003217 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003218
you.chen35020192022-05-06 11:30:57 +08003219 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003220 curr_state.state = NULL;
3221
qs.xiong9fbf74e2023-03-28 13:38:22 +08003222 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3223 {
you.chen35020192022-05-06 11:30:57 +08003224 return 0;
3225 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003226
qs.xiong9fbf74e2023-03-28 13:38:22 +08003227 if (strcmp(ap.ap_ssid, ssid) != 0)
3228 {
you.chen35020192022-05-06 11:30:57 +08003229 return 0;
3230 }
3231
you.chen70f377f2023-04-14 18:17:09 +08003232 pthread_mutex_lock(&s_global_check_mutex);
3233 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3234 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003235 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003236}
qs.xiong97fa59b2022-04-07 05:41:29 -04003237
qs.xiongc93bf2b2023-08-25 10:22:08 +08003238int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
3239{
3240 ap_info_s ap;
3241 curr_status_info curr_state;
3242 ap.ap_ssid[0] = '\0';
3243
3244 if (ssid == NULL || *ssid == '\0')
3245 {
3246 RLOGE("input ssid is NULL\n");
3247 return -1;
3248 }
3249
3250 CHECK_IDX(idx, CTRL_STA);
3251
3252
3253 curr_state.ap = &ap;
3254 curr_state.state = NULL;
3255
3256 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3257 {
3258 return 0;
3259 }
3260
3261 if (strcmp(ap.ap_ssid, ssid) != 0)
3262 {
3263 return 0;
3264 }
3265
3266 pthread_mutex_lock(&s_global_check_mutex);
3267 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3268 pthread_mutex_unlock(&s_global_check_mutex);
3269 return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
3270
3271}
3272
3273
you.chena6cd55a2022-05-08 12:20:18 +08003274int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3275{
qs.xiongad2f89d2023-01-18 13:17:41 +08003276// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3277// const char *lynq_reconnect_cmd = "RECONNECT";
3278 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3279 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3280// 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 -05003281
you.chen35020192022-05-06 11:30:57 +08003282 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003283 CHECK_WPA_CTRL(CTRL_STA);
3284
you.chenc9928582023-04-24 15:39:37 +08003285 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3286 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003287 {
you.chenc9928582023-04-24 15:39:37 +08003288 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003289 return -1;
3290 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003291
qs.xiongad2f89d2023-01-18 13:17:41 +08003292 system(lynq_enable_sta_cmd);
3293 system(lynq_reconnect_cmd);
3294// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003295 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003296}
3297
you.chen6d247052023-06-01 16:39:54 +08003298static int inner_get_status_info_state (int interface, char *state) {
3299 curr_status_info curr_state;
3300 curr_state.ap = NULL;
3301 curr_state.state = state;
3302 return inner_get_status_info(interface, &curr_state);
3303}
qs.xiongc93bf2b2023-08-25 10:22:08 +08003304
3305int lynq_wifi_sta_start_auto(lynq_wifi_index_e idx)
3306{
3307
3308 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3309
3310 CHECK_IDX(idx, CTRL_STA);
3311 CHECK_WPA_CTRL(CTRL_STA);
3312
3313 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3314 if (ret != 0)
3315 {
3316 RLOGE("lynq_wifi_ap_start excute script fail");
3317 return -1;
3318 }
3319
3320// system(lynq_enable_sta_cmd);
3321 system(lynq_reconnect_cmd);
3322
3323 return 0;
3324}
3325
3326
you.chen35020192022-05-06 11:30:57 +08003327int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003328{
qs.xiongad2f89d2023-01-18 13:17:41 +08003329// char lynq_disable_network_cmd[MAX_CMD];
3330// curr_status_info curr_state;
3331// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003332 int i=0;
3333 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003334
you.chen6d247052023-06-01 16:39:54 +08003335// 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 +08003336 CHECK_IDX(idx, CTRL_STA);
3337 CHECK_WPA_CTRL(CTRL_STA);
3338
you.chen6d247052023-06-01 16:39:54 +08003339// system(lynq_disable_sta_cmd);
3340 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003341 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003342
3343 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3344 if (ret != 0)
3345 {
3346 RLOGE("lynq_wifi_ap_start excute script fail");
3347 return -1;
3348 }
3349
you.chen6d247052023-06-01 16:39:54 +08003350 for (i=0; i < 30; i++) // to check if sta is realy stoped
3351 {
3352 if (inner_get_status_info_state(idx, state) != 0)
3353 {
3354 break;
3355 }
3356
3357 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3358 {
3359 break;
3360 }
3361 RLOGD("lynq_wifi_ap_start curr state %s", state);
3362 usleep(SLEEP_TIME_ON_IDLE);
3363 }
qs.xiongc93bf2b2023-08-25 10:22:08 +08003364
you.chena6cd55a2022-05-08 12:20:18 +08003365 return 0;
3366// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003367}
qs.xiongfcc914b2023-07-06 21:16:20 +08003368int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3369{
3370 char LYNQ_DISABLE_CMD[128]={0};
3371 CHECK_IDX(idx, CTRL_STA);
3372 CHECK_WPA_CTRL(CTRL_STA);
3373 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3374 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3375 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3376 return 0;
3377}
qs.xiong7a105ce2022-03-02 09:43:11 -05003378
you.chen35020192022-05-06 11:30:57 +08003379//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3380// int i, count;
3381// char *p;
3382// const char * FLAG_SSID = "ssid=";
3383// const char * FLAG_SBSID = "bssid=";
3384// const char * FLAG_KEY_MGMT = "key_mgmt=";
3385// const char * FLAG_FREQ = "freq=";
3386// char lynq_sta_cmd[MAX_CMD];
3387// char *split_lines[128] = {0};
3388
3389// CHECK_WPA_CTRL(CTRL_AP);
3390
3391// sprintf(lynq_sta_cmd, "STA %s", bssid);
3392
3393// DO_REQUEST(lynq_sta_cmd);
3394
3395// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3396
3397// for(i=0; i < count; i++) {
3398// p = strstr(split_lines[i], FLAG_SSID);
3399// if (p != NULL) {
3400// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3401// continue;
3402// }
3403// }
3404
3405// lynq_get_interface_ip(idx, ap->ap_ip);
3406// lynq_ap_password_set(idx, ap->psw);
3407
3408// return 0;
3409//}
3410
3411static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3412 curr_status_info curr_state;
3413 curr_state.ap = ap;
3414 curr_state.state = NULL;
3415 return inner_get_status_info(interface, &curr_state);
3416}
3417
3418int 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 -04003419{
you.chend2fef3f2023-02-13 10:50:35 +08003420 int index, line_count;
3421 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003422 const char *lynq_first_sta_cmd = "STA-FIRST";
3423 char lynq_next_sta_cmd[MAX_CMD];
3424 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003425 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003426
you.chen35020192022-05-06 11:30:57 +08003427 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003428
you.chen35020192022-05-06 11:30:57 +08003429 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003430
you.chenb95401e2023-05-12 19:39:06 +08003431 // ap_info_s * tmp_ap;
3432 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003433 if (ap == NULL || list == NULL || len == NULL)
3434 {
3435 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003436 return -1;
3437 }
3438
you.chenb95401e2023-05-12 19:39:06 +08003439 // ap = &tmp_ap;
3440 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003441 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003442 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003443
you.chenb95401e2023-05-12 19:39:06 +08003444 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003445 {
you.chenb95401e2023-05-12 19:39:06 +08003446 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003447 return -1;
3448 }
3449
3450 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3451 lynq_ap_password_get(idx, (*ap)->psw);
3452
you.chen35020192022-05-06 11:30:57 +08003453 DO_REQUEST(lynq_first_sta_cmd);
3454
3455 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003456 while (reply_len > 0)
3457 {
3458 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003459 {
you.chen35020192022-05-06 11:30:57 +08003460 break;
3461 }
3462 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3463 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3464 strcpy(bssid[index], split_lines[0]);
3465 index++;
3466 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3467 reply_len = MAX_RET;
3468 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003469 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 +08003470 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003471 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003472 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003473 break;
3474 }
3475 }
3476
3477 *len = index;
3478
3479 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003480 for (index=0; index < *len; index++)
3481 {
you.chend2fef3f2023-02-13 10:50:35 +08003482 dev_info = &(*list)[index];
3483 memset(dev_info, 0, sizeof(device_info_s));
3484 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3485 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3486 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3487 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003488 free(bssid[index]);
3489 }
3490
3491 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003492}
3493
you.chen35020192022-05-06 11:30:57 +08003494int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003495{
you.chen35020192022-05-06 11:30:57 +08003496 int i, count, index, count_words;
3497 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3498 char *split_lines[128] = {0};
3499 char *split_words[128] = {0};
3500 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003501
qs.xiong9fbf74e2023-03-28 13:38:22 +08003502 if (list == NULL || len == NULL)
3503 {
you.chen35020192022-05-06 11:30:57 +08003504 return -1;
3505 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003506
you.chen9ac66392022-08-06 17:01:16 +08003507 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3508 {
3509 usleep(100 * 1000);
3510 }
3511
you.chen35020192022-05-06 11:30:57 +08003512 CHECK_IDX(idx, CTRL_STA);
3513
3514 CHECK_WPA_CTRL(CTRL_STA);
3515
3516 DO_REQUEST(lynq_scan_result_cmd);
3517
3518 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3519 *len = count - 1;
3520 *list = malloc(sizeof (scan_info_s) * *len);
3521
3522 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003523 for (index=0; index <count_words; index++)
3524 {
3525 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003526 }
3527
qs.xiong9fbf74e2023-03-28 13:38:22 +08003528 for(index = 1;index < count; index++)
3529 {
3530 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003531 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003532 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3533 if (count_words < 4)
3534 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003535 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003536 //bssid / frequency / signal level / flags / ssid
3537 p = (*list) + index - 1;
3538 strcpy(p->mac, split_words[0]);
3539 p->band = convert_band_from_freq(atoi(split_words[1]));
3540 p->rssi = -1 * atoi( split_words[2]);
3541 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003542 if (count_words == 4) // ssid hided
3543 {
3544 p->ssid[0] = '\0';
3545 }
3546 else
3547 {
3548 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3549 }
you.chen35020192022-05-06 11:30:57 +08003550 }
3551
3552 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003553}
qs.xiong97fa59b2022-04-07 05:41:29 -04003554
you.chen35020192022-05-06 11:30:57 +08003555int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3556{
3557 int count, net_no, index;
3558 int net_no_list[128];
3559 lynq_wifi_auth_s net_auth;
3560 char lynq_remove_cmd[MAX_CMD];
3561
qs.xiong9fbf74e2023-03-28 13:38:22 +08003562 if (ssid == NULL || *ssid == '\0')
3563 {
3564 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003565 return -1;
3566 }
3567
3568 CHECK_IDX(idx, CTRL_STA);
3569
3570 CHECK_WPA_CTRL(CTRL_STA);
3571
3572 net_no = -1;
3573 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3574
qs.xiong9fbf74e2023-03-28 13:38:22 +08003575 for (index=0; index < count; index++)
3576 {
you.chen35020192022-05-06 11:30:57 +08003577 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003578 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3579 {
you.chen35020192022-05-06 11:30:57 +08003580 net_no = net_no_list[index];
3581 break;
3582 }
3583 }
3584
qs.xiong9fbf74e2023-03-28 13:38:22 +08003585 if (net_no < 0)
3586 {
you.chen35020192022-05-06 11:30:57 +08003587 return 0;
3588 }
3589
3590 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3591
3592 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3593 DO_OK_FAIL_REQUEST(cmd_save_config);
3594
3595 return 0;
3596}
3597
3598int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003599{
you.chend2fef3f2023-02-13 10:50:35 +08003600 int count, index;
you.chen35020192022-05-06 11:30:57 +08003601 int net_no_list[128];
3602 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003603 RLOGD("enter lynq_get_sta_saved_ap api\n");
3604 if (list == NULL || len == NULL)
3605 {
3606 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003607 return -1;
3608 }
3609
3610 CHECK_IDX(idx, CTRL_STA);
3611
3612// CHECK_WPA_CTRL(CTRL_STA);
3613
3614 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003615 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003616
you.chen057aac42023-04-13 14:06:58 +08003617 if (count < 0)
3618 {
3619 RLOGE("list network fail");
3620 return count;
3621 }
3622 else if (count == 0)
3623 {
3624 *list = NULL;
3625 *len = 0;
3626 return 0;
3627 }
3628
you.chen35020192022-05-06 11:30:57 +08003629 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003630 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003631 *len = count;
3632
qs.xiong9fbf74e2023-03-28 13:38:22 +08003633 for (index=0; index < count; index++)
3634 {
you.chen35020192022-05-06 11:30:57 +08003635 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003636 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003637 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003638 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003639 {
you.chen35020192022-05-06 11:30:57 +08003640 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3641 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003642 else
you.chen057aac42023-04-13 14:06:58 +08003643 {
you.chen35020192022-05-06 11:30:57 +08003644 (*list)[index].base_info.band = -1;
3645 }
you.chen057aac42023-04-13 14:06:58 +08003646 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003647 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003648 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003649 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003650 return 0;
3651}
3652
3653int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3654{
qs.xiongc8d92a62023-03-29 17:36:14 +08003655 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003656 const char *lynq_scan_cmd = "SCAN";
3657
3658 CHECK_IDX(idx, CTRL_STA);
3659
3660 CHECK_WPA_CTRL(CTRL_STA);
3661
you.chen0df3e7e2023-05-10 15:56:26 +08003662 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3663 {
3664 RLOGD("tmp clear scanlist");
3665 system(clean_last_re);
3666 }
you.chen9ac66392022-08-06 17:01:16 +08003667 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003668 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003669 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3670 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003671 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003672 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3673 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003674 g_sta_scan_finish_flag = 1;
3675 return -1;
3676 }
you.chen35020192022-05-06 11:30:57 +08003677
3678 return 0;
3679}
3680
3681int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003682 if (cb == NULL)
3683 {
3684 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003685 return -1;
3686 }
3687
you.chen6d247052023-06-01 16:39:54 +08003688 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003689 g_ap_callback_priv = priv;
3690 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003691 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003692
you.chen6d247052023-06-01 16:39:54 +08003693 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003694 if (g_ap_watcher_pid == 0 )
3695 {
3696 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3697 {
3698 g_ap_watcher_pid = 0;
3699 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3700 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3701 return -1;
3702 }
3703 }
3704
3705 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3706 RLOGD("creat APWatcherTheradProc susccs");
3707
you.chen35020192022-05-06 11:30:57 +08003708 return 0;
3709}
3710
3711int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003712 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003713 if (g_ap_callback_priv == priv)
3714 {
you.chen35020192022-05-06 11:30:57 +08003715 g_ap_callback_func = NULL;
3716 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003717 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003718 return 0;
3719 }
you.chen6d247052023-06-01 16:39:54 +08003720 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003721 return -1;
3722}
3723
3724int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003725 if (cb == NULL)
3726 {
3727 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003728 return -1;
3729 }
3730
you.chen6d247052023-06-01 16:39:54 +08003731 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003732 g_sta_callback_priv = priv;
3733 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003734 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003735
you.chen6d247052023-06-01 16:39:54 +08003736 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003737 if (g_sta_watcher_pid == 0 ) {
3738 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3739 {
3740 g_sta_watcher_pid = 0;
3741 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3742 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3743 return -1;
3744 }
3745 }
3746
3747 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3748 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003749 return 0;
3750}
3751
3752int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003753 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003754 if (g_sta_callback_priv == priv)
3755 {
you.chen35020192022-05-06 11:30:57 +08003756 g_sta_callback_func = NULL;
3757 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003758 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003759 return 0;
3760 }
you.chen6d247052023-06-01 16:39:54 +08003761 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003762 return -1;
3763}
3764
qs.xiongfcc914b2023-07-06 21:16:20 +08003765int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3766 if (cb == NULL)
3767 {
3768 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3769 return -1;
3770 }
3771 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3772 g_sta_auto_callback_priv = priv;
3773 g_sta_auto_callback_func = cb;
3774 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3775 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3776 if (g_sta_auto_watcher_pid == 0 ) {
3777 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3778 {
3779 g_sta_auto_watcher_pid = 0;
3780 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3781 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3782 return -1;
3783 }
3784 }
3785 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3786 RLOGD("creat STAWatcherTheradProc susccs");
3787 return 0;
3788}
3789int lynq_unreg_sta_auto_event_callback(void * priv) {
3790 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3791 if (g_sta_auto_callback_priv == priv)
3792 {
3793 g_sta_auto_watcher_stop_flag = 1;
3794 if (g_sta_auto_watcher_pid != 0)
3795 {
3796 pthread_join(g_sta_auto_watcher_pid, NULL);
3797 }
3798 g_sta_auto_watcher_pid = 0;
3799 g_sta_auto_callback_func = NULL;
3800 g_sta_auto_callback_priv = NULL;
3801 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3802 return 0;
3803 }
3804 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3805 return -1;
3806}
you.chen35020192022-05-06 11:30:57 +08003807int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3808{
3809 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003810 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003811 CHECK_IDX(idx, CTRL_AP);
3812
qs.xiong9fbf74e2023-03-28 13:38:22 +08003813 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3814 {
you.chen35020192022-05-06 11:30:57 +08003815 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3816 return 0;
3817 }
3818
qs.xiong9fbf74e2023-03-28 13:38:22 +08003819 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3820 {
you.chen35020192022-05-06 11:30:57 +08003821 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3822 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003823 else
3824 {
you.chen35020192022-05-06 11:30:57 +08003825 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3826 }
3827
3828 return 0;
3829}
3830
3831int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3832 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003833 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08003834 CHECK_IDX(idx, CTRL_STA);
3835
qs.xiong9fbf74e2023-03-28 13:38:22 +08003836 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3837 {
you.chen35020192022-05-06 11:30:57 +08003838 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3839 return 0;
3840 }
3841
qs.xiong9fbf74e2023-03-28 13:38:22 +08003842 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3843 {
you.chen35020192022-05-06 11:30:57 +08003844 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3845 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003846 else
3847 {
you.chen35020192022-05-06 11:30:57 +08003848 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3849 }
3850
3851 return 0;
3852}
3853
3854int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3855// CHECK_IDX(idx, CTRL_AP);
3856// int ret = 0;
3857// size_t reply_len = MAX_RET;
3858// char cmd_reply[MAX_RET]={0};
3859// const char * cmd_str = "GET country";
3860// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3861// do{
3862// if (NULL == s_lynq_wpa_ctrl) {
3863// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3864// if (NULL == s_lynq_wpa_ctrl ) {
3865// printf("wpa_ctrl_open fail\n");
3866// return -1;
3867// }
3868// }
3869// }while(0);
3870
3871// do {
3872// reply_len = MAX_RET;
3873// cmd_reply[0] = '\0';
3874// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08003875// 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 +08003876// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003877// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08003878// return ret;
3879// }
3880// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08003881// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08003882// }while(0);
3883
3884 FILE *fp;
3885 size_t i = 0;
3886 char lynq_cmd_ret[MAX_RET]={0};
3887
3888// CHECK_IDX(idx, CTRL_AP);
3889
3890 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003891 {
3892 perror("popen error!");
3893 return -1;
3894 }
you.chen35020192022-05-06 11:30:57 +08003895 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3896 {
3897 perror("fread fail!");
3898 return -1;
3899 }
3900
qs.xiong9fbf74e2023-03-28 13:38:22 +08003901 for(i=0; i < strlen(lynq_cmd_ret); i++)
3902 {
3903 if (lynq_cmd_ret[i] == ' ')
3904 {
you.chen35020192022-05-06 11:30:57 +08003905 lynq_cmd_ret[i] = '\0';
3906 break;
3907 }
3908 }
3909
3910 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003911 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08003912
3913 int ret=pclose(fp);
3914 if(ret==-1)
3915 {
3916 perror("close file faild");
3917 }
3918
3919 return 0;
3920}
3921
you.chen705a7ef2023-06-01 22:06:45 +08003922static int check_and_init_uci_config(char * country_code)
3923{
3924 FILE * fp;
3925 int is_different = 0;
3926 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
3927 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
3928 const char * commit_uci_cmd ="uci commit";
3929 char set_country_cmd[MAX_CMD];
3930 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08003931
you.chen705a7ef2023-06-01 22:06:45 +08003932 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08003933
you.chen705a7ef2023-06-01 22:06:45 +08003934 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08003935 {
you.chen705a7ef2023-06-01 22:06:45 +08003936 if (0 != system(create_uci_cmd))
3937 {
3938 RLOGE("creat_uci_cmd fail");
3939 return -1;
3940 }
3941 is_different = 1;
3942 }
3943
3944 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
3945 {
3946 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08003947 return -1;
3948 }
3949
you.chen705a7ef2023-06-01 22:06:45 +08003950 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08003951 {
you.chen705a7ef2023-06-01 22:06:45 +08003952 RLOGE("fread fail!");
3953 fclose(fp);
3954 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08003955 }
3956
you.chen705a7ef2023-06-01 22:06:45 +08003957 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
3958 {
3959 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
3960 is_different = 1;
3961 }
3962
3963 fclose(fp);
3964
3965 if (is_different)
3966 {
3967 if ( 0 != system(set_country_cmd))
3968 {
3969 RLOGE("set_country_cmd fail");
3970 return -1;
3971 }
3972 if (0 != system(commit_uci_cmd))
3973 {
3974 RLOGE("commmit fail");
3975 }
3976 }
3977
3978 return is_different;
3979}
3980
3981int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3982 char check_current_code[10];
3983 const char * support_country[] = {"CN", "EU"};
3984
3985 int ret,is_different, i, cc_count;
3986
3987 if (country_code == NULL || country_code[0] == '\0')
3988 {
3989 RLOGE("bad country code\n");
3990 return -1;
3991 }
3992
3993 cc_count = sizeof (support_country) / sizeof (char*);
3994 for(i=0; i < cc_count; i++)
3995 {
3996 if (strcmp(support_country[i], country_code) == 0)
3997 {
3998 break;
3999 }
4000 }
4001
4002 if (i >= cc_count)
4003 {
4004 RLOGE("unspported country code %s\n", country_code);
4005 return -1;
4006 }
4007
4008 is_different = check_and_init_uci_config(country_code);
4009 if( is_different < 0 )
4010 {
4011 RLOGE("init set uci fail\n");
4012 return -1;
4013 }
4014
4015 ret = lynq_get_country_code(idx,check_current_code);
4016 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
4017 {
4018 ret = lynq_wifi_disable();
4019 if(ret != 0 )
4020 {
4021 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
4022 return -1;
4023 }
4024 }
4025
4026 return 0;
you.chen35020192022-05-06 11:30:57 +08004027}
4028
4029int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
4030{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004031 RLOGD("enter lynq_get_connect_ap_mac\n");
4032 if (mac == NULL)
4033 {
4034 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08004035 return -1;
4036 }
4037
4038 CHECK_IDX(idx, CTRL_STA);
4039 ap_info_s ap;
4040 ap.ap_mac[0] = '\0';
4041
qs.xiong9fbf74e2023-03-28 13:38:22 +08004042 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4043 {
you.chen35020192022-05-06 11:30:57 +08004044 return -1;
4045 }
4046 strcpy(mac, ap.ap_mac);
4047
4048 return 0;
4049}
4050
4051int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
4052{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004053 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08004054 struct ifaddrs *ifaddr_header, *ifaddr;
4055 struct in_addr * ifa;
4056 const char * ifaName = "wlan0";
4057 if (ip == NULL)
4058 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004059 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004060 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004061 }
you.chenf58b3c92022-06-21 16:53:48 +08004062
qs.xiong9fbf74e2023-03-28 13:38:22 +08004063 if (idx == 1)
4064 {
you.chen0df3e7e2023-05-10 15:56:26 +08004065 ifaName = inner_get_ap_interface_name();
4066 if (ifaName == NULL)
4067 {
4068 RLOGE("[lynq_get_interface_ip] ap name get fail");
4069 return -1;
4070 }
you.chen9ac66392022-08-06 17:01:16 +08004071 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004072 else if (idx != 0)
4073 {
you.chen35020192022-05-06 11:30:57 +08004074 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004075 }
you.chen35020192022-05-06 11:30:57 +08004076
you.chen9ac66392022-08-06 17:01:16 +08004077 if (getifaddrs(&ifaddr_header) == -1)
4078 {
you.chen35020192022-05-06 11:30:57 +08004079 perror("getifaddrs");
4080 return -1;
4081 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004082 }
you.chen35020192022-05-06 11:30:57 +08004083
4084
you.chen9ac66392022-08-06 17:01:16 +08004085 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4086 {
4087 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004088 continue;
you.chen9ac66392022-08-06 17:01:16 +08004089 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4090 {
4091 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4092 {
4093 // is a valid IP4 Address
4094 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4095 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004096 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004097 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004098 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004099 return 0;
4100 }
4101 }
4102 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004103 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004104 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004105 return -1;
you.chen35020192022-05-06 11:30:57 +08004106}
4107
4108int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4109{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004110 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004111 int count;
4112 size_t i;
qs.xiongdd6e44c2023-08-08 15:02:53 +08004113 int WIFI_INTERFACE_MAC_LEN = 17;
you.chen35020192022-05-06 11:30:57 +08004114 char *split_words[128] = {0};
4115 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4116
4117 CHECK_WPA_CTRL(idx);
4118
4119 DO_REQUEST(lynq_get_mac_cmd);
4120
qs.xiong9fbf74e2023-03-28 13:38:22 +08004121 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4122 {
4123 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004124 return -1;
4125 }
4126
4127 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4128
qs.xiong9fbf74e2023-03-28 13:38:22 +08004129 if (count < 2)
4130 {
you.chen35020192022-05-06 11:30:57 +08004131 return -1;
4132 }
4133
qs.xiong9fbf74e2023-03-28 13:38:22 +08004134 for (i=0; i < strlen(split_words[1]); i++ )
4135 {
4136 if (split_words[1][i] != ' ')
4137 {
you.chen35020192022-05-06 11:30:57 +08004138 break;
4139 }
4140 }
4141
qs.xiongdd6e44c2023-08-08 15:02:53 +08004142 strncpy(mac, split_words[1] + i, WIFI_INTERFACE_MAC_LEN);
you.chen35020192022-05-06 11:30:57 +08004143
4144 return 0;
4145}
4146
4147int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4148{
4149// int count;
4150// char *split_words[128] = {0};
4151// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4152
4153// if (rssi == NULL) {
4154// return -1;
4155// }
4156
4157// CHECK_IDX(idx, CTRL_STA);
4158
4159// CHECK_WPA_CTRL(CTRL_STA);
4160
4161// DO_REQUEST(lynq_get_rssi_cmd);
4162
4163// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4164// return -1;
4165// }
4166
4167// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4168
4169// if (count < 2) {
4170// return -1;
4171// }
4172
4173// *rssi = atoi(split_words[1]) * -1;
4174
you.chen35020192022-05-06 11:30:57 +08004175 char lynq_cmd_ret[MAX_RET]={0};
4176
qs.xiongff0ae0f2022-10-11 15:47:14 +08004177/*******change other cmd to get rssi*******
4178 *
4179 *wl rssi ---> wl -i wlan0 rssi
4180 *
4181 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004182 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004183 {
you.chen23c4a5f2023-04-12 16:46:00 +08004184 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004185 return -1;
4186 }
you.chen9f17e4d2022-06-06 17:18:18 +08004187 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004188/****** if got rssi is 0,means sta didn't connected any device****/
4189 if(*rssi == 0)
4190 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004191 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004192 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004193 }
you.chen35020192022-05-06 11:30:57 +08004194
4195 return 0;
4196}
4197
4198int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4199{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004200 RLOGD("enter lynq_get_connect_ap_band\n");
4201 if (band == NULL)
4202 {
you.chen35020192022-05-06 11:30:57 +08004203 return -1;
4204 }
4205
4206 CHECK_IDX(idx, CTRL_STA);
4207 ap_info_s ap;
4208 ap.band = -1;
4209
qs.xiong9fbf74e2023-03-28 13:38:22 +08004210 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4211 {
you.chen35020192022-05-06 11:30:57 +08004212 return -1;
4213 }
4214 *band = ap.band;
4215
4216 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004217}
you.chenf58b3c92022-06-21 16:53:48 +08004218
4219int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4220{
you.chenb95401e2023-05-12 19:39:06 +08004221 int ret;
4222 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004223 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004224
4225 if (ip == NULL)
4226 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004227 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004228 return -1;
4229 }
4230
4231 CHECK_IDX(idx, CTRL_STA);
4232
qs.xionge4cbf1c2023-02-28 18:22:49 +08004233 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004234 {
4235 return -1;
4236 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004237
you.chenb95401e2023-05-12 19:39:06 +08004238 ip[0] = '\0';
4239 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4240 if (ret != 0)
4241 {
4242 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4243 return -1;
4244 }
4245
4246 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4247 {
4248 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004249 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004250 if (ret != 0)
4251 {
4252 ip[0] = '\0';
4253 return 0;
4254 }
4255 else
4256 {
4257 p = strchr(ip, '\n');
4258 if (p != NULL)
4259 {
4260 *p = '\0';
4261 }
4262 }
4263 }
4264 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004265}
4266
qs.xiong026c5c72022-10-17 11:15:45 +08004267int lynq_ap_connect_num(int sta_number)
4268{
4269 char lynq_limit_cmd[32]={0};
4270 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004271 if((sta_number < 1 ) && (sta_number > 15))
4272 {
4273 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004274 return -1;
4275 }
4276 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4277 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004278 if(ret != 0)
4279 {
4280 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004281 }
4282 return 0;
4283}
you.chenf58b3c92022-06-21 16:53:48 +08004284
qs.xiong77905552022-10-17 11:19:57 +08004285int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4286{
4287
4288 char lynq_wifi_acs_cmd[128]={0};
4289 char lynq_cmd_mode[128]={0};
4290 char lynq_cmd_slect[128]={0};
4291
qs.xiong9fbf74e2023-03-28 13:38:22 +08004292 if((acs_mode != 2) && (acs_mode != 5))
4293 {
qs.xiong77905552022-10-17 11:19:57 +08004294 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4295 }
4296
qs.xiong9fbf74e2023-03-28 13:38:22 +08004297 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4298 {
qs.xiong77905552022-10-17 11:19:57 +08004299 return -1;
4300 }
4301
4302 CHECK_IDX(idx, CTRL_AP);
4303
4304 CHECK_WPA_CTRL(CTRL_AP);
4305
4306 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4307 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4308 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4309
4310 DO_OK_FAIL_REQUEST(cmd_disconnect);
4311 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4312 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4313 DO_OK_FAIL_REQUEST(cmd_save_config);
4314 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4315
4316 return 0;
4317}
you.chen0f5c6432022-11-07 18:31:14 +08004318//you.chen add for tv-box start
4319static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4320 FILE *fp;
4321 //printf("to exec cmd:%s\n", str_cmd);
4322 if((fp=popen(str_cmd,"r"))==NULL)
4323 {
4324 perror("popen error!");
4325 return -1;
4326 }
4327 if((fread(str_cmd_ret,max_len,1,fp))<0)
4328 {
4329 perror("fread fail!");
4330 fclose(fp);
4331 return -1;
4332 }
4333 fclose(fp);
4334 return 0;
4335}
4336
4337static int get_netmask_length(const char* mask)
4338{
4339 int masklen=0, i=0;
4340 int netmask=0;
4341
4342 if(mask == NULL)
4343 {
4344 return 0;
4345 }
4346
4347 struct in_addr ip_addr;
4348 if( inet_aton(mask, &ip_addr) )
4349 {
4350 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004351 }else
4352 {
you.chen0f5c6432022-11-07 18:31:14 +08004353 netmask = 0;
4354 return 0;
4355 }
4356
4357 while(0 == (netmask & 0x01) && i<32)
4358 {
4359 i++;
4360 netmask = netmask>>1;
4361 }
4362 masklen = 32-i;
4363 return masklen;
4364}
4365
4366static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4367 int mask_len;
4368 char *p;
4369 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004370 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4371 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004372 return -1;
4373 p = strstr(str_cmd_ret, "Mask:");
4374 if (p == NULL)
4375 return -1;
4376 mask_len = get_netmask_length(p + 5);
4377 if (mask_len == 0)
4378 return -1;
4379 p = strstr(str_cmd_ret, "inet addr:");
4380 if (p == NULL)
4381 return -1;
4382 strcpy(tmp, p + 10);
4383 p = strstr(tmp, " ");
4384 if (p != NULL)
4385 *p = '\0';
4386 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4387 return 0;
4388}
4389
4390static void GBWWatchThreadProc() {
4391 int i,n, nloop, nmax, ncheckcount, nidlecount;
4392 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4393 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4394 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4395 char *results[16] = {0};
4396 char str_cmd[256] = {0};
4397 char str_cmd_ret[128] = {0};
4398 char dest_ip[32] = {0};
4399 lastAP1Bytes = lastAP2Bytes = 0;
4400 lastAP1Drop = lastAP2Drop = 0;
4401 lastAP1Speed = lastAP2Speed = 0;
4402 setAP1Speed = 50;
4403 setAP2Speed = 80;
4404 nloop = 0;
4405 nmax = 6;
4406 ncheckcount = nidlecount = 0;
4407
you.chen0df3e7e2023-05-10 15:56:26 +08004408 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004409 {
4410 RLOGE("------gbw thread run\n");
4411 return;
4412 }
4413
qs.xiong9fbf74e2023-03-28 13:38:22 +08004414 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004415 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4416 while (dest_ip[0] == '\0') {
4417 sleep(1);
4418 str_cmd_ret[0] = '\0';
4419 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4420 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4421 if (str_cmd_ret[n] == '\n'){
4422 str_cmd_ret[n] = '\0';
4423 break;
4424 }
4425 }
4426 if (str_cmd_ret[0] != '\0')
4427 {
4428 strcpy(dest_ip, str_cmd_ret);
4429 }
4430 }
4431
you.chenc9928582023-04-24 15:39:37 +08004432 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4433 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4434 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 +08004435 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4436 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004437 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004438 return;
4439 }
you.chenc9928582023-04-24 15:39:37 +08004440 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);
4441 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);
4442 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 +08004443
4444 while (1) {
4445 sleep(1);
4446 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004447 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4448 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4449 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004450 continue;
4451 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004452 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004453 if (n > 9) {
4454 if (strcmp(results[1], "Sent") == 0) {
4455 currAP1Bytes = atoll(results[2]);
4456 }
4457 if (strcmp(results[6], "(dropped") == 0) {
4458 currAP1Drop = atoi(results[7]);
4459 }
4460 }
4461
4462 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004463 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4464 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4465 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004466 continue;
4467 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004468 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004469 if (n > 9) {
4470 if (strcmp(results[1], "Sent") == 0) {
4471 currAP2Bytes = atoll(results[2]);
4472 }
4473 if (strcmp(results[6], "(dropped") == 0) {
4474 currAP2Drop = atoi(results[7]);
4475 }
4476 }
4477
4478 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4479 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4480 lastAP1Bytes = currAP1Bytes;
4481 lastAP2Bytes = currAP2Bytes;
4482 continue;
4483 }
4484
4485 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4486 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4487 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4488 lastAP1Speed = currAP1Speed;
4489 lastAP2Speed = currAP2Speed;
4490 lastAP1Bytes = currAP1Bytes;
4491 lastAP2Bytes = currAP2Bytes;
4492
4493 currSetAP1Speed = setAP1Speed;
4494 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4495 ncheckcount++;
4496 if (ncheckcount > 3) {
4497 ncheckcount = 0;
4498 currSetAP1Speed = 5;
4499 }
4500 }
4501 else {
4502 ncheckcount = 0;
4503 if (currAP1Speed < 5)
4504 nidlecount++;
4505 else
4506 nidlecount = 0;
4507
4508 }
4509
4510 if (nidlecount > 60 ){
4511 currSetAP1Speed = 50;
4512 }
4513
4514 if (currSetAP1Speed != setAP1Speed) {
4515 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004516 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4517 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004518 }
4519 }
4520}
4521
4522int enableGBW(const char* mac) {
4523 int i,len;
4524 char get_ipaddr_cmd[128]={0};
4525 ap_info_s *ap;
4526 device_info_s * list;
4527
4528 if (mac == NULL || g_gbw_enabled == 1)
4529 return -1;
4530 len = strlen(mac);
4531 g_gbw_mac = malloc(len + 1);
4532 for(i=0;i<len;i++) {
4533 if (mac[i] >= 'A' && mac[i] <= 'Z')
4534 {
4535 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4536 }
4537 else
4538 g_gbw_mac[i] = mac[i];
4539 }
4540 g_gbw_mac[i] = '\0';
4541 g_gbw_enabled = 1;
4542
4543 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4544 if (system(get_ipaddr_cmd) == 0) {
4545 //startGBW();
4546 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4547 for (i=0;i<len;i++) {
4548 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4549 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4550 startGBW();
4551 }
4552 free(ap);
4553 free(list);
4554 }
4555 }
4556 return 0;
4557}
4558
4559int disableGBW() {
4560 stopGBW();
4561 free(g_gbw_mac);
4562 g_gbw_mac = NULL;
4563 g_gbw_enabled = 1;
4564 return 0;
4565}
4566
4567static int startGBW() {
4568 if (g_gbw_watcher_pid != 0) {
4569 stopGBW();
4570 }
4571 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4572}
4573
4574static int stopGBW() {
4575 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004576 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004577 pthread_cancel(g_gbw_watcher_pid);
4578 pthread_join(g_gbw_watcher_pid, &retval);
4579 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004580 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4581 if (s_ap_iterface_name[0] != '\0')
4582 {
4583 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4584 system(cmd);
4585 }
you.chen0f5c6432022-11-07 18:31:14 +08004586}
4587//you.chen add for tv-box end