blob: 369c6b625e128a5b91ebfdddb1218ba3095db573 [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.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen35020192022-05-06 11:30:57 +080062
qs.xiongfdbc58e2022-09-29 11:37:32 +080063static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +080064
you.chen0f5c6432022-11-07 18:31:14 +080065//you.chen add for tv-box start
66volatile int g_gbw_enabled = 0;
67char * g_gbw_mac = NULL;
68pthread_t g_gbw_watcher_pid = 0;
69static int startGBW();
70static int stopGBW();
71//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +080072
73typedef struct __curr_status_info {
74 ap_info_s *ap;
75 char * state;
76 int net_no;
77}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040078
79#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040080{\
you.chen35020192022-05-06 11:30:57 +080081 perror((str));\
82 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040083}
84
you.chen35020192022-05-06 11:30:57 +080085#define CHECK_IDX(idx, type) do { \
86 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
87 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
88 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
89 return -1; \
90 } \
91 }while (0)
92
93#define CHECK_WPA_CTRL(index) int ret = 0;\
94 size_t reply_len = MAX_RET; \
95 char cmd_reply[MAX_RET]={0}; \
qs.xiongfdbc58e2022-09-29 11:37:32 +080096 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +080097 do{ \
qs.xiongfdbc58e2022-09-29 11:37:32 +080098 if (NULL == g_lynq_wpa_ctrl[index]) { \
99 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
100 if (NULL == g_lynq_wpa_ctrl[index]) { \
101 printf("wpa_ctrl_open fail\n"); \
102 return -1; \
103 } \
104 } \
105 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
you.chen35020192022-05-06 11:30:57 +0800106 }while(0)
107
108#define DO_REQUEST(cmd_str) do { \
109 reply_len = MAX_RET;\
110 cmd_reply[0] = '\0'; \
qs.xiongfdbc58e2022-09-29 11:37:32 +0800111 printf("to call [%s]\n", cmd_str); \
112 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800113 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800114 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800115 return ret; \
116 } \
117 cmd_reply[reply_len+1] = '\0'; \
118 printf("cmd replay [ %s ]\n", cmd_reply); \
119 }while(0)
120
121#define DO_OK_FAIL_REQUEST(cmd_str) do { \
122 DO_REQUEST(cmd_str); \
123 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800124 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800125 return -1; \
126 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800127 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800128 return -1; \
129 } \
130 }while (0)
131
132
133
134static void APWatcherThreadProc() {
135 size_t len = MAX_RET;
136 char msg_notify[MAX_RET];
137
you.chen6c2dd9c2022-05-16 17:55:28 +0800138 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800139 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800140
141 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800142 if (lynq_wpa_ctrl == NULL) {
143 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
144 if (lynq_wpa_ctrl == NULL) {
145 usleep(100*1000);
146 continue;
147 }
148
149 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800150 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800151 }
152
you.chen35020192022-05-06 11:30:57 +0800153 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
154 usleep(100*1000);
155 continue;
156 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800157 memset(msg_notify, 0, MAX_RET);
158 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800159 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
160 msg_notify[len+1] = '\0';
161 printf("ap------> %s\n", msg_notify);
you.chen0f5c6432022-11-07 18:31:14 +0800162//you.chen change for tv-box start
you.chen35020192022-05-06 11:30:57 +0800163 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800164 if (g_ap_callback_func != NULL)
165 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
166 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
167 printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
168 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
169 stopGBW();
170 }
171 }
you.chen35020192022-05-06 11:30:57 +0800172 }
173 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800174 if (g_ap_callback_func != NULL)
175 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
176 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
177 printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
178 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
179 startGBW();
180 }
181 }
you.chen35020192022-05-06 11:30:57 +0800182 }
you.chen0f5c6432022-11-07 18:31:14 +0800183//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800184 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
185 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800186 if (lynq_wpa_ctrl != NULL) {
187 wpa_ctrl_detach(lynq_wpa_ctrl);
188 wpa_ctrl_close(lynq_wpa_ctrl);
189 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400190}
191
you.chen35020192022-05-06 11:30:57 +0800192static void STAWatcherThreadProc() {
193 size_t len = MAX_RET;
194 char msg_notify[MAX_RET];
195 char *pReason;
196 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400197
you.chen6c2dd9c2022-05-16 17:55:28 +0800198 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800199 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800200
201 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800202 if (lynq_wpa_ctrl == NULL) {
203 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
204 if (lynq_wpa_ctrl == NULL) {
205 usleep(100*1000);
206 continue;
207 }
208
209 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800210 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800211 }
212
you.chen35020192022-05-06 11:30:57 +0800213 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
214 usleep(100*1000);
215 continue;
216 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800217 memset(msg_notify, 0, MAX_RET);
218 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800219 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
220 msg_notify[len+1] = '\0';
221 printf("sta ------> %s\n", msg_notify);
222 if (strstr(msg_notify, state_scan_result) != NULL) {
223 g_sta_scan_finish_flag = 1;
224 }
225
226 if (g_sta_callback_func == NULL) {
227 continue;
228 }
229 error = -1;
230 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
231 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
232 }
233 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
234 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
235 }
236 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
237 pReason = strstr(msg_notify, "reason=");
238 if (pReason != NULL) {
239 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800240 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800241 error = LYNQ_TIME_OUT;
242 }
you.chen92fd5d32022-05-25 10:09:47 +0800243 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800244 error = LYNQ_PSW_ERROR;
245 }
246 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
247 }
248 }
249 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
250 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
251 }
252 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
253 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
254 }
255 }
256 }
you.chen92fd5d32022-05-25 10:09:47 +0800257 if (lynq_wpa_ctrl != NULL) {
258 wpa_ctrl_detach(lynq_wpa_ctrl);
259 wpa_ctrl_close(lynq_wpa_ctrl);
260 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400261}
262
qs.xiong1af5daf2022-03-14 09:12:12 -0400263int lynq_wifi_enable(void)
264{
you.chen35020192022-05-06 11:30:57 +0800265 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800266 int i;
you.chen35020192022-05-06 11:30:57 +0800267 const char * cmd_check_service =
268 "state=`systemctl is-active wg870_drv_insmod.service`\n"
269 "[ \"\"$state == \"active\" ] && exit 0\n"
270 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
271// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
272// return 0;
273// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400274
you.chen35020192022-05-06 11:30:57 +0800275 ret = system(cmd_check_service);
276 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800277 printf("service state %d\n", ret);
qs.xiongfdbc58e2022-09-29 11:37:32 +0800278 return -1;
you.chen35020192022-05-06 11:30:57 +0800279 }
lhfe8da902022-10-11 18:55:36 +0800280
you.chen6c2dd9c2022-05-16 17:55:28 +0800281 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800282 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800283 break;
284 }
285 usleep(300*1000);
286 }
287
288 if (i >= 10) {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800289 return -1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800290 }
291
you.chen9f17e4d2022-06-06 17:18:18 +0800292 //@todo delete add temp check for socket avilable start (20220606)
293 for (i=0; i<60; i++)
294 {
295 if (system("netstat -an | grep -q DGRAM") == 0) {
296 break;
297 }
298 sleep(1);
299 }
300
301 if (i >= 60)
302 {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800303 return -1;
you.chen9f17e4d2022-06-06 17:18:18 +0800304 }
305 //@todo delete add temp check for socket avilable end (20220606)
306
you.chena6fa5b22022-05-18 10:28:19 +0800307 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800308 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800309 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800310 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800311 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800312 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800313 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800314 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800315 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800316 }
317
you.chen35020192022-05-06 11:30:57 +0800318 if (g_ap_watcher_pid == 0 ) {
319 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
320 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800321 return -1;
you.chen35020192022-05-06 11:30:57 +0800322 }
323 }
324
325 if (g_sta_watcher_pid == 0 ) {
326 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
327 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800328 return -1;
you.chen35020192022-05-06 11:30:57 +0800329 }
330 }
331
you.chena6fa5b22022-05-18 10:28:19 +0800332 for (i=0; i<10; i++) {
333 usleep(300*1000);
334 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
335 break;
336 }
337 }
338
you.chen35020192022-05-06 11:30:57 +0800339 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500340}
341
qs.xiong1af5daf2022-03-14 09:12:12 -0400342int lynq_wifi_disable(void)
343{
you.chen35020192022-05-06 11:30:57 +0800344 g_ap_watcher_stop_flag = 1;
345 g_sta_watcher_stop_flag = 1;
346 if (g_ap_watcher_pid != 0)
347 pthread_join(g_ap_watcher_pid, NULL);
348 if (g_sta_watcher_pid != 0)
349 pthread_join(g_sta_watcher_pid, NULL);
350 if (g_lynq_wpa_ctrl[0] != NULL)
351 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
352 if (g_lynq_wpa_ctrl[1] != NULL)
353 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
354 g_ap_watcher_pid = 0;
355 g_sta_watcher_pid = 0;
356 g_lynq_wpa_ctrl[0] = NULL;
357 g_lynq_wpa_ctrl[1] = NULL;
358 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500359 return 0;
360}
qs.xiong1af5daf2022-03-14 09:12:12 -0400361
you.chen35020192022-05-06 11:30:57 +0800362static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
363
364 char lynq_cmd_get[128]={0};
365
366 if (out_put == NULL) {
367 printf("output ptr is null\n");
368 return -1;
369 }
370 if (param_name == NULL) {
371 printf("param ptr is null");
372 return -1;
373 }
374 if (param_name[0] == '\0') {
375 printf("param is empty");
376 return -1;
377 }
378
379 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
380
381 CHECK_WPA_CTRL(interface);
382
383 DO_REQUEST(lynq_cmd_get);
384
385 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
386 return -1;
387 }
388
you.chena6fa5b22022-05-18 10:28:19 +0800389// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800390 memcpy(out_put, cmd_reply, reply_len + 1);
391 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500392}
qs.xiong1af5daf2022-03-14 09:12:12 -0400393
you.chen35020192022-05-06 11:30:57 +0800394static int lynq_split(char * str, int len, char delimiter, char * results[]) {
395 int ret = 0;
396 char * end = str + len - 1;
397 results[ret++] = str;
398 while(str < end) {
399 if (*str == delimiter) {
400 *str++ = '\0';
401 results[ret++] = str;
402 continue;
403 }
404 str++;
405 }
406 if (*str == delimiter) {
407 *str = '\0';
408 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400409
you.chen35020192022-05-06 11:30:57 +0800410 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500411}
412
you.chen35020192022-05-06 11:30:57 +0800413static void trim_space(char * p, int count) {
414 char * begin = p;
415 p += count;
416 printf("%C-%C||\n", *begin, *p);
417 while (p >= begin ) {
418 if (*p == ' ') {
419 *p-- = '\0';
420 }
421 else {
422 break;
423 }
424 }
425}
426
427static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
428 FILE * fp;
429 int len, ret;
430 int count, count_words, index;
431 int mac_start, mac_end;
432 int ip_start, ip_end;
433 char *split_lines[128] = {0};
434 char *buff;
435 const char * ip_header = "IP address";
436 const char * mac_header = "HW address";
437 const char * zero_mac = "00:00:00:00:00:00";
438
439 fp = fopen("/proc/net/arp", "rb");
440 if (NULL == fp) {
441 printf("open file fail\n");
442 return -1;
443 }
444
445 buff = alloca(MAX_RET);
446 fseek(fp, 0, SEEK_SET);
447 len = fread(buff, 1, MAX_RET, fp);
448 fclose(fp);
449 if (len <= 0) {
450 printf("read file fail\n");
451 return -1;
452 }
453 printf("file : %s\n", buff);
454
455 count = lynq_split(buff, len, '\n', split_lines);
456 printf("----- %s\n", split_lines[0]);
457
458 mac_end = 0;
459 count_words = strlen(split_lines[0]);
460 if (strstr(split_lines[0], mac_header) != NULL) {
461 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
462 mac_end = mac_start + strlen(mac_header) + 1;
463 while (mac_end < count_words) {
464 if (split_lines[0][mac_end] != ' ') {
465 break;
466 }
467 mac_end++;
468 }
469 }
470
471 ip_end = 0;
472 if (strstr(split_lines[0], ip_header) != NULL) {
473 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
474 ip_end = ip_start + strlen(ip_header) + 1;
475 while (ip_end < count_words) {
476 if (split_lines[0][ip_end] != ' ') {
477 break;
478 }
479 ip_end++;
480 }
481 }
482
483 if (mac_end == 0 || ip_end == 0) {
484 return 0;
485 }
486
487 ret = 0;
488 for(index = 1;index < count; index++) {
489 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
490 continue;
491 }
492 mac_list[ret] = malloc(mac_end - mac_start + 1);
493 ip_list[ret] = malloc(ip_end - ip_start + 1);
494 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
495 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
496 trim_space(mac_list[ret], mac_end - mac_start - 1);
497 trim_space(ip_list[ret], ip_end - ip_start - 1);
498 ret++;
499 }
500
501 return ret;
502}
503
you.chen9ac66392022-08-06 17:01:16 +0800504static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
505{
506 int i;
507 if (mac_list != NULL && mac_cnt > 0) {
508 for(i = 0; i< mac_cnt; i++)
509 {
510 if (NULL != mac_list[i])
511 {
512 free(mac_list[i]);
513 mac_list[i] = NULL;
514 }
515 }
516 }
517 if (ip_list != NULL && ip_cnt > 0) {
518 for(i = 0; i< mac_cnt; i++)
519 {
520 if (NULL != ip_list[i])
521 {
522 free(ip_list[i]);
523 ip_list[i] = NULL;
524 }
525 }
526 }
527}
528
you.chen35020192022-05-06 11:30:57 +0800529static int get_hostname_by_ip(char *ip, char *hostname) {
530 struct in_addr addr ={0};
531 struct hostent *ht;
532
533 if (ip == NULL || *ip == '\0' || hostname == NULL) {
534 return -1;
535 }
536
537 if (inet_aton(ip, &addr) == 0) {
538 printf("---inet_aton fail\n");
539 return -1;
540 }
541
542 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
543
544 if (ht == NULL) {
545 printf("---gethostbyaddr fail\n");
546 herror(NULL);
547 return -1;
548 }
549
550 strcpy(hostname, ht->h_name);
551
552 return 0;
553}
554
555static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
556{
557 int count, index, words_count;
558 char * split_lines[128]= {0};
559 char * split_words[128] = {0};
560 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
561
562 CHECK_WPA_CTRL(ap_sta);
563
564 DO_REQUEST(lynq_wifi_list_networks);
565
566 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
567
568 //@todo check ssid field to compatible
569
570 ret = 0;
571 for(index=1; index < count; index++) {
572 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
573 if (words_count > 2) {
574 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
575 net_no_list[ret++] = atoi(split_words[0]);
576 }
577 }
578 }
579
580 return ret;
581}
582
583static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800584 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800585 CHECK_WPA_CTRL(ap_sta);
586 const char *lynq_wifi_add_network = "ADD_NETWORK";
587
588 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800589 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800590 return -1;
591 }
592
you.chen6c2dd9c2022-05-16 17:55:28 +0800593 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800594 if(cmd_reply[i] == '\n') {
595 cmd_reply[i] = '\0';
596 break;
597 }
598 }
599 return atoi(cmd_reply);
600}
you.chena6cd55a2022-05-08 12:20:18 +0800601
you.chen35020192022-05-06 11:30:57 +0800602static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
603{
604 int count, index;
605 int net_no_list[128];
606
607
608 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
609 for (index=0; index < count; index++) {
610 if (net_no_list[index] == net_no) {
611 return 0;
612 }
613 }
614
615 if (count >= 1)
616 index = net_no_list[count - 1];
617 else
618 index = -1;
619
620 while (index < net_no ) {
621 index = lynq_add_network(ap_sta);
622 if (index >= net_no) { // required network no created
623 return 0;
624 }
you.chena6cd55a2022-05-08 12:20:18 +0800625 else if( index < 0) {
626 printf("add network fail\n");
627 return -1;
628 }
you.chen35020192022-05-06 11:30:57 +0800629 }
630
631 if (index < 0)
632 return -1;
633
634 return 0;
635}
636
637static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
638 if (freq > 5000 && freq < 6000) {
639 return LYNQ_WIFI_5G_band;
640 }
641 else if (freq > 2000 && freq < 3000) {
642 return LYNQ_WIFI_2G_band;
643 }
644 return LYNQ_WIFI_2_and_5G_band;
645}
646
647static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
648 if (key_mgmt != NULL) {
649 if (memcmp( key_mgmt, "NONE", 4) == 0) {
650 return LYNQ_WIFI_AUTH_OPEN;
651 }
652 else if (memcmp( key_mgmt, "WEP", 3) == 0){
653 return LYNQ_WIFI_AUTH_WEP;
654 }
655 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
656 return LYNQ_WIFI_AUTH_WPA_PSK;
657 }
658 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
659 return LYNQ_WIFI_AUTH_WPA2_PSK;
660 }
661 }
662
663 return -1;
664}
665
666static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
667 if (flag != NULL) {
668 if (strstr( flag, "WPA2-PSK") != NULL){
669 return LYNQ_WIFI_AUTH_WPA2_PSK;
670 }
671 else if (strstr( flag, "WPA-PSK") != NULL){
672 return LYNQ_WIFI_AUTH_WPA_PSK;
673 }
674 else if (strstr( flag, "WEP") != NULL){
675 return LYNQ_WIFI_AUTH_WEP;
676 }
677 else if (strstr( flag, "NONE") != NULL) {
678 return LYNQ_WIFI_AUTH_OPEN;
679 }
680 }
681
682 return -1;
683}
684
685static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
686 switch (bw) {
687 case 10:
688 return LYNQ_WIFI_BANDWIDTH_HT10;
689 break;
690 case 20:
691 return LYNQ_WIFI_BANDWIDTH_HT20;
692 break;
693 case 40:
694 return LYNQ_WIFI_BANDWIDTH_HT40;
695 break;
696 case 80:
697 return LYNQ_WIFI_BANDWIDTH_HT80;
698 break;
699 default:
700 break;
701 }
702
703 return -1;
704}
705
706static int inner_get_status_info(int interface, curr_status_info *curr_state) {
707 int i, count;
708 char *p;
709 const char *lynq_status_cmd = "STATUS";
710 const char * FLAG_SSID = "ssid=";
711 const char * FLAG_SBSID = "bssid=";
712 const char * FLAG_KEY_MGMT = "key_mgmt=";
713 const char * FLAG_FREQ = "freq=";
714 const char * FLAG_STATE = "wpa_state=";
715 const char * FLAG_ID = "id=";
716 char *split_lines[128] = {0};
717
718 CHECK_WPA_CTRL(interface);
719
720 if (curr_state == NULL) {
721 return -1;
722 }
723
724 DO_REQUEST(lynq_status_cmd);
725
726 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
727
728 curr_state->net_no = -1;
729 ret = -1;
730 for(i=0; i < count; i++) {
731 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800732 p = strstr(split_lines[i], FLAG_SBSID);
733 if (p != NULL) {
734 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
735 ret = 0;
736 continue;
737 }
you.chenf58b3c92022-06-21 16:53:48 +0800738 p = strstr(split_lines[i], FLAG_SSID);
739 if (p != NULL) {
740 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
741 ret = 0;
742 continue;
743 }
you.chen35020192022-05-06 11:30:57 +0800744 p = strstr(split_lines[i], FLAG_KEY_MGMT);
745 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800746 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
747 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800748 ret = 0;
749 continue;
750 }
751 p = strstr(split_lines[i], FLAG_FREQ);
752 if (p != NULL) {
753 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
754 ret = 0;
755 continue;
756 }
757 } // end if (ap != NULL)
758 if (curr_state->state != NULL) {
759 p = strstr(split_lines[i], FLAG_STATE);
760 if (p != NULL) {
761 strcpy(curr_state->state, p + strlen(FLAG_STATE));
762 ret = 0;
763 continue;
764 }
765
766 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800767 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800768 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800769 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800770 printf("net_no %d, -- %s\n", curr_state->net_no, p);
771 }
772 }
773
774 return ret;
775}
776
777
qs.xiongf1b525b2022-03-31 00:58:23 -0400778int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500779{
you.chen35020192022-05-06 11:30:57 +0800780 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500781
you.chen35020192022-05-06 11:30:57 +0800782 if (ap_ssid == NULL) {
783 printf("ap_ssid is null\n");
784 return -1;
785 }
786 else {
787 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
788 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400789
you.chen35020192022-05-06 11:30:57 +0800790 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
791 return -1;
792 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400793
you.chen35020192022-05-06 11:30:57 +0800794 CHECK_IDX(idx, CTRL_AP);
795
796 CHECK_WPA_CTRL(CTRL_AP);
797
798 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
799
800 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
801 DO_OK_FAIL_REQUEST(cmd_save_config);
802
qs.xiong7a105ce2022-03-02 09:43:11 -0500803 return 0;
you.chen35020192022-05-06 11:30:57 +0800804
qs.xiong7a105ce2022-03-02 09:43:11 -0500805}
806
you.chen35020192022-05-06 11:30:57 +0800807int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500808{
you.chen6c2dd9c2022-05-16 17:55:28 +0800809 int len;
you.chen35020192022-05-06 11:30:57 +0800810 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800811 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
812 return -1;
813 len = strlen(ap_ssid);
814 if (ap_ssid[0] == '\"') {
815 memmove(ap_ssid, ap_ssid + 1, len - 1);
816 len -= 1;
817 }
818 if (len > 0 && ap_ssid[len-1] == '\"') {
819 ap_ssid[len-1] = '\0';
820 }
821 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500822}
823
qs.xiongc9c79f72022-10-17 15:27:18 +0800824/*****
825 *frequency <------>channel
826 *
827 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
828 *
829 *
830 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
831 *
832 *
833 * */
834static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +0800835 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};
836 int i;
837 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
838
839 for(i = 0; i <= arr_len; i++)
840 {
841 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +0800842 break;
qs.xiongc9c79f72022-10-17 15:27:18 +0800843 }
qs.xiongc00b6032022-11-29 16:28:03 +0800844
845 if(i == arr_len)
846 {
847 printf("input frequency is eero--->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +0800848 return -1;
849 }
qs.xiongc00b6032022-11-29 16:28:03 +0800850
qs.xiongc9c79f72022-10-17 15:27:18 +0800851 return 0;
852}
qs.xiongf1b525b2022-03-31 00:58:23 -0400853int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500854{
qs.xiongc00b6032022-11-29 16:28:03 +0800855 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +0800856 char lynq_wifi_frequency_cmd[128]={0};
857 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800858 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500859
qs.xiongc9c79f72022-10-17 15:27:18 +0800860 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +0800861 check = lynq_check_set_frequency(lynq_wifi_frequency);
862 if(check != 0)
863 {
864 printf("do check frequency error\n");
qs.xiongc9c79f72022-10-17 15:27:18 +0800865 return -1;
you.chen35020192022-05-06 11:30:57 +0800866 }
qs.xiongc00b6032022-11-29 16:28:03 +0800867 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
868 {
you.chen35020192022-05-06 11:30:57 +0800869 return -1;
870 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400871
you.chen35020192022-05-06 11:30:57 +0800872 CHECK_IDX(idx, CTRL_AP);
873
874 CHECK_WPA_CTRL(CTRL_AP);
875
876 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
877 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
878 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
879
you.chen6c2dd9c2022-05-16 17:55:28 +0800880 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800881 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
882 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
883 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500884
qs.xiong1af5daf2022-03-14 09:12:12 -0400885 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500886}
887
qs.xiongf1b525b2022-03-31 00:58:23 -0400888int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500889{
you.chen35020192022-05-06 11:30:57 +0800890 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400891
you.chen35020192022-05-06 11:30:57 +0800892 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400893
you.chen35020192022-05-06 11:30:57 +0800894 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
895 return -1;
896 }
897 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400898
899 return 0;
900}
901
qs.xiongf1b525b2022-03-31 00:58:23 -0400902int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
903{
you.chen35020192022-05-06 11:30:57 +0800904 CHECK_IDX(idx, CTRL_AP);
905 switch(bandwidth){
906 case LYNQ_WIFI_BANDWIDTH_HT10:
907 {
908 printf("bandwith [%d] not support now\n", bandwidth);
909 return -1;
910 }
911 case LYNQ_WIFI_BANDWIDTH_HT20:
912 {
913 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
914 system("wl down");
915 if (system(lynq_cmd_bandwith) != 0 ){
916 return -1;
917 }
918 system("wl up");
919 break;
920 }
921 case LYNQ_WIFI_BANDWIDTH_HT40:
922 {
923 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
924 sprintf(lynq_cmd_bandwith, "wl chanspec ");
925 system("wl down");
926 if (system(lynq_cmd_bandwith) != 0 ){
927 return -1;
928 }
929 system("wl up");
930 break;
931 }
932 case LYNQ_WIFI_BANDWIDTH_HT80:
933 {
934 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
935 system("wl down");
936 if (system(lynq_cmd_bandwith) != 0 ){
937 return -1;
938 }
939 system("wl up");
940 break;
941 }
942 default:
943 {
944 printf("auth type [%d] not support now\n", bandwidth);
945 return -1;
946 }
947 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400948
949
you.chen35020192022-05-06 11:30:57 +0800950 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400951}
you.chen35020192022-05-06 11:30:57 +0800952
qs.xiongf1b525b2022-03-31 00:58:23 -0400953int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
954{
you.chen35020192022-05-06 11:30:57 +0800955 int count = 0;
956 int index = 0;
957 char *split_words[128] = {0};
958 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400959
you.chen35020192022-05-06 11:30:57 +0800960 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500961
you.chen35020192022-05-06 11:30:57 +0800962 CHECK_WPA_CTRL(CTRL_AP);
963
964 DO_REQUEST(lynq_chanspec_cmd);
965
966 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
967 for(;index < count; index++) {
968 if (strncmp(split_words[index], "bw", 2) != 0) {
969 continue;
970 }
971
972 index++;
973 if (index >= count) {
974 return -1;
975 }
976
977 printf("bw %s\n", split_words[index]);
978 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
979 return 0;
980 }
981
982 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500983}
qs.xiong0fb469a2022-04-14 03:50:45 -0400984
qs.xiongf1b525b2022-03-31 00:58:23 -0400985int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500986{
you.chen35020192022-05-06 11:30:57 +0800987 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400988
you.chen35020192022-05-06 11:30:57 +0800989 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400990
you.chen35020192022-05-06 11:30:57 +0800991 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400992
you.chen35020192022-05-06 11:30:57 +0800993 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
994 return -1;
995 }
996
997 system("wl down");
998 if (system(lynq_cmd_channel) != 0 ){
999 return -1;
1000 }
1001 system("wl up");
1002 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001003}
qs.xiong0fb469a2022-04-14 03:50:45 -04001004
qs.xiongf1b525b2022-03-31 00:58:23 -04001005int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001006{
you.chen35020192022-05-06 11:30:57 +08001007 int count = 0;
1008 int index = 0;
1009 char *split_words[128] = {0};
1010 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001011
you.chen35020192022-05-06 11:30:57 +08001012 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001013
you.chen35020192022-05-06 11:30:57 +08001014 CHECK_WPA_CTRL(CTRL_AP);
1015
1016 DO_REQUEST(lynq_chanspec_cmd);
1017
1018 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1019 for(;index < count; index++) {
1020 printf("---- %s\n",split_words[index]);
1021 if (strncmp(split_words[index], "channel", 2) != 0) {
1022 continue;
1023 }
1024
1025 index++;
1026 if (index >= count) {
1027 return -1;
1028 }
1029
1030 *channel = atoi(split_words[index]);
1031 return 0;
1032 }
1033
1034 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001035}
1036
1037
you.chen35020192022-05-06 11:30:57 +08001038int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001039{
you.chen6c2dd9c2022-05-16 17:55:28 +08001040 char ssid[MAX_CMD] = {0};
1041 int freq = 0;
1042 char lynq_auth_cmd[64]={0};
1043 char lynq_auth_alg_cmd[64]={0};
1044 char lynq_psk_cmd[64]={0};
1045 char lynq_pairwise_cmd[64]={0};
1046 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001047 CHECK_IDX(idx, CTRL_AP);
1048
you.chen6c2dd9c2022-05-16 17:55:28 +08001049 CHECK_WPA_CTRL(CTRL_AP);
1050
you.chen35020192022-05-06 11:30:57 +08001051 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1052 return -1;
1053 }
1054
you.chen92fd5d32022-05-25 10:09:47 +08001055 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001056 if (org_auth == auth) {
1057 return 0;
1058 }
1059 else {
1060 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1061 ssid[0] = '\0';
1062 }
1063 lynq_wifi_ap_frequency_get(idx, &freq);
1064
1065 DO_OK_FAIL_REQUEST(cmd_disconnect);
1066 DO_OK_FAIL_REQUEST(cmd_remove_all);
1067 if (ssid[0] != '\0') {
1068 lynq_wifi_ap_ssid_set(idx, ssid);
1069 }
1070 if (freq != 0) {
1071 lynq_wifi_ap_frequency_set(idx, freq);
1072 }
1073 }
1074 }
you.chen35020192022-05-06 11:30:57 +08001075
qs.xiong7a105ce2022-03-02 09:43:11 -05001076 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001077 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001078 {
you.chen35020192022-05-06 11:30:57 +08001079 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001080 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001081
you.chen35020192022-05-06 11:30:57 +08001082 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001083 break;
1084 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001085 case LYNQ_WIFI_AUTH_WEP:
1086 {
1087 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001088 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001089 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1090
1091 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1092 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1093 break;
1094 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001095 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001096 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001097 {
you.chen35020192022-05-06 11:30:57 +08001098 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1099 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1100 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1101 }
1102 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001103 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001104 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001105 }
1106// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1107// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1108 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001109
you.chen35020192022-05-06 11:30:57 +08001110 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1111 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1112 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001113 break;
you.chen35020192022-05-06 11:30:57 +08001114 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001115 default:
you.chen35020192022-05-06 11:30:57 +08001116 {
1117 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001118 return -1;
you.chen35020192022-05-06 11:30:57 +08001119 }
1120 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001121 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001122
qs.xiong7a105ce2022-03-02 09:43:11 -05001123 return 0;
1124}
1125
you.chen35020192022-05-06 11:30:57 +08001126int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001127{
you.chen35020192022-05-06 11:30:57 +08001128 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001129 char lynq_auth_alg_str[MAX_RET] = {0};
1130 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001131
1132 CHECK_IDX(idx, CTRL_AP);
1133
1134 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1135 return -1;
1136 }
1137
1138 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001139 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1140 *auth = LYNQ_WIFI_AUTH_OPEN;
1141 }
1142 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1143 *auth = LYNQ_WIFI_AUTH_WEP;
1144 }
1145 else {
1146 *auth = LYNQ_WIFI_AUTH_OPEN;
1147 }
you.chen35020192022-05-06 11:30:57 +08001148 }
you.chen92fd5d32022-05-25 10:09:47 +08001149 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001150 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001151 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001152 }
1153 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1154 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1155 }
1156 else {
1157 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1158 }
you.chen35020192022-05-06 11:30:57 +08001159 }
you.chen92fd5d32022-05-25 10:09:47 +08001160 else {
1161 *auth = -1;
1162 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001163
you.chen6c2dd9c2022-05-16 17:55:28 +08001164 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001165}
qs.xiong1af5daf2022-03-14 09:12:12 -04001166
qs.xiong1af5daf2022-03-14 09:12:12 -04001167
qs.xiongf1b525b2022-03-31 00:58:23 -04001168int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001169{
you.chen35020192022-05-06 11:30:57 +08001170 char LYNQ_WIFI_CMD[128]={0};
1171 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1172 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001173
you.chen35020192022-05-06 11:30:57 +08001174 CHECK_IDX(idx, CTRL_AP);
1175
1176 CHECK_WPA_CTRL(CTRL_AP);
1177
1178// system("connmanctl enable wifi");
1179// system("connmanctl tether wifi on cy-test 12345678");
1180// system("ifconfig wlan0 down");
1181// system("ifconfig wlan0 up");
1182// system("ifconfig wlan0 up");
1183
1184 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1185 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1186
1187 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1188 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1189
qs.xiong7a105ce2022-03-02 09:43:11 -05001190 return 0;
1191}
1192
qs.xiongf1b525b2022-03-31 00:58:23 -04001193int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001194{
you.chen35020192022-05-06 11:30:57 +08001195 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001196}
1197
qs.xiongf1b525b2022-03-31 00:58:23 -04001198int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001199{
you.chen35020192022-05-06 11:30:57 +08001200 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001201
you.chen35020192022-05-06 11:30:57 +08001202 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001203
you.chen35020192022-05-06 11:30:57 +08001204 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001205
you.chen35020192022-05-06 11:30:57 +08001206 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1207
1208 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1209
you.chenb4b121c2022-05-06 17:50:16 +08001210// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001211
qs.xiong7a105ce2022-03-02 09:43:11 -05001212 return 0;
1213}
qs.xiong1af5daf2022-03-14 09:12:12 -04001214
qs.xiongf1b525b2022-03-31 00:58:23 -04001215int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001216{
you.chen35020192022-05-06 11:30:57 +08001217 char lynq_disable_cmd[128] = {0};
1218 char lynq_select_cmd[128] = {0};
1219 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001220
you.chen35020192022-05-06 11:30:57 +08001221 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001222
you.chen35020192022-05-06 11:30:57 +08001223 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001224
you.chen35020192022-05-06 11:30:57 +08001225 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1226 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1227
1228 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1229 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1230 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001231
1232 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001233}
1234
qs.xiongf1b525b2022-03-31 00:58:23 -04001235int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001236{
you.chen35020192022-05-06 11:30:57 +08001237 char lynq_disable_cmd[128] = {0};
1238 char lynq_select_cmd[128] = {0};
1239 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001240
you.chen35020192022-05-06 11:30:57 +08001241 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001242
you.chen35020192022-05-06 11:30:57 +08001243 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001244
you.chen35020192022-05-06 11:30:57 +08001245 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1246 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1247
1248 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1249 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1250 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001251
1252 return 0;
1253}
qs.xiongf1b525b2022-03-31 00:58:23 -04001254
you.chen35020192022-05-06 11:30:57 +08001255int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001256{
qs.xiongf1b525b2022-03-31 00:58:23 -04001257 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001258 char lynq_tmp_cmd[MAX_CMD] = {0};
1259 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001260 if( password == NULL ){
1261 return -1;
1262 }
1263 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001264 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001265 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001266 return -1;
you.chen35020192022-05-06 11:30:57 +08001267 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001268
you.chen35020192022-05-06 11:30:57 +08001269 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001270
you.chen6c2dd9c2022-05-16 17:55:28 +08001271 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1272 return -1;
1273 }
1274 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1275 return -1;
1276 }
1277
you.chen35020192022-05-06 11:30:57 +08001278 CHECK_WPA_CTRL(CTRL_AP);
1279
you.chen6c2dd9c2022-05-16 17:55:28 +08001280 if (auth == LYNQ_WIFI_AUTH_WEP) {
1281 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1282 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1283 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1284 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1285 }
1286 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1287 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1288 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1289 }
1290 else {
1291 return -1;
1292 }
you.chen35020192022-05-06 11:30:57 +08001293
you.chen35020192022-05-06 11:30:57 +08001294 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001295
qs.xiongf1b525b2022-03-31 00:58:23 -04001296 return 0;
1297}
1298
you.chen35020192022-05-06 11:30:57 +08001299int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001300{
you.chen35020192022-05-06 11:30:57 +08001301 FILE * fp;
1302 int len, ret;
1303 int count, index;
1304 char *split_lines[128] = {0};
1305 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001306
you.chen35020192022-05-06 11:30:57 +08001307 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001308
you.chen35020192022-05-06 11:30:57 +08001309 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1310// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1311 if (NULL == fp) {
1312 printf("open file fail\n");
1313 return -1;
1314 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001315
you.chen35020192022-05-06 11:30:57 +08001316 buff = alloca(MAX_RET);
1317 fseek(fp, 0, SEEK_SET);
1318 len = fread(buff, 1, MAX_RET, fp);
1319 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001320
you.chen35020192022-05-06 11:30:57 +08001321 for(index=0; index < len; index ++) {
1322 if (memcmp(buff + index, "network={", 9) != 0) {
1323 continue;
1324 }
1325 p = buff + index + 9;
1326 for (; index < len; index ++ ) {
1327 if (buff[index] != '}') {
1328 continue;
1329 }
1330 buff[index] = '\0';
1331 break;
1332 }
1333 len = buff + index - p;
1334 }
1335
1336 count = lynq_split(p, len, '\n', split_lines);
1337
1338 ret = -1;
1339 for(index=0; index < count; index++) {
1340 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001341 if (p != NULL) {
1342 p += 4;
1343 if (*p == '\"') {
1344 p++;
1345 }
you.chen35020192022-05-06 11:30:57 +08001346 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001347 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1348 p += 9;
1349 if (*p == '\"') {
1350 p++;
1351 }
1352 }
1353 else {
1354 continue;
you.chen35020192022-05-06 11:30:57 +08001355 }
1356
1357 strcpy(password, p);
1358
1359 while(*password != '\0') {
1360 if (*password == '\"') {
1361 *password = '\0';
1362 break;
1363 }
1364 password++;
1365 }
1366 ret = 0;
1367 break;
1368 } //end for(index=0; index < count; index++)
1369
1370 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001371}
1372
you.chen35020192022-05-06 11:30:57 +08001373static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1374 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001375 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001376
you.chen35020192022-05-06 11:30:57 +08001377 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1378 return -1;
1379 }
1380
1381 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001382
1383 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1384 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1385 if (strcmp(lynq_proto_str, "RSN") == 0) {
1386 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1387 }
1388 }
1389 }
you.chen35020192022-05-06 11:30:57 +08001390 return 0;
1391}
1392
1393int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001394{
you.chen35020192022-05-06 11:30:57 +08001395 int pass_len, net_no, count, index;
1396 char lynq_tmp_cmd[300]={0};
1397 int net_no_list[128];
1398 lynq_wifi_auth_s net_auth;
1399 pass_len=strlen(password);
1400 if(pass_len < 8 || pass_len >= 64){
1401 return -1;
1402 }
1403
1404 CHECK_IDX(idx, CTRL_STA);
1405
1406 net_no = -1;
1407 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1408
1409 for (index=0; index < count; index++) {
1410 net_auth = -1;
1411 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1412 net_no = net_no_list[index];
1413 break;
1414 }
1415 }
1416
1417 if (net_no < 0) {
1418 return -1;
1419 }
1420
1421 CHECK_WPA_CTRL(CTRL_STA);
1422
1423 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1424
1425 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1426 DO_OK_FAIL_REQUEST(cmd_save_config);
1427
1428 return 0;
1429}
1430
1431int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1432
1433 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001434 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001435 int count, index;
1436 char *split_lines[128] = {0};
1437 char *buff, *p;
1438
you.chen755332b2022-08-06 16:59:10 +08001439 network_len = 0;
1440 p = NULL;
1441
you.chen35020192022-05-06 11:30:57 +08001442 CHECK_IDX(idx, CTRL_STA);
1443
you.chen755332b2022-08-06 16:59:10 +08001444 if (NULL == password) {
1445 printf("bad param\n");
1446 return -1;
1447 }
1448
you.chen35020192022-05-06 11:30:57 +08001449 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1450 if (NULL == fp) {
1451 printf("open file fail\n");
1452 return -1;
1453 }
1454
1455 buff = alloca(MAX_RET);
1456 fseek(fp, 0, SEEK_SET);
1457 len = fread(buff, 1, MAX_RET, fp);
1458 fclose(fp);
1459
1460 for(index=0; index < len; index ++) {
1461 for(; index < len; index ++) {
1462 if (memcmp(buff + index, "network={", 9) != 0) {
1463 continue;
1464 }
1465 p = buff + index + 9;
1466 for (; index < len; index ++ ) {
1467 if (buff[index] != '}') {
1468 continue;
1469 }
1470 buff[index] = '\0';
1471 break;
1472 }
you.chen755332b2022-08-06 16:59:10 +08001473 network_len = buff + index - p;
1474 break;
you.chen35020192022-05-06 11:30:57 +08001475 }
1476
1477 if (strstr(p, ap->ap_ssid) != NULL) {
1478 break;
1479 }
1480 }
1481
you.chen755332b2022-08-06 16:59:10 +08001482 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001483 return -1;
1484 }
1485
you.chen755332b2022-08-06 16:59:10 +08001486 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001487
1488 ret = -1;
1489 for(index=0; index < count; index++) {
1490 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001491 if (p != NULL) {
1492 p += 4;
1493 if (*p == '\"') {
1494 p++;
1495 }
you.chen35020192022-05-06 11:30:57 +08001496 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001497 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1498 p += 9;
1499 if (*p == '\"') {
1500 p++;
1501 }
1502 }
1503 else {
1504 continue;
you.chen35020192022-05-06 11:30:57 +08001505 }
1506
1507 strcpy(password, p);
1508
1509 while(*password != '\0') {
1510 if (*password == '\"') {
1511 *password = '\0';
1512 break;
1513 }
1514 password++;
1515 }
1516 ret = 0;
1517 break;
1518 } //end for(index=0; index < count; index++)
1519
1520 return ret;
1521}
1522
1523static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1524{
qs.xiong97fa59b2022-04-07 05:41:29 -04001525 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001526
you.chen35020192022-05-06 11:30:57 +08001527 if (sta_ssid == NULL) {
1528 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001529 return -1;
you.chen35020192022-05-06 11:30:57 +08001530 }
1531
1532 CHECK_WPA_CTRL(CTRL_STA);
1533
1534 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1535
1536 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1537// DO_OK_FAIL_REQUEST(cmd_save_config);
1538
qs.xiong7a105ce2022-03-02 09:43:11 -05001539 return 0;
1540
1541}
1542
you.chen35020192022-05-06 11:30:57 +08001543static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001544{
you.chen35020192022-05-06 11:30:57 +08001545 char lynq_disable_cmd[128]={0};
1546 char lynq_select_cmd[128]={0};
1547
1548 CHECK_WPA_CTRL(CTRL_STA);
1549
1550 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001551 if (start_flag != 0)
1552 {
1553 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1554 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1555 }
1556 else
1557 {
1558 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1559 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1560 }
you.chen35020192022-05-06 11:30:57 +08001561 DO_OK_FAIL_REQUEST(cmd_save_config);
1562 }
1563
1564 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001565 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001566 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1567 }
1568 else {
1569 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1570 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1571 }
1572
1573 return 0;
1574}
1575
1576int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1577{
1578 CHECK_IDX(idx, CTRL_STA);
1579
you.chen6c2dd9c2022-05-16 17:55:28 +08001580 curr_status_info curr_state;
1581 ap_info_s ap_info;
1582 curr_state.ap = &ap_info;
1583 curr_state.state = NULL;
1584
1585 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1586 strcpy(sta_ssid, ap_info.ap_ssid);
1587 return 0;
1588 }
1589
1590 return -1;
you.chen35020192022-05-06 11:30:57 +08001591}
1592
1593int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1594{
you.chen9ac66392022-08-06 17:01:16 +08001595 scan_info_s *scan_list = NULL;
1596 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001597 int scan_len=0;
1598 int save_len=0;
1599 int best_index = -1;
1600 int best_scan_index = -1;
1601 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001602 int i, j, ret;
1603
1604 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001605
1606 CHECK_IDX(idx, CTRL_STA);
1607 if (info == NULL) {
1608 return -1;
1609 }
1610
1611 curr_status_info curr_state;
1612 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001613 char status[64];
you.chen35020192022-05-06 11:30:57 +08001614
you.chen9ac66392022-08-06 17:01:16 +08001615 memset(&ap_info, 0, sizeof (ap_info));
1616 memset(status, 0, sizeof (status));
1617
1618 curr_state.ap = &ap_info;
1619 curr_state.state = status;
1620
1621 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001622 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001623 if (strcmp(status, STATE_COMPLETED) == 0)
1624 {
1625 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1626 }
1627 else
1628 {
1629 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1630 }
you.chen35020192022-05-06 11:30:57 +08001631 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001632 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001633 return 0;
1634 }
1635
you.chen9ac66392022-08-06 17:01:16 +08001636 lynq_wifi_sta_start_scan(idx);
1637
you.chen35020192022-05-06 11:30:57 +08001638 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001639 if (NULL != scan_list)
1640 {
1641 free(scan_list);
1642 }
you.chen35020192022-05-06 11:30:57 +08001643 return -1;
1644 }
1645
1646 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001647 if (NULL != scan_list)
1648 {
1649 free(scan_list);
1650 }
1651 if (NULL != save_list)
1652 {
1653 free(save_list);
1654 }
you.chen35020192022-05-06 11:30:57 +08001655 return -1;
1656 }
1657
1658 for (i=0; i < save_len; i++) {
1659 for (j=0; j < scan_len; j++) {
1660 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1661 && save_list[i].base_info.auth == scan_list[j].auth) {
1662 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001663 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001664 best_rssi = scan_list[j].rssi;
1665 }
1666 else if (best_rssi > scan_list[j].rssi) {
1667 best_index = i;
1668 best_scan_index = j;
1669 best_rssi = scan_list[j].rssi;
1670 }
1671 break;
1672 }
1673 }
1674 }
1675
1676 if (best_index >= 0) {
1677 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1678 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1679 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001680 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001681 }
1682
you.chen9ac66392022-08-06 17:01:16 +08001683 if (NULL != scan_list)
1684 {
1685 free(scan_list);
1686 }
1687 if (NULL != save_list)
1688 {
1689 free(save_list);
1690 }
1691
1692 return ret;
you.chen35020192022-05-06 11:30:57 +08001693}
1694
1695static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1696{
1697 char lynq_auth_cmd[64]={0};
1698 char lynq_ket_mgmt_cmd[64]={0};
1699 char lynq_pairwise_cmd[64]={0};
1700 char lynq_psk_cmd[64]={0};
1701
1702 CHECK_WPA_CTRL(CTRL_STA);
1703
qs.xiong1af5daf2022-03-14 09:12:12 -04001704 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001705 case LYNQ_WIFI_AUTH_OPEN:
1706 {
1707 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001708
you.chen35020192022-05-06 11:30:57 +08001709 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1710// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001711 break;
1712 }
you.chen35020192022-05-06 11:30:57 +08001713 case LYNQ_WIFI_AUTH_WPA_PSK:
1714 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001715 {
you.chen35020192022-05-06 11:30:57 +08001716 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1717 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1718 }
1719 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001720 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001721 }
1722 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1723 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001724
you.chen35020192022-05-06 11:30:57 +08001725 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1726 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1727 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001728
you.chen35020192022-05-06 11:30:57 +08001729 if (password != NULL) {
1730 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1731 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1732 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001733
you.chen35020192022-05-06 11:30:57 +08001734// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001735 break;
you.chen35020192022-05-06 11:30:57 +08001736 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001737 default:
1738 return -1;
you.chen35020192022-05-06 11:30:57 +08001739 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001740
qs.xiongf1b525b2022-03-31 00:58:23 -04001741 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001742}
qs.xiong7a105ce2022-03-02 09:43:11 -05001743
you.chen35020192022-05-06 11:30:57 +08001744static int inner_get_curr_net_no(int interface) {
1745 curr_status_info curr_state;
1746 curr_state.ap = NULL;
1747 curr_state.state = NULL;
1748
1749 if (0 != inner_get_status_info(interface, &curr_state)) {
1750 return -1;
1751 }
1752
1753 return curr_state.net_no;
1754}
1755
1756int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001757{
you.chen35020192022-05-06 11:30:57 +08001758 int net_no;
1759 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001760
you.chen35020192022-05-06 11:30:57 +08001761 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001762
you.chen35020192022-05-06 11:30:57 +08001763 if (net_no < 0) {
1764 return -1;
1765 }
1766
1767 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001768}
1769
you.chen35020192022-05-06 11:30:57 +08001770int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong7a105ce2022-03-02 09:43:11 -05001771{
you.chen35020192022-05-06 11:30:57 +08001772 int count, net_no, index;
1773 int net_no_list[128];
1774 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001775
you.chen35020192022-05-06 11:30:57 +08001776 if (ssid == NULL || *ssid == '\0') {
1777 printf("bad ssid\n");
1778 return -1;
1779 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001780
you.chen35020192022-05-06 11:30:57 +08001781 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1782 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1783 printf("bad password\n");
1784 return -1;
1785 }
1786 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001787
you.chen35020192022-05-06 11:30:57 +08001788 CHECK_IDX(idx, CTRL_STA);
1789
1790 net_no = -1;
1791 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1792
1793 for (index=0; index < count; index++) {
1794 net_auth = -1;
1795 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1796 net_no = net_no_list[index];
1797 break;
1798 }
1799 }
1800
1801 if (net_no < 0) {
1802 net_no = lynq_add_network(CTRL_STA);
1803 if (net_no == -1) {
1804 return -1;
1805 }
1806
1807 printf("net no is %d\n", net_no);
1808 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1809 return -1;
1810 }
1811 }
1812
1813 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1814 return -1;
1815 }
1816
1817 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001818}
1819
you.chen35020192022-05-06 11:30:57 +08001820int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001821{
you.chen35020192022-05-06 11:30:57 +08001822 ap_info_s ap;
1823 curr_status_info curr_state;
1824 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001825
you.chen35020192022-05-06 11:30:57 +08001826 if (ssid == NULL || *ssid == '\0') {
1827 return -1;
1828 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001829
you.chen35020192022-05-06 11:30:57 +08001830 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001831
you.chen35020192022-05-06 11:30:57 +08001832 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001833 curr_state.state = NULL;
1834
you.chen35020192022-05-06 11:30:57 +08001835 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1836 return 0;
1837 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001838
you.chen35020192022-05-06 11:30:57 +08001839 if (strcmp(ap.ap_ssid, ssid) != 0) {
1840 return 0;
1841 }
1842
1843 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001844}
qs.xiong97fa59b2022-04-07 05:41:29 -04001845
you.chena6cd55a2022-05-08 12:20:18 +08001846int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1847{
you.chenc29444e2022-06-07 18:01:16 +08001848 const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
you.chen01bfac32022-06-07 10:36:00 +08001849 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001850
you.chen35020192022-05-06 11:30:57 +08001851 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001852 CHECK_WPA_CTRL(CTRL_STA);
1853
1854 system("connmanctl enable wifi");
1855
you.chena6fa5b22022-05-18 10:28:19 +08001856 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001857 return -1;
1858 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001859
you.chen01bfac32022-06-07 10:36:00 +08001860 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001861 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001862 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001863
1864 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001865}
1866
you.chen35020192022-05-06 11:30:57 +08001867int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001868{
you.chena6cd55a2022-05-08 12:20:18 +08001869 char lynq_disable_network_cmd[MAX_CMD];
1870 curr_status_info curr_state;
1871 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001872
you.chena6cd55a2022-05-08 12:20:18 +08001873 CHECK_IDX(idx, CTRL_STA);
1874 CHECK_WPA_CTRL(CTRL_STA);
1875
1876 curr_state.ap = &ap_info;
1877 curr_state.state = NULL;
1878
1879 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1880 return 0;
1881 }
1882
1883 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1884 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1885
1886 DO_OK_FAIL_REQUEST(cmd_save_config);
1887
1888 return 0;
1889// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001890}
qs.xiong7a105ce2022-03-02 09:43:11 -05001891
you.chen35020192022-05-06 11:30:57 +08001892//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1893// int i, count;
1894// char *p;
1895// const char * FLAG_SSID = "ssid=";
1896// const char * FLAG_SBSID = "bssid=";
1897// const char * FLAG_KEY_MGMT = "key_mgmt=";
1898// const char * FLAG_FREQ = "freq=";
1899// char lynq_sta_cmd[MAX_CMD];
1900// char *split_lines[128] = {0};
1901
1902// CHECK_WPA_CTRL(CTRL_AP);
1903
1904// sprintf(lynq_sta_cmd, "STA %s", bssid);
1905
1906// DO_REQUEST(lynq_sta_cmd);
1907
1908// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1909
1910// for(i=0; i < count; i++) {
1911// p = strstr(split_lines[i], FLAG_SSID);
1912// if (p != NULL) {
1913// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1914// continue;
1915// }
1916// }
1917
1918// lynq_get_interface_ip(idx, ap->ap_ip);
1919// lynq_ap_password_set(idx, ap->psw);
1920
1921// return 0;
1922//}
1923
1924static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1925 curr_status_info curr_state;
1926 curr_state.ap = ap;
1927 curr_state.state = NULL;
1928 return inner_get_status_info(interface, &curr_state);
1929}
1930
1931int 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 -04001932{
you.chen35020192022-05-06 11:30:57 +08001933 int ip_count, index, i, line_count;
1934 const char *lynq_first_sta_cmd = "STA-FIRST";
1935 char lynq_next_sta_cmd[MAX_CMD];
1936 char *bssid[1024] = {0};
1937 char *mac_list[128] = {0};
1938 char *ip_list[128] = {0};
1939 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001940
you.chen35020192022-05-06 11:30:57 +08001941 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001942
you.chen35020192022-05-06 11:30:57 +08001943 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001944
you.chen35020192022-05-06 11:30:57 +08001945// ap_info_s * tmp_ap;
1946// device_info_s * tmp_list;
1947 if (ap == NULL || list == NULL || len == NULL) {
1948 printf("bad input param");
1949 return -1;
1950 }
1951
1952// ap = &tmp_ap;
1953// list = &tmp_list;
1954 *ap = malloc(sizeof (ap_info_s));
1955
1956 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1957 return -1;
1958 }
1959
1960 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1961 lynq_ap_password_get(idx, (*ap)->psw);
1962
1963 ip_count = get_ip_mac_list(mac_list, ip_list);
1964 printf("get count %d\n", ip_count);
1965
1966 DO_REQUEST(lynq_first_sta_cmd);
1967
1968 index = 0;
1969 while (reply_len > 0) {
1970 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1971 break;
1972 }
1973 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1974 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1975 strcpy(bssid[index], split_lines[0]);
1976 index++;
1977 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1978 reply_len = MAX_RET;
1979 cmd_reply[0] = '\0';
qs.xiongfdbc58e2022-09-29 11:37:32 +08001980 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen9ac66392022-08-06 17:01:16 +08001981 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1982 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001983 break;
1984 }
1985 }
1986
1987 *len = index;
1988
1989 *list = malloc(sizeof(device_info_s) * (*len));
1990 for (index=0; index < *len; index++) {
1991 strcpy((*list)[index].sta_mac, bssid[index]);
1992 for(i=0;i < ip_count; i++ ) {
1993 if (strcmp(bssid[index], mac_list[i]) == 0) {
1994 strcpy((*list)[index].sta_ip, ip_list[i]);
1995 break;
1996 }
1997 }
1998 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1999 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
2000 free(bssid[index]);
2001 }
2002
you.chen9ac66392022-08-06 17:01:16 +08002003 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2004
you.chen35020192022-05-06 11:30:57 +08002005 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002006}
2007
you.chen35020192022-05-06 11:30:57 +08002008int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002009{
you.chen35020192022-05-06 11:30:57 +08002010 int i, count, index, count_words;
2011 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2012 char *split_lines[128] = {0};
2013 char *split_words[128] = {0};
2014 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002015
you.chen35020192022-05-06 11:30:57 +08002016 if (list == NULL || len == NULL) {
2017 return -1;
2018 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002019
you.chen9ac66392022-08-06 17:01:16 +08002020 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2021 {
2022 usleep(100 * 1000);
2023 }
2024
you.chen35020192022-05-06 11:30:57 +08002025 CHECK_IDX(idx, CTRL_STA);
2026
2027 CHECK_WPA_CTRL(CTRL_STA);
2028
2029 DO_REQUEST(lynq_scan_result_cmd);
2030
2031 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2032 *len = count - 1;
2033 *list = malloc(sizeof (scan_info_s) * *len);
2034
2035 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2036 for (index=0; index <count_words; index++) {
2037 printf("----header: %s\n", split_words[index]);
2038 }
2039
2040 for(index = 1;index < count; index++) {
2041 printf("---- %s\n",split_lines[index]);
2042 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2043 if (count_words < 4)
2044 continue;
2045 printf("count: %d, %s\n", count_words, split_words[0]);
2046 //bssid / frequency / signal level / flags / ssid
2047 p = (*list) + index - 1;
2048 strcpy(p->mac, split_words[0]);
2049 p->band = convert_band_from_freq(atoi(split_words[1]));
2050 p->rssi = -1 * atoi( split_words[2]);
2051 p->auth = convert_max_auth_from_flag(split_words[3]);
2052 strcpy(p->ssid, split_words[4]);
2053 }
2054
2055 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002056}
qs.xiong97fa59b2022-04-07 05:41:29 -04002057
you.chen35020192022-05-06 11:30:57 +08002058int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2059{
2060 int count, net_no, index;
2061 int net_no_list[128];
2062 lynq_wifi_auth_s net_auth;
2063 char lynq_remove_cmd[MAX_CMD];
2064
2065 if (ssid == NULL || *ssid == '\0') {
2066 printf("bad ssid\n");
2067 return -1;
2068 }
2069
2070 CHECK_IDX(idx, CTRL_STA);
2071
2072 CHECK_WPA_CTRL(CTRL_STA);
2073
2074 net_no = -1;
2075 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2076
2077 for (index=0; index < count; index++) {
2078 net_auth = -1;
2079 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2080 net_no = net_no_list[index];
2081 break;
2082 }
2083 }
2084
2085 if (net_no < 0) {
2086 return 0;
2087 }
2088
2089 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2090
2091 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2092 DO_OK_FAIL_REQUEST(cmd_save_config);
2093
2094 return 0;
2095}
2096
2097int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2098{
you.chen755332b2022-08-06 16:59:10 +08002099 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002100 int net_no_list[128];
2101 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002102 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002103
2104 if (list == NULL || len == NULL) {
2105 printf("bad param\n");
2106 return -1;
2107 }
2108
2109 CHECK_IDX(idx, CTRL_STA);
2110
2111// CHECK_WPA_CTRL(CTRL_STA);
2112
2113 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2114 printf("count is %d\n", count);
2115
2116 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002117 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002118 *len = count;
2119
2120 for (index=0; index < count; index++) {
2121 printf("to get ssid %d\n", index);
2122 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002123
2124 ssid_ptr = (*list)[index].base_info.ap_ssid;
2125 ssid_len = strlen(ssid_ptr);
2126 if (ssid_ptr[0] == '\"') {
2127 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2128 ssid_len -= 1;
2129 }
2130 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2131 ssid_ptr[ssid_len - 1] = '\0';
2132 }
2133
you.chen35020192022-05-06 11:30:57 +08002134 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002135 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002136 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2137 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2138 }
2139 else {
2140 (*list)[index].base_info.band = -1;
2141 }
2142
you.chen755332b2022-08-06 16:59:10 +08002143 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002144 }
2145
2146 return 0;
2147}
2148
2149int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2150{
2151 const char *lynq_scan_cmd = "SCAN";
2152
2153 CHECK_IDX(idx, CTRL_STA);
2154
2155 CHECK_WPA_CTRL(CTRL_STA);
2156
2157 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002158 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002159
2160 return 0;
2161}
2162
2163int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2164 if (cb == NULL) {
2165 return -1;
2166 }
2167
2168 g_ap_callback_priv = priv;
2169 g_ap_callback_func = cb;
2170
2171 return 0;
2172}
2173
2174int lynq_unreg_ap_event_callback(void * priv) {
2175 if (g_ap_callback_priv == priv) {
2176 g_ap_callback_func = NULL;
2177 g_ap_callback_priv = NULL;
2178 return 0;
2179 }
2180 return -1;
2181}
2182
2183int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2184 if (cb == NULL) {
2185 return -1;
2186 }
2187
2188 g_sta_callback_priv = priv;
2189 g_sta_callback_func = cb;
2190
2191 return 0;
2192}
2193
2194int lynq_unreg_sta_event_callback(void * priv) {
2195 if (g_sta_callback_priv == priv) {
2196 g_sta_callback_func = NULL;
2197 g_sta_callback_priv = NULL;
2198 return 0;
2199 }
2200 return -1;
2201}
2202
2203
2204static int inner_get_status_info_state (int interface, char *state) {
2205 curr_status_info curr_state;
2206 curr_state.ap = NULL;
2207 curr_state.state = state;
2208 return inner_get_status_info(interface, &curr_state);
2209}
2210
2211int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2212{
2213 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002214 CHECK_IDX(idx, CTRL_AP);
2215
2216 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2217 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2218 return 0;
2219 }
2220
2221 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2222 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2223 }
2224 else {
2225 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2226 }
2227
2228 return 0;
2229}
2230
2231int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2232 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002233 CHECK_IDX(idx, CTRL_STA);
2234
2235 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2236 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2237 return 0;
2238 }
2239
2240 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2241 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2242 }
2243 else {
2244 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2245 }
2246
2247 return 0;
2248}
2249
2250int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2251// CHECK_IDX(idx, CTRL_AP);
2252// int ret = 0;
2253// size_t reply_len = MAX_RET;
2254// char cmd_reply[MAX_RET]={0};
2255// const char * cmd_str = "GET country";
2256// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2257// do{
2258// if (NULL == s_lynq_wpa_ctrl) {
2259// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2260// if (NULL == s_lynq_wpa_ctrl ) {
2261// printf("wpa_ctrl_open fail\n");
2262// return -1;
2263// }
2264// }
2265// }while(0);
2266
2267// do {
2268// reply_len = MAX_RET;
2269// cmd_reply[0] = '\0';
2270// printf("to call [%s]\n", cmd_str);
qs.xiongfdbc58e2022-09-29 11:37:32 +08002271// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen35020192022-05-06 11:30:57 +08002272// if (ret != 0) {
2273// printf("call ##cmd_str fail %d\n", ret);
2274// return ret;
2275// }
2276// cmd_reply[reply_len+1] = '\0';
2277// printf("cmd replay [ %s ]\n", cmd_reply);
2278// }while(0);
2279
2280 FILE *fp;
2281 size_t i = 0;
2282 char lynq_cmd_ret[MAX_RET]={0};
2283
2284// CHECK_IDX(idx, CTRL_AP);
2285
2286 if((fp=popen("wl country","r"))==NULL)
2287 {
2288 perror("popen error!");
2289 return -1;
2290 }
2291 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2292 {
2293 perror("fread fail!");
2294 return -1;
2295 }
2296
2297 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2298 if (lynq_cmd_ret[i] == ' ') {
2299 lynq_cmd_ret[i] = '\0';
2300 break;
2301 }
2302 }
2303
2304 strcpy(country_code,lynq_cmd_ret);
2305 printf("---country code %s\n", country_code);
2306
2307 int ret=pclose(fp);
2308 if(ret==-1)
2309 {
2310 perror("close file faild");
2311 }
2312
2313 return 0;
2314}
2315
2316int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2317// const char * cmd_str = "GET country";
2318// CHECK_IDX(idx, CTRL_AP);
2319// CHECK_WPA_CTRL(CTRL_STA);
2320
2321// DO_REQUEST(cmd_str);
2322// printf("result %s\n", cmd_reply);
2323
2324 if (country_code == NULL || *country_code == '\0') {
2325 printf("bad country code\n");
2326 return -1;
2327 }
2328
2329 char lynq_country_cmd[MAX_CMD];
2330 sprintf(lynq_country_cmd, "wl country %s", country_code);
2331 if (system(lynq_country_cmd) == 0) {
2332 return 0;
2333 }
2334
2335 return -1;
2336}
2337
2338int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2339{
2340 if (mac == NULL) {
2341 return -1;
2342 }
2343
2344 CHECK_IDX(idx, CTRL_STA);
2345 ap_info_s ap;
2346 ap.ap_mac[0] = '\0';
2347
2348 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2349 return -1;
2350 }
2351 strcpy(mac, ap.ap_mac);
2352
2353 return 0;
2354}
2355
2356int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2357{
you.chen9ac66392022-08-06 17:01:16 +08002358 struct ifaddrs *ifaddr_header, *ifaddr;
2359 struct in_addr * ifa;
2360 const char * ifaName = "wlan0";
2361 if (ip == NULL)
2362 {
you.chenf58b3c92022-06-21 16:53:48 +08002363 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002364 }
you.chenf58b3c92022-06-21 16:53:48 +08002365
you.chen9ac66392022-08-06 17:01:16 +08002366 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002367 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002368 }
2369 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002370 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002371 }
you.chen35020192022-05-06 11:30:57 +08002372
you.chen9ac66392022-08-06 17:01:16 +08002373 if (getifaddrs(&ifaddr_header) == -1)
2374 {
you.chen35020192022-05-06 11:30:57 +08002375 perror("getifaddrs");
2376 return -1;
2377 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002378 }
you.chen35020192022-05-06 11:30:57 +08002379
2380
you.chen9ac66392022-08-06 17:01:16 +08002381 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2382 {
2383 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002384 continue;
you.chen9ac66392022-08-06 17:01:16 +08002385 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2386 {
2387 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2388 {
2389 // is a valid IP4 Address
2390 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2391 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2392 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2393 freeifaddrs(ifaddr_header);
2394 printf("ip %s\n", ip);
2395 return 0;
2396 }
2397 }
2398 }
2399 return -1;
you.chen35020192022-05-06 11:30:57 +08002400}
2401
2402int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2403{
2404 int count;
2405 size_t i;
2406 char *split_words[128] = {0};
2407 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2408
2409 CHECK_WPA_CTRL(idx);
2410
2411 DO_REQUEST(lynq_get_mac_cmd);
2412
2413 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2414 return -1;
2415 }
2416
2417 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2418
2419 if (count < 2) {
2420 return -1;
2421 }
2422
2423 for (i=0; i < strlen(split_words[1]); i++ ) {
2424 if (split_words[1][i] != ' ') {
2425 break;
2426 }
2427 }
2428
2429 strcpy(mac, split_words[1] + i);
2430
2431 return 0;
2432}
2433
2434int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2435{
2436// int count;
2437// char *split_words[128] = {0};
2438// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2439
2440// if (rssi == NULL) {
2441// return -1;
2442// }
2443
2444// CHECK_IDX(idx, CTRL_STA);
2445
2446// CHECK_WPA_CTRL(CTRL_STA);
2447
2448// DO_REQUEST(lynq_get_rssi_cmd);
2449
2450// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2451// return -1;
2452// }
2453
2454// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2455
2456// if (count < 2) {
2457// return -1;
2458// }
2459
2460// *rssi = atoi(split_words[1]) * -1;
2461
2462 FILE *fp;
2463 size_t i = 0;
2464 char lynq_cmd_ret[MAX_RET]={0};
2465
2466// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002467/*******change other cmd to get rssi*******
2468 *
2469 *wl rssi ---> wl -i wlan0 rssi
2470 *
2471 ***** change by qs.xiong 20221011*******/
2472 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002473 {
2474 perror("popen error!");
2475 return -1;
2476 }
2477 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2478 {
2479 perror("fread fail!");
2480 return -1;
2481 }
you.chen9f17e4d2022-06-06 17:18:18 +08002482 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002483/****** if got rssi is 0,means sta didn't connected any device****/
2484 if(*rssi == 0)
2485 {
2486 printf("sta didn't connected any ap device,please check connection\n");
2487 }
you.chen35020192022-05-06 11:30:57 +08002488
2489 return 0;
2490}
2491
2492int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2493{
2494 if (band == NULL) {
2495 return -1;
2496 }
2497
2498 CHECK_IDX(idx, CTRL_STA);
2499 ap_info_s ap;
2500 ap.band = -1;
2501
2502 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2503 return -1;
2504 }
2505 *band = ap.band;
2506
2507 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002508}
you.chenf58b3c92022-06-21 16:53:48 +08002509
2510int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2511{
2512 int index;
2513 int ip_count = 0;
2514 char bssid[1024] = {0};
2515 char *mac_list[128] = {0};
2516 char *ip_list[128] = {0};
2517
2518 if (ip == NULL)
2519 {
2520 printf("invalid param");
2521 return -1;
2522 }
2523
2524 CHECK_IDX(idx, CTRL_STA);
2525
2526 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2527 {
2528 return -1;
2529 }
2530
2531 ip_count = get_ip_mac_list(mac_list, ip_list);
2532
2533 for (index=0; index < ip_count; index++)
2534 {
2535 if (strcmp(bssid, mac_list[index]) == 0)
2536 {
2537 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002538
2539 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2540
you.chenf58b3c92022-06-21 16:53:48 +08002541 return 0;
2542 }
2543 }
2544
2545 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002546 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2547
you.chenf58b3c92022-06-21 16:53:48 +08002548 return -1;
2549}
2550
qs.xiong026c5c72022-10-17 11:15:45 +08002551int lynq_ap_connect_num(int sta_number)
2552{
2553 char lynq_limit_cmd[32]={0};
2554 int ret;
2555 if((sta_number < 1 ) && (sta_number > 15)){
2556 printf("sta_number: not in range\n",sta_number);
2557 return -1;
2558 }
2559 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2560 ret = system(lynq_limit_cmd);
2561 if(ret != 0){
2562 printf("cmd of limit ap devices number error\n");
2563 }
2564 return 0;
2565}
you.chenf58b3c92022-06-21 16:53:48 +08002566
qs.xiong77905552022-10-17 11:19:57 +08002567int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2568{
2569
2570 char lynq_wifi_acs_cmd[128]={0};
2571 char lynq_cmd_mode[128]={0};
2572 char lynq_cmd_slect[128]={0};
2573
2574 if((acs_mode != 2) && (acs_mode != 5)){
2575 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2576 }
2577
2578 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2579 return -1;
2580 }
2581
2582 CHECK_IDX(idx, CTRL_AP);
2583
2584 CHECK_WPA_CTRL(CTRL_AP);
2585
2586 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2587 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2588 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2589
2590 DO_OK_FAIL_REQUEST(cmd_disconnect);
2591 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2592 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2593 DO_OK_FAIL_REQUEST(cmd_save_config);
2594 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2595
2596 return 0;
2597}
you.chen0f5c6432022-11-07 18:31:14 +08002598//you.chen add for tv-box start
2599static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2600 FILE *fp;
2601 //printf("to exec cmd:%s\n", str_cmd);
2602 if((fp=popen(str_cmd,"r"))==NULL)
2603 {
2604 perror("popen error!");
2605 return -1;
2606 }
2607 if((fread(str_cmd_ret,max_len,1,fp))<0)
2608 {
2609 perror("fread fail!");
2610 fclose(fp);
2611 return -1;
2612 }
2613 fclose(fp);
2614 return 0;
2615}
2616
2617static int get_netmask_length(const char* mask)
2618{
2619 int masklen=0, i=0;
2620 int netmask=0;
2621
2622 if(mask == NULL)
2623 {
2624 return 0;
2625 }
2626
2627 struct in_addr ip_addr;
2628 if( inet_aton(mask, &ip_addr) )
2629 {
2630 netmask = ntohl(ip_addr.s_addr);
2631 }else{
2632 netmask = 0;
2633 return 0;
2634 }
2635
2636 while(0 == (netmask & 0x01) && i<32)
2637 {
2638 i++;
2639 netmask = netmask>>1;
2640 }
2641 masklen = 32-i;
2642 return masklen;
2643}
2644
2645static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2646 int mask_len;
2647 char *p;
2648 char tmp[64] = {0};
2649 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2650 return -1;
2651 p = strstr(str_cmd_ret, "Mask:");
2652 if (p == NULL)
2653 return -1;
2654 mask_len = get_netmask_length(p + 5);
2655 if (mask_len == 0)
2656 return -1;
2657 p = strstr(str_cmd_ret, "inet addr:");
2658 if (p == NULL)
2659 return -1;
2660 strcpy(tmp, p + 10);
2661 p = strstr(tmp, " ");
2662 if (p != NULL)
2663 *p = '\0';
2664 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2665 return 0;
2666}
2667
2668static void GBWWatchThreadProc() {
2669 int i,n, nloop, nmax, ncheckcount, nidlecount;
2670 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2671 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2672 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2673 char *results[16] = {0};
2674 char str_cmd[256] = {0};
2675 char str_cmd_ret[128] = {0};
2676 char dest_ip[32] = {0};
2677 lastAP1Bytes = lastAP2Bytes = 0;
2678 lastAP1Drop = lastAP2Drop = 0;
2679 lastAP1Speed = lastAP2Speed = 0;
2680 setAP1Speed = 50;
2681 setAP2Speed = 80;
2682 nloop = 0;
2683 nmax = 6;
2684 ncheckcount = nidlecount = 0;
2685
2686 printf("------gbw thread run\n");
2687 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2688 while (dest_ip[0] == '\0') {
2689 sleep(1);
2690 str_cmd_ret[0] = '\0';
2691 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2692 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2693 if (str_cmd_ret[n] == '\n'){
2694 str_cmd_ret[n] = '\0';
2695 break;
2696 }
2697 }
2698 if (str_cmd_ret[0] != '\0')
2699 {
2700 strcpy(dest_ip, str_cmd_ret);
2701 }
2702 }
2703
2704 system("tc qdisc del dev tether root > /dev/null 2>&1");
2705 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2706 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2707 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2708 {
2709 printf("not get tether info\n");
2710 return;
2711 }
2712 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret);
2713 system(str_cmd);
2714 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2715 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2716 //printf("----cmd:%s\n", str_cmd);
2717 system(str_cmd);
2718
2719 while (1) {
2720 sleep(1);
2721 memset(str_cmd, 0, sizeof(str_cmd));
2722 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2723 continue;
2724 //printf("ap1 --- %s\n", str_cmd);
2725 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2726 if (n > 9) {
2727 if (strcmp(results[1], "Sent") == 0) {
2728 currAP1Bytes = atoll(results[2]);
2729 }
2730 if (strcmp(results[6], "(dropped") == 0) {
2731 currAP1Drop = atoi(results[7]);
2732 }
2733 }
2734
2735 memset(str_cmd, 0, sizeof(str_cmd));
2736 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2737 continue;
2738 //printf("ap2 --- %s\n", str_cmd);
2739 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2740 if (n > 9) {
2741 if (strcmp(results[1], "Sent") == 0) {
2742 currAP2Bytes = atoll(results[2]);
2743 }
2744 if (strcmp(results[6], "(dropped") == 0) {
2745 currAP2Drop = atoi(results[7]);
2746 }
2747 }
2748
2749 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2750 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2751 lastAP1Bytes = currAP1Bytes;
2752 lastAP2Bytes = currAP2Bytes;
2753 continue;
2754 }
2755
2756 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2757 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2758 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2759 lastAP1Speed = currAP1Speed;
2760 lastAP2Speed = currAP2Speed;
2761 lastAP1Bytes = currAP1Bytes;
2762 lastAP2Bytes = currAP2Bytes;
2763
2764 currSetAP1Speed = setAP1Speed;
2765 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2766 ncheckcount++;
2767 if (ncheckcount > 3) {
2768 ncheckcount = 0;
2769 currSetAP1Speed = 5;
2770 }
2771 }
2772 else {
2773 ncheckcount = 0;
2774 if (currAP1Speed < 5)
2775 nidlecount++;
2776 else
2777 nidlecount = 0;
2778
2779 }
2780
2781 if (nidlecount > 60 ){
2782 currSetAP1Speed = 50;
2783 }
2784
2785 if (currSetAP1Speed != setAP1Speed) {
2786 setAP1Speed = currSetAP1Speed;
2787 sprintf(str_cmd, "tc class replace dev tether parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4));
2788 //printf("------***change speed: %s\n", str_cmd);
2789 system(str_cmd);
2790 }
2791 }
2792}
2793
2794int enableGBW(const char* mac) {
2795 int i,len;
2796 char get_ipaddr_cmd[128]={0};
2797 ap_info_s *ap;
2798 device_info_s * list;
2799
2800 if (mac == NULL || g_gbw_enabled == 1)
2801 return -1;
2802 len = strlen(mac);
2803 g_gbw_mac = malloc(len + 1);
2804 for(i=0;i<len;i++) {
2805 if (mac[i] >= 'A' && mac[i] <= 'Z')
2806 {
2807 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2808 }
2809 else
2810 g_gbw_mac[i] = mac[i];
2811 }
2812 g_gbw_mac[i] = '\0';
2813 g_gbw_enabled = 1;
2814
2815 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2816 if (system(get_ipaddr_cmd) == 0) {
2817 //startGBW();
2818 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2819 for (i=0;i<len;i++) {
2820 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2821 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2822 startGBW();
2823 }
2824 free(ap);
2825 free(list);
2826 }
2827 }
2828 return 0;
2829}
2830
2831int disableGBW() {
2832 stopGBW();
2833 free(g_gbw_mac);
2834 g_gbw_mac = NULL;
2835 g_gbw_enabled = 1;
2836 return 0;
2837}
2838
2839static int startGBW() {
2840 if (g_gbw_watcher_pid != 0) {
2841 stopGBW();
2842 }
2843 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2844}
2845
2846static int stopGBW() {
2847 void* retval;
2848 pthread_cancel(g_gbw_watcher_pid);
2849 pthread_join(g_gbw_watcher_pid, &retval);
2850 g_gbw_watcher_pid = 0;
2851 system("tc qdisc del dev tether root");
2852}
2853//you.chen add for tv-box end