blob: 579da4a3e446046e6718b8442f2c18b463e8acba [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);
544
545 return 0;
546
547}
548
549
qs.xiong455c30b2023-04-12 11:40:02 +0800550void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
551{
552 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800553 char *wpanetid;
554 char destid[3] = {0};
555 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800556 *error = LYNQ_WAIT_CONNECT_ACTIVE;
557 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
558 {
559 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
560 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
561 return;
562 }
563
564 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
565 {
566 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
567 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
568 return;
569 }
570
571 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
572 {
qs.xiongf0128b12023-06-29 17:29:39 +0800573 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
574 wpanetid = strstr(modify,"id=");
575 if ( wpanetid != NULL )
576 {
577 wpanetid +=strlen("id=");
578 memcpy(destid,wpanetid,2);
579 tmpdisid = atoi(destid);
580
581 }
qs.xiong455c30b2023-04-12 11:40:02 +0800582 pReason = strstr(modify, "reason=");
583 if (pReason != NULL)
584 {
585 pReason += strlen("reason=");
586 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
587 {
588 *error = LYNQ_TIME_OUT;
589 }
590 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
591 {
592 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800593 // tmp fix sta autoconnect connect and disconnect
594 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
595 {
596 RLOGE("stop wlan0 network %d fail",tmpdisid);
597 }
qs.xiong455c30b2023-04-12 11:40:02 +0800598 }
599 else
600 {
601 *error = LYNQ_UNSPECIFIED_REASON;
602 }
qs.xiong455c30b2023-04-12 11:40:02 +0800603 }
604 else
605 {
606 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800607 }
qs.xiongf0128b12023-06-29 17:29:39 +0800608 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
609 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800610
611 }
612
613 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
614 {
615 *error = LYNQ_NOT_FIND_AP;
616 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
617 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
618 return;
619 }
620
621
622 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
623 {
624 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
625 pReason = strstr(modify, "status_code=");
626 if (pReason != NULL)
627 {
628 pReason += strlen("status_code=");
629 if (memcmp(pReason, "17", 2) == 0)
630 {
631 *error = LYNQ_AP_UNABLE_HANDLE;
632 }
633 else if (memcmp(pReason, "1",1) == 0)
634 {
635 *error = LYNQ_UNSPECIFIED_REASON;
636 }
637 else
638 {
639 *error = LYNQ_UNSPECIFIED_REASON;
640 }
641
642 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
643 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
644 return;
645 }
646 else
647 {
648 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
649 *error = LYNQ_UNSPECIFIED_REASON;
650 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
651 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
652 return;
653 }
654 }
655
656 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
657 {
658 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
659 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
660 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
661 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
662 return;
663 }
664
665 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
666 {
667 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
668 *error = LYNQ_WAIT_CONNECT_ACTIVE;
669 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
670 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
671 return;
672 }
673
you.chen32cb31e2023-04-13 14:05:45 +0800674 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800675 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800676 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800677 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
678 return;
679
680}
681
qs.xiongfcc914b2023-07-06 21:16:20 +0800682void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
683{
684 char *pReason;
685 char *wpanetid;
686 char destid[3];
687 *error = LYNQ_WAIT_CONNECT_ACTIVE;
688 *networkid = -1;
689 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
690 {
691 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
692 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
693 return;
694 }
695 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
696 {
697 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
698 wpanetid = strstr(modify,"id=");
699 if ( wpanetid != NULL )
700 {
701 wpanetid +=strlen("id=");
702 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
703 if (memcpy(destid,wpanetid,2) != NULL)
704 {
705 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
706 *networkid = atoi(destid);
707 RLOGD("get networkid is %d\n",*networkid);
708 }
709 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
710 }
711 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
712 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
713 return;
714 }
715 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
716 {
717 wpanetid = strstr(modify,"id=");
718 if ( wpanetid != NULL )
719 {
720 wpanetid +=strlen("id=");
721 if (memcpy(destid,wpanetid,2) != NULL)
722 {
723 *networkid = atoi(destid);
724 RLOGD("get networkid is %d\n",*networkid);
725 }
726 }
727 pReason = strstr(modify, "reason=");
728 if (pReason != NULL)
729 {
730 pReason += strlen("reason=");
731 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
732 {
733 *error = LYNQ_TIME_OUT;
734 }
735 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
736 {
737 *error = LYNQ_PSW_ERROR;
738 }
739 else
740 {
741 *error = LYNQ_UNSPECIFIED_REASON;
742 }
743 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
744 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
745 return;
746 }
747 else
748 {
749 *error = LYNQ_UNSPECIFIED_REASON;
750 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
751 return;
752 }
753 }
754 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
755 {
756 wpanetid = strstr(modify,"id=");
757 if ( wpanetid != NULL )
758 {
759 wpanetid +=strlen("id=");
760 if (memcpy(destid,wpanetid,2) != NULL)
761 {
762 *networkid = atoi(destid);
763 RLOGD("get networkid is %d\n",*networkid);
764 }
765 }
766 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
767 pReason = strstr(modify, "status_code=");
768 if (pReason != NULL)
769 {
770 pReason += strlen("status_code=");
771 if (memcmp(pReason, "17", 2) == 0)
772 {
773 *error = LYNQ_AP_UNABLE_HANDLE;
774 }
775 else if (memcmp(pReason, "1",1) == 0)
776 {
777 *error = LYNQ_UNSPECIFIED_REASON;
778 }
779 else
780 {
781 *error = LYNQ_UNSPECIFIED_REASON;
782 }
783 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
784 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
785 return;
786 }
787 else
788 {
789 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
790 *error = LYNQ_UNSPECIFIED_REASON;
791 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
792 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d£¬networkid:%d\n",*state,*error,*networkid);
793 return;
794 }
795 }
796 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
797 {
798 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
799 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
800 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
801 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
802 return;
803 }
804 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
805 {
806 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
807 *error = LYNQ_WAIT_CONNECT_ACTIVE;
808 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
809 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
810 return;
811 }
812 RLOGD("EVENT : %s\n", modify);
813 *error = LYNQ_UNSPECIFIED_REASON;
814 *state = LYNQ_WIFI_STATUS_EGNORE;
815 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
816 return;
817}
you.chen70f377f2023-04-14 18:17:09 +0800818static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
819{
you.chen6d247052023-06-01 16:39:54 +0800820 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800821 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
822 {
823 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
824 g_sta_callback_func(g_sta_callback_priv, state, error);
825 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
826 }
you.chen6d247052023-06-01 16:39:54 +0800827 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800828}
qs.xiongfcc914b2023-07-06 21:16:20 +0800829static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
830{
831 pthread_mutex_lock(&s_sta_callback_mutex);
832 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
833 {
834 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
835 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
836 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
837 }
838 pthread_mutex_unlock(&s_sta_callback_mutex);
839}
you.chen70f377f2023-04-14 18:17:09 +0800840
you.chen35020192022-05-06 11:30:57 +0800841static void STAWatcherThreadProc() {
842 size_t len = MAX_RET;
843 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +0800844 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +0800845 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +0800846 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400847
you.chen6c2dd9c2022-05-16 17:55:28 +0800848 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800849 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800850
you.chen70f377f2023-04-14 18:17:09 +0800851 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +0800852 while (g_sta_watcher_stop_flag == 0)
853 {
you.chenf711c8a2023-04-13 13:49:45 +0800854 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +0800855 {
you.chen35020192022-05-06 11:30:57 +0800856 continue;
857 }
you.chenf711c8a2023-04-13 13:49:45 +0800858
you.chen6c2dd9c2022-05-16 17:55:28 +0800859 memset(msg_notify, 0, MAX_RET);
860 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800861 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +0800862 {
you.chen35020192022-05-06 11:30:57 +0800863 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +0800864 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
865 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800866 {
you.chen35020192022-05-06 11:30:57 +0800867 g_sta_scan_finish_flag = 1;
868 }
869
qs.xiong9fbf74e2023-03-28 13:38:22 +0800870 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800871 {
you.chen35020192022-05-06 11:30:57 +0800872 continue;
873 }
qs.xiong455c30b2023-04-12 11:40:02 +0800874 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +0800875 notify_connect_status(state, error);
876
877 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +0800878 {
you.chen70f377f2023-04-14 18:17:09 +0800879 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +0800880 if (last_state != state)
881 {
882 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
883 {
884 system_call_v("%s %s", sta_status_change_script, "connect");
885 }
886 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
887 {
888 system_call_v("%s %s", sta_status_change_script, "disconnect");
889 }
890 }
891
892 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +0800893 }
you.chen35020192022-05-06 11:30:57 +0800894 }
895 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800896 if (lynq_wpa_ctrl != NULL)
897 {
you.chen92fd5d32022-05-25 10:09:47 +0800898 wpa_ctrl_detach(lynq_wpa_ctrl);
899 wpa_ctrl_close(lynq_wpa_ctrl);
900 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400901}
qs.xiongfcc914b2023-07-06 21:16:20 +0800902static void STAAutoWatcherThreadProc() {
903 size_t len = MAX_RET;
904 char msg_notify[MAX_RET];
905 error_number_s error;
906 lynq_wifi_sta_status_s state, last_state = -1;
907 int idle_count = 0;
908 int networkid;
909 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
910 g_sta_auto_watcher_stop_flag = 0;
911 RLOGD("STAAutoWatcherThreadProc thread started ------");
912 while (g_sta_auto_watcher_stop_flag == 0)
913 {
914 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
915 {
916 continue;
917 }
918 memset(msg_notify, 0, MAX_RET);
919 len = MAX_RET;
920 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
921 {
922 msg_notify[len+1] = '\0';
923 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
924 if (strstr(msg_notify, state_scan_result) != NULL)
925 {
926 g_sta_auto_scan_finish_flag = 1;
927 }
928 if (g_sta_auto_callback_func == NULL)
929 {
930 continue;
931 }
932 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
933 notify_auto_connect_status(state, error,networkid);
934 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
935 {
936 inner_check_connect_error(msg_notify, state, error);
937 if (last_state != state)
938 {
939 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
940 {
941 system_call_v("%s %s", sta_status_change_script, "connect");
942 }
943 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
944 {
945 system_call_v("%s %s", sta_status_change_script, "disconnect");
946 }
947 }
948 last_state = state;
949 }
950 }
951 }
952 if (lynq_wpa_ctrl != NULL)
953 {
954 wpa_ctrl_detach(lynq_wpa_ctrl);
955 wpa_ctrl_close(lynq_wpa_ctrl);
956 }
957}
qs.xiongf1b525b2022-03-31 00:58:23 -0400958
you.chen70f377f2023-04-14 18:17:09 +0800959// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
960static void GlobalWatcherThreadProc()
961{
962 int ret, connect_timeout, service_abnormal;
963 error_number_s error_num = -1;
964 inner_sta_status_s sta_status;
965 scan_info_s *scan_list = NULL;
966 int i, scan_len=0;
967 char connecting_ssid[64];
968 struct timeval now;
969
970 RLOGD("GlobalWatcherThreadProc start to run");
971
972 while (1)
973 {
974 pthread_mutex_lock(&s_global_check_mutex);
975 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
976 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
977 {
978 pthread_mutex_unlock(&s_global_check_mutex);
979 usleep(50*1000);
980 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
981 continue;
982 }
983
984 connect_timeout = 0;
985 service_abnormal = 0;
986 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
987 {
988 while (1)
989 {
990 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
991 if (ret == ETIME)
992 {
993 connect_timeout = 1;
994 }
995 else if (ret != 0)
996 {
997 gettimeofday(&now,NULL);
998 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
999 {
1000 usleep(SLEEP_TIME_ON_IDLE);
1001 continue;
1002 }
1003 connect_timeout = 1;
1004 }
1005 sta_status = s_sta_status;
1006 error_num = s_sta_error_number;
1007 s_sta_status = INNER_STA_STATUS_INIT;
1008 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1009 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1010 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1011 break;
1012 }
1013 }
1014 if (s_service_invoke_timeout_cnt > 10)
1015 {
1016 service_abnormal = 1;
1017 s_service_invoke_timeout_cnt = 0;
1018 }
1019 pthread_mutex_unlock(&s_global_check_mutex);
1020
1021 if (service_abnormal == 1)
1022 {
1023 sleep(1);
1024 RLOGE("wpa service is abnormal info app to exit");
1025 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001026
1027 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1028
you.chen70f377f2023-04-14 18:17:09 +08001029 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1030 }
1031
1032 if (sta_status == INNER_STA_STATUS_CANCEL)
1033 {
1034 continue;
1035 }
1036 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1037 {
1038 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1039 }
1040 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1041 {
1042 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1043 {
1044 for(i=0; i < scan_len;i++)
1045 {
1046 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1047 {
1048 error_num = LYNQ_AUTH_ERROR;
1049 break;
1050 }
1051 }
1052 free(scan_list);
1053 }
1054 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1055 }
1056 else if (connect_timeout == 0)
1057 {
1058 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1059 }
1060 else // wait timeout
1061 {
1062 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1063 {
1064 ; // wpa service abnormal
1065 }
1066 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1067 {
1068 RLOGD("GlobalWatcherThreadProc notify connected");
1069 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1070 }
1071 else
1072 {
1073 RLOGD("GlobalWatcherThreadProc notify timeout");
1074 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1075 }
1076 }
1077 } // while (1)
1078}
1079
qs.xiong1af5daf2022-03-14 09:12:12 -04001080int lynq_wifi_enable(void)
1081{
you.chen35020192022-05-06 11:30:57 +08001082 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001083 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001084 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001085 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1086
qs.xiong9fbf74e2023-03-28 13:38:22 +08001087 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1088 {
you.chend2fef3f2023-02-13 10:50:35 +08001089 goto out_enable;
1090 }
1091
you.chenc9928582023-04-24 15:39:37 +08001092 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001093 if (ret != 0)
1094 {
1095 //printf("service state %d\n", ret);
1096 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001097 ret = -1;
1098 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001099 }
lhfe8da902022-10-11 18:55:36 +08001100
you.chen70f377f2023-04-14 18:17:09 +08001101 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1102 {
1103 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1104 if(ret<0)
1105 {
1106 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1107 ret = -1;
1108 goto out_enable;
1109 }
1110 }
1111
you.chend2fef3f2023-02-13 10:50:35 +08001112 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1113 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1114 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1115 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1116out_enable:
1117 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001118 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001119}
1120
qs.xiong1af5daf2022-03-14 09:12:12 -04001121int lynq_wifi_disable(void)
1122{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001123 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001124 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001125 g_ap_watcher_stop_flag = 1;
1126 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001127 g_sta_auto_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001128 if (g_ap_watcher_pid != 0)
1129 pthread_join(g_ap_watcher_pid, NULL);
1130 if (g_sta_watcher_pid != 0)
1131 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001132 if (g_sta_auto_watcher_pid != 0)
1133 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001134 if (g_lynq_wpa_ctrl[0] != NULL)
1135 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1136 if (g_lynq_wpa_ctrl[1] != NULL)
1137 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
1138 g_ap_watcher_pid = 0;
1139 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001140 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001141 g_lynq_wpa_ctrl[0] = NULL;
1142 g_lynq_wpa_ctrl[1] = NULL;
1143 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001144 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1145 return 0;
1146}
1147
1148static inline char inner_convert_char(char in)
1149{
1150 if (in >= '0' && in <= '9')
1151 {
1152 return in - '0';
1153 }
1154 else if (in >= 'a' && in <= 'f')
1155 {
1156 return in - 'a' + 10;
1157 }
1158 else if (in >= 'A' && in <= 'F')
1159 {
1160 return in - 'A' + 10;
1161 }
1162 else
1163 {
1164 return '\xff';
1165 }
1166}
1167
1168static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1169{
1170 char *p;
1171 size_t pos = 0;
1172 if (NULL == out_ssid)
1173 return;
1174 //printf("input ssid=[%s]\n", ssid);
1175 memset(out_ssid, 0, out_ssid_len);
1176 if (NULL == ssid)
1177 return;
1178 p = strchr(ssid, '\\');
1179 if (NULL == p)
1180 {
1181 strncpy(out_ssid, ssid, out_ssid_len);
1182 //printf(" first %s\n", out_ssid);
1183 }
1184 else
1185 {
1186 pos = p - ssid;
1187 memcpy(out_ssid, ssid, pos);
1188 //printf("pos %lu -- %s\n", pos, out_ssid);
1189 for(; pos < out_ssid_len; pos ++)
1190 {
1191 if (p[0] == '\0')
1192 {
1193 //printf(" out %s\n", out_ssid);
1194 return;
1195 }
1196 else if (p[0] != '\\')
1197 {
1198 out_ssid[pos] = p[0];
1199 p += 1;
1200 }
1201 else if (p[1] == 'x' || p[1] == 'X')
1202 {
1203 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1204 p += 4;
1205 }
1206 else if (p[1] == '\\')
1207 {
1208 out_ssid[pos] = '\\';
1209 p += 2;
1210 }
1211 else if (p[1] == 't')
1212 {
1213 out_ssid[pos] = '\t';
1214 p += 2;
1215 }
1216 else if (p[1] == 'r')
1217 {
1218 out_ssid[pos] = '\r';
1219 p += 2;
1220 }
1221 else if (p[1] == 'n')
1222 {
1223 out_ssid[pos] = '\n';
1224 p += 2;
1225 }//todo find a better way to convert?
1226 }
1227 }
1228 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001229}
qs.xiong1af5daf2022-03-14 09:12:12 -04001230
you.chen35020192022-05-06 11:30:57 +08001231static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001232 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001233 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001234 RLOGD("enter inner_get_param");
1235 if (out_put == NULL)
1236 {
1237 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001238 return -1;
1239 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001240 if (param_name == NULL)
1241 {
1242 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001243 return -1;
1244 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001245 if (param_name[0] == '\0')
1246 {
1247 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001248 return -1;
1249 }
1250
1251 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1252
1253 CHECK_WPA_CTRL(interface);
1254
1255 DO_REQUEST(lynq_cmd_get);
1256
qs.xiong9fbf74e2023-03-28 13:38:22 +08001257 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1258 {
1259 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001260 return -1;
1261 }
1262
you.chena6fa5b22022-05-18 10:28:19 +08001263// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001264 if (strcmp(param_name, "ssid") == 0)
1265 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001266 if (cmd_reply[0] == '\"')
1267 {
you.chend2fef3f2023-02-13 10:50:35 +08001268 ssid_len = reply_len - 1;
1269 memcpy(out_put, cmd_reply + 1, ssid_len);
1270 if (out_put[ssid_len-1] == '\"')
1271 {
1272 out_put[ssid_len-1] = '\0';
1273 }
1274 else
1275 {
1276 out_put[ssid_len] = '\0';
1277 }
1278 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001279 else
1280 {
you.chend2fef3f2023-02-13 10:50:35 +08001281 ssid_len = reply_len / 2;
1282 for(i=0; i<ssid_len; i++)
1283 {
1284 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1285 }
1286 out_put[ssid_len] = '\0';
1287 }
1288 }
1289 else
1290 {
1291 memcpy(out_put, cmd_reply, reply_len + 1);
1292 }
you.chen35020192022-05-06 11:30:57 +08001293 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001294}
qs.xiong1af5daf2022-03-14 09:12:12 -04001295
you.chen35020192022-05-06 11:30:57 +08001296static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1297 int ret = 0;
1298 char * end = str + len - 1;
1299 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001300 while(str < end)
1301 {
1302 if (*str == delimiter)
1303 {
you.chen35020192022-05-06 11:30:57 +08001304 *str++ = '\0';
1305 results[ret++] = str;
1306 continue;
1307 }
1308 str++;
1309 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001310 if (*str == delimiter)
1311 {
you.chen35020192022-05-06 11:30:57 +08001312 *str = '\0';
1313 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001314
you.chen6ed36a62023-04-27 17:51:56 +08001315 results[ret] = NULL;
1316
you.chen35020192022-05-06 11:30:57 +08001317 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001318}
1319
you.chend2fef3f2023-02-13 10:50:35 +08001320static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1321{
1322 char * p;
1323 int ret = 0;
1324 char cmd[256]={0};
1325 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001326 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001327 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001328 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001329 ret = exec_cmd(cmd, ip, ip_len);
1330 p = strchr(ip, '\n');
1331 if (NULL != p)
1332 {
1333 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001334 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001335 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001336 return ret;
1337}
1338
you.chend2fef3f2023-02-13 10:50:35 +08001339static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001340 struct in_addr addr ={0};
1341 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001342 char cmd[64] = {0};
1343 char * p;
1344 int ret;
you.chen35020192022-05-06 11:30:57 +08001345
qs.xiong9fbf74e2023-03-28 13:38:22 +08001346 if (ip == NULL || *ip == '\0' || hostname == NULL)
1347 {
1348 RLOGE("ip == NULL or hostname == NULL");
1349 return -1;
you.chen35020192022-05-06 11:30:57 +08001350 }
1351
you.chend2fef3f2023-02-13 10:50:35 +08001352 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001353 if (inet_aton(ip, &addr) == 0)
1354 {
you.chen35020192022-05-06 11:30:57 +08001355 printf("---inet_aton fail\n");
1356 return -1;
1357 }
1358
1359 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1360
qs.xiong9fbf74e2023-03-28 13:38:22 +08001361 if (ht == NULL)
1362 {
you.chen186d3c32023-05-18 14:19:46 +08001363 hostname[0] = '\0';
1364 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1365 ret = exec_cmd(cmd, hostname, 32);
1366 if (ret == 0)
1367 {
1368 p = strchr(hostname, '\n');
1369 if (p != NULL)
1370 {
1371 *p = '\0';
1372 }
1373 return 0;
1374 }
1375 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001376 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001377 herror(NULL);
1378 return -1;
1379 }
1380
1381 strcpy(hostname, ht->h_name);
1382
1383 return 0;
1384}
1385
1386static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1387{
1388 int count, index, words_count;
1389 char * split_lines[128]= {0};
1390 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001391 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001392 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001393 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001394
1395 CHECK_WPA_CTRL(ap_sta);
1396
1397 DO_REQUEST(lynq_wifi_list_networks);
1398
1399 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1400
1401 //@todo check ssid field to compatible
1402
1403 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001404 for(index=1; index < count; index++)
1405 {
you.chen35020192022-05-06 11:30:57 +08001406 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001407 if (words_count > 2)
1408 {
you.chend2fef3f2023-02-13 10:50:35 +08001409 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001410 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1411 {
you.chen35020192022-05-06 11:30:57 +08001412 net_no_list[ret++] = atoi(split_words[0]);
1413 }
1414 }
1415 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001416 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001417 return ret;
1418}
1419
1420static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001421 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001422 CHECK_WPA_CTRL(ap_sta);
1423 const char *lynq_wifi_add_network = "ADD_NETWORK";
1424
qs.xiong9fbf74e2023-03-28 13:38:22 +08001425 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001426 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001427 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1428 {
1429 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001430 return -1;
1431 }
1432
qs.xiong9fbf74e2023-03-28 13:38:22 +08001433 for(i=0;i<reply_len;i++)
1434 {
1435 if(cmd_reply[i] == '\n')
1436 {
you.chen35020192022-05-06 11:30:57 +08001437 cmd_reply[i] = '\0';
1438 break;
1439 }
1440 }
1441 return atoi(cmd_reply);
1442}
you.chena6cd55a2022-05-08 12:20:18 +08001443
you.chen35020192022-05-06 11:30:57 +08001444static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1445{
1446 int count, index;
1447 int net_no_list[128];
1448
qs.xiong9fbf74e2023-03-28 13:38:22 +08001449 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001450 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001451 for (index=0; index < count; index++)
1452 {
1453 if (net_no_list[index] == net_no)
1454 {
you.chen35020192022-05-06 11:30:57 +08001455 return 0;
1456 }
1457 }
1458
1459 if (count >= 1)
1460 index = net_no_list[count - 1];
1461 else
1462 index = -1;
1463
qs.xiong9fbf74e2023-03-28 13:38:22 +08001464 while (index < net_no )
1465 {
you.chen35020192022-05-06 11:30:57 +08001466 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001467 if (index >= net_no)
1468 { // required network no created
1469 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001470 return 0;
1471 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001472 else if( index < 0)
1473 {
1474 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001475 return -1;
1476 }
you.chen35020192022-05-06 11:30:57 +08001477 }
1478
1479 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001480 {
1481 RLOGE("[lynq_check_network_number] network index < 0");
1482 return -1;
1483 }
1484 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001485 return 0;
1486}
1487
1488static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001489 if (freq > 5000 && freq < 6000)
1490 {
you.chen35020192022-05-06 11:30:57 +08001491 return LYNQ_WIFI_5G_band;
1492 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001493 else if (freq > 2000 && freq < 3000)
1494 {
you.chen35020192022-05-06 11:30:57 +08001495 return LYNQ_WIFI_2G_band;
1496 }
1497 return LYNQ_WIFI_2_and_5G_band;
1498}
1499
1500static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001501 if (key_mgmt != NULL)
1502 {
1503 if (memcmp( key_mgmt, "NONE", 4) == 0)
1504 {
you.chen35020192022-05-06 11:30:57 +08001505 return LYNQ_WIFI_AUTH_OPEN;
1506 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001507 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1508 {
you.chen35020192022-05-06 11:30:57 +08001509 return LYNQ_WIFI_AUTH_WEP;
1510 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001511 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1512 {
you.chen35020192022-05-06 11:30:57 +08001513 return LYNQ_WIFI_AUTH_WPA_PSK;
1514 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001515 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1516 {
you.chen35020192022-05-06 11:30:57 +08001517 return LYNQ_WIFI_AUTH_WPA2_PSK;
1518 }
1519 }
1520
1521 return -1;
1522}
1523
1524static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001525 if (flag != NULL)
1526 {
qs.xiong3e506812023-04-06 11:08:48 +08001527 if ( strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || strstr(flag,"SAE-H2E") != NULL)
1528 {
1529 return LYNQ_WIFI_AUTH_WPA3_PSK;
1530 }else if ( strstr( flag,"SAE-CCMP") != NULL )
1531 {
1532 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1533 }else if (strstr( flag, "WPA2-PSK") != NULL)
1534 {
you.chen35020192022-05-06 11:30:57 +08001535 return LYNQ_WIFI_AUTH_WPA2_PSK;
1536 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001537 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001538 {
you.chen35020192022-05-06 11:30:57 +08001539 return LYNQ_WIFI_AUTH_WPA_PSK;
1540 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001541 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001542 {
you.chen35020192022-05-06 11:30:57 +08001543 return LYNQ_WIFI_AUTH_WEP;
1544 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001545 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001546 {
you.chen35020192022-05-06 11:30:57 +08001547 return LYNQ_WIFI_AUTH_OPEN;
1548 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001549 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001550 {
you.chend2fef3f2023-02-13 10:50:35 +08001551 return LYNQ_WIFI_AUTH_OPEN;
1552 }
you.chen35020192022-05-06 11:30:57 +08001553 }
1554
1555 return -1;
1556}
1557
1558static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1559 switch (bw) {
1560 case 10:
1561 return LYNQ_WIFI_BANDWIDTH_HT10;
1562 break;
1563 case 20:
1564 return LYNQ_WIFI_BANDWIDTH_HT20;
1565 break;
1566 case 40:
1567 return LYNQ_WIFI_BANDWIDTH_HT40;
1568 break;
1569 case 80:
1570 return LYNQ_WIFI_BANDWIDTH_HT80;
1571 break;
1572 default:
1573 break;
1574 }
1575
1576 return -1;
1577}
1578
you.chen70f377f2023-04-14 18:17:09 +08001579static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001580static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1581 int i, count;
1582 char *p;
1583 const char *lynq_status_cmd = "STATUS";
1584 const char * FLAG_SSID = "ssid=";
1585 const char * FLAG_SBSID = "bssid=";
1586 const char * FLAG_KEY_MGMT = "key_mgmt=";
1587 const char * FLAG_FREQ = "freq=";
1588 const char * FLAG_STATE = "wpa_state=";
1589 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001590 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001591 char *split_lines[128] = {0};
1592
1593 CHECK_WPA_CTRL(interface);
1594
qs.xiong9fbf74e2023-03-28 13:38:22 +08001595 if (curr_state == NULL)
1596 {
1597 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001598 return -1;
1599 }
1600
1601 DO_REQUEST(lynq_status_cmd);
1602
1603 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1604
1605 curr_state->net_no = -1;
1606 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001607 for(i=0; i < count; i++)
1608 {
1609 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001610 {
you.chen35020192022-05-06 11:30:57 +08001611 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001612 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001613 {
you.chend2fef3f2023-02-13 10:50:35 +08001614 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001615 ret = 0;
1616 continue;
1617 }
you.chenf58b3c92022-06-21 16:53:48 +08001618 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001619 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001620 {
you.chend2fef3f2023-02-13 10:50:35 +08001621 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 +08001622 ret = 0;
1623 continue;
1624 }
you.chen35020192022-05-06 11:30:57 +08001625 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001626 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001627 {
you.chen450d0172022-07-15 17:56:48 +08001628 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001629 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001630 ret = 0;
1631 continue;
1632 }
1633 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001634 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001635 {
you.chen35020192022-05-06 11:30:57 +08001636 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1637 ret = 0;
1638 continue;
1639 }
you.chend2fef3f2023-02-13 10:50:35 +08001640 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001641 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001642 {
you.chend2fef3f2023-02-13 10:50:35 +08001643 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1644 ret = 0;
1645 continue;
1646 }
you.chen35020192022-05-06 11:30:57 +08001647 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001648 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001649 {
you.chen35020192022-05-06 11:30:57 +08001650 p = strstr(split_lines[i], FLAG_STATE);
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 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1654 ret = 0;
1655 continue;
1656 }
1657
1658 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001659 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001660 {
you.chen35020192022-05-06 11:30:57 +08001661 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001662 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001663 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001664 }
1665 }
1666
you.chen70f377f2023-04-14 18:17:09 +08001667 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1668 {
1669 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1670 }
1671
you.chen35020192022-05-06 11:30:57 +08001672 return ret;
1673}
1674
qs.xiongf1b525b2022-03-31 00:58:23 -04001675int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001676{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001677 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001678 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001679
qs.xiong9fbf74e2023-03-28 13:38:22 +08001680 if (ap_ssid == NULL)
1681 {
1682 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001683 return -1;
1684 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001685 else
1686 {
1687 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001688 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001689
qs.xiong9fbf74e2023-03-28 13:38:22 +08001690 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1691 {
1692 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001693 return -1;
1694 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001695
you.chen35020192022-05-06 11:30:57 +08001696 CHECK_IDX(idx, CTRL_AP);
1697
1698 CHECK_WPA_CTRL(CTRL_AP);
1699
1700 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1701
1702 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1703 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001704 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1705 return 0;
you.chen35020192022-05-06 11:30:57 +08001706
qs.xiong7a105ce2022-03-02 09:43:11 -05001707}
1708
you.chen35020192022-05-06 11:30:57 +08001709int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001710{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001711 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001712 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001713 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001714}
1715
qs.xiongc9c79f72022-10-17 15:27:18 +08001716/*****
1717 *frequency <------>channel
1718 *
1719 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1720 *
1721 *
1722 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1723 *
1724 *
1725 * */
1726static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001727 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};
1728 int i;
1729 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1730
qs.xiong69a332b2022-12-02 09:58:57 +08001731 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001732 {
1733 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001734 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001735 }
qs.xiongc00b6032022-11-29 16:28:03 +08001736
1737 if(i == arr_len)
1738 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001739 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001740 return -1;
1741 }
qs.xiongc00b6032022-11-29 16:28:03 +08001742
qs.xiongc9c79f72022-10-17 15:27:18 +08001743 return 0;
1744}
qs.xiong13673462023-02-21 19:12:54 +08001745
1746static int lynq_check_frequencyby_country_code(int input_frequency)
1747{
1748 char str_cnc[]="CN";
1749 char str_dest[20]="";
1750
1751 if( lynq_get_country_code(1,str_dest) != 0 )
1752 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001753 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001754 return -1;
1755 }
1756 if( strncmp(str_dest,str_cnc,2) != 0 )
1757 {
1758 return 0;
1759 }else if( 2473 < input_frequency && input_frequency < 5744)
1760 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001761 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001762 return -1;
1763 }
1764 return 0;
1765}
qs.xiongf1b525b2022-03-31 00:58:23 -04001766int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001767{
qs.xiongc00b6032022-11-29 16:28:03 +08001768 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001769 char lynq_wifi_frequency_cmd[128]={0};
1770 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001771 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001772 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001773 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001774 check = lynq_check_set_frequency(lynq_wifi_frequency);
1775 if(check != 0)
1776 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001778 return -1;
you.chen35020192022-05-06 11:30:57 +08001779 }
qs.xiong13673462023-02-21 19:12:54 +08001780 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1781 if(check != 0)
1782 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001783 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001784 return -1;
1785 }
1786
qs.xiongc00b6032022-11-29 16:28:03 +08001787 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1788 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001789 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001790 return -1;
1791 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001792
you.chen35020192022-05-06 11:30:57 +08001793 CHECK_IDX(idx, CTRL_AP);
1794
1795 CHECK_WPA_CTRL(CTRL_AP);
1796
1797 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1798 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1799 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1800
you.chen6c2dd9c2022-05-16 17:55:28 +08001801 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001802 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1803 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1804 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001805
qs.xiong9fbf74e2023-03-28 13:38:22 +08001806 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001807}
1808
qs.xiongf1b525b2022-03-31 00:58:23 -04001809int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001810{
you.chen35020192022-05-06 11:30:57 +08001811 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001812 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001813 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001814
qs.xiong9fbf74e2023-03-28 13:38:22 +08001815 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1816 {
1817 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001818 return -1;
1819 }
1820 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001821
qs.xiong9fbf74e2023-03-28 13:38:22 +08001822 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001823}
1824
qs.xiongf1b525b2022-03-31 00:58:23 -04001825int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1826{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001827 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08001828 CHECK_IDX(idx, CTRL_AP);
1829 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001830 case LYNQ_WIFI_BANDWIDTH_HT10:
1831 {
1832 RLOGE("bandwith [%d] not support now\n", bandwidth);
1833 return -1;
1834 }
1835 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08001836 {
1837 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1838 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 if (system(lynq_cmd_bandwith) != 0 )
1840 {
1841 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001842 return -1;
1843 }
1844 system("wl up");
1845 break;
1846 }
1847 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001848 {
qs.xiong10379192023-02-21 13:19:42 +08001849 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001850 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1851 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001852 if (system(lynq_cmd_bandwith) != 0 )
1853 {
1854 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001855 return -1;
1856 }
1857 system("wl up");
1858 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001859 }
you.chen35020192022-05-06 11:30:57 +08001860 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001861 {
qs.xiong10379192023-02-21 13:19:42 +08001862 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001863 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 if (system(lynq_cmd_bandwith) != 0 )
1865 {
1866 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001867 return -1;
1868 }
1869 system("wl up");
1870 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001871 }
1872 default:
you.chen35020192022-05-06 11:30:57 +08001873 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001874 RLOGE("auth type [%d] not support now\n", bandwidth);
1875 return -1;
you.chen35020192022-05-06 11:30:57 +08001876 }
1877 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001878
1879
you.chen35020192022-05-06 11:30:57 +08001880 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001881}
you.chen35020192022-05-06 11:30:57 +08001882
qs.xiongf1b525b2022-03-31 00:58:23 -04001883int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1884{
you.chen35020192022-05-06 11:30:57 +08001885 int count = 0;
1886 int index = 0;
1887 char *split_words[128] = {0};
1888 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001889 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001890 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001891
you.chen35020192022-05-06 11:30:57 +08001892 CHECK_WPA_CTRL(CTRL_AP);
1893
1894 DO_REQUEST(lynq_chanspec_cmd);
1895
1896 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1897 for(;index < count; index++) {
1898 if (strncmp(split_words[index], "bw", 2) != 0) {
1899 continue;
1900 }
1901
1902 index++;
1903 if (index >= count) {
1904 return -1;
1905 }
1906
qs.xiong9fbf74e2023-03-28 13:38:22 +08001907 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001908 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1909 return 0;
1910 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001911 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001912 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001913}
qs.xiong0fb469a2022-04-14 03:50:45 -04001914
qs.xiongf1b525b2022-03-31 00:58:23 -04001915int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001916{
you.chen35020192022-05-06 11:30:57 +08001917 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001918 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08001919 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001920
you.chen35020192022-05-06 11:30:57 +08001921 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001922
qs.xiong9fbf74e2023-03-28 13:38:22 +08001923 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1924 {
you.chen35020192022-05-06 11:30:57 +08001925 return -1;
1926 }
1927
1928 system("wl down");
1929 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001930 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08001931 return -1;
1932 }
1933 system("wl up");
1934 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001935}
qs.xiong0fb469a2022-04-14 03:50:45 -04001936
qs.xiongf1b525b2022-03-31 00:58:23 -04001937int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001938{
you.chen35020192022-05-06 11:30:57 +08001939 int count = 0;
1940 int index = 0;
1941 char *split_words[128] = {0};
1942 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001943 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08001944 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001945
you.chen35020192022-05-06 11:30:57 +08001946 CHECK_WPA_CTRL(CTRL_AP);
1947
1948 DO_REQUEST(lynq_chanspec_cmd);
1949
1950 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001951 for(;index < count; index++)
1952 {
1953 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001954 if (strncmp(split_words[index], "channel", 2) != 0) {
1955 continue;
1956 }
1957
1958 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001959 if (index >= count)
1960 {
you.chen35020192022-05-06 11:30:57 +08001961 return -1;
1962 }
1963
1964 *channel = atoi(split_words[index]);
1965 return 0;
1966 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001967 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08001968 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001969}
1970
1971
you.chen35020192022-05-06 11:30:57 +08001972int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001973{
you.chen6c2dd9c2022-05-16 17:55:28 +08001974 char ssid[MAX_CMD] = {0};
1975 int freq = 0;
1976 char lynq_auth_cmd[64]={0};
1977 char lynq_auth_alg_cmd[64]={0};
1978 char lynq_psk_cmd[64]={0};
1979 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001980 char lynq_ieee80211_cmd[64]={0};
1981 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001982 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001983 CHECK_IDX(idx, CTRL_AP);
1984
you.chen6c2dd9c2022-05-16 17:55:28 +08001985 CHECK_WPA_CTRL(CTRL_AP);
1986
qs.xiong9fbf74e2023-03-28 13:38:22 +08001987 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
1988 {
1989 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08001990 return -1;
1991 }
1992
you.chen92fd5d32022-05-25 10:09:47 +08001993 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001994 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08001995 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001996 return 0;
1997 }
1998 else {
1999 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2000 ssid[0] = '\0';
2001 }
2002 lynq_wifi_ap_frequency_get(idx, &freq);
2003
2004 DO_OK_FAIL_REQUEST(cmd_disconnect);
2005 DO_OK_FAIL_REQUEST(cmd_remove_all);
2006 if (ssid[0] != '\0') {
2007 lynq_wifi_ap_ssid_set(idx, ssid);
2008 }
2009 if (freq != 0) {
2010 lynq_wifi_ap_frequency_set(idx, freq);
2011 }
2012 }
2013 }
you.chen35020192022-05-06 11:30:57 +08002014
qs.xiong9fbf74e2023-03-28 13:38:22 +08002015 switch(auth){
2016 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002017 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002018 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002019 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002020 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002021 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002022 break;
2023 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002024 case LYNQ_WIFI_AUTH_WEP:
2025 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002026 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002027 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002028 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002029 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2030
2031 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2032 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2033 break;
2034 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002035 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002036 {
2037 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2038 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2039 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2040
2041 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2042 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2043 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2044 break;
2045
2046 }
you.chen35020192022-05-06 11:30:57 +08002047 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002048 {
2049 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2050 {
you.chen35020192022-05-06 11:30:57 +08002051 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2052 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2053 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002054 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2055 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002056 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002057 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002058 }
2059// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2060// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2061 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002062
you.chen35020192022-05-06 11:30:57 +08002063 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2064 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2065 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002066 break;
2067 }
2068 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2069 {
2070 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2071 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2072 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2073 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2074
2075 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2076 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2077 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2078 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2079 break;
2080 }
2081 case LYNQ_WIFI_AUTH_WPA3_PSK:
2082 {
2083 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2084 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002085 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002086 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2087
2088 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2089 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2090 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2091 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2092 break;
2093 }
2094 default:
you.chen35020192022-05-06 11:30:57 +08002095 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002096 RLOGE("auth type [%d] not support now\n", auth);
2097 return -1;
you.chen35020192022-05-06 11:30:57 +08002098 }
2099 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002100 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002101
qs.xiong9fbf74e2023-03-28 13:38:22 +08002102 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002103}
2104
you.chen35020192022-05-06 11:30:57 +08002105int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002106{
you.chen35020192022-05-06 11:30:57 +08002107 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002108 char lynq_auth_alg_str[MAX_RET] = {0};
2109 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002111 CHECK_IDX(idx, CTRL_AP);
2112
qs.xiong9fbf74e2023-03-28 13:38:22 +08002113 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2114 {
2115 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002116 return -1;
2117 }
2118
qs.xiong9fbf74e2023-03-28 13:38:22 +08002119 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2120 {
2121 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2122 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002123 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002124 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002125 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002126 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002127 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2128 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002129 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002130 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002131 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002132 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002133 else
2134 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002135 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002136 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002137 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002138 }
you.chen35020192022-05-06 11:30:57 +08002139 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002140 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2141 {
2142 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2143 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002144 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002145 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002146 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002147 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2148 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002149 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002150 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002151 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002152 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002153 else
2154 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002155 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002156 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002157 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002158 }
you.chen35020192022-05-06 11:30:57 +08002159 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002160
2161 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2162 {
2163 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2164 return -1;
2165 }
2166
2167 if (memcmp(lynq_auth_str,"1",1) == 0 )
2168 {
2169 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2170 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002171 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002172 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2173 {
2174 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2175 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002176 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002177 }
2178 else
2179 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002180 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002181 *auth = -1;
2182 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002183
you.chen6c2dd9c2022-05-16 17:55:28 +08002184 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002185}
qs.xiong1af5daf2022-03-14 09:12:12 -04002186
you.chenb95401e2023-05-12 19:39:06 +08002187static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2188{
2189 char status[64];
you.chencba13492023-05-19 13:53:43 +08002190 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002191 curr_status_info curr_state;
2192
2193 CHECK_WPA_CTRL(CTRL_AP);
2194
2195 memset(status, 0, sizeof (status));
2196
2197 curr_state.ap = NULL;
2198 curr_state.state = status;
2199
2200 printf("inner_check_ap_connected %d\n", retry_count);
2201 usleep(500*1000);
2202 if (0 == inner_get_status_info(idx, &curr_state))
2203 {
2204 if (strcmp(status, STATE_COMPLETED) == 0)
2205 {
2206 return 0;
2207 }
2208 else if (retry_count == 4) //not ok in 2s, do reconnect
2209 {
2210 DO_REQUEST("RECONNECT");
2211 return inner_check_ap_connected(idx, retry_count+1);
2212 }
you.chencba13492023-05-19 13:53:43 +08002213 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002214 {
2215 printf("retry 10 time\n");
2216 return -1;
2217 }
2218 else
2219 {
you.chen6d247052023-06-01 16:39:54 +08002220 if (strcmp(status, STATE_DISCONNECTED) == 0)
2221 {
2222 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2223 DO_REQUEST(LYNQ_WIFI_CMD);
2224 }
you.chenb95401e2023-05-12 19:39:06 +08002225 return inner_check_ap_connected(idx, retry_count+1);
2226 }
2227 }
2228 return -1;
2229}
qs.xiong1af5daf2022-03-14 09:12:12 -04002230
qs.xiongf1b525b2022-03-31 00:58:23 -04002231int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002232{
you.chen35020192022-05-06 11:30:57 +08002233 char LYNQ_WIFI_CMD[128]={0};
2234 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2235 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002236 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002237 CHECK_IDX(idx, CTRL_AP);
2238
2239 CHECK_WPA_CTRL(CTRL_AP);
2240
you.chen0df3e7e2023-05-10 15:56:26 +08002241 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002242 {
you.chen0df3e7e2023-05-10 15:56:26 +08002243 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002244 return -1;
2245 }
you.chen35020192022-05-06 11:30:57 +08002246
2247 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2248 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2249
2250 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2251 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2252
you.chenc9928582023-04-24 15:39:37 +08002253 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2254 if (ret != 0)
2255 {
2256 RLOGE("lynq_wifi_ap_start excute script fail");
2257 return -1;
2258 }
2259
you.chenb95401e2023-05-12 19:39:06 +08002260 if (inner_check_ap_connected(idx, 0) != 0)
2261 {
2262 return -1;
2263 }
2264
you.chen0df3e7e2023-05-10 15:56:26 +08002265 check_tether_and_notify();
2266
qs.xiong9fbf74e2023-03-28 13:38:22 +08002267 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002268}
2269
qs.xiongf1b525b2022-03-31 00:58:23 -04002270int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002271{
you.chen35020192022-05-06 11:30:57 +08002272 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002273}
2274
qs.xiongf1b525b2022-03-31 00:58:23 -04002275int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002276{
you.chen35020192022-05-06 11:30:57 +08002277 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002278
you.chen35020192022-05-06 11:30:57 +08002279 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002280
you.chen35020192022-05-06 11:30:57 +08002281 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002282
you.chen35020192022-05-06 11:30:57 +08002283 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2284
2285 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2286
you.chenb4b121c2022-05-06 17:50:16 +08002287// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002288
2289 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2290 if (ret != 0)
2291 {
2292 RLOGE("lynq_wifi_ap_start excute script fail");
2293 return -1;
2294 }
2295
qs.xiong9fbf74e2023-03-28 13:38:22 +08002296 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002297}
qs.xiong1af5daf2022-03-14 09:12:12 -04002298
qs.xiongf1b525b2022-03-31 00:58:23 -04002299int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002300{
you.chen35020192022-05-06 11:30:57 +08002301 char lynq_disable_cmd[128] = {0};
2302 char lynq_select_cmd[128] = {0};
2303 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002304 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002305 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002306
you.chen35020192022-05-06 11:30:57 +08002307 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002308 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2309 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2310
2311 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2312 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2313 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002314
qs.xiong9fbf74e2023-03-28 13:38:22 +08002315 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002316}
2317
qs.xiongf1b525b2022-03-31 00:58:23 -04002318int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002319{
you.chen35020192022-05-06 11:30:57 +08002320 char lynq_disable_cmd[128] = {0};
2321 char lynq_select_cmd[128] = {0};
2322 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002323 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002324 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002325
you.chen35020192022-05-06 11:30:57 +08002326 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002327
you.chen35020192022-05-06 11:30:57 +08002328 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2329 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2330
2331 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2332 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2333 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002334
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002336}
qs.xiongf1b525b2022-03-31 00:58:23 -04002337
you.chen35020192022-05-06 11:30:57 +08002338int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002339{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002340 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002341 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002342 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002343 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002344 RLOGD("enter lynq_ap_password_set");
2345 if( password == NULL )
2346 {
2347 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002348 return -1;
2349 }
2350 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002351 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002352 if(pass_len < 8 || pass_len >= 64)
2353 {
2354 RLOGE("[lynq_ap_password_set]input password len not in rage");
2355 return -1;
you.chen35020192022-05-06 11:30:57 +08002356 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002357
you.chen35020192022-05-06 11:30:57 +08002358 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002359
qs.xiong9fbf74e2023-03-28 13:38:22 +08002360 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2361 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002362 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002363 return -1;
2364 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002365 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2366 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002367 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2368 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002369 }
2370
you.chen35020192022-05-06 11:30:57 +08002371 CHECK_WPA_CTRL(CTRL_AP);
2372
qs.xiong9fbf74e2023-03-28 13:38:22 +08002373 if (auth == LYNQ_WIFI_AUTH_WEP)
2374 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002375 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002376 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2377 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2378 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2379 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2380 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002381 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2382 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002383 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 +08002384 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2385 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2386 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002387 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2388 {
2389
qs.xiongc8d92a62023-03-29 17:36:14 +08002390 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 +08002391 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002392 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002393 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2394 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2395
2396 }
2397 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002398 {
2399 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002400 return -1;
2401 }
you.chen35020192022-05-06 11:30:57 +08002402
you.chen35020192022-05-06 11:30:57 +08002403 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002404
qs.xiong9fbf74e2023-03-28 13:38:22 +08002405 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002406}
2407
you.chen35020192022-05-06 11:30:57 +08002408int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002409{
you.chen35020192022-05-06 11:30:57 +08002410 FILE * fp;
2411 int len, ret;
2412 int count, index;
2413 char *split_lines[128] = {0};
2414 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002415 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002416
you.chen35020192022-05-06 11:30:57 +08002417 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002418
you.chen35020192022-05-06 11:30:57 +08002419 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2420// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 if (NULL == fp)
2422 {
2423 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002424 return -1;
2425 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002426
you.chen35020192022-05-06 11:30:57 +08002427 buff = alloca(MAX_RET);
2428 fseek(fp, 0, SEEK_SET);
2429 len = fread(buff, 1, MAX_RET, fp);
2430 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002431
qs.xiong9fbf74e2023-03-28 13:38:22 +08002432 for(index=0; index < len; index ++)
2433 {
2434 if (memcmp(buff + index, "network={", 9) != 0)
2435 {
you.chen35020192022-05-06 11:30:57 +08002436 continue;
2437 }
2438 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002439 for (; index < len; index ++ )
2440 {
2441 if (buff[index] != '}')
2442 {
you.chen35020192022-05-06 11:30:57 +08002443 continue;
2444 }
2445 buff[index] = '\0';
2446 break;
2447 }
2448 len = buff + index - p;
2449 }
2450
2451 count = lynq_split(p, len, '\n', split_lines);
2452
2453 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002454 for(index=0; index < count; index++)
2455 {
you.chen35020192022-05-06 11:30:57 +08002456 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002457 if (p != NULL)
2458 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002459 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002460 if (*p == '\"')
2461 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002462 p++;
2463 }
you.chen35020192022-05-06 11:30:57 +08002464 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002465 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2466 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002467 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002468 if (*p == '\"')
2469 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002470 p++;
2471 }
2472 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002473 else
2474 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002475 continue;
you.chen35020192022-05-06 11:30:57 +08002476 }
2477
2478 strcpy(password, p);
2479
qs.xiong9fbf74e2023-03-28 13:38:22 +08002480 while(*password != '\0')
2481 {
2482 if (*password == '\"')
2483 {
you.chen35020192022-05-06 11:30:57 +08002484 *password = '\0';
2485 break;
2486 }
2487 password++;
2488 }
2489 ret = 0;
2490 break;
2491 } //end for(index=0; index < count; index++)
2492
2493 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002494}
2495
you.chen35020192022-05-06 11:30:57 +08002496static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2497 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002498 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002499
qs.xiong9fbf74e2023-03-28 13:38:22 +08002500 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2501 {
you.chen35020192022-05-06 11:30:57 +08002502 return -1;
2503 }
2504
2505 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002506
qs.xiong9fbf74e2023-03-28 13:38:22 +08002507 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2508 {
2509 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002510 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002511 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002512 {
you.chena6cd55a2022-05-08 12:20:18 +08002513 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002514 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002515 }
you.chen70f377f2023-04-14 18:17:09 +08002516 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2517 {
2518 return 0;
2519 }
you.chena6cd55a2022-05-08 12:20:18 +08002520 }
2521 }
you.chen70f377f2023-04-14 18:17:09 +08002522 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2523 {
2524 return 0;
2525 }
2526
qs.xiong9fbf74e2023-03-28 13:38:22 +08002527 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2528 {
you.chen70f377f2023-04-14 18:17:09 +08002529 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002530 return -1;
2531 }
2532 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2533 {
2534
you.chen70f377f2023-04-14 18:17:09 +08002535 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2536 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002537 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2538 {
2539
2540 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002541 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002542 }else
2543 {
you.chen70f377f2023-04-14 18:17:09 +08002544 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002545 *auth = -1;
2546 return -1;
2547 }
you.chen35020192022-05-06 11:30:57 +08002548 return 0;
2549}
2550
2551int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002552{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002553 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002554 int pass_len, net_no, count, index;
2555 char lynq_tmp_cmd[300]={0};
2556 int net_no_list[128];
2557 lynq_wifi_auth_s net_auth;
2558 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002559 if(pass_len < 8 || pass_len >= 64)
2560 {
2561 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002562 return -1;
2563 }
2564
2565 CHECK_IDX(idx, CTRL_STA);
2566
2567 net_no = -1;
2568 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2569
qs.xiong9fbf74e2023-03-28 13:38:22 +08002570 for (index=0; index < count; index++)
2571 {
you.chen35020192022-05-06 11:30:57 +08002572 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002573 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2574 {
you.chen35020192022-05-06 11:30:57 +08002575 net_no = net_no_list[index];
2576 break;
2577 }
2578 }
2579
qs.xiong9fbf74e2023-03-28 13:38:22 +08002580 if (net_no < 0)
2581 {
you.chen35020192022-05-06 11:30:57 +08002582 return -1;
2583 }
2584
2585 CHECK_WPA_CTRL(CTRL_STA);
2586
2587 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2588
2589 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2590 DO_OK_FAIL_REQUEST(cmd_save_config);
2591
2592 return 0;
2593}
2594
2595int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2596
2597 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002598 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002599 int count, index;
2600 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002601 char *buff, *p, *ssid, *ssid_end_flag;
2602 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002603 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002604
you.chen755332b2022-08-06 16:59:10 +08002605 network_len = 0;
2606 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002607 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002608
you.chen35020192022-05-06 11:30:57 +08002609 CHECK_IDX(idx, CTRL_STA);
2610
qs.xiong9fbf74e2023-03-28 13:38:22 +08002611 if (NULL == password)
2612 {
2613 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002614 return -1;
2615 }
2616
you.chen35020192022-05-06 11:30:57 +08002617 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002618 if (NULL == fp)
2619 {
2620 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002621 return -1;
2622 }
2623
you.chen6d247052023-06-01 16:39:54 +08002624 fseek(fp, 0, SEEK_END);
2625 len = ftell(fp);
2626 buff = malloc(len + 1);
2627
2628 if (buff == NULL)
2629 {
2630 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2631 return -1;
2632 }
2633
you.chen35020192022-05-06 11:30:57 +08002634 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002635 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002636 fclose(fp);
2637
qs.xiong9fbf74e2023-03-28 13:38:22 +08002638 for(index=0; index < len; index ++)
2639 {
2640 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002641 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002642 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002643 {
you.chen35020192022-05-06 11:30:57 +08002644 continue;
2645 }
you.chen6d247052023-06-01 16:39:54 +08002646 p = buff + index + 9;
2647 for (; index < len; index ++ )
2648 {
2649 if (buff[index] != '}')
2650 {
2651 continue;
2652 }
2653 buff[index] = '\0';
2654 break;
2655 }
2656 network_len = buff + index - p;
2657 break;
you.chen35020192022-05-06 11:30:57 +08002658 }
2659
qs.xiongb3f26af2023-02-17 18:41:07 +08002660 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002661 {
2662 if (buff != NULL)
2663 {
2664 free(buff);
2665 }
2666
qs.xiongb3f26af2023-02-17 18:41:07 +08002667 return -1;
you.chen6d247052023-06-01 16:39:54 +08002668 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002669
you.chend2fef3f2023-02-13 10:50:35 +08002670 ssid = strstr(p, "ssid=");
2671 if (ssid != NULL) {
2672 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002673 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002674 {
you.chend2fef3f2023-02-13 10:50:35 +08002675 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2676 break;
2677 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002678 else
you.chen6d247052023-06-01 16:39:54 +08002679 {
you.chend2fef3f2023-02-13 10:50:35 +08002680 ssid_end_flag = strstr(ssid, "\n");
2681 if (ssid_end_flag != NULL)
2682 {
2683 ssid_len = (ssid_end_flag - ssid) / 2;
2684 for(i=0; i<ssid_len; i++)
2685 {
2686 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2687 }
2688 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2689 break;
2690 }
2691 }
you.chen35020192022-05-06 11:30:57 +08002692 }
you.chend2fef3f2023-02-13 10:50:35 +08002693
you.chen35020192022-05-06 11:30:57 +08002694 }
2695
qs.xiong9fbf74e2023-03-28 13:38:22 +08002696 if (index >= len || NULL == p || network_len <= 0)
2697 {
you.chen6d247052023-06-01 16:39:54 +08002698 if (buff != NULL)
2699 {
2700 free(buff);
2701 }
you.chen35020192022-05-06 11:30:57 +08002702 return -1;
2703 }
2704
you.chen755332b2022-08-06 16:59:10 +08002705 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002706
2707 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002708 for(index=0; index < count; index++)
2709 {
you.chen35020192022-05-06 11:30:57 +08002710 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002711 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002712 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002713 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002714 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002715 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002716 p++;
2717 }
you.chen35020192022-05-06 11:30:57 +08002718 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002719 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002720 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002721 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002722 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002723 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002724 p++;
2725 }
2726 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002727 else
you.chen6d247052023-06-01 16:39:54 +08002728 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002729 continue;
you.chen35020192022-05-06 11:30:57 +08002730 }
2731
qs.xiong13673462023-02-21 19:12:54 +08002732 if (*p == '\"')
2733 p++;
2734 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002735
qs.xiong13673462023-02-21 19:12:54 +08002736 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002737 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002738 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002739 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002740 {
you.chen35020192022-05-06 11:30:57 +08002741 *password = '\0';
2742 break;
2743 }
2744 password++;
2745 }
2746 ret = 0;
2747 break;
2748 } //end for(index=0; index < count; index++)
2749
you.chen6d247052023-06-01 16:39:54 +08002750 if (buff != NULL)
2751 {
2752 free(buff);
2753 }
2754
you.chen35020192022-05-06 11:30:57 +08002755 return ret;
2756}
2757
2758static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2759{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002760 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002761
qs.xiong9fbf74e2023-03-28 13:38:22 +08002762 if (sta_ssid == NULL)
2763 {
2764 RLOGE("sta_ssid is null\n");
2765 return -1;
you.chen35020192022-05-06 11:30:57 +08002766 }
2767
qs.xiong9fbf74e2023-03-28 13:38:22 +08002768 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002769
2770 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2771
2772 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2773// DO_OK_FAIL_REQUEST(cmd_save_config);
2774
qs.xiong9fbf74e2023-03-28 13:38:22 +08002775 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002776
2777}
2778
you.chen35020192022-05-06 11:30:57 +08002779static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002780{
you.chen35020192022-05-06 11:30:57 +08002781 char lynq_disable_cmd[128]={0};
2782 char lynq_select_cmd[128]={0};
2783
2784 CHECK_WPA_CTRL(CTRL_STA);
2785
qs.xiong9fbf74e2023-03-28 13:38:22 +08002786 if (save != 0)
2787 {
you.chenc29444e2022-06-07 18:01:16 +08002788 if (start_flag != 0)
2789 {
2790 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2791 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2792 }
2793 else
2794 {
2795 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2796 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2797 }
you.chen35020192022-05-06 11:30:57 +08002798 DO_OK_FAIL_REQUEST(cmd_save_config);
2799 }
2800
qs.xiong9fbf74e2023-03-28 13:38:22 +08002801 if (start_flag == 0)
2802 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002803 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002804 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2805 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002806 else
2807 {
you.chen35020192022-05-06 11:30:57 +08002808 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2809 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2810 }
2811
2812 return 0;
2813}
2814
2815int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2816{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002817 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002818 CHECK_IDX(idx, CTRL_STA);
2819
you.chen6c2dd9c2022-05-16 17:55:28 +08002820 curr_status_info curr_state;
2821 ap_info_s ap_info;
2822 curr_state.ap = &ap_info;
2823 curr_state.state = NULL;
2824
qs.xiong9fbf74e2023-03-28 13:38:22 +08002825 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2826 {
you.chend2fef3f2023-02-13 10:50:35 +08002827 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08002828 return 0;
2829 }
2830
2831 return -1;
you.chen35020192022-05-06 11:30:57 +08002832}
2833
2834int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2835{
you.chen9ac66392022-08-06 17:01:16 +08002836 scan_info_s *scan_list = NULL;
2837 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08002838 int scan_len=0;
2839 int save_len=0;
2840 int best_index = -1;
2841 int best_scan_index = -1;
2842 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08002843 int i, j, ret;
2844
2845 ret = -1;
you.chen35020192022-05-06 11:30:57 +08002846
2847 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002848 if (info == NULL)
2849 {
you.chen35020192022-05-06 11:30:57 +08002850 return -1;
2851 }
2852
2853 curr_status_info curr_state;
2854 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08002855 char status[64];
you.chen35020192022-05-06 11:30:57 +08002856
you.chen9ac66392022-08-06 17:01:16 +08002857 memset(&ap_info, 0, sizeof (ap_info));
2858 memset(status, 0, sizeof (status));
2859
2860 curr_state.ap = &ap_info;
2861 curr_state.state = status;
2862
qs.xiong9fbf74e2023-03-28 13:38:22 +08002863 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2864 {
you.chen35020192022-05-06 11:30:57 +08002865 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08002866 if (strcmp(status, STATE_COMPLETED) == 0)
2867 {
2868 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2869 }
2870 else
2871 {
2872 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2873 }
you.chen593621d2023-04-27 17:52:44 +08002874 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08002875 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08002876 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002877 return 0;
2878 }
2879
you.chen9ac66392022-08-06 17:01:16 +08002880 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08002881 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002882 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2883 {
you.chen9ac66392022-08-06 17:01:16 +08002884 if (NULL != scan_list)
2885 {
2886 free(scan_list);
2887 }
you.chen35020192022-05-06 11:30:57 +08002888 return -1;
2889 }
2890
qs.xiong9fbf74e2023-03-28 13:38:22 +08002891 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2892 {
you.chen9ac66392022-08-06 17:01:16 +08002893 if (NULL != scan_list)
2894 {
2895 free(scan_list);
2896 }
2897 if (NULL != save_list)
2898 {
2899 free(save_list);
2900 }
you.chen35020192022-05-06 11:30:57 +08002901 return -1;
2902 }
2903
qs.xiong9fbf74e2023-03-28 13:38:22 +08002904 for (i=0; i < save_len; i++)
2905 {
2906 for (j=0; j < scan_len; j++)
2907 {
you.chen35020192022-05-06 11:30:57 +08002908 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08002909 && save_list[i].base_info.auth == scan_list[j].auth)
2910 {
2911 if (best_rssi == 0)
2912 {
you.chen9ac66392022-08-06 17:01:16 +08002913 best_index = i;
you.chen35020192022-05-06 11:30:57 +08002914 best_rssi = scan_list[j].rssi;
2915 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002916 else if (best_rssi > scan_list[j].rssi)
2917 {
you.chen35020192022-05-06 11:30:57 +08002918 best_index = i;
2919 best_scan_index = j;
2920 best_rssi = scan_list[j].rssi;
2921 }
you.chend2fef3f2023-02-13 10:50:35 +08002922 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 +08002923 break;
2924 }
2925 }
2926 }
2927
qs.xiong9fbf74e2023-03-28 13:38:22 +08002928 if (best_index >= 0)
2929 {
you.chen35020192022-05-06 11:30:57 +08002930 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08002931 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 +08002932 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2933 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08002934 ret = 0;
you.chen35020192022-05-06 11:30:57 +08002935 }
2936
you.chen9ac66392022-08-06 17:01:16 +08002937 if (NULL != scan_list)
2938 {
2939 free(scan_list);
2940 }
2941 if (NULL != save_list)
2942 {
2943 free(save_list);
2944 }
2945
2946 return ret;
you.chen35020192022-05-06 11:30:57 +08002947}
2948
2949static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2950{
qs.xiongc8d92a62023-03-29 17:36:14 +08002951 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08002952 char lynq_ket_mgmt_cmd[64]={0};
2953 char lynq_pairwise_cmd[64]={0};
2954 char lynq_psk_cmd[64]={0};
2955
2956 CHECK_WPA_CTRL(CTRL_STA);
2957
qs.xiong9fbf74e2023-03-28 13:38:22 +08002958 switch(auth)
2959 {
2960 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08002961 {
2962 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002963
you.chen35020192022-05-06 11:30:57 +08002964 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002965// DO_OK_FAIL_REQUEST(cmd_save_config);
2966 break;
2967 }
2968 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08002969 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002970 {
2971 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2972 {
you.chen35020192022-05-06 11:30:57 +08002973 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2974 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002975 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2976 {
you.chena6cd55a2022-05-08 12:20:18 +08002977 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08002978 }
2979 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2980 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002981
you.chen35020192022-05-06 11:30:57 +08002982 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2983 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2984 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04002985
qs.xiong9fbf74e2023-03-28 13:38:22 +08002986 if (password != NULL)
2987 {
you.chen35020192022-05-06 11:30:57 +08002988 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2989 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08002991 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002992
you.chen35020192022-05-06 11:30:57 +08002993// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002994 break;
2995 }
2996 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2997 {
qs.xiong3e506812023-04-06 11:08:48 +08002998 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08002999 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003000 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3001 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3002
qs.xiong3e506812023-04-06 11:08:48 +08003003 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003004 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3005 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3006 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3007
3008 break;
3009 }
3010 case LYNQ_WIFI_AUTH_WPA3_PSK:
3011 {
qs.xiong3e506812023-04-06 11:08:48 +08003012 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003013 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003014 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3015 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3016
qs.xiong3e506812023-04-06 11:08:48 +08003017 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003018 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3019 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3020 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3021
3022 break;
3023 }
3024 default:
3025 return -1;
you.chen35020192022-05-06 11:30:57 +08003026 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003027
qs.xiong9fbf74e2023-03-28 13:38:22 +08003028 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003029}
qs.xiong7a105ce2022-03-02 09:43:11 -05003030
you.chen35020192022-05-06 11:30:57 +08003031static int inner_get_curr_net_no(int interface) {
3032 curr_status_info curr_state;
3033 curr_state.ap = NULL;
3034 curr_state.state = NULL;
3035
qs.xiong9fbf74e2023-03-28 13:38:22 +08003036 if (0 != inner_get_status_info(interface, &curr_state))
3037 {
you.chen35020192022-05-06 11:30:57 +08003038 return -1;
3039 }
3040
3041 return curr_state.net_no;
3042}
3043
3044int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003045{
you.chen35020192022-05-06 11:30:57 +08003046 int net_no;
3047 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003048
you.chen35020192022-05-06 11:30:57 +08003049 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003050
qs.xiong9fbf74e2023-03-28 13:38:22 +08003051 if (net_no < 0)
3052 {
you.chen35020192022-05-06 11:30:57 +08003053 return -1;
3054 }
3055
3056 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003057}
3058
you.chenb95401e2023-05-12 19:39:06 +08003059int 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 -05003060{
you.chen35020192022-05-06 11:30:57 +08003061 int count, net_no, index;
3062 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003063 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003064 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003065 curr_status_info curr_state;
3066 ap_info_s ap_info;
3067 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003068
qs.xiong9fbf74e2023-03-28 13:38:22 +08003069 if (ssid == NULL || *ssid == '\0')
3070 {
3071 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003072 return -1;
3073 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003074
qs.xiong9fbf74e2023-03-28 13:38:22 +08003075 if (LYNQ_WIFI_AUTH_OPEN != auth)
3076 {
3077 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003078 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003079 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003080 return -1;
3081 }
3082 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003083
you.chen70f377f2023-04-14 18:17:09 +08003084
3085 pthread_mutex_lock(&s_global_check_mutex);
3086 if (s_sta_status != INNER_STA_STATUS_INIT)
3087 {
3088 s_sta_status = INNER_STA_STATUS_CANCEL;
3089 pthread_cond_signal(&s_global_check_cond);
3090 }
3091 pthread_mutex_unlock(&s_global_check_mutex);
3092
you.chen35020192022-05-06 11:30:57 +08003093 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003094 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003095
3096 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003097 memset(&ap_info, 0, sizeof (ap_info));
3098 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003099
you.chen70f377f2023-04-14 18:17:09 +08003100 curr_state.ap = &ap_info;
3101 curr_state.state = status;
3102
3103 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003104 {
you.chen70f377f2023-04-14 18:17:09 +08003105 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3106 {
3107 net_no = curr_state.net_no;
3108 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3109 && strcmp(ap_info.psw, psw) == 0)
3110 {
3111 RLOGD("already connected\n");
3112
3113 pthread_mutex_lock(&s_global_check_mutex);
3114 s_sta_status = INNER_STA_STATUS_CONNECTED;
3115 pthread_cond_signal(&s_global_check_cond);
3116 pthread_mutex_unlock(&s_global_check_mutex);
3117 return 0;
3118 }
you.chen35020192022-05-06 11:30:57 +08003119 }
3120 }
3121
you.chen70f377f2023-04-14 18:17:09 +08003122 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003123 {
you.chen70f377f2023-04-14 18:17:09 +08003124 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3125
3126 for (index=0; index < count; index++)
3127 {
3128 net_auth = -1;
3129 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3130 {
3131 net_no = net_no_list[index];
3132 break;
3133 }
you.chen35020192022-05-06 11:30:57 +08003134 }
3135
you.chen70f377f2023-04-14 18:17:09 +08003136 if (net_no < 0)
3137 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003138 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3139 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3140 {
3141 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3142 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3143 DO_OK_FAIL_REQUEST(rm_net_cmd);
3144 }
you.chen70f377f2023-04-14 18:17:09 +08003145 net_no = lynq_add_network(CTRL_STA);
3146 if (net_no == -1)
3147 {
3148 return -1;
3149 }
3150
3151 RLOGD("net no is %d\n", net_no);
3152 if (0 != inner_set_sta_ssid(net_no, ssid))
3153 {
3154 return -1;
3155 }
you.chen35020192022-05-06 11:30:57 +08003156 }
3157 }
3158
qs.xiong9fbf74e2023-03-28 13:38:22 +08003159 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3160 {
you.chen35020192022-05-06 11:30:57 +08003161 return -1;
3162 }
3163
you.chen70f377f2023-04-14 18:17:09 +08003164
3165 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003166 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003167 usleep(200*1000);
3168
3169 ret = inner_sta_start_stop(net_no, 1, 1);
3170
3171 pthread_mutex_lock(&s_global_check_mutex);
3172 s_sta_status = INNER_STA_STATUS_CONNECTING;
3173 strcpy(s_sta_current_connecting_ssid, ssid);
3174 struct timeval now;
3175 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003176 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003177 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3178 pthread_cond_signal(&s_global_check_cond);
3179 pthread_mutex_unlock(&s_global_check_mutex);
3180 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003181}
3182
you.chenb95401e2023-05-12 19:39:06 +08003183int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3184{
3185 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3186}
3187
you.chen35020192022-05-06 11:30:57 +08003188int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003189{
you.chen35020192022-05-06 11:30:57 +08003190 ap_info_s ap;
3191 curr_status_info curr_state;
3192 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003193
qs.xiong9fbf74e2023-03-28 13:38:22 +08003194 if (ssid == NULL || *ssid == '\0')
3195 {
3196 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003197 return -1;
3198 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003199
you.chen35020192022-05-06 11:30:57 +08003200 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003201
you.chen35020192022-05-06 11:30:57 +08003202 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003203 curr_state.state = NULL;
3204
qs.xiong9fbf74e2023-03-28 13:38:22 +08003205 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3206 {
you.chen35020192022-05-06 11:30:57 +08003207 return 0;
3208 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003209
qs.xiong9fbf74e2023-03-28 13:38:22 +08003210 if (strcmp(ap.ap_ssid, ssid) != 0)
3211 {
you.chen35020192022-05-06 11:30:57 +08003212 return 0;
3213 }
3214
you.chen70f377f2023-04-14 18:17:09 +08003215 pthread_mutex_lock(&s_global_check_mutex);
3216 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3217 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003218 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003219}
qs.xiong97fa59b2022-04-07 05:41:29 -04003220
you.chena6cd55a2022-05-08 12:20:18 +08003221int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3222{
qs.xiongad2f89d2023-01-18 13:17:41 +08003223// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3224// const char *lynq_reconnect_cmd = "RECONNECT";
3225 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3226 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3227// 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 -05003228
you.chen35020192022-05-06 11:30:57 +08003229 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003230 CHECK_WPA_CTRL(CTRL_STA);
3231
you.chenc9928582023-04-24 15:39:37 +08003232 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3233 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003234 {
you.chenc9928582023-04-24 15:39:37 +08003235 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003236 return -1;
3237 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003238
qs.xiongad2f89d2023-01-18 13:17:41 +08003239 system(lynq_enable_sta_cmd);
3240 system(lynq_reconnect_cmd);
3241// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003242 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003243}
3244
you.chen6d247052023-06-01 16:39:54 +08003245static int inner_get_status_info_state (int interface, char *state) {
3246 curr_status_info curr_state;
3247 curr_state.ap = NULL;
3248 curr_state.state = state;
3249 return inner_get_status_info(interface, &curr_state);
3250}
you.chen35020192022-05-06 11:30:57 +08003251int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003252{
qs.xiongad2f89d2023-01-18 13:17:41 +08003253// char lynq_disable_network_cmd[MAX_CMD];
3254// curr_status_info curr_state;
3255// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003256 int i=0;
3257 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003258
you.chen6d247052023-06-01 16:39:54 +08003259// 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 +08003260 CHECK_IDX(idx, CTRL_STA);
3261 CHECK_WPA_CTRL(CTRL_STA);
3262
you.chen6d247052023-06-01 16:39:54 +08003263// system(lynq_disable_sta_cmd);
3264 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003265 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003266
3267 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3268 if (ret != 0)
3269 {
3270 RLOGE("lynq_wifi_ap_start excute script fail");
3271 return -1;
3272 }
3273
you.chen6d247052023-06-01 16:39:54 +08003274 for (i=0; i < 30; i++) // to check if sta is realy stoped
3275 {
3276 if (inner_get_status_info_state(idx, state) != 0)
3277 {
3278 break;
3279 }
3280
3281 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3282 {
3283 break;
3284 }
3285 RLOGD("lynq_wifi_ap_start curr state %s", state);
3286 usleep(SLEEP_TIME_ON_IDLE);
3287 }
3288
you.chena6cd55a2022-05-08 12:20:18 +08003289 return 0;
3290// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003291}
qs.xiongfcc914b2023-07-06 21:16:20 +08003292int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3293{
3294 char LYNQ_DISABLE_CMD[128]={0};
3295 CHECK_IDX(idx, CTRL_STA);
3296 CHECK_WPA_CTRL(CTRL_STA);
3297 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3298 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3299 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3300 return 0;
3301}
qs.xiong7a105ce2022-03-02 09:43:11 -05003302
you.chen35020192022-05-06 11:30:57 +08003303//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3304// int i, count;
3305// char *p;
3306// const char * FLAG_SSID = "ssid=";
3307// const char * FLAG_SBSID = "bssid=";
3308// const char * FLAG_KEY_MGMT = "key_mgmt=";
3309// const char * FLAG_FREQ = "freq=";
3310// char lynq_sta_cmd[MAX_CMD];
3311// char *split_lines[128] = {0};
3312
3313// CHECK_WPA_CTRL(CTRL_AP);
3314
3315// sprintf(lynq_sta_cmd, "STA %s", bssid);
3316
3317// DO_REQUEST(lynq_sta_cmd);
3318
3319// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3320
3321// for(i=0; i < count; i++) {
3322// p = strstr(split_lines[i], FLAG_SSID);
3323// if (p != NULL) {
3324// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3325// continue;
3326// }
3327// }
3328
3329// lynq_get_interface_ip(idx, ap->ap_ip);
3330// lynq_ap_password_set(idx, ap->psw);
3331
3332// return 0;
3333//}
3334
3335static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3336 curr_status_info curr_state;
3337 curr_state.ap = ap;
3338 curr_state.state = NULL;
3339 return inner_get_status_info(interface, &curr_state);
3340}
3341
3342int 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 -04003343{
you.chend2fef3f2023-02-13 10:50:35 +08003344 int index, line_count;
3345 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003346 const char *lynq_first_sta_cmd = "STA-FIRST";
3347 char lynq_next_sta_cmd[MAX_CMD];
3348 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003349 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003350
you.chen35020192022-05-06 11:30:57 +08003351 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003352
you.chen35020192022-05-06 11:30:57 +08003353 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003354
you.chenb95401e2023-05-12 19:39:06 +08003355 // ap_info_s * tmp_ap;
3356 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003357 if (ap == NULL || list == NULL || len == NULL)
3358 {
3359 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003360 return -1;
3361 }
3362
you.chenb95401e2023-05-12 19:39:06 +08003363 // ap = &tmp_ap;
3364 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003365 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003366 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003367
you.chenb95401e2023-05-12 19:39:06 +08003368 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003369 {
you.chenb95401e2023-05-12 19:39:06 +08003370 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003371 return -1;
3372 }
3373
3374 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3375 lynq_ap_password_get(idx, (*ap)->psw);
3376
you.chen35020192022-05-06 11:30:57 +08003377 DO_REQUEST(lynq_first_sta_cmd);
3378
3379 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003380 while (reply_len > 0)
3381 {
3382 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003383 {
you.chen35020192022-05-06 11:30:57 +08003384 break;
3385 }
3386 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3387 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3388 strcpy(bssid[index], split_lines[0]);
3389 index++;
3390 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3391 reply_len = MAX_RET;
3392 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003393 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 +08003394 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003395 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003396 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003397 break;
3398 }
3399 }
3400
3401 *len = index;
3402
3403 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003404 for (index=0; index < *len; index++)
3405 {
you.chend2fef3f2023-02-13 10:50:35 +08003406 dev_info = &(*list)[index];
3407 memset(dev_info, 0, sizeof(device_info_s));
3408 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3409 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3410 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3411 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003412 free(bssid[index]);
3413 }
3414
3415 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003416}
3417
you.chen35020192022-05-06 11:30:57 +08003418int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003419{
you.chen35020192022-05-06 11:30:57 +08003420 int i, count, index, count_words;
3421 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3422 char *split_lines[128] = {0};
3423 char *split_words[128] = {0};
3424 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003425
qs.xiong9fbf74e2023-03-28 13:38:22 +08003426 if (list == NULL || len == NULL)
3427 {
you.chen35020192022-05-06 11:30:57 +08003428 return -1;
3429 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003430
you.chen9ac66392022-08-06 17:01:16 +08003431 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3432 {
3433 usleep(100 * 1000);
3434 }
3435
you.chen35020192022-05-06 11:30:57 +08003436 CHECK_IDX(idx, CTRL_STA);
3437
3438 CHECK_WPA_CTRL(CTRL_STA);
3439
3440 DO_REQUEST(lynq_scan_result_cmd);
3441
3442 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3443 *len = count - 1;
3444 *list = malloc(sizeof (scan_info_s) * *len);
3445
3446 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003447 for (index=0; index <count_words; index++)
3448 {
3449 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003450 }
3451
qs.xiong9fbf74e2023-03-28 13:38:22 +08003452 for(index = 1;index < count; index++)
3453 {
3454 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003455 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003456 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3457 if (count_words < 4)
3458 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003459 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003460 //bssid / frequency / signal level / flags / ssid
3461 p = (*list) + index - 1;
3462 strcpy(p->mac, split_words[0]);
3463 p->band = convert_band_from_freq(atoi(split_words[1]));
3464 p->rssi = -1 * atoi( split_words[2]);
3465 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003466 if (count_words == 4) // ssid hided
3467 {
3468 p->ssid[0] = '\0';
3469 }
3470 else
3471 {
3472 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3473 }
you.chen35020192022-05-06 11:30:57 +08003474 }
3475
3476 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003477}
qs.xiong97fa59b2022-04-07 05:41:29 -04003478
you.chen35020192022-05-06 11:30:57 +08003479int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3480{
3481 int count, net_no, index;
3482 int net_no_list[128];
3483 lynq_wifi_auth_s net_auth;
3484 char lynq_remove_cmd[MAX_CMD];
3485
qs.xiong9fbf74e2023-03-28 13:38:22 +08003486 if (ssid == NULL || *ssid == '\0')
3487 {
3488 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003489 return -1;
3490 }
3491
3492 CHECK_IDX(idx, CTRL_STA);
3493
3494 CHECK_WPA_CTRL(CTRL_STA);
3495
3496 net_no = -1;
3497 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3498
qs.xiong9fbf74e2023-03-28 13:38:22 +08003499 for (index=0; index < count; index++)
3500 {
you.chen35020192022-05-06 11:30:57 +08003501 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003502 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3503 {
you.chen35020192022-05-06 11:30:57 +08003504 net_no = net_no_list[index];
3505 break;
3506 }
3507 }
3508
qs.xiong9fbf74e2023-03-28 13:38:22 +08003509 if (net_no < 0)
3510 {
you.chen35020192022-05-06 11:30:57 +08003511 return 0;
3512 }
3513
3514 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3515
3516 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3517 DO_OK_FAIL_REQUEST(cmd_save_config);
3518
3519 return 0;
3520}
3521
3522int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003523{
you.chend2fef3f2023-02-13 10:50:35 +08003524 int count, index;
you.chen35020192022-05-06 11:30:57 +08003525 int net_no_list[128];
3526 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003527 RLOGD("enter lynq_get_sta_saved_ap api\n");
3528 if (list == NULL || len == NULL)
3529 {
3530 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003531 return -1;
3532 }
3533
3534 CHECK_IDX(idx, CTRL_STA);
3535
3536// CHECK_WPA_CTRL(CTRL_STA);
3537
3538 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003539 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003540
you.chen057aac42023-04-13 14:06:58 +08003541 if (count < 0)
3542 {
3543 RLOGE("list network fail");
3544 return count;
3545 }
3546 else if (count == 0)
3547 {
3548 *list = NULL;
3549 *len = 0;
3550 return 0;
3551 }
3552
you.chen35020192022-05-06 11:30:57 +08003553 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003554 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003555 *len = count;
3556
qs.xiong9fbf74e2023-03-28 13:38:22 +08003557 for (index=0; index < count; index++)
3558 {
you.chen35020192022-05-06 11:30:57 +08003559 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003560 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003561 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003562 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003563 {
you.chen35020192022-05-06 11:30:57 +08003564 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3565 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003566 else
you.chen057aac42023-04-13 14:06:58 +08003567 {
you.chen35020192022-05-06 11:30:57 +08003568 (*list)[index].base_info.band = -1;
3569 }
you.chen057aac42023-04-13 14:06:58 +08003570 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003571 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003572 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003573 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003574 return 0;
3575}
3576
3577int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3578{
qs.xiongc8d92a62023-03-29 17:36:14 +08003579 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003580 const char *lynq_scan_cmd = "SCAN";
3581
3582 CHECK_IDX(idx, CTRL_STA);
3583
3584 CHECK_WPA_CTRL(CTRL_STA);
3585
you.chen0df3e7e2023-05-10 15:56:26 +08003586 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3587 {
3588 RLOGD("tmp clear scanlist");
3589 system(clean_last_re);
3590 }
you.chen9ac66392022-08-06 17:01:16 +08003591 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003592 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003593 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3594 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003595 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003596 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3597 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003598 g_sta_scan_finish_flag = 1;
3599 return -1;
3600 }
you.chen35020192022-05-06 11:30:57 +08003601
3602 return 0;
3603}
3604
3605int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003606 if (cb == NULL)
3607 {
3608 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003609 return -1;
3610 }
3611
you.chen6d247052023-06-01 16:39:54 +08003612 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003613 g_ap_callback_priv = priv;
3614 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003615 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003616
you.chen6d247052023-06-01 16:39:54 +08003617 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003618 if (g_ap_watcher_pid == 0 )
3619 {
3620 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3621 {
3622 g_ap_watcher_pid = 0;
3623 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3624 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3625 return -1;
3626 }
3627 }
3628
3629 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3630 RLOGD("creat APWatcherTheradProc susccs");
3631
you.chen35020192022-05-06 11:30:57 +08003632 return 0;
3633}
3634
3635int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003636 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003637 if (g_ap_callback_priv == priv)
3638 {
you.chen35020192022-05-06 11:30:57 +08003639 g_ap_callback_func = NULL;
3640 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003641 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003642 return 0;
3643 }
you.chen6d247052023-06-01 16:39:54 +08003644 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003645 return -1;
3646}
3647
3648int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003649 if (cb == NULL)
3650 {
3651 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003652 return -1;
3653 }
3654
you.chen6d247052023-06-01 16:39:54 +08003655 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003656 g_sta_callback_priv = priv;
3657 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003658 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003659
you.chen6d247052023-06-01 16:39:54 +08003660 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003661 if (g_sta_watcher_pid == 0 ) {
3662 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3663 {
3664 g_sta_watcher_pid = 0;
3665 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3666 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3667 return -1;
3668 }
3669 }
3670
3671 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3672 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003673 return 0;
3674}
3675
3676int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003677 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003678 if (g_sta_callback_priv == priv)
3679 {
you.chen35020192022-05-06 11:30:57 +08003680 g_sta_callback_func = NULL;
3681 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003682 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003683 return 0;
3684 }
you.chen6d247052023-06-01 16:39:54 +08003685 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003686 return -1;
3687}
3688
qs.xiongfcc914b2023-07-06 21:16:20 +08003689int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3690 if (cb == NULL)
3691 {
3692 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3693 return -1;
3694 }
3695 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3696 g_sta_auto_callback_priv = priv;
3697 g_sta_auto_callback_func = cb;
3698 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3699 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3700 if (g_sta_auto_watcher_pid == 0 ) {
3701 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3702 {
3703 g_sta_auto_watcher_pid = 0;
3704 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3705 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3706 return -1;
3707 }
3708 }
3709 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3710 RLOGD("creat STAWatcherTheradProc susccs");
3711 return 0;
3712}
3713int lynq_unreg_sta_auto_event_callback(void * priv) {
3714 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3715 if (g_sta_auto_callback_priv == priv)
3716 {
3717 g_sta_auto_watcher_stop_flag = 1;
3718 if (g_sta_auto_watcher_pid != 0)
3719 {
3720 pthread_join(g_sta_auto_watcher_pid, NULL);
3721 }
3722 g_sta_auto_watcher_pid = 0;
3723 g_sta_auto_callback_func = NULL;
3724 g_sta_auto_callback_priv = NULL;
3725 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3726 return 0;
3727 }
3728 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3729 return -1;
3730}
you.chen35020192022-05-06 11:30:57 +08003731int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3732{
3733 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003734 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003735 CHECK_IDX(idx, CTRL_AP);
3736
qs.xiong9fbf74e2023-03-28 13:38:22 +08003737 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3738 {
you.chen35020192022-05-06 11:30:57 +08003739 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3740 return 0;
3741 }
3742
qs.xiong9fbf74e2023-03-28 13:38:22 +08003743 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3744 {
you.chen35020192022-05-06 11:30:57 +08003745 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3746 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003747 else
3748 {
you.chen35020192022-05-06 11:30:57 +08003749 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3750 }
3751
3752 return 0;
3753}
3754
3755int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3756 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003757 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08003758 CHECK_IDX(idx, CTRL_STA);
3759
qs.xiong9fbf74e2023-03-28 13:38:22 +08003760 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3761 {
you.chen35020192022-05-06 11:30:57 +08003762 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3763 return 0;
3764 }
3765
qs.xiong9fbf74e2023-03-28 13:38:22 +08003766 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3767 {
you.chen35020192022-05-06 11:30:57 +08003768 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3769 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003770 else
3771 {
you.chen35020192022-05-06 11:30:57 +08003772 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3773 }
3774
3775 return 0;
3776}
3777
3778int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3779// CHECK_IDX(idx, CTRL_AP);
3780// int ret = 0;
3781// size_t reply_len = MAX_RET;
3782// char cmd_reply[MAX_RET]={0};
3783// const char * cmd_str = "GET country";
3784// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3785// do{
3786// if (NULL == s_lynq_wpa_ctrl) {
3787// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3788// if (NULL == s_lynq_wpa_ctrl ) {
3789// printf("wpa_ctrl_open fail\n");
3790// return -1;
3791// }
3792// }
3793// }while(0);
3794
3795// do {
3796// reply_len = MAX_RET;
3797// cmd_reply[0] = '\0';
3798// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08003799// 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 +08003800// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003801// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08003802// return ret;
3803// }
3804// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08003805// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08003806// }while(0);
3807
3808 FILE *fp;
3809 size_t i = 0;
3810 char lynq_cmd_ret[MAX_RET]={0};
3811
3812// CHECK_IDX(idx, CTRL_AP);
3813
3814 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003815 {
3816 perror("popen error!");
3817 return -1;
3818 }
you.chen35020192022-05-06 11:30:57 +08003819 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3820 {
3821 perror("fread fail!");
3822 return -1;
3823 }
3824
qs.xiong9fbf74e2023-03-28 13:38:22 +08003825 for(i=0; i < strlen(lynq_cmd_ret); i++)
3826 {
3827 if (lynq_cmd_ret[i] == ' ')
3828 {
you.chen35020192022-05-06 11:30:57 +08003829 lynq_cmd_ret[i] = '\0';
3830 break;
3831 }
3832 }
3833
3834 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003835 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08003836
3837 int ret=pclose(fp);
3838 if(ret==-1)
3839 {
3840 perror("close file faild");
3841 }
3842
3843 return 0;
3844}
3845
you.chen705a7ef2023-06-01 22:06:45 +08003846static int check_and_init_uci_config(char * country_code)
3847{
3848 FILE * fp;
3849 int is_different = 0;
3850 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
3851 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
3852 const char * commit_uci_cmd ="uci commit";
3853 char set_country_cmd[MAX_CMD];
3854 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08003855
you.chen705a7ef2023-06-01 22:06:45 +08003856 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08003857
you.chen705a7ef2023-06-01 22:06:45 +08003858 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08003859 {
you.chen705a7ef2023-06-01 22:06:45 +08003860 if (0 != system(create_uci_cmd))
3861 {
3862 RLOGE("creat_uci_cmd fail");
3863 return -1;
3864 }
3865 is_different = 1;
3866 }
3867
3868 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
3869 {
3870 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08003871 return -1;
3872 }
3873
you.chen705a7ef2023-06-01 22:06:45 +08003874 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08003875 {
you.chen705a7ef2023-06-01 22:06:45 +08003876 RLOGE("fread fail!");
3877 fclose(fp);
3878 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08003879 }
3880
you.chen705a7ef2023-06-01 22:06:45 +08003881 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
3882 {
3883 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
3884 is_different = 1;
3885 }
3886
3887 fclose(fp);
3888
3889 if (is_different)
3890 {
3891 if ( 0 != system(set_country_cmd))
3892 {
3893 RLOGE("set_country_cmd fail");
3894 return -1;
3895 }
3896 if (0 != system(commit_uci_cmd))
3897 {
3898 RLOGE("commmit fail");
3899 }
3900 }
3901
3902 return is_different;
3903}
3904
3905int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3906 char check_current_code[10];
3907 const char * support_country[] = {"CN", "EU"};
3908
3909 int ret,is_different, i, cc_count;
3910
3911 if (country_code == NULL || country_code[0] == '\0')
3912 {
3913 RLOGE("bad country code\n");
3914 return -1;
3915 }
3916
3917 cc_count = sizeof (support_country) / sizeof (char*);
3918 for(i=0; i < cc_count; i++)
3919 {
3920 if (strcmp(support_country[i], country_code) == 0)
3921 {
3922 break;
3923 }
3924 }
3925
3926 if (i >= cc_count)
3927 {
3928 RLOGE("unspported country code %s\n", country_code);
3929 return -1;
3930 }
3931
3932 is_different = check_and_init_uci_config(country_code);
3933 if( is_different < 0 )
3934 {
3935 RLOGE("init set uci fail\n");
3936 return -1;
3937 }
3938
3939 ret = lynq_get_country_code(idx,check_current_code);
3940 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
3941 {
3942 ret = lynq_wifi_disable();
3943 if(ret != 0 )
3944 {
3945 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
3946 return -1;
3947 }
3948 }
3949
3950 return 0;
you.chen35020192022-05-06 11:30:57 +08003951}
3952
3953int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
3954{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003955 RLOGD("enter lynq_get_connect_ap_mac\n");
3956 if (mac == NULL)
3957 {
3958 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08003959 return -1;
3960 }
3961
3962 CHECK_IDX(idx, CTRL_STA);
3963 ap_info_s ap;
3964 ap.ap_mac[0] = '\0';
3965
qs.xiong9fbf74e2023-03-28 13:38:22 +08003966 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3967 {
you.chen35020192022-05-06 11:30:57 +08003968 return -1;
3969 }
3970 strcpy(mac, ap.ap_mac);
3971
3972 return 0;
3973}
3974
3975int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
3976{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003977 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08003978 struct ifaddrs *ifaddr_header, *ifaddr;
3979 struct in_addr * ifa;
3980 const char * ifaName = "wlan0";
3981 if (ip == NULL)
3982 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003983 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08003984 return -1;
you.chen9ac66392022-08-06 17:01:16 +08003985 }
you.chenf58b3c92022-06-21 16:53:48 +08003986
qs.xiong9fbf74e2023-03-28 13:38:22 +08003987 if (idx == 1)
3988 {
you.chen0df3e7e2023-05-10 15:56:26 +08003989 ifaName = inner_get_ap_interface_name();
3990 if (ifaName == NULL)
3991 {
3992 RLOGE("[lynq_get_interface_ip] ap name get fail");
3993 return -1;
3994 }
you.chen9ac66392022-08-06 17:01:16 +08003995 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003996 else if (idx != 0)
3997 {
you.chen35020192022-05-06 11:30:57 +08003998 return -1;
you.chen9ac66392022-08-06 17:01:16 +08003999 }
you.chen35020192022-05-06 11:30:57 +08004000
you.chen9ac66392022-08-06 17:01:16 +08004001 if (getifaddrs(&ifaddr_header) == -1)
4002 {
you.chen35020192022-05-06 11:30:57 +08004003 perror("getifaddrs");
4004 return -1;
4005 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004006 }
you.chen35020192022-05-06 11:30:57 +08004007
4008
you.chen9ac66392022-08-06 17:01:16 +08004009 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4010 {
4011 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004012 continue;
you.chen9ac66392022-08-06 17:01:16 +08004013 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4014 {
4015 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4016 {
4017 // is a valid IP4 Address
4018 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4019 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004020 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004021 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004022 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004023 return 0;
4024 }
4025 }
4026 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004027 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004028 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004029 return -1;
you.chen35020192022-05-06 11:30:57 +08004030}
4031
4032int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4033{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004034 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004035 int count;
4036 size_t i;
4037 char *split_words[128] = {0};
4038 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4039
4040 CHECK_WPA_CTRL(idx);
4041
4042 DO_REQUEST(lynq_get_mac_cmd);
4043
qs.xiong9fbf74e2023-03-28 13:38:22 +08004044 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4045 {
4046 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004047 return -1;
4048 }
4049
4050 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4051
qs.xiong9fbf74e2023-03-28 13:38:22 +08004052 if (count < 2)
4053 {
you.chen35020192022-05-06 11:30:57 +08004054 return -1;
4055 }
4056
qs.xiong9fbf74e2023-03-28 13:38:22 +08004057 for (i=0; i < strlen(split_words[1]); i++ )
4058 {
4059 if (split_words[1][i] != ' ')
4060 {
you.chen35020192022-05-06 11:30:57 +08004061 break;
4062 }
4063 }
4064
4065 strcpy(mac, split_words[1] + i);
4066
4067 return 0;
4068}
4069
4070int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4071{
4072// int count;
4073// char *split_words[128] = {0};
4074// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4075
4076// if (rssi == NULL) {
4077// return -1;
4078// }
4079
4080// CHECK_IDX(idx, CTRL_STA);
4081
4082// CHECK_WPA_CTRL(CTRL_STA);
4083
4084// DO_REQUEST(lynq_get_rssi_cmd);
4085
4086// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4087// return -1;
4088// }
4089
4090// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4091
4092// if (count < 2) {
4093// return -1;
4094// }
4095
4096// *rssi = atoi(split_words[1]) * -1;
4097
you.chen35020192022-05-06 11:30:57 +08004098 char lynq_cmd_ret[MAX_RET]={0};
4099
qs.xiongff0ae0f2022-10-11 15:47:14 +08004100/*******change other cmd to get rssi*******
4101 *
4102 *wl rssi ---> wl -i wlan0 rssi
4103 *
4104 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004105 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004106 {
you.chen23c4a5f2023-04-12 16:46:00 +08004107 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004108 return -1;
4109 }
you.chen9f17e4d2022-06-06 17:18:18 +08004110 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004111/****** if got rssi is 0,means sta didn't connected any device****/
4112 if(*rssi == 0)
4113 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004114 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004115 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004116 }
you.chen35020192022-05-06 11:30:57 +08004117
4118 return 0;
4119}
4120
4121int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4122{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004123 RLOGD("enter lynq_get_connect_ap_band\n");
4124 if (band == NULL)
4125 {
you.chen35020192022-05-06 11:30:57 +08004126 return -1;
4127 }
4128
4129 CHECK_IDX(idx, CTRL_STA);
4130 ap_info_s ap;
4131 ap.band = -1;
4132
qs.xiong9fbf74e2023-03-28 13:38:22 +08004133 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4134 {
you.chen35020192022-05-06 11:30:57 +08004135 return -1;
4136 }
4137 *band = ap.band;
4138
4139 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004140}
you.chenf58b3c92022-06-21 16:53:48 +08004141
4142int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4143{
you.chenb95401e2023-05-12 19:39:06 +08004144 int ret;
4145 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004146 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004147
4148 if (ip == NULL)
4149 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004150 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004151 return -1;
4152 }
4153
4154 CHECK_IDX(idx, CTRL_STA);
4155
qs.xionge4cbf1c2023-02-28 18:22:49 +08004156 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004157 {
4158 return -1;
4159 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004160
you.chenb95401e2023-05-12 19:39:06 +08004161 ip[0] = '\0';
4162 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4163 if (ret != 0)
4164 {
4165 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4166 return -1;
4167 }
4168
4169 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4170 {
4171 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004172 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004173 if (ret != 0)
4174 {
4175 ip[0] = '\0';
4176 return 0;
4177 }
4178 else
4179 {
4180 p = strchr(ip, '\n');
4181 if (p != NULL)
4182 {
4183 *p = '\0';
4184 }
4185 }
4186 }
4187 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004188}
4189
qs.xiong026c5c72022-10-17 11:15:45 +08004190int lynq_ap_connect_num(int sta_number)
4191{
4192 char lynq_limit_cmd[32]={0};
4193 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004194 if((sta_number < 1 ) && (sta_number > 15))
4195 {
4196 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004197 return -1;
4198 }
4199 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4200 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004201 if(ret != 0)
4202 {
4203 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004204 }
4205 return 0;
4206}
you.chenf58b3c92022-06-21 16:53:48 +08004207
qs.xiong77905552022-10-17 11:19:57 +08004208int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4209{
4210
4211 char lynq_wifi_acs_cmd[128]={0};
4212 char lynq_cmd_mode[128]={0};
4213 char lynq_cmd_slect[128]={0};
4214
qs.xiong9fbf74e2023-03-28 13:38:22 +08004215 if((acs_mode != 2) && (acs_mode != 5))
4216 {
qs.xiong77905552022-10-17 11:19:57 +08004217 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4218 }
4219
qs.xiong9fbf74e2023-03-28 13:38:22 +08004220 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4221 {
qs.xiong77905552022-10-17 11:19:57 +08004222 return -1;
4223 }
4224
4225 CHECK_IDX(idx, CTRL_AP);
4226
4227 CHECK_WPA_CTRL(CTRL_AP);
4228
4229 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4230 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4231 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4232
4233 DO_OK_FAIL_REQUEST(cmd_disconnect);
4234 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4235 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4236 DO_OK_FAIL_REQUEST(cmd_save_config);
4237 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4238
4239 return 0;
4240}
you.chen0f5c6432022-11-07 18:31:14 +08004241//you.chen add for tv-box start
4242static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4243 FILE *fp;
4244 //printf("to exec cmd:%s\n", str_cmd);
4245 if((fp=popen(str_cmd,"r"))==NULL)
4246 {
4247 perror("popen error!");
4248 return -1;
4249 }
4250 if((fread(str_cmd_ret,max_len,1,fp))<0)
4251 {
4252 perror("fread fail!");
4253 fclose(fp);
4254 return -1;
4255 }
4256 fclose(fp);
4257 return 0;
4258}
4259
4260static int get_netmask_length(const char* mask)
4261{
4262 int masklen=0, i=0;
4263 int netmask=0;
4264
4265 if(mask == NULL)
4266 {
4267 return 0;
4268 }
4269
4270 struct in_addr ip_addr;
4271 if( inet_aton(mask, &ip_addr) )
4272 {
4273 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004274 }else
4275 {
you.chen0f5c6432022-11-07 18:31:14 +08004276 netmask = 0;
4277 return 0;
4278 }
4279
4280 while(0 == (netmask & 0x01) && i<32)
4281 {
4282 i++;
4283 netmask = netmask>>1;
4284 }
4285 masklen = 32-i;
4286 return masklen;
4287}
4288
4289static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4290 int mask_len;
4291 char *p;
4292 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004293 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4294 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004295 return -1;
4296 p = strstr(str_cmd_ret, "Mask:");
4297 if (p == NULL)
4298 return -1;
4299 mask_len = get_netmask_length(p + 5);
4300 if (mask_len == 0)
4301 return -1;
4302 p = strstr(str_cmd_ret, "inet addr:");
4303 if (p == NULL)
4304 return -1;
4305 strcpy(tmp, p + 10);
4306 p = strstr(tmp, " ");
4307 if (p != NULL)
4308 *p = '\0';
4309 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4310 return 0;
4311}
4312
4313static void GBWWatchThreadProc() {
4314 int i,n, nloop, nmax, ncheckcount, nidlecount;
4315 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4316 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4317 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4318 char *results[16] = {0};
4319 char str_cmd[256] = {0};
4320 char str_cmd_ret[128] = {0};
4321 char dest_ip[32] = {0};
4322 lastAP1Bytes = lastAP2Bytes = 0;
4323 lastAP1Drop = lastAP2Drop = 0;
4324 lastAP1Speed = lastAP2Speed = 0;
4325 setAP1Speed = 50;
4326 setAP2Speed = 80;
4327 nloop = 0;
4328 nmax = 6;
4329 ncheckcount = nidlecount = 0;
4330
you.chen0df3e7e2023-05-10 15:56:26 +08004331 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004332 {
4333 RLOGE("------gbw thread run\n");
4334 return;
4335 }
4336
qs.xiong9fbf74e2023-03-28 13:38:22 +08004337 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004338 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4339 while (dest_ip[0] == '\0') {
4340 sleep(1);
4341 str_cmd_ret[0] = '\0';
4342 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4343 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4344 if (str_cmd_ret[n] == '\n'){
4345 str_cmd_ret[n] = '\0';
4346 break;
4347 }
4348 }
4349 if (str_cmd_ret[0] != '\0')
4350 {
4351 strcpy(dest_ip, str_cmd_ret);
4352 }
4353 }
4354
you.chenc9928582023-04-24 15:39:37 +08004355 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4356 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4357 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 +08004358 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4359 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004360 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004361 return;
4362 }
you.chenc9928582023-04-24 15:39:37 +08004363 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);
4364 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);
4365 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 +08004366
4367 while (1) {
4368 sleep(1);
4369 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004370 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4371 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4372 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004373 continue;
4374 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004375 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004376 if (n > 9) {
4377 if (strcmp(results[1], "Sent") == 0) {
4378 currAP1Bytes = atoll(results[2]);
4379 }
4380 if (strcmp(results[6], "(dropped") == 0) {
4381 currAP1Drop = atoi(results[7]);
4382 }
4383 }
4384
4385 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004386 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4387 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4388 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004389 continue;
4390 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004391 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004392 if (n > 9) {
4393 if (strcmp(results[1], "Sent") == 0) {
4394 currAP2Bytes = atoll(results[2]);
4395 }
4396 if (strcmp(results[6], "(dropped") == 0) {
4397 currAP2Drop = atoi(results[7]);
4398 }
4399 }
4400
4401 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4402 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4403 lastAP1Bytes = currAP1Bytes;
4404 lastAP2Bytes = currAP2Bytes;
4405 continue;
4406 }
4407
4408 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4409 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4410 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4411 lastAP1Speed = currAP1Speed;
4412 lastAP2Speed = currAP2Speed;
4413 lastAP1Bytes = currAP1Bytes;
4414 lastAP2Bytes = currAP2Bytes;
4415
4416 currSetAP1Speed = setAP1Speed;
4417 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4418 ncheckcount++;
4419 if (ncheckcount > 3) {
4420 ncheckcount = 0;
4421 currSetAP1Speed = 5;
4422 }
4423 }
4424 else {
4425 ncheckcount = 0;
4426 if (currAP1Speed < 5)
4427 nidlecount++;
4428 else
4429 nidlecount = 0;
4430
4431 }
4432
4433 if (nidlecount > 60 ){
4434 currSetAP1Speed = 50;
4435 }
4436
4437 if (currSetAP1Speed != setAP1Speed) {
4438 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004439 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4440 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004441 }
4442 }
4443}
4444
4445int enableGBW(const char* mac) {
4446 int i,len;
4447 char get_ipaddr_cmd[128]={0};
4448 ap_info_s *ap;
4449 device_info_s * list;
4450
4451 if (mac == NULL || g_gbw_enabled == 1)
4452 return -1;
4453 len = strlen(mac);
4454 g_gbw_mac = malloc(len + 1);
4455 for(i=0;i<len;i++) {
4456 if (mac[i] >= 'A' && mac[i] <= 'Z')
4457 {
4458 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4459 }
4460 else
4461 g_gbw_mac[i] = mac[i];
4462 }
4463 g_gbw_mac[i] = '\0';
4464 g_gbw_enabled = 1;
4465
4466 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4467 if (system(get_ipaddr_cmd) == 0) {
4468 //startGBW();
4469 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4470 for (i=0;i<len;i++) {
4471 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4472 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4473 startGBW();
4474 }
4475 free(ap);
4476 free(list);
4477 }
4478 }
4479 return 0;
4480}
4481
4482int disableGBW() {
4483 stopGBW();
4484 free(g_gbw_mac);
4485 g_gbw_mac = NULL;
4486 g_gbw_enabled = 1;
4487 return 0;
4488}
4489
4490static int startGBW() {
4491 if (g_gbw_watcher_pid != 0) {
4492 stopGBW();
4493 }
4494 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4495}
4496
4497static int stopGBW() {
4498 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004499 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004500 pthread_cancel(g_gbw_watcher_pid);
4501 pthread_join(g_gbw_watcher_pid, &retval);
4502 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004503 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4504 if (s_ap_iterface_name[0] != '\0')
4505 {
4506 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4507 system(cmd);
4508 }
you.chen0f5c6432022-11-07 18:31:14 +08004509}
4510//you.chen add for tv-box end