blob: 8828184ec73d32315f7cea72c5b7977e970603e8 [file] [log] [blame]
developer91f80742022-10-04 15:20:18 +08001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include <uci.h>
6#include "wifi-test-tool.h"
7
developer50614832022-11-17 20:42:05 +08008static int mac_addr_aton(unsigned char *mac_addr, char *arg)
9{
10 sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
11 return 0;
12}
13
14static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
15{
16 snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", arg[0], arg[1],arg[2],arg[3],arg[4],arg[5]);
17 return;
18}
developer8d8d6302022-10-18 16:36:37 +080019
20static int _syscmd(char *cmd, char *retBuf, int retBufSize)
21{
22 FILE *f;
23 char *ptr = retBuf;
24 int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0;
25
26
27 if((f = popen(cmd, "r")) == NULL) {
28 fprintf(stderr,"\npopen %s error\n", cmd);
29 return RETURN_ERR;
30 }
31
32 while(!feof(f))
33 {
34 *ptr = 0;
35 if(bufSize>=128) {
36 bufbytes=128;
37 } else {
38 bufbytes=bufSize-1;
39 }
40
41 fgets(ptr,bufbytes,f);
42 readbytes=strlen(ptr);
43
44 if(!readbytes)
45 break;
46
47 bufSize-=readbytes;
48 ptr += readbytes;
49 }
50 cmd_ret = pclose(f);
51 retBuf[retBufSize-1]=0;
52
53 return cmd_ret >> 8;
54}
55
56int phy_index_to_radio(int phyIndex)
57{
58 char cmd[128] = {0};
59 char buf[64] = {0};
60 int radioIndex = 0;
61 snprintf(cmd, sizeof(cmd), "ls /tmp | grep phy%d | cut -d '-' -f2 | tr -d '\n'", phyIndex);
62 _syscmd(cmd, buf, sizeof(buf));
63
64 if (strlen(buf) == 0 || strstr(buf, "wifi") == NULL) {
65 fprintf(stderr, "%s: failed to get wifi index\n", __func__);
66 return RETURN_ERR;
67 }
68 sscanf(buf, "wifi%d", &radioIndex);
developerc6a27322023-01-13 15:23:06 +080069 return radioIndex;
developer8d8d6302022-10-18 16:36:37 +080070}
71
developer91f80742022-10-04 15:20:18 +080072void set_channel(wifi_radio_param *radio_param, char *channel)
73{
developer63d72772022-10-07 09:42:31 +080074 if (strcmp(channel, "auto") == 0) {
75 radio_param->auto_channel = TRUE;
76 radio_param->channel = 0;
77 } else {
developer91f80742022-10-04 15:20:18 +080078 radio_param->auto_channel = FALSE;
developer63d72772022-10-07 09:42:31 +080079 radio_param->channel = strtol(channel, NULL, 10);
developer91f80742022-10-04 15:20:18 +080080 }
developer63d72772022-10-07 09:42:31 +080081 return;
developer91f80742022-10-04 15:20:18 +080082}
83
developer52c6ca22022-10-06 17:16:43 +080084void set_country(wifi_radio_param *radio_param, char *country)
developer91f80742022-10-04 15:20:18 +080085{
86 strcpy(radio_param->country, country);
87}
88
developer52c6ca22022-10-06 17:16:43 +080089void set_band(wifi_radio_param *radio_param, char *band)
developer91f80742022-10-04 15:20:18 +080090{
91 strcpy(radio_param->band, band);
92}
93
developer6feac682022-10-18 17:44:13 +080094void set_noscan(wifi_radio_param *radio_param, char *noscan)
95{
96 snprintf(radio_param->noscan, 2, "%s", noscan);
97 radio_param->noscan[1] = '\0';
98}
99
developer91f80742022-10-04 15:20:18 +0800100void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
101{
102 if (strncmp(hwmode, "11a", 3) == 0)
103 strcpy(radio_param->hwmode, "a");
104 if (strncmp(hwmode, "11b", 3) == 0)
105 strcpy(radio_param->hwmode, "b");
106 if (strncmp(hwmode, "11g", 3) == 0)
107 strcpy(radio_param->hwmode, "g");
108}
109
110void set_htmode(wifi_radio_param *radio_param, char *htmode)
111{
112 char tmp[16] = {0};
113 char *ptr = htmode;
114 ULONG bandwidth = 0;
115 radio_param->bandwidth = 20;
116 while (*ptr) {
117 if (isdigit(*ptr)) {
118 bandwidth = strtoul(ptr, NULL, 10);
119 radio_param->bandwidth = bandwidth;
120 break;
121 }
122 ptr++;
123 }
124
125 // HT40 -> 11NGHT40PLUS
126 // VHT40+ -> 11ACVHT40PLUS
127 // HE80 -> 11AXHE80
developerca672072023-02-24 18:01:27 +0800128 // EHT320-1 -> 11BEEHT320-1
129 if (strstr(htmode, "40+") != NULL) {
developer91f80742022-10-04 15:20:18 +0800130 strncpy(tmp, htmode, strlen(htmode) - 1);
131 strcat(tmp, "PLUS");
developerca672072023-02-24 18:01:27 +0800132 } else if (strstr(htmode, "40-") != NULL) {
developer91f80742022-10-04 15:20:18 +0800133 strncpy(tmp, htmode, strlen(htmode) - 1);
134 strcat(tmp, "MINUS");
135 } else
136 strcpy(tmp, htmode);
137
138
139 if (strstr(htmode, "VHT") != NULL) {
140 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
developerca672072023-02-24 18:01:27 +0800141 } else if (strstr(htmode, "EHT") != NULL) {
142 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11BE%s", tmp);
143 if (strstr(htmode, "320-1") != NULL)
144 radio_param->eht_320_conf = 1;
145 else if (strstr(htmode, "320") != NULL) // EHT320 or EHT320-2
146 radio_param->eht_320_conf = 2;
147 else
148 radio_param->eht_320_conf = 0;
developer91f80742022-10-04 15:20:18 +0800149 } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
150 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
151 } else if (strstr(htmode, "HE") != NULL) {
152 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
153 } else { // NOHT or NONE should be parsed with the band, so just fill the original string.
154 strcpy(radio_param->htmode, tmp);
155 }
156
157}
158
159void set_disable(wifi_radio_param *radio_param, char *disable)
160{
161 if (strcmp(disable, "1") == 0)
162 radio_param->disabled = TRUE;
163 else
164 radio_param->disabled = FALSE;
165}
166
developer128f8aa2022-12-26 17:09:00 +0800167void set_rxant(wifi_radio_param *radio_param, char *mask)
168{
169 radio_param->rxantenna = strtol(mask, NULL, 16);
170}
171
172void set_txant(wifi_radio_param *radio_param, char *mask)
173{
174 radio_param->txantenna = strtol(mask, NULL, 16);
175}
176
developera3ec58b2023-02-28 15:59:38 +0800177void set_igmpsn_enable(wifi_intf_param *intf_param, char *enable)
178{
179 if (strcmp(enable, "1") == 0)
180 intf_param->igmpsn_enable = TRUE;
181 else
182 intf_param->igmpsn_enable = FALSE;
183}
184
185void set_wps_state(wifi_intf_param *intf_param, char *enable)
186{
187 if (strcmp(enable, "1") == 0)
188 intf_param->wps_state = TRUE;
189 else
190 intf_param->wps_state = FALSE;
191}
192
193void set_wps_cancel(wifi_intf_param *intf_param, char *enable)
194{
195 if (strcmp(enable, "1") == 0)
196 intf_param->wps_cancel = TRUE;
197 else
198 intf_param->wps_cancel = FALSE;
199}
200
201void set_wps_pushbutton(wifi_intf_param *intf_param, char *enable)
202{
203 if (strcmp(enable, "1") == 0)
204 intf_param->wps_pushbutton = TRUE;
205 else
206 intf_param->wps_pushbutton = FALSE;
207}
208
209
210void set_macfilter(wifi_intf_param *intf_param, char *macfilter)
211{
212 strncpy(intf_param->macfilter, macfilter, 10);
213}
214
215void set_maclist(wifi_intf_param *intf_param, char *maclist)
216{
217 strncpy(intf_param->maclist, maclist, 512);
218}
219
developer13ec7352023-02-06 20:02:10 +0800220void set_htcoex(wifi_radio_param *radio_param, char *ht_coex)
221{
222 radio_param->ht_coex = strtol(ht_coex, NULL, 10);
223}
224
225void set_rts(wifi_radio_param *radio_param, char *rts)
226{
227 radio_param->rtsThreshold = strtol(rts, NULL, 10);
228}
229
developer465ca0c2022-11-25 14:30:05 +0800230void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800231{
developer50614832022-11-17 20:42:05 +0800232 int radio_num = 0;
233 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800234 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800235
236 while (*ptr) {
237 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800238 phyId = strtoul(ptr, NULL, 10);
239 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800240 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800241 break;
242 }
243 ptr++;
244 }
245}
246
developer465ca0c2022-11-25 14:30:05 +0800247void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800248{
developer465ca0c2022-11-25 14:30:05 +0800249 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800250}
251
developer465ca0c2022-11-25 14:30:05 +0800252void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800253{
developerff378f22022-10-13 13:33:57 +0800254 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800255 intf_param->security.mode = wifi_security_mode_none;
256 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800257 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800258 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800259 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800260 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800261 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800262 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800263 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800264 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800265 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800266 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800267 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800268 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800269 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800270 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800271 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800272 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerc6a27322023-01-13 15:23:06 +0800273 }else if(strcmp(encryption_mode, "owe") == 0){
274 intf_param->security.mode = wifi_security_mode_owe;
developer91f80742022-10-04 15:20:18 +0800275 }
276
developerff378f22022-10-13 13:33:57 +0800277 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800278 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800279 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800280 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800281 }else{
developer465ca0c2022-11-25 14:30:05 +0800282 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800283 }
developerff378f22022-10-13 13:33:57 +0800284
developerc6a27322023-01-13 15:23:06 +0800285 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer465ca0c2022-11-25 14:30:05 +0800286 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800287 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800288 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800289 }else{
developer465ca0c2022-11-25 14:30:05 +0800290 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800291 }
developer91f80742022-10-04 15:20:18 +0800292}
293
developer465ca0c2022-11-25 14:30:05 +0800294void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800295{
developer465ca0c2022-11-25 14:30:05 +0800296 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800297}
298
developer7ac3bd52022-12-13 16:19:09 +0800299void set_ifname(wifi_intf_param *intf_param, char *ifname)
300{
301 if (strlen(ifname) > 15)
302 return;
303 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
304}
305
developerb2123782022-12-29 09:35:55 +0800306void set_wds(wifi_intf_param *intf_param, char *wds_enable)
307{
308 intf_param->wds_mode = FALSE;
309 if (strncmp(wds_enable, "1", 1) == 0)
310 intf_param->wds_mode = TRUE;
311}
312
developer13ec7352023-02-06 20:02:10 +0800313void set_hidden(wifi_intf_param *intf_param, char *hidden)
314{
315 intf_param->hidden = strtol(hidden, NULL, 10);
316}
317
developer50614832022-11-17 20:42:05 +0800318int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800319{
320 FILE *f;
321 char mac_file[64] = {0};
322 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800323
developer1ac426a2022-12-22 19:44:36 +0800324 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800325 f = fopen(mac_file, "r");
326 if (f == NULL)
327 return -1;
328 fgets(mac_address, 20, f);
329 fclose(f);
330
developer50614832022-11-17 20:42:05 +0800331 mac_addr_aton(&(*bssid)[0], mac_address);
332 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800333 return 0;
334}
335
developer91f80742022-10-04 15:20:18 +0800336void set_radio_param(wifi_radio_param radio_parameter)
337{
developer91f80742022-10-04 15:20:18 +0800338 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800339 wifi_radio_operationParam_t operationParam = {0};
340
developer8d8d6302022-10-18 16:36:37 +0800341 if(radio_parameter.radio_index == -1)
342 return;
343
developer63d72772022-10-07 09:42:31 +0800344 if (radio_parameter.disabled == TRUE) {
345 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
346 return;
347 }
developer91f80742022-10-04 15:20:18 +0800348
349 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800350
developer128f8aa2022-12-26 17:09:00 +0800351 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
352 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
353 if (ret != RETURN_OK)
354 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
355 }
356
developerbf812932022-10-17 17:37:29 +0800357 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800358 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
359 if (ret != RETURN_OK)
360 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800361 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800362
developer91f80742022-10-04 15:20:18 +0800363 // Channel
developer63d72772022-10-07 09:42:31 +0800364 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
365 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800366
developerca672072023-02-24 18:01:27 +0800367 // bandwidth
developer25e07812022-10-13 15:27:02 +0800368 if (radio_parameter.bandwidth == 20){
369 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
370 }else if (radio_parameter.bandwidth == 40){
371 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
372 }else if (radio_parameter.bandwidth == 80){
373 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
374 }else if (radio_parameter.bandwidth == 160){
375 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
developerca672072023-02-24 18:01:27 +0800376 }else if (radio_parameter.bandwidth == 320){
377 if ((radio_parameter.eht_320_conf == 1 || radio_parameter.channel <= 29) && radio_parameter.channel < 193)
378 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_1MHZ;
379 else if (radio_parameter.eht_320_conf == 2 || radio_parameter.channel >= 193)
380 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_2MHZ;
381 else
382 fprintf(stderr, "[Set EHT 320 bandwidth error with conf %d!!!]\n", radio_parameter.eht_320_conf);
developer25e07812022-10-13 15:27:02 +0800383 }
developer91f80742022-10-04 15:20:18 +0800384
385 // htmode
developer63d72772022-10-07 09:42:31 +0800386 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800387 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800388 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800389 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
390 strcpy(radio_parameter.htmode, "11G");
developerca672072023-02-24 18:01:27 +0800391 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developer63d72772022-10-07 09:42:31 +0800392 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developerca672072023-02-24 18:01:27 +0800393 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
394 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
395 else if (strstr(radio_parameter.htmode, "HT") != NULL)
396 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800397
developer63d72772022-10-07 09:42:31 +0800398 } else if (strcmp(radio_parameter.band, "5g") == 0) {
399 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800400 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
401 strcpy(radio_parameter.htmode, "11A");
developerca672072023-02-24 18:01:27 +0800402 else if (strstr(radio_parameter.htmode, "VHT") != NULL)
403 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
404 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developer63d72772022-10-07 09:42:31 +0800405 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developerca672072023-02-24 18:01:27 +0800406 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
407 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
408 else if (strstr(radio_parameter.htmode, "HT") != NULL)
409 mode |= WIFI_80211_VARIANT_N;
410 } else if (strcmp(radio_parameter.band, "6g") == 0) {
411 mode |= WIFI_80211_VARIANT_AX;
412 if (strstr(radio_parameter.htmode, "EHT") != NULL)
413 mode |= WIFI_80211_VARIANT_BE;
414 }
developer91f80742022-10-04 15:20:18 +0800415
developer63d72772022-10-07 09:42:31 +0800416 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800417
developer13ec7352023-02-06 20:02:10 +0800418 // rtsThreshold, zero means not set
419 if ((radio_parameter.rtsThreshold < 65535) && radio_parameter.rtsThreshold)
420 operationParam.rtsThreshold = radio_parameter.rtsThreshold;
421
422 //ht_coex
423 operationParam.obssCoex = radio_parameter.ht_coex;
424
developer63d72772022-10-07 09:42:31 +0800425 // apply setting
426 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800427 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800428 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800429
developerbb58a932022-11-07 16:58:17 +0800430 // Country
431 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
432 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
433 if (ret != RETURN_OK)
434 fprintf(stderr, "[Set Country failed!!!]\n");
435 ret = 0;
436
437 // hwmode
438 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
439 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
440 if (ret != RETURN_OK)
441 fprintf(stderr, "[Set hwmode failed!!!]\n");
442 ret = 0;
443
444 // noscan
445 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
446 if(strlen(radio_parameter.noscan)){
447 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
448 if (ret != RETURN_OK)
449 fprintf(stderr, "[Set noscan failed!!!]\n");
450 }
451 ret = 0;
452
developer91f80742022-10-04 15:20:18 +0800453}
454
developer465ca0c2022-11-25 14:30:05 +0800455void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800456{
457 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800458 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800459 int phy_index = 0;
developerbaf61262023-02-17 15:52:18 +0800460 int key_len = 0;
developer63d72772022-10-07 09:42:31 +0800461 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800462 BOOL radio_enable = FALSE;
463
developerff42a302022-10-19 17:40:23 +0800464 if(ap_param.radio_index == -1)
465 return;
466
developerbf812932022-10-17 17:37:29 +0800467 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
468 if (radio_enable == FALSE)
469 return;
developer63d72772022-10-07 09:42:31 +0800470
developerff42a302022-10-19 17:40:23 +0800471
472 // get the index of the map
developerd9bf1dd2023-02-22 15:49:20 +0800473 for (int i = 0; i < MAX_NUM_VAP_PER_RADIO; i++) {
developerff42a302022-10-19 17:40:23 +0800474 if (map->vap_array[i].vap_index == ap_param.ap_index) {
475 vap_index_in_map = i;
476 break;
developer63d72772022-10-07 09:42:31 +0800477 }
478 }
479
developerff42a302022-10-19 17:40:23 +0800480
developerd9bf1dd2023-02-22 15:49:20 +0800481 fprintf(stderr, "Start setting ap vap_index_in_map=%d\n", vap_index_in_map);
developerf7e50b02022-10-14 10:07:58 +0800482
developerff42a302022-10-19 17:40:23 +0800483 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800484 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800485 phy_index = radio_index_to_phy(vap_info.radio_index);
486 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800487 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800488 return;
developerf7e50b02022-10-14 10:07:58 +0800489 }
developer91f80742022-10-04 15:20:18 +0800490
developer91f80742022-10-04 15:20:18 +0800491 // SSID
developer63d72772022-10-07 09:42:31 +0800492 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
493 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800494
developer7ac3bd52022-12-13 16:19:09 +0800495 // interface
496 if (strlen(ap_param.ifname) != 0) {
497 strncpy(vap_info.vap_name, ap_param.ifname, 16);
498 vap_info.vap_name[15] = "\0";
499 }
500
developerbaf61262023-02-17 15:52:18 +0800501 // Security
502 if (ap_param.security.mode == wifi_security_mode_wpa3_personal || ap_param.security.mode == wifi_security_mode_wpa3_transition){
503 // OpenWrt script only set psk, here we choose to set both psk and sae.
504 ap_param.security.u.key.type = wifi_security_key_type_psk_sae;
505 } else {
506 key_len = strlen(ap_param.security.u.key.key);
507 if (key_len == 64)
508 ap_param.security.u.key.type = wifi_security_key_type_psk;
509 else if (key_len >= 8 && key_len < 64)
510 ap_param.security.u.key.type = wifi_security_key_type_pass;
511 }
512
developerff378f22022-10-13 13:33:57 +0800513 vap_info.u.bss_info.security.mode = ap_param.security.mode;
514 vap_info.u.bss_info.security.encr = ap_param.security.encr;
515 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
516 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
517 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer13ec7352023-02-06 20:02:10 +0800518
519 // hidden
520 vap_info.u.bss_info.showSsid = (ap_param.hidden ? 0 : 1);
developer8d8d6302022-10-18 16:36:37 +0800521
developera3ec58b2023-02-28 15:59:38 +0800522 // igmpsn_enable
523 vap_info.u.bss_info.mcast2ucast = ap_param.igmpsn_enable;
524 fprintf(stderr, "Set igmpsn_enable: %d \n", ap_param.igmpsn_enable);
525
526 // wps_state
527 fprintf(stderr, "Set wps_state: %d \n", ap_param.wps_state);
528 ret = wifi_setApWpsEnable(ap_param.ap_index, ap_param.wps_state);
529 if (ret != RETURN_OK)
530 fprintf(stderr, "[Set wps_state failed!!!]\n");
531 ret = 0;
532
533 // wps_cancel
534 fprintf(stderr, "Set wps_cancel: %d \n", ap_param.wps_cancel);
535 if (ap_param.wps_cancel){
536 ret = wifi_cancelApWPS(ap_param.ap_index);
537 if (ret != RETURN_OK)
538 fprintf(stderr, "[Set wps_cancel failed!!!]\n");
539 ret = 0;
540 }
541
542 // wps_pushbutton
543 fprintf(stderr, "Set wps_pushbutton: %d \n", ap_param.wps_pushbutton);
544 if (ap_param.wps_pushbutton){
545 ret = wifi_setApWpsButtonPush(ap_param.ap_index);
546 if (ret != RETURN_OK)
547 fprintf(stderr, "[Set wps_pushbutton failed!!!]\n");
548 ret = 0;
549 }
550
551 // macfilter
552 if ((strcmp(ap_param.macfilter, "disable") == 0) || (strcmp(ap_param.macfilter, "\0") == 0) )
553 vap_info.u.bss_info.mac_filter_enable = false;
554 else if (strcmp(ap_param.macfilter, "deny") == 0){
555 vap_info.u.bss_info.mac_filter_enable = true;
556 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
557 }
558 else if (strcmp(ap_param.macfilter, "allow") == 0){
559 vap_info.u.bss_info.mac_filter_enable = true;
560 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
561 }
562 else
563 fprintf(stderr, "The macfilter tpye: %s is invalid!!!\n", ap_param.macfilter);
564 fprintf(stderr, "Set macfilter: %s \n", ap_param.macfilter);
565
566 // maclist
567 if ((strcmp(ap_param.macfilter, "\0") == 0)){
568 ret = wifi_delApAclDevices(ap_param.ap_index);
569 if (ret != RETURN_OK)
570 fprintf(stderr, "[Del all maclist failed!!!]\n");
571 ret = 0;
572 }
573 else{
574 ret = wifi_addApAclDevice(ap_param.ap_index, ap_param.maclist);
575 if (ret != RETURN_OK)
576 fprintf(stderr, "[Add maclist failed!!!]\n");
577 ret = 0;
578 }
579 fprintf(stderr, "Set maclist: %s \n", ap_param.maclist);
580
581 ret = 0;
developer63d72772022-10-07 09:42:31 +0800582 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800583 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800584}
585
developer465ca0c2022-11-25 14:30:05 +0800586void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800587{
588 wifi_sta_network_t *sta = NULL;
589 mac_address_t sta_mac = {0};
590 char sta_mac_str[20] = {0};
591 char key_mgmt[16] = {0};
592 char pairwise[16] = {0};
593 int phy_index = 0;
594
595 sta = calloc(1, sizeof(wifi_sta_network_t));
596
597 phy_index = radio_index_to_phy(sta_param.radio_index);
598 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
599 mac_addr_ntoa(sta_mac_str, sta_mac);
600 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
601 sta->ssid[31] = '\0';
602 snprintf(sta->psk, 64, "%s", sta_param.password);
603
604 if (sta_param.security.mode == wifi_security_mode_none)
605 strcpy(key_mgmt, "NONE");
606 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
607 strcpy(key_mgmt, "SAE");
developerc6a27322023-01-13 15:23:06 +0800608 else if (sta_param.security.mode == wifi_security_mode_owe)
609 strcpy(key_mgmt, "OWE");
developer50614832022-11-17 20:42:05 +0800610 else
611 strcpy(key_mgmt, "WPA-PSK");
612 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
613
614 if (sta_param.security.encr == wifi_encryption_aes)
615 strcpy(pairwise, "CCMP");
616 else if (sta_param.security.encr == wifi_encryption_tkip)
617 strcpy(pairwise, "TKIP");
618 else
619 strcpy(pairwise, "CCMP TKIP");
620 snprintf(sta->pairwise, 64, "%s", pairwise);
621
622 if (strlen(sta_param.security.u.key.key) > 0)
623 strncpy(sta->psk, sta_param.security.u.key.key, 127);
624 sta->psk[127] = '\0';
625 sta->psk_len = strlen(sta->psk);
626
developerb2123782022-12-29 09:35:55 +0800627 if (sta_param.wds_mode == TRUE)
628 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
629
630 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer50614832022-11-17 20:42:05 +0800631
632 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
633 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
634 free(sta);
635 return;
636 }
637 free(sta);
638
639 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
640 fprintf(stderr, "Enable station failed\n");
641 return;
642 }
643}
644
developera3ec58b2023-02-28 15:59:38 +0800645void show_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
646{
647 int ret = 0;
648 int vap_index_in_map = 0;
649 int phy_index = 0;
650 wifi_vap_info_t vap_info = {0};
651 BOOL radio_enable = FALSE;
652 BOOL wps_state = FALSE;
653 UINT buf_size = 1024;
654 char macArray[1024] = "";
655
656 if(ap_param.radio_index == -1)
657 return;
658
659 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
660 if (radio_enable == FALSE)
661 return;
662
663
664 // get the index of the map
665 for (int i = 0; i < map->num_vaps; i++) {
666 if (map->vap_array[i].vap_index == ap_param.ap_index) {
667 vap_index_in_map = i;
668 break;
669 }
670 }
671
672 vap_info = map->vap_array[vap_index_in_map];
673 vap_info.u.bss_info.enabled = TRUE;
674 phy_index = radio_index_to_phy(vap_info.radio_index);
675
676 // SSID
677 printf("wifi%d ssid: %s\n", ap_param.ap_index, vap_info.u.bss_info.ssid);
678
679 // igmpsn_enable
680 printf("wifi%d igmpsn_enable: %d\n", ap_param.ap_index, vap_info.u.bss_info.mcast2ucast);
681
682 // wps_state
683 ret = wifi_getApWpsEnable(ap_param.ap_index, &wps_state);
684 if (ret != RETURN_OK)
685 fprintf(stderr, "[Set wps_state failed!!!]\n");
686 else
687 printf("wifi%d wps_state: %d\n", ap_param.ap_index, wps_state);
688 ret = 0;
689
690 // macfilter
691 if (vap_info.u.bss_info.mac_filter_enable == FALSE)
692 printf("wifi%d macfilter: disable\n", ap_param.ap_index);
693 else if (vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list)
694 printf("wifi%d macfilter: allow\n", ap_param.ap_index);
695 else if (vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list)
696 printf("wifi%d macfilter: deny\n", ap_param.ap_index);
697
698 printf("daisy wifi%d macfilter: %d\n", ap_param.ap_index, vap_info.u.bss_info.mac_filter_enable);
699 printf("daisy wifi%d macfilter: %d\n", ap_param.ap_index, vap_info.u.bss_info.mac_filter_mode);
700
701 // maclist
702 printf("wifi%d maclist: \n", ap_param.ap_index);
703 ret = wifi_getApAclDevices(ap_param.ap_index, macArray, buf_size);
704 if (ret != RETURN_OK)
705 fprintf(stderr, "[Get maclist failed!!!]\n");
706 else
707 printf("%s \n", macArray);
708 ret = 0;
709
710}
711
developer91f80742022-10-04 15:20:18 +0800712int apply_uci_config ()
713{
714 struct uci_context *uci_ctx = uci_alloc_context();
715 struct uci_package *uci_pkg = NULL;
716 struct uci_element *e;
717 // struct uci_section *s;
718 const char cfg_name[] = "wireless";
719 int max_radio_num = 0;
720 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800721 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800722 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800723 wifi_vap_info_map_t vap_map[3] = {0};
724 int ret = 0;
developerd9bf1dd2023-02-22 15:49:20 +0800725 int i = 0, j = 0;
developer91f80742022-10-04 15:20:18 +0800726
727 wifi_getMaxRadioNumber(&max_radio_num);
728 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800729 for (i = 0; i < max_radio_num ;i++ ){
730 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
731 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
732 fprintf(stderr, "[Get vap map failed!!!]\n");
developerff42a302022-10-19 17:40:23 +0800733 }
developerd9bf1dd2023-02-22 15:49:20 +0800734
735 /*reset radio's ap number, ap number ++ by uci config */
736 vap_map[i].num_vaps = 0;
developerff42a302022-10-19 17:40:23 +0800737 }
developer91f80742022-10-04 15:20:18 +0800738 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
739 uci_free_context(uci_ctx);
740 fprintf(stderr, "%s: load uci failed.\n", __func__);
741 return RETURN_ERR;
742 }
743
744 uci_foreach_element(&uci_pkg->sections, e) {
745
746 struct uci_section *s = uci_to_section(e);
747 struct uci_element *option = NULL;
748 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800749 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800750 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800751 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800752 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800753
754 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800755 sscanf(s->e.name, "radio%d", &phyId);
756 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800757 parsing_radio = TRUE;
758 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
759 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800760 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800761 }
762
763 uci_foreach_element(&s->options, option) {
764
765 struct uci_option *op = uci_to_option(option);
766 if (parsing_radio == TRUE) {
767 // transform the type from input string and store the value in radio_param.
768 if (strcmp(op->e.name, "channel") == 0)
769 set_channel(&radio_param, op->v.string);
770 else if (strcmp(op->e.name, "hwmode") == 0)
771 set_hwmode(&radio_param, op->v.string);
772 else if (strcmp(op->e.name, "htmode") == 0)
773 set_htmode(&radio_param, op->v.string);
774 else if (strcmp(op->e.name, "disabled") == 0)
775 set_disable(&radio_param, op->v.string);
776 else if (strcmp(op->e.name, "band") == 0)
777 set_band(&radio_param, op->v.string);
778 else if (strcmp(op->e.name, "country") == 0)
779 set_country(&radio_param, op->v.string);
780 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800781 set_noscan(&radio_param, op->v.string);
developer128f8aa2022-12-26 17:09:00 +0800782 else if (strcmp(op->e.name, "rxantenna") == 0)
783 set_rxant(&radio_param, op->v.string);
784 else if (strcmp(op->e.name, "txantenna") == 0)
785 set_txant(&radio_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800786 else if (strcmp(op->e.name, "ht_coex") == 0)
787 set_htcoex(&radio_param, op->v.string);
788 else if (strcmp(op->e.name, "rts") == 0)
789 set_rts(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800790 else
791 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
792 } else {
793 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800794 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800795 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800796 }else if (strcmp(op->e.name, "mode") == 0){
developer1ac426a2022-12-22 19:44:36 +0800797 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer50614832022-11-17 20:42:05 +0800798 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800799 intf_param.sta_mode = TRUE;
800 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
801 staCount[intf_param.radio_index] ++ ;
802 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800803 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800804 intf_param.sta_mode = FALSE;
805 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
806 apCount[intf_param.radio_index] ++ ;
807 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800808 }
developer8d8d6302022-10-18 16:36:37 +0800809 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800810 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800811 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800812 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800813 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800814 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800815 }else if (strcmp(op->e.name, "ifname") == 0){
816 set_ifname(&intf_param, op->v.string);
developerb2123782022-12-29 09:35:55 +0800817 }else if (strcmp(op->e.name, "wds") == 0){
818 set_wds(&intf_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800819 }else if (strcmp(op->e.name, "hidden") == 0){
820 set_hidden(&intf_param, op->v.string);
developera3ec58b2023-02-28 15:59:38 +0800821 }else if (strcmp(op->e.name, "igmpsn_enable") == 0){
822 set_igmpsn_enable(&intf_param, op->v.string);
823 }else if (strcmp(op->e.name, "wps_state") == 0){
824 set_wps_state(&intf_param, op->v.string);
825 }else if (strcmp(op->e.name, "wps_cancel") == 0){
826 set_wps_cancel(&intf_param, op->v.string);
827 }else if (strcmp(op->e.name, "wps_pushbutton") == 0){
828 set_wps_pushbutton(&intf_param, op->v.string);
829 }else if (strcmp(op->e.name, "macfilter") == 0){
830 set_macfilter(&intf_param, op->v.string);
831 }else if (strcmp(op->e.name, "maclist") == 0){
832 set_maclist(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800833 }else{
developer91f80742022-10-04 15:20:18 +0800834 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developera3ec58b2023-02-28 15:59:38 +0800835 }
developer91f80742022-10-04 15:20:18 +0800836 }
837 }
developerd9bf1dd2023-02-22 15:49:20 +0800838 if (parsing_radio == TRUE) {
839 char cmd[32] = {0};
840 char buf[32] = {0};
developer91f80742022-10-04 15:20:18 +0800841 set_radio_param(radio_param);
developerd9bf1dd2023-02-22 15:49:20 +0800842
843 /* sleep 3 to wait ifconfig up state update, workaround to avoid
844 disable ap interface fail. */
845 sprintf(cmd, "sleep 5");
846 _syscmd(cmd, buf, sizeof(buf));
847 }
developer465ca0c2022-11-25 14:30:05 +0800848 else if (intf_param.sta_mode == TRUE)
849 set_sta_param(intf_param);
developerd9bf1dd2023-02-22 15:49:20 +0800850 else {
developer465ca0c2022-11-25 14:30:05 +0800851 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developerd9bf1dd2023-02-22 15:49:20 +0800852 vap_map[intf_param.radio_index].num_vaps++;
853 }
developer91f80742022-10-04 15:20:18 +0800854 }
developerd9bf1dd2023-02-22 15:49:20 +0800855
856 /* disable all interface, re-enable by UCI configuration */
857
858 fprintf(stderr, "\n----- Disable all interfaces. -----\n");
859
860 for (i = 0; i < max_radio_num; i++ ){
861 for(j = MAX_NUM_VAP_PER_RADIO - 1; j >= 0 ; j--) {
862 ret = wifi_setApEnable(i*max_radio_num+j, FALSE);
863 if (ret != RETURN_OK)
864 fprintf(stderr, "[disable ap %d failed!!!]\n", i*max_radio_num+j);
865 }
866 }
developer50614832022-11-17 20:42:05 +0800867 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800868
developerd9bf1dd2023-02-22 15:49:20 +0800869 for (i = 0; i < max_radio_num ;i++ ){
870
871 fprintf(stderr, "\n create Vap radio:%d num_vaps=%d.\n", i, vap_map[i].num_vaps);
developerff42a302022-10-19 17:40:23 +0800872 ret = wifi_createVAP(i, &vap_map[i]);
873 if (ret != RETURN_OK)
874 fprintf(stderr, "[Apply vap setting failed!!!]\n");
875 }
developera3ec58b2023-02-28 15:59:38 +0800876
developer91f80742022-10-04 15:20:18 +0800877 uci_unload(uci_ctx, uci_pkg);
878 uci_free_context(uci_ctx);
879 return RETURN_OK;
880}
881
developera3ec58b2023-02-28 15:59:38 +0800882int dump_wifi_status ()
883{
884
885 struct uci_context *uci_ctx = uci_alloc_context();
886 struct uci_package *uci_pkg = NULL;
887 struct uci_element *e;
888 // struct uci_section *s;
889 const char cfg_name[] = "wireless";
890 int max_radio_num = 0;
891 BOOL parsing_radio = FALSE;
892 int apCount[3] = {0};
893 int staCount[3] = {0};
894 wifi_vap_info_map_t vap_map[3] = {0};
895 int ret = 0;
896 int i = 0;
897
898 wifi_getMaxRadioNumber(&max_radio_num);
899 fprintf(stderr, "max radio number: %d\n", max_radio_num);
900 for (i = 0; i < max_radio_num ;i++ ){
901 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
902 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
903 fprintf(stderr, "[Get vap map failed!!!]\n");
904 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
905 }
906 }
907 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
908 uci_free_context(uci_ctx);
909 fprintf(stderr, "%s: load uci failed.\n", __func__);
910 return RETURN_ERR;
911 }
912
913 uci_foreach_element(&uci_pkg->sections, e) {
914
915 struct uci_section *s = uci_to_section(e);
916 struct uci_element *option = NULL;
917 wifi_radio_param radio_param = {0};
918 wifi_intf_param intf_param = {0};
919 int phyId = 0;
920 radio_param.radio_index = -1;
921 intf_param.ap_index = -1;
922
923 if (strcmp(s->type, "wifi-device") == 0) {
924 sscanf(s->e.name, "radio%d", &phyId);
925 radio_param.radio_index = phy_index_to_radio(phyId);
926 parsing_radio = TRUE;
927 fprintf(stderr, "\n----- Start show radio %d config. -----\n", radio_param.radio_index);
928 } else if (strcmp(s->type, "wifi-iface") == 0) {
929 parsing_radio = FALSE;
930 }
931
932 uci_foreach_element(&s->options, option) {
933
934 struct uci_option *op = uci_to_option(option);
935 if (parsing_radio == TRUE) {
936 // transform the type from input string and store the value in radio_param.
937 } else {
938 // parsing iface
939 if (strcmp(op->e.name, "device") == 0){
940 set_radionum(&intf_param, op->v.string);
941 }else if (strcmp(op->e.name, "mode") == 0){
942 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
943 if (strncmp(op->v.string, "sta", 3) == 0) {
944 intf_param.sta_mode = TRUE;
945 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
946 staCount[intf_param.radio_index] ++ ;
947 fprintf(stderr, "\n----- Start show sta %d config. -----\n", intf_param.sta_index);
948 } else if (strncmp(op->v.string, "ap", 2) == 0) {
949 intf_param.sta_mode = FALSE;
950 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
951 apCount[intf_param.radio_index] ++ ;
952 fprintf(stderr, "\n----- Start show ap %d config. -----\n", intf_param.ap_index);
953 }
954 }
955 }
956 }
957 if (parsing_radio == TRUE)
958 printf("show radio params: \n");
959 // show_radio_param(radio_param);
960 else if (intf_param.sta_mode == TRUE)
961 printf("show sta params: \n");
962 // show_sta_param(intf_param);
963 else{
964 printf("show ap params: \n");
965 show_ap_param(intf_param, &vap_map[intf_param.radio_index]);
966 }
967 }
968
969 uci_unload(uci_ctx, uci_pkg);
970 uci_free_context(uci_ctx);
971 return RETURN_OK;
972
973}
974
developer91f80742022-10-04 15:20:18 +0800975int main(int argc, char **argv)
976{
developera3ec58b2023-02-28 15:59:38 +0800977 if (argc != 2 || (strcmp(argv[1], "reload") != 0 && strcmp(argv[1], "dump") != 0) ){
978 fprintf(stderr, "Usage: wifi reload/wifi dump.\nThis tool is only for RDKB MSP/SQC test.\n");
developer91f80742022-10-04 15:20:18 +0800979 return -1;
980 }
developera3ec58b2023-02-28 15:59:38 +0800981 if (strcmp(argv[1], "reload") == 0)
982 apply_uci_config();
983 else if (strcmp(argv[1], "dump") == 0)
984 dump_wifi_status();
developer91f80742022-10-04 15:20:18 +0800985 return 0;
986}