blob: 78c756e478af85675404b651a8457a293a523d07 [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){
835 static int legitimate_frequency[21]={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 = 0;
837 int flag_check = 0;
838 for(i=0 ;i<= 21; i++){
839 if(input_frequency == legitimate_frequency[i]){
840 flag_check = 1;
841 break;
842 }
843 }
844 if(flag_check == 1){
845 printf("input frequency in range\n");
846 }else{
847 printf("input frequency is eero--->%d,please check it\n",input_frequency);
848 return -1;
849 }
850 return 0;
851}
qs.xiongf1b525b2022-03-31 00:58:23 -0400852int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500853{
qs.xiongc9c79f72022-10-17 15:27:18 +0800854 char lynq_wifi_frequency_cmd[128]={0};
855 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800856 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500857
qs.xiongc9c79f72022-10-17 15:27:18 +0800858 //@do check input frequency
859 if((lynq_check_set_frequency(lynq_wifi_frequency)) != 0){
860 return -1;
you.chen35020192022-05-06 11:30:57 +0800861 }
you.chen35020192022-05-06 11:30:57 +0800862 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
863 return -1;
864 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400865
you.chen35020192022-05-06 11:30:57 +0800866 CHECK_IDX(idx, CTRL_AP);
867
868 CHECK_WPA_CTRL(CTRL_AP);
869
870 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
871 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
872 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
873
you.chen6c2dd9c2022-05-16 17:55:28 +0800874 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800875 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
876 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
877 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500878
qs.xiong1af5daf2022-03-14 09:12:12 -0400879 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500880}
881
qs.xiongf1b525b2022-03-31 00:58:23 -0400882int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500883{
you.chen35020192022-05-06 11:30:57 +0800884 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400885
you.chen35020192022-05-06 11:30:57 +0800886 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400887
you.chen35020192022-05-06 11:30:57 +0800888 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
889 return -1;
890 }
891 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400892
893 return 0;
894}
895
qs.xiongf1b525b2022-03-31 00:58:23 -0400896int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
897{
you.chen35020192022-05-06 11:30:57 +0800898 CHECK_IDX(idx, CTRL_AP);
899 switch(bandwidth){
900 case LYNQ_WIFI_BANDWIDTH_HT10:
901 {
902 printf("bandwith [%d] not support now\n", bandwidth);
903 return -1;
904 }
905 case LYNQ_WIFI_BANDWIDTH_HT20:
906 {
907 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
908 system("wl down");
909 if (system(lynq_cmd_bandwith) != 0 ){
910 return -1;
911 }
912 system("wl up");
913 break;
914 }
915 case LYNQ_WIFI_BANDWIDTH_HT40:
916 {
917 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
918 sprintf(lynq_cmd_bandwith, "wl chanspec ");
919 system("wl down");
920 if (system(lynq_cmd_bandwith) != 0 ){
921 return -1;
922 }
923 system("wl up");
924 break;
925 }
926 case LYNQ_WIFI_BANDWIDTH_HT80:
927 {
928 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
929 system("wl down");
930 if (system(lynq_cmd_bandwith) != 0 ){
931 return -1;
932 }
933 system("wl up");
934 break;
935 }
936 default:
937 {
938 printf("auth type [%d] not support now\n", bandwidth);
939 return -1;
940 }
941 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400942
943
you.chen35020192022-05-06 11:30:57 +0800944 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400945}
you.chen35020192022-05-06 11:30:57 +0800946
qs.xiongf1b525b2022-03-31 00:58:23 -0400947int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
948{
you.chen35020192022-05-06 11:30:57 +0800949 int count = 0;
950 int index = 0;
951 char *split_words[128] = {0};
952 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400953
you.chen35020192022-05-06 11:30:57 +0800954 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500955
you.chen35020192022-05-06 11:30:57 +0800956 CHECK_WPA_CTRL(CTRL_AP);
957
958 DO_REQUEST(lynq_chanspec_cmd);
959
960 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
961 for(;index < count; index++) {
962 if (strncmp(split_words[index], "bw", 2) != 0) {
963 continue;
964 }
965
966 index++;
967 if (index >= count) {
968 return -1;
969 }
970
971 printf("bw %s\n", split_words[index]);
972 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
973 return 0;
974 }
975
976 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500977}
qs.xiong0fb469a2022-04-14 03:50:45 -0400978
qs.xiongf1b525b2022-03-31 00:58:23 -0400979int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500980{
you.chen35020192022-05-06 11:30:57 +0800981 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400982
you.chen35020192022-05-06 11:30:57 +0800983 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400984
you.chen35020192022-05-06 11:30:57 +0800985 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400986
you.chen35020192022-05-06 11:30:57 +0800987 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
988 return -1;
989 }
990
991 system("wl down");
992 if (system(lynq_cmd_channel) != 0 ){
993 return -1;
994 }
995 system("wl up");
996 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500997}
qs.xiong0fb469a2022-04-14 03:50:45 -0400998
qs.xiongf1b525b2022-03-31 00:58:23 -0400999int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001000{
you.chen35020192022-05-06 11:30:57 +08001001 int count = 0;
1002 int index = 0;
1003 char *split_words[128] = {0};
1004 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001005
you.chen35020192022-05-06 11:30:57 +08001006 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001007
you.chen35020192022-05-06 11:30:57 +08001008 CHECK_WPA_CTRL(CTRL_AP);
1009
1010 DO_REQUEST(lynq_chanspec_cmd);
1011
1012 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1013 for(;index < count; index++) {
1014 printf("---- %s\n",split_words[index]);
1015 if (strncmp(split_words[index], "channel", 2) != 0) {
1016 continue;
1017 }
1018
1019 index++;
1020 if (index >= count) {
1021 return -1;
1022 }
1023
1024 *channel = atoi(split_words[index]);
1025 return 0;
1026 }
1027
1028 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001029}
1030
1031
you.chen35020192022-05-06 11:30:57 +08001032int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001033{
you.chen6c2dd9c2022-05-16 17:55:28 +08001034 char ssid[MAX_CMD] = {0};
1035 int freq = 0;
1036 char lynq_auth_cmd[64]={0};
1037 char lynq_auth_alg_cmd[64]={0};
1038 char lynq_psk_cmd[64]={0};
1039 char lynq_pairwise_cmd[64]={0};
1040 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001041 CHECK_IDX(idx, CTRL_AP);
1042
you.chen6c2dd9c2022-05-16 17:55:28 +08001043 CHECK_WPA_CTRL(CTRL_AP);
1044
you.chen35020192022-05-06 11:30:57 +08001045 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1046 return -1;
1047 }
1048
you.chen92fd5d32022-05-25 10:09:47 +08001049 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001050 if (org_auth == auth) {
1051 return 0;
1052 }
1053 else {
1054 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1055 ssid[0] = '\0';
1056 }
1057 lynq_wifi_ap_frequency_get(idx, &freq);
1058
1059 DO_OK_FAIL_REQUEST(cmd_disconnect);
1060 DO_OK_FAIL_REQUEST(cmd_remove_all);
1061 if (ssid[0] != '\0') {
1062 lynq_wifi_ap_ssid_set(idx, ssid);
1063 }
1064 if (freq != 0) {
1065 lynq_wifi_ap_frequency_set(idx, freq);
1066 }
1067 }
1068 }
you.chen35020192022-05-06 11:30:57 +08001069
qs.xiong7a105ce2022-03-02 09:43:11 -05001070 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001071 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001072 {
you.chen35020192022-05-06 11:30:57 +08001073 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001074 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001075
you.chen35020192022-05-06 11:30:57 +08001076 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001077 break;
1078 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001079 case LYNQ_WIFI_AUTH_WEP:
1080 {
1081 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001082 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001083 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1084
1085 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1086 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1087 break;
1088 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001089 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001090 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001091 {
you.chen35020192022-05-06 11:30:57 +08001092 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1093 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1094 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1095 }
1096 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001097 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001098 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001099 }
1100// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1101// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1102 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001103
you.chen35020192022-05-06 11:30:57 +08001104 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1105 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1106 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001107 break;
you.chen35020192022-05-06 11:30:57 +08001108 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001109 default:
you.chen35020192022-05-06 11:30:57 +08001110 {
1111 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001112 return -1;
you.chen35020192022-05-06 11:30:57 +08001113 }
1114 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001115 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001116
qs.xiong7a105ce2022-03-02 09:43:11 -05001117 return 0;
1118}
1119
you.chen35020192022-05-06 11:30:57 +08001120int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001121{
you.chen35020192022-05-06 11:30:57 +08001122 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001123 char lynq_auth_alg_str[MAX_RET] = {0};
1124 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001125
1126 CHECK_IDX(idx, CTRL_AP);
1127
1128 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1129 return -1;
1130 }
1131
1132 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001133 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1134 *auth = LYNQ_WIFI_AUTH_OPEN;
1135 }
1136 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1137 *auth = LYNQ_WIFI_AUTH_WEP;
1138 }
1139 else {
1140 *auth = LYNQ_WIFI_AUTH_OPEN;
1141 }
you.chen35020192022-05-06 11:30:57 +08001142 }
you.chen92fd5d32022-05-25 10:09:47 +08001143 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001144 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001145 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001146 }
1147 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1148 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1149 }
1150 else {
1151 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1152 }
you.chen35020192022-05-06 11:30:57 +08001153 }
you.chen92fd5d32022-05-25 10:09:47 +08001154 else {
1155 *auth = -1;
1156 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001157
you.chen6c2dd9c2022-05-16 17:55:28 +08001158 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001159}
qs.xiong1af5daf2022-03-14 09:12:12 -04001160
qs.xiong1af5daf2022-03-14 09:12:12 -04001161
qs.xiongf1b525b2022-03-31 00:58:23 -04001162int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001163{
you.chen35020192022-05-06 11:30:57 +08001164 char LYNQ_WIFI_CMD[128]={0};
1165 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1166 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001167
you.chen35020192022-05-06 11:30:57 +08001168 CHECK_IDX(idx, CTRL_AP);
1169
1170 CHECK_WPA_CTRL(CTRL_AP);
1171
1172// system("connmanctl enable wifi");
1173// system("connmanctl tether wifi on cy-test 12345678");
1174// system("ifconfig wlan0 down");
1175// system("ifconfig wlan0 up");
1176// system("ifconfig wlan0 up");
1177
1178 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1179 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1180
1181 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1182 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1183
qs.xiong7a105ce2022-03-02 09:43:11 -05001184 return 0;
1185}
1186
qs.xiongf1b525b2022-03-31 00:58:23 -04001187int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001188{
you.chen35020192022-05-06 11:30:57 +08001189 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001190}
1191
qs.xiongf1b525b2022-03-31 00:58:23 -04001192int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001193{
you.chen35020192022-05-06 11:30:57 +08001194 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001195
you.chen35020192022-05-06 11:30:57 +08001196 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001197
you.chen35020192022-05-06 11:30:57 +08001198 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001199
you.chen35020192022-05-06 11:30:57 +08001200 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1201
1202 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1203
you.chenb4b121c2022-05-06 17:50:16 +08001204// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001205
qs.xiong7a105ce2022-03-02 09:43:11 -05001206 return 0;
1207}
qs.xiong1af5daf2022-03-14 09:12:12 -04001208
qs.xiongf1b525b2022-03-31 00:58:23 -04001209int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001210{
you.chen35020192022-05-06 11:30:57 +08001211 char lynq_disable_cmd[128] = {0};
1212 char lynq_select_cmd[128] = {0};
1213 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001214
you.chen35020192022-05-06 11:30:57 +08001215 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001216
you.chen35020192022-05-06 11:30:57 +08001217 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001218
you.chen35020192022-05-06 11:30:57 +08001219 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1220 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1221
1222 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1223 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1224 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001225
1226 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001227}
1228
qs.xiongf1b525b2022-03-31 00:58:23 -04001229int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001230{
you.chen35020192022-05-06 11:30:57 +08001231 char lynq_disable_cmd[128] = {0};
1232 char lynq_select_cmd[128] = {0};
1233 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001234
you.chen35020192022-05-06 11:30:57 +08001235 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001236
you.chen35020192022-05-06 11:30:57 +08001237 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001238
you.chen35020192022-05-06 11:30:57 +08001239 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1240 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1241
1242 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1243 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1244 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001245
1246 return 0;
1247}
qs.xiongf1b525b2022-03-31 00:58:23 -04001248
you.chen35020192022-05-06 11:30:57 +08001249int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001250{
qs.xiongf1b525b2022-03-31 00:58:23 -04001251 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001252 char lynq_tmp_cmd[MAX_CMD] = {0};
1253 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001254 if( password == NULL ){
1255 return -1;
1256 }
1257 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001258 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001259 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001260 return -1;
you.chen35020192022-05-06 11:30:57 +08001261 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001262
you.chen35020192022-05-06 11:30:57 +08001263 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001264
you.chen6c2dd9c2022-05-16 17:55:28 +08001265 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1266 return -1;
1267 }
1268 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1269 return -1;
1270 }
1271
you.chen35020192022-05-06 11:30:57 +08001272 CHECK_WPA_CTRL(CTRL_AP);
1273
you.chen6c2dd9c2022-05-16 17:55:28 +08001274 if (auth == LYNQ_WIFI_AUTH_WEP) {
1275 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1276 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1277 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1278 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1279 }
1280 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1281 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1282 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1283 }
1284 else {
1285 return -1;
1286 }
you.chen35020192022-05-06 11:30:57 +08001287
you.chen35020192022-05-06 11:30:57 +08001288 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001289
qs.xiongf1b525b2022-03-31 00:58:23 -04001290 return 0;
1291}
1292
you.chen35020192022-05-06 11:30:57 +08001293int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001294{
you.chen35020192022-05-06 11:30:57 +08001295 FILE * fp;
1296 int len, ret;
1297 int count, index;
1298 char *split_lines[128] = {0};
1299 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001300
you.chen35020192022-05-06 11:30:57 +08001301 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001302
you.chen35020192022-05-06 11:30:57 +08001303 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1304// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1305 if (NULL == fp) {
1306 printf("open file fail\n");
1307 return -1;
1308 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001309
you.chen35020192022-05-06 11:30:57 +08001310 buff = alloca(MAX_RET);
1311 fseek(fp, 0, SEEK_SET);
1312 len = fread(buff, 1, MAX_RET, fp);
1313 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001314
you.chen35020192022-05-06 11:30:57 +08001315 for(index=0; index < len; index ++) {
1316 if (memcmp(buff + index, "network={", 9) != 0) {
1317 continue;
1318 }
1319 p = buff + index + 9;
1320 for (; index < len; index ++ ) {
1321 if (buff[index] != '}') {
1322 continue;
1323 }
1324 buff[index] = '\0';
1325 break;
1326 }
1327 len = buff + index - p;
1328 }
1329
1330 count = lynq_split(p, len, '\n', split_lines);
1331
1332 ret = -1;
1333 for(index=0; index < count; index++) {
1334 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001335 if (p != NULL) {
1336 p += 4;
1337 if (*p == '\"') {
1338 p++;
1339 }
you.chen35020192022-05-06 11:30:57 +08001340 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001341 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1342 p += 9;
1343 if (*p == '\"') {
1344 p++;
1345 }
1346 }
1347 else {
1348 continue;
you.chen35020192022-05-06 11:30:57 +08001349 }
1350
1351 strcpy(password, p);
1352
1353 while(*password != '\0') {
1354 if (*password == '\"') {
1355 *password = '\0';
1356 break;
1357 }
1358 password++;
1359 }
1360 ret = 0;
1361 break;
1362 } //end for(index=0; index < count; index++)
1363
1364 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001365}
1366
you.chen35020192022-05-06 11:30:57 +08001367static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1368 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001369 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001370
you.chen35020192022-05-06 11:30:57 +08001371 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1372 return -1;
1373 }
1374
1375 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001376
1377 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1378 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1379 if (strcmp(lynq_proto_str, "RSN") == 0) {
1380 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1381 }
1382 }
1383 }
you.chen35020192022-05-06 11:30:57 +08001384 return 0;
1385}
1386
1387int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001388{
you.chen35020192022-05-06 11:30:57 +08001389 int pass_len, net_no, count, index;
1390 char lynq_tmp_cmd[300]={0};
1391 int net_no_list[128];
1392 lynq_wifi_auth_s net_auth;
1393 pass_len=strlen(password);
1394 if(pass_len < 8 || pass_len >= 64){
1395 return -1;
1396 }
1397
1398 CHECK_IDX(idx, CTRL_STA);
1399
1400 net_no = -1;
1401 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1402
1403 for (index=0; index < count; index++) {
1404 net_auth = -1;
1405 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1406 net_no = net_no_list[index];
1407 break;
1408 }
1409 }
1410
1411 if (net_no < 0) {
1412 return -1;
1413 }
1414
1415 CHECK_WPA_CTRL(CTRL_STA);
1416
1417 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1418
1419 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1420 DO_OK_FAIL_REQUEST(cmd_save_config);
1421
1422 return 0;
1423}
1424
1425int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1426
1427 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001428 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001429 int count, index;
1430 char *split_lines[128] = {0};
1431 char *buff, *p;
1432
you.chen755332b2022-08-06 16:59:10 +08001433 network_len = 0;
1434 p = NULL;
1435
you.chen35020192022-05-06 11:30:57 +08001436 CHECK_IDX(idx, CTRL_STA);
1437
you.chen755332b2022-08-06 16:59:10 +08001438 if (NULL == password) {
1439 printf("bad param\n");
1440 return -1;
1441 }
1442
you.chen35020192022-05-06 11:30:57 +08001443 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1444 if (NULL == fp) {
1445 printf("open file fail\n");
1446 return -1;
1447 }
1448
1449 buff = alloca(MAX_RET);
1450 fseek(fp, 0, SEEK_SET);
1451 len = fread(buff, 1, MAX_RET, fp);
1452 fclose(fp);
1453
1454 for(index=0; index < len; index ++) {
1455 for(; index < len; index ++) {
1456 if (memcmp(buff + index, "network={", 9) != 0) {
1457 continue;
1458 }
1459 p = buff + index + 9;
1460 for (; index < len; index ++ ) {
1461 if (buff[index] != '}') {
1462 continue;
1463 }
1464 buff[index] = '\0';
1465 break;
1466 }
you.chen755332b2022-08-06 16:59:10 +08001467 network_len = buff + index - p;
1468 break;
you.chen35020192022-05-06 11:30:57 +08001469 }
1470
1471 if (strstr(p, ap->ap_ssid) != NULL) {
1472 break;
1473 }
1474 }
1475
you.chen755332b2022-08-06 16:59:10 +08001476 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001477 return -1;
1478 }
1479
you.chen755332b2022-08-06 16:59:10 +08001480 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001481
1482 ret = -1;
1483 for(index=0; index < count; index++) {
1484 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001485 if (p != NULL) {
1486 p += 4;
1487 if (*p == '\"') {
1488 p++;
1489 }
you.chen35020192022-05-06 11:30:57 +08001490 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001491 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1492 p += 9;
1493 if (*p == '\"') {
1494 p++;
1495 }
1496 }
1497 else {
1498 continue;
you.chen35020192022-05-06 11:30:57 +08001499 }
1500
1501 strcpy(password, p);
1502
1503 while(*password != '\0') {
1504 if (*password == '\"') {
1505 *password = '\0';
1506 break;
1507 }
1508 password++;
1509 }
1510 ret = 0;
1511 break;
1512 } //end for(index=0; index < count; index++)
1513
1514 return ret;
1515}
1516
1517static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1518{
qs.xiong97fa59b2022-04-07 05:41:29 -04001519 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001520
you.chen35020192022-05-06 11:30:57 +08001521 if (sta_ssid == NULL) {
1522 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001523 return -1;
you.chen35020192022-05-06 11:30:57 +08001524 }
1525
1526 CHECK_WPA_CTRL(CTRL_STA);
1527
1528 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1529
1530 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1531// DO_OK_FAIL_REQUEST(cmd_save_config);
1532
qs.xiong7a105ce2022-03-02 09:43:11 -05001533 return 0;
1534
1535}
1536
you.chen35020192022-05-06 11:30:57 +08001537static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001538{
you.chen35020192022-05-06 11:30:57 +08001539 char lynq_disable_cmd[128]={0};
1540 char lynq_select_cmd[128]={0};
1541
1542 CHECK_WPA_CTRL(CTRL_STA);
1543
1544 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001545 if (start_flag != 0)
1546 {
1547 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1548 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1549 }
1550 else
1551 {
1552 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1553 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1554 }
you.chen35020192022-05-06 11:30:57 +08001555 DO_OK_FAIL_REQUEST(cmd_save_config);
1556 }
1557
1558 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001559 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001560 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1561 }
1562 else {
1563 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1564 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1565 }
1566
1567 return 0;
1568}
1569
1570int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1571{
1572 CHECK_IDX(idx, CTRL_STA);
1573
you.chen6c2dd9c2022-05-16 17:55:28 +08001574 curr_status_info curr_state;
1575 ap_info_s ap_info;
1576 curr_state.ap = &ap_info;
1577 curr_state.state = NULL;
1578
1579 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1580 strcpy(sta_ssid, ap_info.ap_ssid);
1581 return 0;
1582 }
1583
1584 return -1;
you.chen35020192022-05-06 11:30:57 +08001585}
1586
1587int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1588{
you.chen9ac66392022-08-06 17:01:16 +08001589 scan_info_s *scan_list = NULL;
1590 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001591 int scan_len=0;
1592 int save_len=0;
1593 int best_index = -1;
1594 int best_scan_index = -1;
1595 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001596 int i, j, ret;
1597
1598 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001599
1600 CHECK_IDX(idx, CTRL_STA);
1601 if (info == NULL) {
1602 return -1;
1603 }
1604
1605 curr_status_info curr_state;
1606 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001607 char status[64];
you.chen35020192022-05-06 11:30:57 +08001608
you.chen9ac66392022-08-06 17:01:16 +08001609 memset(&ap_info, 0, sizeof (ap_info));
1610 memset(status, 0, sizeof (status));
1611
1612 curr_state.ap = &ap_info;
1613 curr_state.state = status;
1614
1615 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001616 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001617 if (strcmp(status, STATE_COMPLETED) == 0)
1618 {
1619 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1620 }
1621 else
1622 {
1623 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1624 }
you.chen35020192022-05-06 11:30:57 +08001625 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001626 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001627 return 0;
1628 }
1629
you.chen9ac66392022-08-06 17:01:16 +08001630 lynq_wifi_sta_start_scan(idx);
1631
you.chen35020192022-05-06 11:30:57 +08001632 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001633 if (NULL != scan_list)
1634 {
1635 free(scan_list);
1636 }
you.chen35020192022-05-06 11:30:57 +08001637 return -1;
1638 }
1639
1640 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001641 if (NULL != scan_list)
1642 {
1643 free(scan_list);
1644 }
1645 if (NULL != save_list)
1646 {
1647 free(save_list);
1648 }
you.chen35020192022-05-06 11:30:57 +08001649 return -1;
1650 }
1651
1652 for (i=0; i < save_len; i++) {
1653 for (j=0; j < scan_len; j++) {
1654 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1655 && save_list[i].base_info.auth == scan_list[j].auth) {
1656 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001657 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001658 best_rssi = scan_list[j].rssi;
1659 }
1660 else if (best_rssi > scan_list[j].rssi) {
1661 best_index = i;
1662 best_scan_index = j;
1663 best_rssi = scan_list[j].rssi;
1664 }
1665 break;
1666 }
1667 }
1668 }
1669
1670 if (best_index >= 0) {
1671 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1672 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1673 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001674 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001675 }
1676
you.chen9ac66392022-08-06 17:01:16 +08001677 if (NULL != scan_list)
1678 {
1679 free(scan_list);
1680 }
1681 if (NULL != save_list)
1682 {
1683 free(save_list);
1684 }
1685
1686 return ret;
you.chen35020192022-05-06 11:30:57 +08001687}
1688
1689static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1690{
1691 char lynq_auth_cmd[64]={0};
1692 char lynq_ket_mgmt_cmd[64]={0};
1693 char lynq_pairwise_cmd[64]={0};
1694 char lynq_psk_cmd[64]={0};
1695
1696 CHECK_WPA_CTRL(CTRL_STA);
1697
qs.xiong1af5daf2022-03-14 09:12:12 -04001698 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001699 case LYNQ_WIFI_AUTH_OPEN:
1700 {
1701 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001702
you.chen35020192022-05-06 11:30:57 +08001703 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1704// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001705 break;
1706 }
you.chen35020192022-05-06 11:30:57 +08001707 case LYNQ_WIFI_AUTH_WPA_PSK:
1708 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001709 {
you.chen35020192022-05-06 11:30:57 +08001710 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1711 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1712 }
1713 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001714 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001715 }
1716 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1717 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001718
you.chen35020192022-05-06 11:30:57 +08001719 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1720 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1721 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001722
you.chen35020192022-05-06 11:30:57 +08001723 if (password != NULL) {
1724 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1725 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1726 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001727
you.chen35020192022-05-06 11:30:57 +08001728// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001729 break;
you.chen35020192022-05-06 11:30:57 +08001730 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001731 default:
1732 return -1;
you.chen35020192022-05-06 11:30:57 +08001733 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001734
qs.xiongf1b525b2022-03-31 00:58:23 -04001735 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001736}
qs.xiong7a105ce2022-03-02 09:43:11 -05001737
you.chen35020192022-05-06 11:30:57 +08001738static int inner_get_curr_net_no(int interface) {
1739 curr_status_info curr_state;
1740 curr_state.ap = NULL;
1741 curr_state.state = NULL;
1742
1743 if (0 != inner_get_status_info(interface, &curr_state)) {
1744 return -1;
1745 }
1746
1747 return curr_state.net_no;
1748}
1749
1750int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001751{
you.chen35020192022-05-06 11:30:57 +08001752 int net_no;
1753 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001754
you.chen35020192022-05-06 11:30:57 +08001755 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001756
you.chen35020192022-05-06 11:30:57 +08001757 if (net_no < 0) {
1758 return -1;
1759 }
1760
1761 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001762}
1763
you.chen35020192022-05-06 11:30:57 +08001764int 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 -05001765{
you.chen35020192022-05-06 11:30:57 +08001766 int count, net_no, index;
1767 int net_no_list[128];
1768 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001769
you.chen35020192022-05-06 11:30:57 +08001770 if (ssid == NULL || *ssid == '\0') {
1771 printf("bad ssid\n");
1772 return -1;
1773 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001774
you.chen35020192022-05-06 11:30:57 +08001775 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1776 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1777 printf("bad password\n");
1778 return -1;
1779 }
1780 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001781
you.chen35020192022-05-06 11:30:57 +08001782 CHECK_IDX(idx, CTRL_STA);
1783
1784 net_no = -1;
1785 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1786
1787 for (index=0; index < count; index++) {
1788 net_auth = -1;
1789 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1790 net_no = net_no_list[index];
1791 break;
1792 }
1793 }
1794
1795 if (net_no < 0) {
1796 net_no = lynq_add_network(CTRL_STA);
1797 if (net_no == -1) {
1798 return -1;
1799 }
1800
1801 printf("net no is %d\n", net_no);
1802 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1803 return -1;
1804 }
1805 }
1806
1807 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1808 return -1;
1809 }
1810
1811 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001812}
1813
you.chen35020192022-05-06 11:30:57 +08001814int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001815{
you.chen35020192022-05-06 11:30:57 +08001816 ap_info_s ap;
1817 curr_status_info curr_state;
1818 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001819
you.chen35020192022-05-06 11:30:57 +08001820 if (ssid == NULL || *ssid == '\0') {
1821 return -1;
1822 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001823
you.chen35020192022-05-06 11:30:57 +08001824 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001825
you.chen35020192022-05-06 11:30:57 +08001826 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001827 curr_state.state = NULL;
1828
you.chen35020192022-05-06 11:30:57 +08001829 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1830 return 0;
1831 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001832
you.chen35020192022-05-06 11:30:57 +08001833 if (strcmp(ap.ap_ssid, ssid) != 0) {
1834 return 0;
1835 }
1836
1837 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001838}
qs.xiong97fa59b2022-04-07 05:41:29 -04001839
you.chena6cd55a2022-05-08 12:20:18 +08001840int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1841{
you.chenc29444e2022-06-07 18:01:16 +08001842 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 +08001843 const char *lynq_reconnect_cmd = "RECONNECT";
qs.xiong7a105ce2022-03-02 09:43:11 -05001844
you.chen35020192022-05-06 11:30:57 +08001845 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001846 CHECK_WPA_CTRL(CTRL_STA);
1847
1848 system("connmanctl enable wifi");
1849
you.chena6fa5b22022-05-18 10:28:19 +08001850 if (system("ifconfig | grep -q wlan0") != 0) {
you.chen35020192022-05-06 11:30:57 +08001851 return -1;
1852 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001853
you.chen01bfac32022-06-07 10:36:00 +08001854 DO_OK_FAIL_REQUEST(cmd_remove_all);
you.chenc29444e2022-06-07 18:01:16 +08001855 system(lynq_reconfigure_cmd);
you.chen01bfac32022-06-07 10:36:00 +08001856 DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001857
1858 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001859}
1860
you.chen35020192022-05-06 11:30:57 +08001861int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001862{
you.chena6cd55a2022-05-08 12:20:18 +08001863 char lynq_disable_network_cmd[MAX_CMD];
1864 curr_status_info curr_state;
1865 ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001866
you.chena6cd55a2022-05-08 12:20:18 +08001867 CHECK_IDX(idx, CTRL_STA);
1868 CHECK_WPA_CTRL(CTRL_STA);
1869
1870 curr_state.ap = &ap_info;
1871 curr_state.state = NULL;
1872
1873 if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1874 return 0;
1875 }
1876
1877 sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1878 DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1879
1880 DO_OK_FAIL_REQUEST(cmd_save_config);
1881
1882 return 0;
1883// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001884}
qs.xiong7a105ce2022-03-02 09:43:11 -05001885
you.chen35020192022-05-06 11:30:57 +08001886//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1887// int i, count;
1888// char *p;
1889// const char * FLAG_SSID = "ssid=";
1890// const char * FLAG_SBSID = "bssid=";
1891// const char * FLAG_KEY_MGMT = "key_mgmt=";
1892// const char * FLAG_FREQ = "freq=";
1893// char lynq_sta_cmd[MAX_CMD];
1894// char *split_lines[128] = {0};
1895
1896// CHECK_WPA_CTRL(CTRL_AP);
1897
1898// sprintf(lynq_sta_cmd, "STA %s", bssid);
1899
1900// DO_REQUEST(lynq_sta_cmd);
1901
1902// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1903
1904// for(i=0; i < count; i++) {
1905// p = strstr(split_lines[i], FLAG_SSID);
1906// if (p != NULL) {
1907// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1908// continue;
1909// }
1910// }
1911
1912// lynq_get_interface_ip(idx, ap->ap_ip);
1913// lynq_ap_password_set(idx, ap->psw);
1914
1915// return 0;
1916//}
1917
1918static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1919 curr_status_info curr_state;
1920 curr_state.ap = ap;
1921 curr_state.state = NULL;
1922 return inner_get_status_info(interface, &curr_state);
1923}
1924
1925int 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 -04001926{
you.chen35020192022-05-06 11:30:57 +08001927 int ip_count, index, i, line_count;
1928 const char *lynq_first_sta_cmd = "STA-FIRST";
1929 char lynq_next_sta_cmd[MAX_CMD];
1930 char *bssid[1024] = {0};
1931 char *mac_list[128] = {0};
1932 char *ip_list[128] = {0};
1933 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001934
you.chen35020192022-05-06 11:30:57 +08001935 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001936
you.chen35020192022-05-06 11:30:57 +08001937 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001938
you.chen35020192022-05-06 11:30:57 +08001939// ap_info_s * tmp_ap;
1940// device_info_s * tmp_list;
1941 if (ap == NULL || list == NULL || len == NULL) {
1942 printf("bad input param");
1943 return -1;
1944 }
1945
1946// ap = &tmp_ap;
1947// list = &tmp_list;
1948 *ap = malloc(sizeof (ap_info_s));
1949
1950 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1951 return -1;
1952 }
1953
1954 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1955 lynq_ap_password_get(idx, (*ap)->psw);
1956
1957 ip_count = get_ip_mac_list(mac_list, ip_list);
1958 printf("get count %d\n", ip_count);
1959
1960 DO_REQUEST(lynq_first_sta_cmd);
1961
1962 index = 0;
1963 while (reply_len > 0) {
1964 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1965 break;
1966 }
1967 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1968 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1969 strcpy(bssid[index], split_lines[0]);
1970 index++;
1971 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1972 reply_len = MAX_RET;
1973 cmd_reply[0] = '\0';
qs.xiongfdbc58e2022-09-29 11:37:32 +08001974 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 +08001975 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1976 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001977 break;
1978 }
1979 }
1980
1981 *len = index;
1982
1983 *list = malloc(sizeof(device_info_s) * (*len));
1984 for (index=0; index < *len; index++) {
1985 strcpy((*list)[index].sta_mac, bssid[index]);
1986 for(i=0;i < ip_count; i++ ) {
1987 if (strcmp(bssid[index], mac_list[i]) == 0) {
1988 strcpy((*list)[index].sta_ip, ip_list[i]);
1989 break;
1990 }
1991 }
1992 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
1993 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
1994 free(bssid[index]);
1995 }
1996
you.chen9ac66392022-08-06 17:01:16 +08001997 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
1998
you.chen35020192022-05-06 11:30:57 +08001999 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002000}
2001
you.chen35020192022-05-06 11:30:57 +08002002int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002003{
you.chen35020192022-05-06 11:30:57 +08002004 int i, count, index, count_words;
2005 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2006 char *split_lines[128] = {0};
2007 char *split_words[128] = {0};
2008 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002009
you.chen35020192022-05-06 11:30:57 +08002010 if (list == NULL || len == NULL) {
2011 return -1;
2012 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002013
you.chen9ac66392022-08-06 17:01:16 +08002014 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2015 {
2016 usleep(100 * 1000);
2017 }
2018
you.chen35020192022-05-06 11:30:57 +08002019 CHECK_IDX(idx, CTRL_STA);
2020
2021 CHECK_WPA_CTRL(CTRL_STA);
2022
2023 DO_REQUEST(lynq_scan_result_cmd);
2024
2025 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2026 *len = count - 1;
2027 *list = malloc(sizeof (scan_info_s) * *len);
2028
2029 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2030 for (index=0; index <count_words; index++) {
2031 printf("----header: %s\n", split_words[index]);
2032 }
2033
2034 for(index = 1;index < count; index++) {
2035 printf("---- %s\n",split_lines[index]);
2036 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2037 if (count_words < 4)
2038 continue;
2039 printf("count: %d, %s\n", count_words, split_words[0]);
2040 //bssid / frequency / signal level / flags / ssid
2041 p = (*list) + index - 1;
2042 strcpy(p->mac, split_words[0]);
2043 p->band = convert_band_from_freq(atoi(split_words[1]));
2044 p->rssi = -1 * atoi( split_words[2]);
2045 p->auth = convert_max_auth_from_flag(split_words[3]);
2046 strcpy(p->ssid, split_words[4]);
2047 }
2048
2049 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002050}
qs.xiong97fa59b2022-04-07 05:41:29 -04002051
you.chen35020192022-05-06 11:30:57 +08002052int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2053{
2054 int count, net_no, index;
2055 int net_no_list[128];
2056 lynq_wifi_auth_s net_auth;
2057 char lynq_remove_cmd[MAX_CMD];
2058
2059 if (ssid == NULL || *ssid == '\0') {
2060 printf("bad ssid\n");
2061 return -1;
2062 }
2063
2064 CHECK_IDX(idx, CTRL_STA);
2065
2066 CHECK_WPA_CTRL(CTRL_STA);
2067
2068 net_no = -1;
2069 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2070
2071 for (index=0; index < count; index++) {
2072 net_auth = -1;
2073 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2074 net_no = net_no_list[index];
2075 break;
2076 }
2077 }
2078
2079 if (net_no < 0) {
2080 return 0;
2081 }
2082
2083 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2084
2085 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2086 DO_OK_FAIL_REQUEST(cmd_save_config);
2087
2088 return 0;
2089}
2090
2091int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2092{
you.chen755332b2022-08-06 16:59:10 +08002093 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002094 int net_no_list[128];
2095 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002096 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002097
2098 if (list == NULL || len == NULL) {
2099 printf("bad param\n");
2100 return -1;
2101 }
2102
2103 CHECK_IDX(idx, CTRL_STA);
2104
2105// CHECK_WPA_CTRL(CTRL_STA);
2106
2107 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2108 printf("count is %d\n", count);
2109
2110 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002111 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002112 *len = count;
2113
2114 for (index=0; index < count; index++) {
2115 printf("to get ssid %d\n", index);
2116 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002117
2118 ssid_ptr = (*list)[index].base_info.ap_ssid;
2119 ssid_len = strlen(ssid_ptr);
2120 if (ssid_ptr[0] == '\"') {
2121 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2122 ssid_len -= 1;
2123 }
2124 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2125 ssid_ptr[ssid_len - 1] = '\0';
2126 }
2127
you.chen35020192022-05-06 11:30:57 +08002128 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002129 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002130 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2131 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2132 }
2133 else {
2134 (*list)[index].base_info.band = -1;
2135 }
2136
you.chen755332b2022-08-06 16:59:10 +08002137 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002138 }
2139
2140 return 0;
2141}
2142
2143int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2144{
2145 const char *lynq_scan_cmd = "SCAN";
2146
2147 CHECK_IDX(idx, CTRL_STA);
2148
2149 CHECK_WPA_CTRL(CTRL_STA);
2150
2151 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002152 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002153
2154 return 0;
2155}
2156
2157int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2158 if (cb == NULL) {
2159 return -1;
2160 }
2161
2162 g_ap_callback_priv = priv;
2163 g_ap_callback_func = cb;
2164
2165 return 0;
2166}
2167
2168int lynq_unreg_ap_event_callback(void * priv) {
2169 if (g_ap_callback_priv == priv) {
2170 g_ap_callback_func = NULL;
2171 g_ap_callback_priv = NULL;
2172 return 0;
2173 }
2174 return -1;
2175}
2176
2177int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2178 if (cb == NULL) {
2179 return -1;
2180 }
2181
2182 g_sta_callback_priv = priv;
2183 g_sta_callback_func = cb;
2184
2185 return 0;
2186}
2187
2188int lynq_unreg_sta_event_callback(void * priv) {
2189 if (g_sta_callback_priv == priv) {
2190 g_sta_callback_func = NULL;
2191 g_sta_callback_priv = NULL;
2192 return 0;
2193 }
2194 return -1;
2195}
2196
2197
2198static int inner_get_status_info_state (int interface, char *state) {
2199 curr_status_info curr_state;
2200 curr_state.ap = NULL;
2201 curr_state.state = state;
2202 return inner_get_status_info(interface, &curr_state);
2203}
2204
2205int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2206{
2207 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002208 CHECK_IDX(idx, CTRL_AP);
2209
2210 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2211 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2212 return 0;
2213 }
2214
2215 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2216 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2217 }
2218 else {
2219 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2220 }
2221
2222 return 0;
2223}
2224
2225int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2226 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002227 CHECK_IDX(idx, CTRL_STA);
2228
2229 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2230 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2231 return 0;
2232 }
2233
2234 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2235 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2236 }
2237 else {
2238 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2239 }
2240
2241 return 0;
2242}
2243
2244int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2245// CHECK_IDX(idx, CTRL_AP);
2246// int ret = 0;
2247// size_t reply_len = MAX_RET;
2248// char cmd_reply[MAX_RET]={0};
2249// const char * cmd_str = "GET country";
2250// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2251// do{
2252// if (NULL == s_lynq_wpa_ctrl) {
2253// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2254// if (NULL == s_lynq_wpa_ctrl ) {
2255// printf("wpa_ctrl_open fail\n");
2256// return -1;
2257// }
2258// }
2259// }while(0);
2260
2261// do {
2262// reply_len = MAX_RET;
2263// cmd_reply[0] = '\0';
2264// printf("to call [%s]\n", cmd_str);
qs.xiongfdbc58e2022-09-29 11:37:32 +08002265// 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 +08002266// if (ret != 0) {
2267// printf("call ##cmd_str fail %d\n", ret);
2268// return ret;
2269// }
2270// cmd_reply[reply_len+1] = '\0';
2271// printf("cmd replay [ %s ]\n", cmd_reply);
2272// }while(0);
2273
2274 FILE *fp;
2275 size_t i = 0;
2276 char lynq_cmd_ret[MAX_RET]={0};
2277
2278// CHECK_IDX(idx, CTRL_AP);
2279
2280 if((fp=popen("wl country","r"))==NULL)
2281 {
2282 perror("popen error!");
2283 return -1;
2284 }
2285 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2286 {
2287 perror("fread fail!");
2288 return -1;
2289 }
2290
2291 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2292 if (lynq_cmd_ret[i] == ' ') {
2293 lynq_cmd_ret[i] = '\0';
2294 break;
2295 }
2296 }
2297
2298 strcpy(country_code,lynq_cmd_ret);
2299 printf("---country code %s\n", country_code);
2300
2301 int ret=pclose(fp);
2302 if(ret==-1)
2303 {
2304 perror("close file faild");
2305 }
2306
2307 return 0;
2308}
2309
2310int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2311// const char * cmd_str = "GET country";
2312// CHECK_IDX(idx, CTRL_AP);
2313// CHECK_WPA_CTRL(CTRL_STA);
2314
2315// DO_REQUEST(cmd_str);
2316// printf("result %s\n", cmd_reply);
2317
2318 if (country_code == NULL || *country_code == '\0') {
2319 printf("bad country code\n");
2320 return -1;
2321 }
2322
2323 char lynq_country_cmd[MAX_CMD];
2324 sprintf(lynq_country_cmd, "wl country %s", country_code);
2325 if (system(lynq_country_cmd) == 0) {
2326 return 0;
2327 }
2328
2329 return -1;
2330}
2331
2332int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2333{
2334 if (mac == NULL) {
2335 return -1;
2336 }
2337
2338 CHECK_IDX(idx, CTRL_STA);
2339 ap_info_s ap;
2340 ap.ap_mac[0] = '\0';
2341
2342 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2343 return -1;
2344 }
2345 strcpy(mac, ap.ap_mac);
2346
2347 return 0;
2348}
2349
2350int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2351{
you.chen9ac66392022-08-06 17:01:16 +08002352 struct ifaddrs *ifaddr_header, *ifaddr;
2353 struct in_addr * ifa;
2354 const char * ifaName = "wlan0";
2355 if (ip == NULL)
2356 {
you.chenf58b3c92022-06-21 16:53:48 +08002357 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002358 }
you.chenf58b3c92022-06-21 16:53:48 +08002359
you.chen9ac66392022-08-06 17:01:16 +08002360 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002361 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002362 }
2363 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002364 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002365 }
you.chen35020192022-05-06 11:30:57 +08002366
you.chen9ac66392022-08-06 17:01:16 +08002367 if (getifaddrs(&ifaddr_header) == -1)
2368 {
you.chen35020192022-05-06 11:30:57 +08002369 perror("getifaddrs");
2370 return -1;
2371 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002372 }
you.chen35020192022-05-06 11:30:57 +08002373
2374
you.chen9ac66392022-08-06 17:01:16 +08002375 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2376 {
2377 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002378 continue;
you.chen9ac66392022-08-06 17:01:16 +08002379 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2380 {
2381 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2382 {
2383 // is a valid IP4 Address
2384 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2385 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2386 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2387 freeifaddrs(ifaddr_header);
2388 printf("ip %s\n", ip);
2389 return 0;
2390 }
2391 }
2392 }
2393 return -1;
you.chen35020192022-05-06 11:30:57 +08002394}
2395
2396int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2397{
2398 int count;
2399 size_t i;
2400 char *split_words[128] = {0};
2401 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2402
2403 CHECK_WPA_CTRL(idx);
2404
2405 DO_REQUEST(lynq_get_mac_cmd);
2406
2407 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2408 return -1;
2409 }
2410
2411 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2412
2413 if (count < 2) {
2414 return -1;
2415 }
2416
2417 for (i=0; i < strlen(split_words[1]); i++ ) {
2418 if (split_words[1][i] != ' ') {
2419 break;
2420 }
2421 }
2422
2423 strcpy(mac, split_words[1] + i);
2424
2425 return 0;
2426}
2427
2428int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2429{
2430// int count;
2431// char *split_words[128] = {0};
2432// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2433
2434// if (rssi == NULL) {
2435// return -1;
2436// }
2437
2438// CHECK_IDX(idx, CTRL_STA);
2439
2440// CHECK_WPA_CTRL(CTRL_STA);
2441
2442// DO_REQUEST(lynq_get_rssi_cmd);
2443
2444// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2445// return -1;
2446// }
2447
2448// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2449
2450// if (count < 2) {
2451// return -1;
2452// }
2453
2454// *rssi = atoi(split_words[1]) * -1;
2455
2456 FILE *fp;
2457 size_t i = 0;
2458 char lynq_cmd_ret[MAX_RET]={0};
2459
2460// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002461/*******change other cmd to get rssi*******
2462 *
2463 *wl rssi ---> wl -i wlan0 rssi
2464 *
2465 ***** change by qs.xiong 20221011*******/
2466 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002467 {
2468 perror("popen error!");
2469 return -1;
2470 }
2471 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2472 {
2473 perror("fread fail!");
2474 return -1;
2475 }
you.chen9f17e4d2022-06-06 17:18:18 +08002476 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002477/****** if got rssi is 0,means sta didn't connected any device****/
2478 if(*rssi == 0)
2479 {
2480 printf("sta didn't connected any ap device,please check connection\n");
2481 }
you.chen35020192022-05-06 11:30:57 +08002482
2483 return 0;
2484}
2485
2486int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2487{
2488 if (band == NULL) {
2489 return -1;
2490 }
2491
2492 CHECK_IDX(idx, CTRL_STA);
2493 ap_info_s ap;
2494 ap.band = -1;
2495
2496 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2497 return -1;
2498 }
2499 *band = ap.band;
2500
2501 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002502}
you.chenf58b3c92022-06-21 16:53:48 +08002503
2504int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2505{
2506 int index;
2507 int ip_count = 0;
2508 char bssid[1024] = {0};
2509 char *mac_list[128] = {0};
2510 char *ip_list[128] = {0};
2511
2512 if (ip == NULL)
2513 {
2514 printf("invalid param");
2515 return -1;
2516 }
2517
2518 CHECK_IDX(idx, CTRL_STA);
2519
2520 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2521 {
2522 return -1;
2523 }
2524
2525 ip_count = get_ip_mac_list(mac_list, ip_list);
2526
2527 for (index=0; index < ip_count; index++)
2528 {
2529 if (strcmp(bssid, mac_list[index]) == 0)
2530 {
2531 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002532
2533 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2534
you.chenf58b3c92022-06-21 16:53:48 +08002535 return 0;
2536 }
2537 }
2538
2539 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002540 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2541
you.chenf58b3c92022-06-21 16:53:48 +08002542 return -1;
2543}
2544
qs.xiong026c5c72022-10-17 11:15:45 +08002545int lynq_ap_connect_num(int sta_number)
2546{
2547 char lynq_limit_cmd[32]={0};
2548 int ret;
2549 if((sta_number < 1 ) && (sta_number > 15)){
2550 printf("sta_number: not in range\n",sta_number);
2551 return -1;
2552 }
2553 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2554 ret = system(lynq_limit_cmd);
2555 if(ret != 0){
2556 printf("cmd of limit ap devices number error\n");
2557 }
2558 return 0;
2559}
you.chenf58b3c92022-06-21 16:53:48 +08002560
qs.xiong77905552022-10-17 11:19:57 +08002561int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2562{
2563
2564 char lynq_wifi_acs_cmd[128]={0};
2565 char lynq_cmd_mode[128]={0};
2566 char lynq_cmd_slect[128]={0};
2567
2568 if((acs_mode != 2) && (acs_mode != 5)){
2569 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2570 }
2571
2572 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2573 return -1;
2574 }
2575
2576 CHECK_IDX(idx, CTRL_AP);
2577
2578 CHECK_WPA_CTRL(CTRL_AP);
2579
2580 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2581 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2582 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2583
2584 DO_OK_FAIL_REQUEST(cmd_disconnect);
2585 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2586 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2587 DO_OK_FAIL_REQUEST(cmd_save_config);
2588 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2589
2590 return 0;
2591}
you.chen0f5c6432022-11-07 18:31:14 +08002592//you.chen add for tv-box start
2593static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2594 FILE *fp;
2595 //printf("to exec cmd:%s\n", str_cmd);
2596 if((fp=popen(str_cmd,"r"))==NULL)
2597 {
2598 perror("popen error!");
2599 return -1;
2600 }
2601 if((fread(str_cmd_ret,max_len,1,fp))<0)
2602 {
2603 perror("fread fail!");
2604 fclose(fp);
2605 return -1;
2606 }
2607 fclose(fp);
2608 return 0;
2609}
2610
2611static int get_netmask_length(const char* mask)
2612{
2613 int masklen=0, i=0;
2614 int netmask=0;
2615
2616 if(mask == NULL)
2617 {
2618 return 0;
2619 }
2620
2621 struct in_addr ip_addr;
2622 if( inet_aton(mask, &ip_addr) )
2623 {
2624 netmask = ntohl(ip_addr.s_addr);
2625 }else{
2626 netmask = 0;
2627 return 0;
2628 }
2629
2630 while(0 == (netmask & 0x01) && i<32)
2631 {
2632 i++;
2633 netmask = netmask>>1;
2634 }
2635 masklen = 32-i;
2636 return masklen;
2637}
2638
2639static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2640 int mask_len;
2641 char *p;
2642 char tmp[64] = {0};
2643 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2644 return -1;
2645 p = strstr(str_cmd_ret, "Mask:");
2646 if (p == NULL)
2647 return -1;
2648 mask_len = get_netmask_length(p + 5);
2649 if (mask_len == 0)
2650 return -1;
2651 p = strstr(str_cmd_ret, "inet addr:");
2652 if (p == NULL)
2653 return -1;
2654 strcpy(tmp, p + 10);
2655 p = strstr(tmp, " ");
2656 if (p != NULL)
2657 *p = '\0';
2658 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2659 return 0;
2660}
2661
2662static void GBWWatchThreadProc() {
2663 int i,n, nloop, nmax, ncheckcount, nidlecount;
2664 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2665 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2666 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2667 char *results[16] = {0};
2668 char str_cmd[256] = {0};
2669 char str_cmd_ret[128] = {0};
2670 char dest_ip[32] = {0};
2671 lastAP1Bytes = lastAP2Bytes = 0;
2672 lastAP1Drop = lastAP2Drop = 0;
2673 lastAP1Speed = lastAP2Speed = 0;
2674 setAP1Speed = 50;
2675 setAP2Speed = 80;
2676 nloop = 0;
2677 nmax = 6;
2678 ncheckcount = nidlecount = 0;
2679
2680 printf("------gbw thread run\n");
2681 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2682 while (dest_ip[0] == '\0') {
2683 sleep(1);
2684 str_cmd_ret[0] = '\0';
2685 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2686 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2687 if (str_cmd_ret[n] == '\n'){
2688 str_cmd_ret[n] = '\0';
2689 break;
2690 }
2691 }
2692 if (str_cmd_ret[0] != '\0')
2693 {
2694 strcpy(dest_ip, str_cmd_ret);
2695 }
2696 }
2697
2698 system("tc qdisc del dev tether root > /dev/null 2>&1");
2699 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2700 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2701 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2702 {
2703 printf("not get tether info\n");
2704 return;
2705 }
2706 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);
2707 system(str_cmd);
2708 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2709 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2710 //printf("----cmd:%s\n", str_cmd);
2711 system(str_cmd);
2712
2713 while (1) {
2714 sleep(1);
2715 memset(str_cmd, 0, sizeof(str_cmd));
2716 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2717 continue;
2718 //printf("ap1 --- %s\n", str_cmd);
2719 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2720 if (n > 9) {
2721 if (strcmp(results[1], "Sent") == 0) {
2722 currAP1Bytes = atoll(results[2]);
2723 }
2724 if (strcmp(results[6], "(dropped") == 0) {
2725 currAP1Drop = atoi(results[7]);
2726 }
2727 }
2728
2729 memset(str_cmd, 0, sizeof(str_cmd));
2730 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2731 continue;
2732 //printf("ap2 --- %s\n", str_cmd);
2733 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2734 if (n > 9) {
2735 if (strcmp(results[1], "Sent") == 0) {
2736 currAP2Bytes = atoll(results[2]);
2737 }
2738 if (strcmp(results[6], "(dropped") == 0) {
2739 currAP2Drop = atoi(results[7]);
2740 }
2741 }
2742
2743 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2744 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2745 lastAP1Bytes = currAP1Bytes;
2746 lastAP2Bytes = currAP2Bytes;
2747 continue;
2748 }
2749
2750 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2751 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2752 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2753 lastAP1Speed = currAP1Speed;
2754 lastAP2Speed = currAP2Speed;
2755 lastAP1Bytes = currAP1Bytes;
2756 lastAP2Bytes = currAP2Bytes;
2757
2758 currSetAP1Speed = setAP1Speed;
2759 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2760 ncheckcount++;
2761 if (ncheckcount > 3) {
2762 ncheckcount = 0;
2763 currSetAP1Speed = 5;
2764 }
2765 }
2766 else {
2767 ncheckcount = 0;
2768 if (currAP1Speed < 5)
2769 nidlecount++;
2770 else
2771 nidlecount = 0;
2772
2773 }
2774
2775 if (nidlecount > 60 ){
2776 currSetAP1Speed = 50;
2777 }
2778
2779 if (currSetAP1Speed != setAP1Speed) {
2780 setAP1Speed = currSetAP1Speed;
2781 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));
2782 //printf("------***change speed: %s\n", str_cmd);
2783 system(str_cmd);
2784 }
2785 }
2786}
2787
2788int enableGBW(const char* mac) {
2789 int i,len;
2790 char get_ipaddr_cmd[128]={0};
2791 ap_info_s *ap;
2792 device_info_s * list;
2793
2794 if (mac == NULL || g_gbw_enabled == 1)
2795 return -1;
2796 len = strlen(mac);
2797 g_gbw_mac = malloc(len + 1);
2798 for(i=0;i<len;i++) {
2799 if (mac[i] >= 'A' && mac[i] <= 'Z')
2800 {
2801 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2802 }
2803 else
2804 g_gbw_mac[i] = mac[i];
2805 }
2806 g_gbw_mac[i] = '\0';
2807 g_gbw_enabled = 1;
2808
2809 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2810 if (system(get_ipaddr_cmd) == 0) {
2811 //startGBW();
2812 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2813 for (i=0;i<len;i++) {
2814 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2815 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2816 startGBW();
2817 }
2818 free(ap);
2819 free(list);
2820 }
2821 }
2822 return 0;
2823}
2824
2825int disableGBW() {
2826 stopGBW();
2827 free(g_gbw_mac);
2828 g_gbw_mac = NULL;
2829 g_gbw_enabled = 1;
2830 return 0;
2831}
2832
2833static int startGBW() {
2834 if (g_gbw_watcher_pid != 0) {
2835 stopGBW();
2836 }
2837 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2838}
2839
2840static int stopGBW() {
2841 void* retval;
2842 pthread_cancel(g_gbw_watcher_pid);
2843 pthread_join(g_gbw_watcher_pid, &retval);
2844 g_gbw_watcher_pid = 0;
2845 system("tc qdisc del dev tether root");
2846}
2847//you.chen add for tv-box end