blob: 2c79075222c4bfe76faa7bb5a117511fa6214100 [file] [log] [blame]
developer402bf2a2022-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
developer9567a462022-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}
developer4c2edbe2022-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);
developerf634eab2023-01-13 15:23:06 +080069 return radioIndex;
developer4c2edbe2022-10-18 16:36:37 +080070}
71
developer402bf2a2022-10-04 15:20:18 +080072void set_channel(wifi_radio_param *radio_param, char *channel)
73{
developerab985802022-10-07 09:42:31 +080074 if (strcmp(channel, "auto") == 0) {
75 radio_param->auto_channel = TRUE;
76 radio_param->channel = 0;
77 } else {
developer402bf2a2022-10-04 15:20:18 +080078 radio_param->auto_channel = FALSE;
developerab985802022-10-07 09:42:31 +080079 radio_param->channel = strtol(channel, NULL, 10);
developer402bf2a2022-10-04 15:20:18 +080080 }
developerab985802022-10-07 09:42:31 +080081 return;
developer402bf2a2022-10-04 15:20:18 +080082}
83
developer02a4a1b2022-10-06 17:16:43 +080084void set_country(wifi_radio_param *radio_param, char *country)
developer402bf2a2022-10-04 15:20:18 +080085{
86 strcpy(radio_param->country, country);
87}
88
developer02a4a1b2022-10-06 17:16:43 +080089void set_band(wifi_radio_param *radio_param, char *band)
developer402bf2a2022-10-04 15:20:18 +080090{
91 strcpy(radio_param->band, band);
92}
93
developer54afa2c2022-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
developer402bf2a2022-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
developer02cc9292023-02-24 18:01:27 +0800128 // EHT320-1 -> 11BEEHT320-1
129 if (strstr(htmode, "40+") != NULL) {
developer402bf2a2022-10-04 15:20:18 +0800130 strncpy(tmp, htmode, strlen(htmode) - 1);
131 strcat(tmp, "PLUS");
developer02cc9292023-02-24 18:01:27 +0800132 } else if (strstr(htmode, "40-") != NULL) {
developer402bf2a2022-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);
developer02cc9292023-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;
developer402bf2a2022-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
developer1e946fd2022-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
developer223f5982023-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
developer0dff3a92023-03-17 15:08:42 +0800185void set_wps_state(wifi_intf_param *intf_param, char *wps_state)
developer223f5982023-02-28 15:59:38 +0800186{
developer0dff3a92023-03-17 15:08:42 +0800187 intf_param->wps_state = strtol(wps_state, NULL, 10);
developer223f5982023-02-28 15:59:38 +0800188}
189
190void set_wps_cancel(wifi_intf_param *intf_param, char *enable)
191{
192 if (strcmp(enable, "1") == 0)
193 intf_param->wps_cancel = TRUE;
194 else
195 intf_param->wps_cancel = FALSE;
196}
197
198void set_wps_pushbutton(wifi_intf_param *intf_param, char *enable)
199{
200 if (strcmp(enable, "1") == 0)
201 intf_param->wps_pushbutton = TRUE;
202 else
203 intf_param->wps_pushbutton = FALSE;
204}
205
206
207void set_macfilter(wifi_intf_param *intf_param, char *macfilter)
208{
209 strncpy(intf_param->macfilter, macfilter, 10);
210}
211
212void set_maclist(wifi_intf_param *intf_param, char *maclist)
213{
214 strncpy(intf_param->maclist, maclist, 512);
215}
216
developerc4548732023-02-06 20:02:10 +0800217void set_htcoex(wifi_radio_param *radio_param, char *ht_coex)
218{
219 radio_param->ht_coex = strtol(ht_coex, NULL, 10);
220}
221
222void set_rts(wifi_radio_param *radio_param, char *rts)
223{
224 radio_param->rtsThreshold = strtol(rts, NULL, 10);
225}
226
developer1ac0e892022-11-25 14:30:05 +0800227void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer402bf2a2022-10-04 15:20:18 +0800228{
developer9567a462022-11-17 20:42:05 +0800229 int radio_num = 0;
230 char *ptr = phy_name;
developer4c2edbe2022-10-18 16:36:37 +0800231 int phyId = 0;
developer402bf2a2022-10-04 15:20:18 +0800232
233 while (*ptr) {
234 if (isdigit(*ptr)) {
developer9567a462022-11-17 20:42:05 +0800235 phyId = strtoul(ptr, NULL, 10);
236 radio_num = phy_index_to_radio(phyId);
developer1ac0e892022-11-25 14:30:05 +0800237 intf_param->radio_index = radio_num;
developer402bf2a2022-10-04 15:20:18 +0800238 break;
239 }
240 ptr++;
241 }
242}
243
developer1ac0e892022-11-25 14:30:05 +0800244void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer402bf2a2022-10-04 15:20:18 +0800245{
developer1ac0e892022-11-25 14:30:05 +0800246 strncpy(intf_param->ssid, ssid, 32);
developer402bf2a2022-10-04 15:20:18 +0800247}
248
developer1ac0e892022-11-25 14:30:05 +0800249void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer402bf2a2022-10-04 15:20:18 +0800250{
developer3ddad2f2022-10-13 13:33:57 +0800251 if (strcmp(encryption_mode, "none") == 0) {
developer1ac0e892022-11-25 14:30:05 +0800252 intf_param->security.mode = wifi_security_mode_none;
253 intf_param->security.encr = wifi_encryption_none;
developer3ddad2f2022-10-13 13:33:57 +0800254 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800255 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developer3ddad2f2022-10-13 13:33:57 +0800256 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800257 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developer3ddad2f2022-10-13 13:33:57 +0800258 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer1ac0e892022-11-25 14:30:05 +0800259 intf_param->security.mode = wifi_security_mode_wpa_personal;
developer3ddad2f2022-10-13 13:33:57 +0800260 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800261 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800262 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800263 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800264 }else if(strcmp(encryption_mode, "sae") == 0){
developer1ac0e892022-11-25 14:30:05 +0800265 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developer3ddad2f2022-10-13 13:33:57 +0800266 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer1ac0e892022-11-25 14:30:05 +0800267 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800268 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer1ac0e892022-11-25 14:30:05 +0800269 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerf634eab2023-01-13 15:23:06 +0800270 }else if(strcmp(encryption_mode, "owe") == 0){
developer53364a72023-04-24 10:59:12 +0800271 intf_param->security.mode = wifi_security_mode_enhanced_open;
developer402bf2a2022-10-04 15:20:18 +0800272 }
273
developer3ddad2f2022-10-13 13:33:57 +0800274 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer1ac0e892022-11-25 14:30:05 +0800275 intf_param->security.encr = wifi_encryption_aes_tkip;
developer3ddad2f2022-10-13 13:33:57 +0800276 }else if (strstr(encryption_mode, "tkip")){
developer1ac0e892022-11-25 14:30:05 +0800277 intf_param->security.encr = wifi_encryption_tkip;
developer3ddad2f2022-10-13 13:33:57 +0800278 }else{
developer1ac0e892022-11-25 14:30:05 +0800279 intf_param->security.encr = wifi_encryption_aes;
developer402bf2a2022-10-04 15:20:18 +0800280 }
developer3ddad2f2022-10-13 13:33:57 +0800281
developerf634eab2023-01-13 15:23:06 +0800282 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer1ac0e892022-11-25 14:30:05 +0800283 intf_param->security.mfp = wifi_mfp_cfg_required;
developer3ddad2f2022-10-13 13:33:57 +0800284 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer1ac0e892022-11-25 14:30:05 +0800285 intf_param->security.mfp = wifi_mfp_cfg_optional;
developer3ddad2f2022-10-13 13:33:57 +0800286 }else{
developer1ac0e892022-11-25 14:30:05 +0800287 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developer3ddad2f2022-10-13 13:33:57 +0800288 }
developer402bf2a2022-10-04 15:20:18 +0800289}
290
developer1ac0e892022-11-25 14:30:05 +0800291void set_key(wifi_intf_param *intf_param, char *key)
developer402bf2a2022-10-04 15:20:18 +0800292{
developer1ac0e892022-11-25 14:30:05 +0800293 strncpy(intf_param->security.u.key.key, key, 64);
developer402bf2a2022-10-04 15:20:18 +0800294}
295
developer2bcdef92022-12-13 16:19:09 +0800296void set_ifname(wifi_intf_param *intf_param, char *ifname)
297{
298 if (strlen(ifname) > 15)
299 return;
300 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
301}
302
developerb3f00662022-12-29 09:35:55 +0800303void set_wds(wifi_intf_param *intf_param, char *wds_enable)
304{
305 intf_param->wds_mode = FALSE;
306 if (strncmp(wds_enable, "1", 1) == 0)
307 intf_param->wds_mode = TRUE;
308}
309
developerc4548732023-02-06 20:02:10 +0800310void set_hidden(wifi_intf_param *intf_param, char *hidden)
311{
312 intf_param->hidden = strtol(hidden, NULL, 10);
313}
314
developer9567a462022-11-17 20:42:05 +0800315int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf9b2ef02022-10-14 10:07:58 +0800316{
317 FILE *f;
318 char mac_file[64] = {0};
319 char mac_address[20] = {0};
developerf9b2ef02022-10-14 10:07:58 +0800320
developerb97b5492022-12-22 19:44:36 +0800321 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf9b2ef02022-10-14 10:07:58 +0800322 f = fopen(mac_file, "r");
323 if (f == NULL)
324 return -1;
325 fgets(mac_address, 20, f);
326 fclose(f);
327
developer9567a462022-11-17 20:42:05 +0800328 mac_addr_aton(&(*bssid)[0], mac_address);
329 (*bssid)[0] += offset*2;
developerf9b2ef02022-10-14 10:07:58 +0800330 return 0;
331}
332
developer402bf2a2022-10-04 15:20:18 +0800333void set_radio_param(wifi_radio_param radio_parameter)
334{
developer402bf2a2022-10-04 15:20:18 +0800335 int ret = 0;
developerab985802022-10-07 09:42:31 +0800336 wifi_radio_operationParam_t operationParam = {0};
337
developer4c2edbe2022-10-18 16:36:37 +0800338 if(radio_parameter.radio_index == -1)
339 return;
340
developerab985802022-10-07 09:42:31 +0800341 if (radio_parameter.disabled == TRUE) {
342 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
343 return;
344 }
developer402bf2a2022-10-04 15:20:18 +0800345
346 fprintf(stderr, "Start setting radio\n");
developer18615812022-10-17 17:37:29 +0800347
developer1e946fd2022-12-26 17:09:00 +0800348 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
349 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
350 if (ret != RETURN_OK)
351 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
352 }
353
developer18615812022-10-17 17:37:29 +0800354 // Get current radio setting
developerab985802022-10-07 09:42:31 +0800355 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
356 if (ret != RETURN_OK)
357 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developer18615812022-10-17 17:37:29 +0800358 operationParam.enable = TRUE;
developer402bf2a2022-10-04 15:20:18 +0800359
developer402bf2a2022-10-04 15:20:18 +0800360 // Channel
developerab985802022-10-07 09:42:31 +0800361 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
362 operationParam.channel = radio_parameter.channel;
developer402bf2a2022-10-04 15:20:18 +0800363
developer02cc9292023-02-24 18:01:27 +0800364 // bandwidth
developerae3f8412022-10-13 15:27:02 +0800365 if (radio_parameter.bandwidth == 20){
366 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
367 }else if (radio_parameter.bandwidth == 40){
368 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
369 }else if (radio_parameter.bandwidth == 80){
370 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
371 }else if (radio_parameter.bandwidth == 160){
372 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
developer02cc9292023-02-24 18:01:27 +0800373 }else if (radio_parameter.bandwidth == 320){
374 if ((radio_parameter.eht_320_conf == 1 || radio_parameter.channel <= 29) && radio_parameter.channel < 193)
375 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_1MHZ;
376 else if (radio_parameter.eht_320_conf == 2 || radio_parameter.channel >= 193)
377 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_2MHZ;
378 else
379 fprintf(stderr, "[Set EHT 320 bandwidth error with conf %d!!!]\n", radio_parameter.eht_320_conf);
developerae3f8412022-10-13 15:27:02 +0800380 }
developer402bf2a2022-10-04 15:20:18 +0800381
382 // htmode
developerab985802022-10-07 09:42:31 +0800383 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer402bf2a2022-10-04 15:20:18 +0800384 if (strcmp(radio_parameter.band, "2g") == 0) {
developerab985802022-10-07 09:42:31 +0800385 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer402bf2a2022-10-04 15:20:18 +0800386 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
387 strcpy(radio_parameter.htmode, "11G");
developer02cc9292023-02-24 18:01:27 +0800388 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developerab985802022-10-07 09:42:31 +0800389 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer02cc9292023-02-24 18:01:27 +0800390 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
391 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
392 else if (strstr(radio_parameter.htmode, "HT") != NULL)
393 mode |= WIFI_80211_VARIANT_N;
developer402bf2a2022-10-04 15:20:18 +0800394
developerab985802022-10-07 09:42:31 +0800395 } else if (strcmp(radio_parameter.band, "5g") == 0) {
396 mode |= WIFI_80211_VARIANT_A;
developer402bf2a2022-10-04 15:20:18 +0800397 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
398 strcpy(radio_parameter.htmode, "11A");
developer02cc9292023-02-24 18:01:27 +0800399 else if (strstr(radio_parameter.htmode, "VHT") != NULL)
400 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
401 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developerab985802022-10-07 09:42:31 +0800402 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer02cc9292023-02-24 18:01:27 +0800403 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
404 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
405 else if (strstr(radio_parameter.htmode, "HT") != NULL)
406 mode |= WIFI_80211_VARIANT_N;
407 } else if (strcmp(radio_parameter.band, "6g") == 0) {
408 mode |= WIFI_80211_VARIANT_AX;
409 if (strstr(radio_parameter.htmode, "EHT") != NULL)
410 mode |= WIFI_80211_VARIANT_BE;
411 }
developer402bf2a2022-10-04 15:20:18 +0800412
developerab985802022-10-07 09:42:31 +0800413 operationParam.variant = mode;
developer402bf2a2022-10-04 15:20:18 +0800414
developerc4548732023-02-06 20:02:10 +0800415 // rtsThreshold, zero means not set
416 if ((radio_parameter.rtsThreshold < 65535) && radio_parameter.rtsThreshold)
417 operationParam.rtsThreshold = radio_parameter.rtsThreshold;
418
419 //ht_coex
420 operationParam.obssCoex = radio_parameter.ht_coex;
421
developerab985802022-10-07 09:42:31 +0800422 // apply setting
423 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer402bf2a2022-10-04 15:20:18 +0800424 if (ret != RETURN_OK)
developerab985802022-10-07 09:42:31 +0800425 fprintf(stderr, "[Apply setting failed!!!]\n");
developer402bf2a2022-10-04 15:20:18 +0800426
developerb7f28c42022-11-07 16:58:17 +0800427 // Country
428 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
429 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
430 if (ret != RETURN_OK)
431 fprintf(stderr, "[Set Country failed!!!]\n");
432 ret = 0;
433
434 // hwmode
435 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
436 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
437 if (ret != RETURN_OK)
438 fprintf(stderr, "[Set hwmode failed!!!]\n");
439 ret = 0;
440
441 // noscan
442 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
443 if(strlen(radio_parameter.noscan)){
444 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
445 if (ret != RETURN_OK)
446 fprintf(stderr, "[Set noscan failed!!!]\n");
447 }
448 ret = 0;
449
developer402bf2a2022-10-04 15:20:18 +0800450}
451
developer1ac0e892022-11-25 14:30:05 +0800452void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer402bf2a2022-10-04 15:20:18 +0800453{
454 int ret = 0;
developerab985802022-10-07 09:42:31 +0800455 int vap_index_in_map = 0;
developer9567a462022-11-17 20:42:05 +0800456 int phy_index = 0;
developer9feeaf12023-02-17 15:52:18 +0800457 int key_len = 0;
developerab985802022-10-07 09:42:31 +0800458 wifi_vap_info_t vap_info = {0};
developer18615812022-10-17 17:37:29 +0800459 BOOL radio_enable = FALSE;
developer0dff3a92023-03-17 15:08:42 +0800460 char *maclist;
developer18615812022-10-17 17:37:29 +0800461
developer41965b52022-10-19 17:40:23 +0800462 if(ap_param.radio_index == -1)
463 return;
464
developer18615812022-10-17 17:37:29 +0800465 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
466 if (radio_enable == FALSE)
467 return;
developerab985802022-10-07 09:42:31 +0800468
developer41965b52022-10-19 17:40:23 +0800469
470 // get the index of the map
developer5f8ef5c2023-02-22 15:49:20 +0800471 for (int i = 0; i < MAX_NUM_VAP_PER_RADIO; i++) {
developer41965b52022-10-19 17:40:23 +0800472 if (map->vap_array[i].vap_index == ap_param.ap_index) {
473 vap_index_in_map = i;
474 break;
developerab985802022-10-07 09:42:31 +0800475 }
476 }
477
developer41965b52022-10-19 17:40:23 +0800478
developer5f8ef5c2023-02-22 15:49:20 +0800479 fprintf(stderr, "Start setting ap vap_index_in_map=%d\n", vap_index_in_map);
developerf9b2ef02022-10-14 10:07:58 +0800480
developer41965b52022-10-19 17:40:23 +0800481 vap_info = map->vap_array[vap_index_in_map];
developerf9b2ef02022-10-14 10:07:58 +0800482 vap_info.u.bss_info.enabled = TRUE;
developer9567a462022-11-17 20:42:05 +0800483 phy_index = radio_index_to_phy(vap_info.radio_index);
484 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf9b2ef02022-10-14 10:07:58 +0800485 fprintf(stderr, "Get mac address failed.\n");
developer9567a462022-11-17 20:42:05 +0800486 return;
developerf9b2ef02022-10-14 10:07:58 +0800487 }
developer402bf2a2022-10-04 15:20:18 +0800488
developer402bf2a2022-10-04 15:20:18 +0800489 // SSID
developerab985802022-10-07 09:42:31 +0800490 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
491 vap_info.u.bss_info.ssid[32] = '\0';
developer402bf2a2022-10-04 15:20:18 +0800492
developer2bcdef92022-12-13 16:19:09 +0800493 // interface
494 if (strlen(ap_param.ifname) != 0) {
495 strncpy(vap_info.vap_name, ap_param.ifname, 16);
developer53364a72023-04-24 10:59:12 +0800496 vap_info.vap_name[15] = '\0';
developer2bcdef92022-12-13 16:19:09 +0800497 }
498
developer9feeaf12023-02-17 15:52:18 +0800499 // Security
500 if (ap_param.security.mode == wifi_security_mode_wpa3_personal || ap_param.security.mode == wifi_security_mode_wpa3_transition){
501 // OpenWrt script only set psk, here we choose to set both psk and sae.
502 ap_param.security.u.key.type = wifi_security_key_type_psk_sae;
503 } else {
504 key_len = strlen(ap_param.security.u.key.key);
505 if (key_len == 64)
506 ap_param.security.u.key.type = wifi_security_key_type_psk;
507 else if (key_len >= 8 && key_len < 64)
508 ap_param.security.u.key.type = wifi_security_key_type_pass;
509 }
510
developer3ddad2f2022-10-13 13:33:57 +0800511 vap_info.u.bss_info.security.mode = ap_param.security.mode;
512 vap_info.u.bss_info.security.encr = ap_param.security.encr;
513 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
514 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
515 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developerc4548732023-02-06 20:02:10 +0800516
517 // hidden
518 vap_info.u.bss_info.showSsid = (ap_param.hidden ? 0 : 1);
developer4c2edbe2022-10-18 16:36:37 +0800519
developer223f5982023-02-28 15:59:38 +0800520 // igmpsn_enable
521 vap_info.u.bss_info.mcast2ucast = ap_param.igmpsn_enable;
522 fprintf(stderr, "Set igmpsn_enable: %d \n", ap_param.igmpsn_enable);
523
524 // wps_state
525 fprintf(stderr, "Set wps_state: %d \n", ap_param.wps_state);
developer0dff3a92023-03-17 15:08:42 +0800526 if (ap_param.wps_state == 2)
527 ret = wifi_setApWpsEnable(ap_param.ap_index, 1);
528 else
529 ret = wifi_setApWpsEnable(ap_param.ap_index, 0);
developer223f5982023-02-28 15:59:38 +0800530 if (ret != RETURN_OK)
531 fprintf(stderr, "[Set wps_state failed!!!]\n");
532 ret = 0;
533
developer223f5982023-02-28 15:59:38 +0800534 // macfilter
developer0dff3a92023-03-17 15:08:42 +0800535 fprintf(stderr, "Set macfilter: %s \n", ap_param.macfilter);
developer223f5982023-02-28 15:59:38 +0800536 if ((strcmp(ap_param.macfilter, "disable") == 0) || (strcmp(ap_param.macfilter, "\0") == 0) )
537 vap_info.u.bss_info.mac_filter_enable = false;
538 else if (strcmp(ap_param.macfilter, "deny") == 0){
539 vap_info.u.bss_info.mac_filter_enable = true;
540 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
541 }
542 else if (strcmp(ap_param.macfilter, "allow") == 0){
543 vap_info.u.bss_info.mac_filter_enable = true;
544 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
545 }
546 else
547 fprintf(stderr, "The macfilter tpye: %s is invalid!!!\n", ap_param.macfilter);
developer223f5982023-02-28 15:59:38 +0800548
549 // maclist
developer0dff3a92023-03-17 15:08:42 +0800550 fprintf(stderr, "Set maclist: %s \n", ap_param.maclist);
551 if ((strlen(ap_param.maclist) == 0)){
developer223f5982023-02-28 15:59:38 +0800552 ret = wifi_delApAclDevices(ap_param.ap_index);
553 if (ret != RETURN_OK)
554 fprintf(stderr, "[Del all maclist failed!!!]\n");
555 ret = 0;
developer0dff3a92023-03-17 15:08:42 +0800556 }
developera982e8f2023-03-09 15:47:10 +0800557 else if (strcmp(ap_param.macfilter, "allow") == 0) {
developer0dff3a92023-03-17 15:08:42 +0800558 maclist = strtok(ap_param.maclist, ";");
559 while (maclist != NULL)
560 {
561 ret = wifi_addApAclDevice(ap_param.ap_index, maclist);
562 if (ret != RETURN_OK)
563 fprintf(stderr, "[Add maclist failed!!!]\n");
564 ret = 0;
565 maclist = strtok(NULL, ";");
developer223f5982023-02-28 15:59:38 +0800566 }
developer0dff3a92023-03-17 15:08:42 +0800567 }
developera982e8f2023-03-09 15:47:10 +0800568 else if (strcmp(ap_param.macfilter, "deny") == 0) {
developer0dff3a92023-03-17 15:08:42 +0800569 maclist = strtok(ap_param.maclist, ";");
570 while (maclist != NULL)
571 {
572 ret = wifi_addApDenyAclDevice(ap_param.ap_index, maclist);
573 if (ret != RETURN_OK)
574 fprintf(stderr, "[Add maclist failed!!!]\n");
575 ret = 0;
576 maclist = strtok(NULL, ";");
developera982e8f2023-03-09 15:47:10 +0800577 }
developer0dff3a92023-03-17 15:08:42 +0800578 }
developer223f5982023-02-28 15:59:38 +0800579
580 ret = 0;
developerab985802022-10-07 09:42:31 +0800581 // Replace the setting with uci config
developer41965b52022-10-19 17:40:23 +0800582 map->vap_array[vap_index_in_map] = vap_info;
developer402bf2a2022-10-04 15:20:18 +0800583}
584
developer1ac0e892022-11-25 14:30:05 +0800585void set_sta_param(wifi_intf_param sta_param)
developer9567a462022-11-17 20:42:05 +0800586{
587 wifi_sta_network_t *sta = NULL;
588 mac_address_t sta_mac = {0};
589 char sta_mac_str[20] = {0};
590 char key_mgmt[16] = {0};
591 char pairwise[16] = {0};
592 int phy_index = 0;
593
594 sta = calloc(1, sizeof(wifi_sta_network_t));
595
596 phy_index = radio_index_to_phy(sta_param.radio_index);
597 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
598 mac_addr_ntoa(sta_mac_str, sta_mac);
599 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
600 sta->ssid[31] = '\0';
601 snprintf(sta->psk, 64, "%s", sta_param.password);
602
603 if (sta_param.security.mode == wifi_security_mode_none)
604 strcpy(key_mgmt, "NONE");
605 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
606 strcpy(key_mgmt, "SAE");
developer53364a72023-04-24 10:59:12 +0800607 else if (sta_param.security.mode == wifi_security_mode_enhanced_open)
developerf634eab2023-01-13 15:23:06 +0800608 strcpy(key_mgmt, "OWE");
developer9567a462022-11-17 20:42:05 +0800609 else
610 strcpy(key_mgmt, "WPA-PSK");
611 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
612
613 if (sta_param.security.encr == wifi_encryption_aes)
614 strcpy(pairwise, "CCMP");
615 else if (sta_param.security.encr == wifi_encryption_tkip)
616 strcpy(pairwise, "TKIP");
617 else
618 strcpy(pairwise, "CCMP TKIP");
619 snprintf(sta->pairwise, 64, "%s", pairwise);
620
621 if (strlen(sta_param.security.u.key.key) > 0)
622 strncpy(sta->psk, sta_param.security.u.key.key, 127);
623 sta->psk[127] = '\0';
624 sta->psk_len = strlen(sta->psk);
625
developerb3f00662022-12-29 09:35:55 +0800626 if (sta_param.wds_mode == TRUE)
627 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
628
629 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer9567a462022-11-17 20:42:05 +0800630
631 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
632 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
633 free(sta);
634 return;
635 }
636 free(sta);
637
638 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
639 fprintf(stderr, "Enable station failed\n");
640 return;
641 }
642}
643
developer223f5982023-02-28 15:59:38 +0800644void show_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
645{
646 int ret = 0;
developera982e8f2023-03-09 15:47:10 +0800647 int filter_mode = 0;
developer223f5982023-02-28 15:59:38 +0800648 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;
developera982e8f2023-03-09 15:47:10 +0800653 BOOL igmpsn_state = FALSE;
developer223f5982023-02-28 15:59:38 +0800654 UINT buf_size = 1024;
655 char macArray[1024] = "";
656
657 if(ap_param.radio_index == -1)
658 return;
659
660 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
661 if (radio_enable == FALSE)
662 return;
663
664
665 // get the index of the map
666 for (int i = 0; i < map->num_vaps; i++) {
667 if (map->vap_array[i].vap_index == ap_param.ap_index) {
668 vap_index_in_map = i;
669 break;
670 }
671 }
672
673 vap_info = map->vap_array[vap_index_in_map];
674 vap_info.u.bss_info.enabled = TRUE;
675 phy_index = radio_index_to_phy(vap_info.radio_index);
676
677 // SSID
678 printf("wifi%d ssid: %s\n", ap_param.ap_index, vap_info.u.bss_info.ssid);
679
developer0dff3a92023-03-17 15:08:42 +0800680 // igmpsn_enable
developera982e8f2023-03-09 15:47:10 +0800681 ret = wifi_getRadioIGMPSnoopingEnable(ap_param.radio_index, &igmpsn_state);
682 if (ret != RETURN_OK)
683 fprintf(stderr, "[Get igmpsn_state failed!!!]\n");
684 else
685 printf("wifi%d igmpsn_state: %d\n", ap_param.ap_index, igmpsn_state);
686 ret = 0;
developer223f5982023-02-28 15:59:38 +0800687
688 // wps_state
689 ret = wifi_getApWpsEnable(ap_param.ap_index, &wps_state);
690 if (ret != RETURN_OK)
developera982e8f2023-03-09 15:47:10 +0800691 fprintf(stderr, "[Get wps_state failed!!!]\n");
developer223f5982023-02-28 15:59:38 +0800692 else
693 printf("wifi%d wps_state: %d\n", ap_param.ap_index, wps_state);
694 ret = 0;
695
696 // macfilter
developera982e8f2023-03-09 15:47:10 +0800697 ret = wifi_getApMacAddressControlMode(ap_param.ap_index, &filter_mode);
698 if (ret != RETURN_OK)
699 fprintf(stderr, "[Get macfilter failed!!!]\n");
700 else{
701 if (filter_mode == 0)
702 printf("wifi%d macfilter: disable\n", ap_param.ap_index);
703 else if (filter_mode == 1)
704 printf("wifi%d macfilter: allow\n", ap_param.ap_index);
705 else if (filter_mode == 2)
706 printf("wifi%d macfilter: deny\n", ap_param.ap_index);
707 }
708 ret = 0;
developer223f5982023-02-28 15:59:38 +0800709
710 // maclist
711 printf("wifi%d maclist: \n", ap_param.ap_index);
developera982e8f2023-03-09 15:47:10 +0800712 if (filter_mode == 0)
713 printf("wifi%d macfilter is disable\n", ap_param.ap_index);
714 else if (filter_mode == 1){
715 ret = wifi_getApAclDevices(ap_param.ap_index, macArray, buf_size);
716 if (ret != RETURN_OK)
717 fprintf(stderr, "[Get maclist failed!!!]\n");
718 else
719 printf("%s \n", macArray);
720 ret = 0;
721 }
722 else if (filter_mode == 2){
723 ret = wifi_getApDenyAclDevices(ap_param.ap_index, macArray, buf_size);
724 if (ret != RETURN_OK)
725 fprintf(stderr, "[Get maclist failed!!!]\n");
726 else
727 printf("%s \n", macArray);
728 ret = 0;
729 }
developer223f5982023-02-28 15:59:38 +0800730
731}
732
developer402bf2a2022-10-04 15:20:18 +0800733int apply_uci_config ()
734{
735 struct uci_context *uci_ctx = uci_alloc_context();
736 struct uci_package *uci_pkg = NULL;
737 struct uci_element *e;
738 // struct uci_section *s;
739 const char cfg_name[] = "wireless";
740 int max_radio_num = 0;
741 BOOL parsing_radio = FALSE;
developer4c2edbe2022-10-18 16:36:37 +0800742 int apCount[3] = {0};
developer9567a462022-11-17 20:42:05 +0800743 int staCount[3] = {0};
developer41965b52022-10-19 17:40:23 +0800744 wifi_vap_info_map_t vap_map[3] = {0};
745 int ret = 0;
developer5f8ef5c2023-02-22 15:49:20 +0800746 int i = 0, j = 0;
developer402bf2a2022-10-04 15:20:18 +0800747
748 wifi_getMaxRadioNumber(&max_radio_num);
749 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developer41965b52022-10-19 17:40:23 +0800750 for (i = 0; i < max_radio_num ;i++ ){
751 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
752 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
753 fprintf(stderr, "[Get vap map failed!!!]\n");
developer41965b52022-10-19 17:40:23 +0800754 }
developer5f8ef5c2023-02-22 15:49:20 +0800755
756 /*reset radio's ap number, ap number ++ by uci config */
757 vap_map[i].num_vaps = 0;
developer41965b52022-10-19 17:40:23 +0800758 }
developer402bf2a2022-10-04 15:20:18 +0800759 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
760 uci_free_context(uci_ctx);
761 fprintf(stderr, "%s: load uci failed.\n", __func__);
762 return RETURN_ERR;
763 }
764
765 uci_foreach_element(&uci_pkg->sections, e) {
766
767 struct uci_section *s = uci_to_section(e);
768 struct uci_element *option = NULL;
769 wifi_radio_param radio_param = {0};
developer1ac0e892022-11-25 14:30:05 +0800770 wifi_intf_param intf_param = {0};
developer4c2edbe2022-10-18 16:36:37 +0800771 int phyId = 0;
developer402bf2a2022-10-04 15:20:18 +0800772 radio_param.radio_index = -1;
developer1ac0e892022-11-25 14:30:05 +0800773 intf_param.ap_index = -1;
developer402bf2a2022-10-04 15:20:18 +0800774
775 if (strcmp(s->type, "wifi-device") == 0) {
developer4c2edbe2022-10-18 16:36:37 +0800776 sscanf(s->e.name, "radio%d", &phyId);
777 radio_param.radio_index = phy_index_to_radio(phyId);
developer402bf2a2022-10-04 15:20:18 +0800778 parsing_radio = TRUE;
779 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
780 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer402bf2a2022-10-04 15:20:18 +0800781 parsing_radio = FALSE;
developer402bf2a2022-10-04 15:20:18 +0800782 }
783
784 uci_foreach_element(&s->options, option) {
785
786 struct uci_option *op = uci_to_option(option);
787 if (parsing_radio == TRUE) {
788 // transform the type from input string and store the value in radio_param.
789 if (strcmp(op->e.name, "channel") == 0)
790 set_channel(&radio_param, op->v.string);
791 else if (strcmp(op->e.name, "hwmode") == 0)
792 set_hwmode(&radio_param, op->v.string);
793 else if (strcmp(op->e.name, "htmode") == 0)
794 set_htmode(&radio_param, op->v.string);
795 else if (strcmp(op->e.name, "disabled") == 0)
796 set_disable(&radio_param, op->v.string);
797 else if (strcmp(op->e.name, "band") == 0)
798 set_band(&radio_param, op->v.string);
799 else if (strcmp(op->e.name, "country") == 0)
800 set_country(&radio_param, op->v.string);
801 else if (strcmp(op->e.name, "noscan") == 0)
developer54afa2c2022-10-18 17:44:13 +0800802 set_noscan(&radio_param, op->v.string);
developer1e946fd2022-12-26 17:09:00 +0800803 else if (strcmp(op->e.name, "rxantenna") == 0)
804 set_rxant(&radio_param, op->v.string);
805 else if (strcmp(op->e.name, "txantenna") == 0)
806 set_txant(&radio_param, op->v.string);
developerc4548732023-02-06 20:02:10 +0800807 else if (strcmp(op->e.name, "ht_coex") == 0)
808 set_htcoex(&radio_param, op->v.string);
809 else if (strcmp(op->e.name, "rts") == 0)
810 set_rts(&radio_param, op->v.string);
developer402bf2a2022-10-04 15:20:18 +0800811 else
812 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
813 } else {
814 // parsing iface
developer4c2edbe2022-10-18 16:36:37 +0800815 if (strcmp(op->e.name, "device") == 0){
developer1ac0e892022-11-25 14:30:05 +0800816 set_radionum(&intf_param, op->v.string);
developer9567a462022-11-17 20:42:05 +0800817 }else if (strcmp(op->e.name, "mode") == 0){
developerb97b5492022-12-22 19:44:36 +0800818 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer9567a462022-11-17 20:42:05 +0800819 if (strncmp(op->v.string, "sta", 3) == 0) {
developer1ac0e892022-11-25 14:30:05 +0800820 intf_param.sta_mode = TRUE;
821 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
822 staCount[intf_param.radio_index] ++ ;
823 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer9567a462022-11-17 20:42:05 +0800824 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer1ac0e892022-11-25 14:30:05 +0800825 intf_param.sta_mode = FALSE;
826 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
827 apCount[intf_param.radio_index] ++ ;
828 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer9567a462022-11-17 20:42:05 +0800829 }
developer4c2edbe2022-10-18 16:36:37 +0800830 }else if (strcmp(op->e.name, "ssid") == 0){
developer1ac0e892022-11-25 14:30:05 +0800831 set_ssid(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800832 }else if (strcmp(op->e.name, "encryption") == 0){
developer1ac0e892022-11-25 14:30:05 +0800833 set_encryption(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800834 }else if (strcmp(op->e.name, "key") == 0){
developer1ac0e892022-11-25 14:30:05 +0800835 set_key(&intf_param, op->v.string);
developer2bcdef92022-12-13 16:19:09 +0800836 }else if (strcmp(op->e.name, "ifname") == 0){
837 set_ifname(&intf_param, op->v.string);
developerb3f00662022-12-29 09:35:55 +0800838 }else if (strcmp(op->e.name, "wds") == 0){
839 set_wds(&intf_param, op->v.string);
developerc4548732023-02-06 20:02:10 +0800840 }else if (strcmp(op->e.name, "hidden") == 0){
841 set_hidden(&intf_param, op->v.string);
developer223f5982023-02-28 15:59:38 +0800842 }else if (strcmp(op->e.name, "igmpsn_enable") == 0){
843 set_igmpsn_enable(&intf_param, op->v.string);
844 }else if (strcmp(op->e.name, "wps_state") == 0){
845 set_wps_state(&intf_param, op->v.string);
developer223f5982023-02-28 15:59:38 +0800846 }else if (strcmp(op->e.name, "macfilter") == 0){
847 set_macfilter(&intf_param, op->v.string);
848 }else if (strcmp(op->e.name, "maclist") == 0){
849 set_maclist(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800850 }else{
developer402bf2a2022-10-04 15:20:18 +0800851 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer223f5982023-02-28 15:59:38 +0800852 }
developer402bf2a2022-10-04 15:20:18 +0800853 }
854 }
developer5f8ef5c2023-02-22 15:49:20 +0800855 if (parsing_radio == TRUE) {
developer402bf2a2022-10-04 15:20:18 +0800856 set_radio_param(radio_param);
developer5f8ef5c2023-02-22 15:49:20 +0800857 }
developer1ac0e892022-11-25 14:30:05 +0800858 else if (intf_param.sta_mode == TRUE)
859 set_sta_param(intf_param);
developer5f8ef5c2023-02-22 15:49:20 +0800860 else {
developer1ac0e892022-11-25 14:30:05 +0800861 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer5f8ef5c2023-02-22 15:49:20 +0800862 vap_map[intf_param.radio_index].num_vaps++;
863 }
developer402bf2a2022-10-04 15:20:18 +0800864 }
developer5f8ef5c2023-02-22 15:49:20 +0800865
866 /* disable all interface, re-enable by UCI configuration */
867
868 fprintf(stderr, "\n----- Disable all interfaces. -----\n");
869
870 for (i = 0; i < max_radio_num; i++ ){
developer0148a3d2023-03-08 09:29:34 +0800871 for(j = max_radio_num*(MAX_NUM_VAP_PER_RADIO-1) + i; j >= 0; j -= max_radio_num) {
872 ret = wifi_setApEnable(j, FALSE);
developer5f8ef5c2023-02-22 15:49:20 +0800873 if (ret != RETURN_OK)
developer0148a3d2023-03-08 09:29:34 +0800874 fprintf(stderr, "[disable ap %d failed!!!]\n", j);
875 else
876 fprintf(stderr, "[disable ap %d success.]\n", j);
developer5f8ef5c2023-02-22 15:49:20 +0800877 }
878 }
developer9567a462022-11-17 20:42:05 +0800879 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer402bf2a2022-10-04 15:20:18 +0800880
developer5f8ef5c2023-02-22 15:49:20 +0800881 for (i = 0; i < max_radio_num ;i++ ){
882
883 fprintf(stderr, "\n create Vap radio:%d num_vaps=%d.\n", i, vap_map[i].num_vaps);
developer41965b52022-10-19 17:40:23 +0800884 ret = wifi_createVAP(i, &vap_map[i]);
885 if (ret != RETURN_OK)
886 fprintf(stderr, "[Apply vap setting failed!!!]\n");
887 }
developer223f5982023-02-28 15:59:38 +0800888
developer0dff3a92023-03-17 15:08:42 +0800889 for (i = 0; i < 3 ;i++ ){
890 apCount[i] = 0;
891 staCount[i] = 0;
892 }
893
894 uci_foreach_element(&uci_pkg->sections, e) {
895
896 struct uci_section *s = uci_to_section(e);
897 struct uci_element *option = NULL;
898 wifi_radio_param radio_param = {0};
899 wifi_intf_param intf_param = {0};
900 int phyId = 0;
901 radio_param.radio_index = -1;
902 intf_param.ap_index = -1;
903
904 if (strcmp(s->type, "wifi-iface") == 0) {
905 uci_foreach_element(&s->options, option) {
906
907 struct uci_option *op = uci_to_option(option);
908 {
909 // parsing iface
910 if (strcmp(op->e.name, "device") == 0){
911 set_radionum(&intf_param, op->v.string);
912 }else if (strcmp(op->e.name, "mode") == 0){
913 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
914 if (strncmp(op->v.string, "sta", 3) == 0) {
915 intf_param.sta_mode = TRUE;
916 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
917 staCount[intf_param.radio_index] ++ ;
918 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
919 } else if (strncmp(op->v.string, "ap", 2) == 0) {
920 intf_param.sta_mode = FALSE;
921 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
922 apCount[intf_param.radio_index] ++ ;
923 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
924 }
925 }else if (strcmp(op->e.name, "wps_cancel") == 0){
926 set_wps_cancel(&intf_param, op->v.string);
927 }else if (strcmp(op->e.name, "wps_pushbutton") == 0){
928 set_wps_pushbutton(&intf_param, op->v.string);
929 }
930 }
931 }
932
933 // wps_cancel
934 fprintf(stderr, "Set wps_cancel: %d \n", intf_param.wps_cancel);
935 if (intf_param.wps_cancel){
936 ret = wifi_cancelApWPS(intf_param.ap_index);
937 if (ret != RETURN_OK)
938 fprintf(stderr, "[Set wps_cancel failed!!!]\n");
939 ret = 0;
940 }
941
942 // wps_pushbutton
943 fprintf(stderr, "Set wps_pushbutton: %d \n", intf_param.wps_pushbutton);
944 if (intf_param.wps_pushbutton){
945 ret = wifi_setApWpsButtonPush(intf_param.ap_index);
946 if (ret != RETURN_OK)
947 fprintf(stderr, "[Set wps_pushbutton failed!!!]\n");
948 ret = 0;
949 }
950 }
951 }
952
developer402bf2a2022-10-04 15:20:18 +0800953 uci_unload(uci_ctx, uci_pkg);
954 uci_free_context(uci_ctx);
955 return RETURN_OK;
956}
957
developer223f5982023-02-28 15:59:38 +0800958int dump_wifi_status ()
959{
960
961 struct uci_context *uci_ctx = uci_alloc_context();
962 struct uci_package *uci_pkg = NULL;
963 struct uci_element *e;
964 // struct uci_section *s;
965 const char cfg_name[] = "wireless";
966 int max_radio_num = 0;
967 BOOL parsing_radio = FALSE;
968 int apCount[3] = {0};
969 int staCount[3] = {0};
970 wifi_vap_info_map_t vap_map[3] = {0};
971 int ret = 0;
972 int i = 0;
973
974 wifi_getMaxRadioNumber(&max_radio_num);
975 fprintf(stderr, "max radio number: %d\n", max_radio_num);
976 for (i = 0; i < max_radio_num ;i++ ){
977 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
978 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
979 fprintf(stderr, "[Get vap map failed!!!]\n");
980 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
981 }
982 }
983 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
984 uci_free_context(uci_ctx);
985 fprintf(stderr, "%s: load uci failed.\n", __func__);
986 return RETURN_ERR;
987 }
988
989 uci_foreach_element(&uci_pkg->sections, e) {
990
991 struct uci_section *s = uci_to_section(e);
992 struct uci_element *option = NULL;
993 wifi_radio_param radio_param = {0};
994 wifi_intf_param intf_param = {0};
995 int phyId = 0;
996 radio_param.radio_index = -1;
997 intf_param.ap_index = -1;
998
999 if (strcmp(s->type, "wifi-device") == 0) {
1000 sscanf(s->e.name, "radio%d", &phyId);
1001 radio_param.radio_index = phy_index_to_radio(phyId);
1002 parsing_radio = TRUE;
1003 fprintf(stderr, "\n----- Start show radio %d config. -----\n", radio_param.radio_index);
1004 } else if (strcmp(s->type, "wifi-iface") == 0) {
1005 parsing_radio = FALSE;
1006 }
1007
1008 uci_foreach_element(&s->options, option) {
1009
1010 struct uci_option *op = uci_to_option(option);
1011 if (parsing_radio == TRUE) {
1012 // transform the type from input string and store the value in radio_param.
1013 } else {
1014 // parsing iface
1015 if (strcmp(op->e.name, "device") == 0){
1016 set_radionum(&intf_param, op->v.string);
1017 }else if (strcmp(op->e.name, "mode") == 0){
1018 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
1019 if (strncmp(op->v.string, "sta", 3) == 0) {
1020 intf_param.sta_mode = TRUE;
1021 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
1022 staCount[intf_param.radio_index] ++ ;
1023 fprintf(stderr, "\n----- Start show sta %d config. -----\n", intf_param.sta_index);
1024 } else if (strncmp(op->v.string, "ap", 2) == 0) {
1025 intf_param.sta_mode = FALSE;
1026 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
1027 apCount[intf_param.radio_index] ++ ;
1028 fprintf(stderr, "\n----- Start show ap %d config. -----\n", intf_param.ap_index);
1029 }
1030 }
1031 }
1032 }
1033 if (parsing_radio == TRUE)
1034 printf("show radio params: \n");
1035 // show_radio_param(radio_param);
1036 else if (intf_param.sta_mode == TRUE)
1037 printf("show sta params: \n");
1038 // show_sta_param(intf_param);
1039 else{
1040 printf("show ap params: \n");
1041 show_ap_param(intf_param, &vap_map[intf_param.radio_index]);
1042 }
1043 }
1044
1045 uci_unload(uci_ctx, uci_pkg);
1046 uci_free_context(uci_ctx);
1047 return RETURN_OK;
1048
1049}
1050
developer402bf2a2022-10-04 15:20:18 +08001051int main(int argc, char **argv)
1052{
developer223f5982023-02-28 15:59:38 +08001053 if (argc != 2 || (strcmp(argv[1], "reload") != 0 && strcmp(argv[1], "dump") != 0) ){
1054 fprintf(stderr, "Usage: wifi reload/wifi dump.\nThis tool is only for RDKB MSP/SQC test.\n");
developer402bf2a2022-10-04 15:20:18 +08001055 return -1;
1056 }
developer223f5982023-02-28 15:59:38 +08001057 if (strcmp(argv[1], "reload") == 0)
1058 apply_uci_config();
1059 else if (strcmp(argv[1], "dump") == 0)
1060 dump_wifi_status();
developer402bf2a2022-10-04 15:20:18 +08001061 return 0;
1062}