blob: 51140e83df5521e3ece45742fe4aa86a8a01c316 [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
developer0e3ff462023-04-28 17:48:40 +0800227void set_background_radar(wifi_radio_param *radio_param, char *enable)
228{
229 if (strcmp(enable, "1") == 0)
230 radio_param->background_radar = TRUE;
231 else
232 radio_param->background_radar = FALSE;
233}
234
developer1ac0e892022-11-25 14:30:05 +0800235void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer402bf2a2022-10-04 15:20:18 +0800236{
developer9567a462022-11-17 20:42:05 +0800237 int radio_num = 0;
238 char *ptr = phy_name;
developer4c2edbe2022-10-18 16:36:37 +0800239 int phyId = 0;
developer402bf2a2022-10-04 15:20:18 +0800240
241 while (*ptr) {
242 if (isdigit(*ptr)) {
developer9567a462022-11-17 20:42:05 +0800243 phyId = strtoul(ptr, NULL, 10);
244 radio_num = phy_index_to_radio(phyId);
developer1ac0e892022-11-25 14:30:05 +0800245 intf_param->radio_index = radio_num;
developer402bf2a2022-10-04 15:20:18 +0800246 break;
247 }
248 ptr++;
249 }
250}
251
developer1ac0e892022-11-25 14:30:05 +0800252void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer402bf2a2022-10-04 15:20:18 +0800253{
developer1ac0e892022-11-25 14:30:05 +0800254 strncpy(intf_param->ssid, ssid, 32);
developer402bf2a2022-10-04 15:20:18 +0800255}
256
developer1ac0e892022-11-25 14:30:05 +0800257void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer402bf2a2022-10-04 15:20:18 +0800258{
developer3ddad2f2022-10-13 13:33:57 +0800259 if (strcmp(encryption_mode, "none") == 0) {
developer1ac0e892022-11-25 14:30:05 +0800260 intf_param->security.mode = wifi_security_mode_none;
261 intf_param->security.encr = wifi_encryption_none;
developer3ddad2f2022-10-13 13:33:57 +0800262 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800263 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developer3ddad2f2022-10-13 13:33:57 +0800264 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800265 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developer3ddad2f2022-10-13 13:33:57 +0800266 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer1ac0e892022-11-25 14:30:05 +0800267 intf_param->security.mode = wifi_security_mode_wpa_personal;
developer3ddad2f2022-10-13 13:33:57 +0800268 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800269 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800270 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer1ac0e892022-11-25 14:30:05 +0800271 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800272 }else if(strcmp(encryption_mode, "sae") == 0){
developer1ac0e892022-11-25 14:30:05 +0800273 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developer3ddad2f2022-10-13 13:33:57 +0800274 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer1ac0e892022-11-25 14:30:05 +0800275 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developer3ddad2f2022-10-13 13:33:57 +0800276 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer1ac0e892022-11-25 14:30:05 +0800277 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerf634eab2023-01-13 15:23:06 +0800278 }else if(strcmp(encryption_mode, "owe") == 0){
developer53364a72023-04-24 10:59:12 +0800279 intf_param->security.mode = wifi_security_mode_enhanced_open;
developer402bf2a2022-10-04 15:20:18 +0800280 }
281
developer3ddad2f2022-10-13 13:33:57 +0800282 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer1ac0e892022-11-25 14:30:05 +0800283 intf_param->security.encr = wifi_encryption_aes_tkip;
developer3ddad2f2022-10-13 13:33:57 +0800284 }else if (strstr(encryption_mode, "tkip")){
developer1ac0e892022-11-25 14:30:05 +0800285 intf_param->security.encr = wifi_encryption_tkip;
developer3ddad2f2022-10-13 13:33:57 +0800286 }else{
developer1ac0e892022-11-25 14:30:05 +0800287 intf_param->security.encr = wifi_encryption_aes;
developer402bf2a2022-10-04 15:20:18 +0800288 }
developer3ddad2f2022-10-13 13:33:57 +0800289
developerf634eab2023-01-13 15:23:06 +0800290 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer1ac0e892022-11-25 14:30:05 +0800291 intf_param->security.mfp = wifi_mfp_cfg_required;
developer3ddad2f2022-10-13 13:33:57 +0800292 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer1ac0e892022-11-25 14:30:05 +0800293 intf_param->security.mfp = wifi_mfp_cfg_optional;
developer3ddad2f2022-10-13 13:33:57 +0800294 }else{
developer1ac0e892022-11-25 14:30:05 +0800295 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developer3ddad2f2022-10-13 13:33:57 +0800296 }
developer402bf2a2022-10-04 15:20:18 +0800297}
298
developer1ac0e892022-11-25 14:30:05 +0800299void set_key(wifi_intf_param *intf_param, char *key)
developer402bf2a2022-10-04 15:20:18 +0800300{
developer1ac0e892022-11-25 14:30:05 +0800301 strncpy(intf_param->security.u.key.key, key, 64);
developer402bf2a2022-10-04 15:20:18 +0800302}
303
developer2bcdef92022-12-13 16:19:09 +0800304void set_ifname(wifi_intf_param *intf_param, char *ifname)
305{
306 if (strlen(ifname) > 15)
307 return;
308 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
309}
310
developerb3f00662022-12-29 09:35:55 +0800311void set_wds(wifi_intf_param *intf_param, char *wds_enable)
312{
313 intf_param->wds_mode = FALSE;
314 if (strncmp(wds_enable, "1", 1) == 0)
315 intf_param->wds_mode = TRUE;
316}
317
developerc4548732023-02-06 20:02:10 +0800318void set_hidden(wifi_intf_param *intf_param, char *hidden)
319{
320 intf_param->hidden = strtol(hidden, NULL, 10);
321}
322
developer9567a462022-11-17 20:42:05 +0800323int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf9b2ef02022-10-14 10:07:58 +0800324{
325 FILE *f;
326 char mac_file[64] = {0};
327 char mac_address[20] = {0};
developerf9b2ef02022-10-14 10:07:58 +0800328
developerb97b5492022-12-22 19:44:36 +0800329 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf9b2ef02022-10-14 10:07:58 +0800330 f = fopen(mac_file, "r");
331 if (f == NULL)
332 return -1;
333 fgets(mac_address, 20, f);
334 fclose(f);
335
developer9567a462022-11-17 20:42:05 +0800336 mac_addr_aton(&(*bssid)[0], mac_address);
337 (*bssid)[0] += offset*2;
developerf9b2ef02022-10-14 10:07:58 +0800338 return 0;
339}
340
developer402bf2a2022-10-04 15:20:18 +0800341void set_radio_param(wifi_radio_param radio_parameter)
342{
developer402bf2a2022-10-04 15:20:18 +0800343 int ret = 0;
developerab985802022-10-07 09:42:31 +0800344 wifi_radio_operationParam_t operationParam = {0};
345
developer4c2edbe2022-10-18 16:36:37 +0800346 if(radio_parameter.radio_index == -1)
347 return;
348
developerab985802022-10-07 09:42:31 +0800349 if (radio_parameter.disabled == TRUE) {
350 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
351 return;
352 }
developer402bf2a2022-10-04 15:20:18 +0800353
354 fprintf(stderr, "Start setting radio\n");
developer18615812022-10-17 17:37:29 +0800355
developer1e946fd2022-12-26 17:09:00 +0800356 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
357 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
358 if (ret != RETURN_OK)
359 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
360 }
361
developer18615812022-10-17 17:37:29 +0800362 // Get current radio setting
developerab985802022-10-07 09:42:31 +0800363 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
364 if (ret != RETURN_OK)
365 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developer18615812022-10-17 17:37:29 +0800366 operationParam.enable = TRUE;
developer402bf2a2022-10-04 15:20:18 +0800367
developer402bf2a2022-10-04 15:20:18 +0800368 // Channel
developerab985802022-10-07 09:42:31 +0800369 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
370 operationParam.channel = radio_parameter.channel;
developer402bf2a2022-10-04 15:20:18 +0800371
developer02cc9292023-02-24 18:01:27 +0800372 // bandwidth
developerae3f8412022-10-13 15:27:02 +0800373 if (radio_parameter.bandwidth == 20){
374 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
375 }else if (radio_parameter.bandwidth == 40){
376 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
377 }else if (radio_parameter.bandwidth == 80){
378 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
379 }else if (radio_parameter.bandwidth == 160){
380 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
developer02cc9292023-02-24 18:01:27 +0800381 }else if (radio_parameter.bandwidth == 320){
382 if ((radio_parameter.eht_320_conf == 1 || radio_parameter.channel <= 29) && radio_parameter.channel < 193)
383 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_1MHZ;
384 else if (radio_parameter.eht_320_conf == 2 || radio_parameter.channel >= 193)
385 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_2MHZ;
386 else
387 fprintf(stderr, "[Set EHT 320 bandwidth error with conf %d!!!]\n", radio_parameter.eht_320_conf);
developerae3f8412022-10-13 15:27:02 +0800388 }
developer402bf2a2022-10-04 15:20:18 +0800389
390 // htmode
developerab985802022-10-07 09:42:31 +0800391 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer402bf2a2022-10-04 15:20:18 +0800392 if (strcmp(radio_parameter.band, "2g") == 0) {
developerab985802022-10-07 09:42:31 +0800393 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer402bf2a2022-10-04 15:20:18 +0800394 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
395 strcpy(radio_parameter.htmode, "11G");
developer02cc9292023-02-24 18:01:27 +0800396 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developerab985802022-10-07 09:42:31 +0800397 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer02cc9292023-02-24 18:01:27 +0800398 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
399 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
400 else if (strstr(radio_parameter.htmode, "HT") != NULL)
401 mode |= WIFI_80211_VARIANT_N;
developer402bf2a2022-10-04 15:20:18 +0800402
developerab985802022-10-07 09:42:31 +0800403 } else if (strcmp(radio_parameter.band, "5g") == 0) {
404 mode |= WIFI_80211_VARIANT_A;
developer402bf2a2022-10-04 15:20:18 +0800405 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
406 strcpy(radio_parameter.htmode, "11A");
developer02cc9292023-02-24 18:01:27 +0800407 else if (strstr(radio_parameter.htmode, "VHT") != NULL)
408 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
409 else if (strstr(radio_parameter.htmode, "HE") != NULL)
developerab985802022-10-07 09:42:31 +0800410 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer02cc9292023-02-24 18:01:27 +0800411 else if (strstr(radio_parameter.htmode, "EHT") != NULL)
412 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
413 else if (strstr(radio_parameter.htmode, "HT") != NULL)
414 mode |= WIFI_80211_VARIANT_N;
415 } else if (strcmp(radio_parameter.band, "6g") == 0) {
416 mode |= WIFI_80211_VARIANT_AX;
417 if (strstr(radio_parameter.htmode, "EHT") != NULL)
418 mode |= WIFI_80211_VARIANT_BE;
419 }
developer402bf2a2022-10-04 15:20:18 +0800420
developerab985802022-10-07 09:42:31 +0800421 operationParam.variant = mode;
developer402bf2a2022-10-04 15:20:18 +0800422
developerc4548732023-02-06 20:02:10 +0800423 // rtsThreshold, zero means not set
424 if ((radio_parameter.rtsThreshold < 65535) && radio_parameter.rtsThreshold)
425 operationParam.rtsThreshold = radio_parameter.rtsThreshold;
426
427 //ht_coex
428 operationParam.obssCoex = radio_parameter.ht_coex;
429
developerab985802022-10-07 09:42:31 +0800430 // apply setting
431 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer402bf2a2022-10-04 15:20:18 +0800432 if (ret != RETURN_OK)
developerab985802022-10-07 09:42:31 +0800433 fprintf(stderr, "[Apply setting failed!!!]\n");
developer402bf2a2022-10-04 15:20:18 +0800434
developerb7f28c42022-11-07 16:58:17 +0800435 // Country
436 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
437 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
438 if (ret != RETURN_OK)
439 fprintf(stderr, "[Set Country failed!!!]\n");
440 ret = 0;
441
442 // hwmode
443 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
444 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
445 if (ret != RETURN_OK)
446 fprintf(stderr, "[Set hwmode failed!!!]\n");
447 ret = 0;
448
449 // noscan
450 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
451 if(strlen(radio_parameter.noscan)){
452 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
453 if (ret != RETURN_OK)
454 fprintf(stderr, "[Set noscan failed!!!]\n");
455 }
456 ret = 0;
457
developer0e3ff462023-04-28 17:48:40 +0800458 // background_radar (Zero-Wait DFS)
459 fprintf(stderr, "Set background_radar(Zero-Wait DFS): %d\n", radio_parameter.background_radar);
460 ret = wifi_setZeroDFSState(radio_parameter.radio_index, radio_parameter.background_radar, TRUE);
461 if (ret != RETURN_OK)
462 fprintf(stderr, "[Set background_radar failed!!!]\n");
463 ret = 0;
464
developer402bf2a2022-10-04 15:20:18 +0800465}
466
developer1ac0e892022-11-25 14:30:05 +0800467void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer402bf2a2022-10-04 15:20:18 +0800468{
469 int ret = 0;
developerab985802022-10-07 09:42:31 +0800470 int vap_index_in_map = 0;
developer9567a462022-11-17 20:42:05 +0800471 int phy_index = 0;
developer9feeaf12023-02-17 15:52:18 +0800472 int key_len = 0;
developerab985802022-10-07 09:42:31 +0800473 wifi_vap_info_t vap_info = {0};
developer18615812022-10-17 17:37:29 +0800474 BOOL radio_enable = FALSE;
developer0dff3a92023-03-17 15:08:42 +0800475 char *maclist;
developer18615812022-10-17 17:37:29 +0800476
developer41965b52022-10-19 17:40:23 +0800477 if(ap_param.radio_index == -1)
478 return;
479
developer18615812022-10-17 17:37:29 +0800480 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
481 if (radio_enable == FALSE)
482 return;
developerab985802022-10-07 09:42:31 +0800483
developer41965b52022-10-19 17:40:23 +0800484
485 // get the index of the map
developer5f8ef5c2023-02-22 15:49:20 +0800486 for (int i = 0; i < MAX_NUM_VAP_PER_RADIO; i++) {
developer41965b52022-10-19 17:40:23 +0800487 if (map->vap_array[i].vap_index == ap_param.ap_index) {
488 vap_index_in_map = i;
489 break;
developerab985802022-10-07 09:42:31 +0800490 }
491 }
492
developer41965b52022-10-19 17:40:23 +0800493
developer5f8ef5c2023-02-22 15:49:20 +0800494 fprintf(stderr, "Start setting ap vap_index_in_map=%d\n", vap_index_in_map);
developerf9b2ef02022-10-14 10:07:58 +0800495
developer41965b52022-10-19 17:40:23 +0800496 vap_info = map->vap_array[vap_index_in_map];
developerf9b2ef02022-10-14 10:07:58 +0800497 vap_info.u.bss_info.enabled = TRUE;
developer9567a462022-11-17 20:42:05 +0800498 phy_index = radio_index_to_phy(vap_info.radio_index);
499 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf9b2ef02022-10-14 10:07:58 +0800500 fprintf(stderr, "Get mac address failed.\n");
developer9567a462022-11-17 20:42:05 +0800501 return;
developerf9b2ef02022-10-14 10:07:58 +0800502 }
developer402bf2a2022-10-04 15:20:18 +0800503
developer402bf2a2022-10-04 15:20:18 +0800504 // SSID
developerab985802022-10-07 09:42:31 +0800505 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
506 vap_info.u.bss_info.ssid[32] = '\0';
developer402bf2a2022-10-04 15:20:18 +0800507
developer2bcdef92022-12-13 16:19:09 +0800508 // interface
509 if (strlen(ap_param.ifname) != 0) {
510 strncpy(vap_info.vap_name, ap_param.ifname, 16);
developer53364a72023-04-24 10:59:12 +0800511 vap_info.vap_name[15] = '\0';
developer2bcdef92022-12-13 16:19:09 +0800512 }
513
developer9feeaf12023-02-17 15:52:18 +0800514 // Security
515 if (ap_param.security.mode == wifi_security_mode_wpa3_personal || ap_param.security.mode == wifi_security_mode_wpa3_transition){
516 // OpenWrt script only set psk, here we choose to set both psk and sae.
517 ap_param.security.u.key.type = wifi_security_key_type_psk_sae;
518 } else {
519 key_len = strlen(ap_param.security.u.key.key);
520 if (key_len == 64)
521 ap_param.security.u.key.type = wifi_security_key_type_psk;
522 else if (key_len >= 8 && key_len < 64)
523 ap_param.security.u.key.type = wifi_security_key_type_pass;
524 }
525
developer3ddad2f2022-10-13 13:33:57 +0800526 vap_info.u.bss_info.security.mode = ap_param.security.mode;
527 vap_info.u.bss_info.security.encr = ap_param.security.encr;
528 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
529 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
530 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developerc4548732023-02-06 20:02:10 +0800531
532 // hidden
533 vap_info.u.bss_info.showSsid = (ap_param.hidden ? 0 : 1);
developer4c2edbe2022-10-18 16:36:37 +0800534
developer223f5982023-02-28 15:59:38 +0800535 // igmpsn_enable
536 vap_info.u.bss_info.mcast2ucast = ap_param.igmpsn_enable;
537 fprintf(stderr, "Set igmpsn_enable: %d \n", ap_param.igmpsn_enable);
538
539 // wps_state
540 fprintf(stderr, "Set wps_state: %d \n", ap_param.wps_state);
developer0dff3a92023-03-17 15:08:42 +0800541 if (ap_param.wps_state == 2)
542 ret = wifi_setApWpsEnable(ap_param.ap_index, 1);
543 else
544 ret = wifi_setApWpsEnable(ap_param.ap_index, 0);
developer223f5982023-02-28 15:59:38 +0800545 if (ret != RETURN_OK)
546 fprintf(stderr, "[Set wps_state failed!!!]\n");
547 ret = 0;
548
developer223f5982023-02-28 15:59:38 +0800549 // macfilter
developer0dff3a92023-03-17 15:08:42 +0800550 fprintf(stderr, "Set macfilter: %s \n", ap_param.macfilter);
developer223f5982023-02-28 15:59:38 +0800551 if ((strcmp(ap_param.macfilter, "disable") == 0) || (strcmp(ap_param.macfilter, "\0") == 0) )
552 vap_info.u.bss_info.mac_filter_enable = false;
553 else if (strcmp(ap_param.macfilter, "deny") == 0){
554 vap_info.u.bss_info.mac_filter_enable = true;
555 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
556 }
557 else if (strcmp(ap_param.macfilter, "allow") == 0){
558 vap_info.u.bss_info.mac_filter_enable = true;
559 vap_info.u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
560 }
561 else
562 fprintf(stderr, "The macfilter tpye: %s is invalid!!!\n", ap_param.macfilter);
developer223f5982023-02-28 15:59:38 +0800563
564 // maclist
developer0dff3a92023-03-17 15:08:42 +0800565 fprintf(stderr, "Set maclist: %s \n", ap_param.maclist);
566 if ((strlen(ap_param.maclist) == 0)){
developer223f5982023-02-28 15:59:38 +0800567 ret = wifi_delApAclDevices(ap_param.ap_index);
568 if (ret != RETURN_OK)
569 fprintf(stderr, "[Del all maclist failed!!!]\n");
570 ret = 0;
developer0dff3a92023-03-17 15:08:42 +0800571 }
developera982e8f2023-03-09 15:47:10 +0800572 else if (strcmp(ap_param.macfilter, "allow") == 0) {
developer0dff3a92023-03-17 15:08:42 +0800573 maclist = strtok(ap_param.maclist, ";");
574 while (maclist != NULL)
575 {
576 ret = wifi_addApAclDevice(ap_param.ap_index, maclist);
577 if (ret != RETURN_OK)
578 fprintf(stderr, "[Add maclist failed!!!]\n");
579 ret = 0;
580 maclist = strtok(NULL, ";");
developer223f5982023-02-28 15:59:38 +0800581 }
developer0dff3a92023-03-17 15:08:42 +0800582 }
developera982e8f2023-03-09 15:47:10 +0800583 else if (strcmp(ap_param.macfilter, "deny") == 0) {
developer0dff3a92023-03-17 15:08:42 +0800584 maclist = strtok(ap_param.maclist, ";");
585 while (maclist != NULL)
586 {
587 ret = wifi_addApDenyAclDevice(ap_param.ap_index, maclist);
588 if (ret != RETURN_OK)
589 fprintf(stderr, "[Add maclist failed!!!]\n");
590 ret = 0;
591 maclist = strtok(NULL, ";");
developera982e8f2023-03-09 15:47:10 +0800592 }
developer0dff3a92023-03-17 15:08:42 +0800593 }
developer223f5982023-02-28 15:59:38 +0800594
595 ret = 0;
developerab985802022-10-07 09:42:31 +0800596 // Replace the setting with uci config
developer41965b52022-10-19 17:40:23 +0800597 map->vap_array[vap_index_in_map] = vap_info;
developer402bf2a2022-10-04 15:20:18 +0800598}
599
developer1ac0e892022-11-25 14:30:05 +0800600void set_sta_param(wifi_intf_param sta_param)
developer9567a462022-11-17 20:42:05 +0800601{
602 wifi_sta_network_t *sta = NULL;
603 mac_address_t sta_mac = {0};
604 char sta_mac_str[20] = {0};
605 char key_mgmt[16] = {0};
606 char pairwise[16] = {0};
607 int phy_index = 0;
608
609 sta = calloc(1, sizeof(wifi_sta_network_t));
610
611 phy_index = radio_index_to_phy(sta_param.radio_index);
612 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
613 mac_addr_ntoa(sta_mac_str, sta_mac);
614 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
615 sta->ssid[31] = '\0';
616 snprintf(sta->psk, 64, "%s", sta_param.password);
617
618 if (sta_param.security.mode == wifi_security_mode_none)
619 strcpy(key_mgmt, "NONE");
620 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
621 strcpy(key_mgmt, "SAE");
developer53364a72023-04-24 10:59:12 +0800622 else if (sta_param.security.mode == wifi_security_mode_enhanced_open)
developerf634eab2023-01-13 15:23:06 +0800623 strcpy(key_mgmt, "OWE");
developer9567a462022-11-17 20:42:05 +0800624 else
625 strcpy(key_mgmt, "WPA-PSK");
626 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
627
628 if (sta_param.security.encr == wifi_encryption_aes)
629 strcpy(pairwise, "CCMP");
630 else if (sta_param.security.encr == wifi_encryption_tkip)
631 strcpy(pairwise, "TKIP");
632 else
633 strcpy(pairwise, "CCMP TKIP");
634 snprintf(sta->pairwise, 64, "%s", pairwise);
635
636 if (strlen(sta_param.security.u.key.key) > 0)
637 strncpy(sta->psk, sta_param.security.u.key.key, 127);
638 sta->psk[127] = '\0';
639 sta->psk_len = strlen(sta->psk);
640
developerb3f00662022-12-29 09:35:55 +0800641 if (sta_param.wds_mode == TRUE)
642 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
643
644 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer9567a462022-11-17 20:42:05 +0800645
646 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
647 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
648 free(sta);
649 return;
650 }
651 free(sta);
652
653 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
654 fprintf(stderr, "Enable station failed\n");
655 return;
656 }
657}
658
developer0e3ff462023-04-28 17:48:40 +0800659void show_radio_param(wifi_radio_param radio_parameter)
660{
661 int ret = 0;
662 BOOL radio_enable = FALSE;
663 BOOL auto_channel_state = FALSE;
664 BOOL DFS_state = FALSE;
665 BOOL ZeroDFS_state = FALSE;
666 BOOL precac_state = FALSE;
667 BOOL AMSDU_state = FALSE;
668 BOOL Wmm_state = FALSE;
669 char RadioRates[128] = "";
670 UINT array_size = 128;
671 wifi_channelStats_t channelStats_array[128] = {0};
672
673 // radio
674 if(radio_parameter.radio_index == -1)
675 return;
676
677 wifi_getRadioEnable(radio_parameter.radio_index, &radio_enable);
678 if (radio_enable == FALSE)
679 return;
680
681 // RadioRates
682 ret = wifi_getRadioOperationalDataTransmitRates(radio_parameter.radio_index, RadioRates);
683 if (ret != RETURN_OK)
684 fprintf(stderr, "[Get Radio Operational Data Transmit Rates failed!!!]\n");
685 else
686 printf("radio%d Operational Data Transmit Rates: %s\n", radio_parameter.radio_index, RadioRates);
687 ret = 0;
688
689 // DFS
690 ret = wifi_getRadioDfsEnable(radio_parameter.radio_index, &DFS_state);
691 if (ret != RETURN_OK)
692 fprintf(stderr, "[Get Radio DFS state failed!!!]\n");
693 else
694 printf("radio%d DFS state: %d\n", radio_parameter.radio_index, DFS_state);
695 ret = 0;
696
697 ret = wifi_getZeroDFSState(radio_parameter.radio_index, &ZeroDFS_state, &precac_state);
698 if (ret != RETURN_OK)
699 fprintf(stderr, "[Get Radio ZeroDFS state failed!!!]\n");
700 else
701 printf("radio%d ZeroDFS state: %d, precac_state:%d\n", radio_parameter.radio_index, ZeroDFS_state, precac_state);
702 ret = 0;
703
704 // AMSDU
705 ret = wifi_getRadioAMSDUEnable(radio_parameter.radio_index, &AMSDU_state);
706 if (ret != RETURN_OK)
707 fprintf(stderr, "[Get Radio AMSDU state failed!!!]\n");
708 else
709 printf("radio%d AMSDU state: %d\n", radio_parameter.radio_index, AMSDU_state);
710 ret = 0;
711
712 // Wmm
713 ret = wifi_getApWmmUapsdEnable(radio_parameter.radio_index, &Wmm_state);
714 if (ret != RETURN_OK)
715 fprintf(stderr, "[Get Radio Wmm state failed!!!]\n");
716 else
717 printf("radio%d Wmm state: %d\n", radio_parameter.radio_index, Wmm_state);
718 ret = 0;
719
720 // channel
721 ret = wifi_getRadioChannelStats(radio_parameter.radio_index, channelStats_array, array_size);
722 if (ret != RETURN_OK)
723 fprintf(stderr, "[Get igmpsn_state failed!!!]\n");
724 else
725 printf("radio%d ch_number: %d\n", radio_parameter.radio_index, channelStats_array[0].ch_number);
726 ret = 0;
727
728 // auto_channel_state
729 ret = wifi_getRadioAutoChannelEnable(radio_parameter.radio_index, &auto_channel_state);
730 if (ret != RETURN_OK)
731 fprintf(stderr, "[Get auto_channel_state failed!!!]\n");
732 else
733 printf("radio%d auto_channel_state: %d\n", radio_parameter.radio_index, auto_channel_state);
734 ret = 0;
735
736}
737
developer223f5982023-02-28 15:59:38 +0800738void show_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
739{
740 int ret = 0;
developera982e8f2023-03-09 15:47:10 +0800741 int filter_mode = 0;
developer223f5982023-02-28 15:59:38 +0800742 int vap_index_in_map = 0;
743 int phy_index = 0;
744 wifi_vap_info_t vap_info = {0};
745 BOOL radio_enable = FALSE;
746 BOOL wps_state = FALSE;
developera982e8f2023-03-09 15:47:10 +0800747 BOOL igmpsn_state = FALSE;
developer223f5982023-02-28 15:59:38 +0800748 UINT buf_size = 1024;
749 char macArray[1024] = "";
developer0e3ff462023-04-28 17:48:40 +0800750 char RadiusIP[64] = "";
751 char RadiusSecret[64] = "";
752 UINT RadiusPort = 0;
developer223f5982023-02-28 15:59:38 +0800753
754 if(ap_param.radio_index == -1)
755 return;
756
757 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
758 if (radio_enable == FALSE)
759 return;
760
761
762 // get the index of the map
763 for (int i = 0; i < map->num_vaps; i++) {
764 if (map->vap_array[i].vap_index == ap_param.ap_index) {
765 vap_index_in_map = i;
766 break;
767 }
768 }
769
770 vap_info = map->vap_array[vap_index_in_map];
771 vap_info.u.bss_info.enabled = TRUE;
772 phy_index = radio_index_to_phy(vap_info.radio_index);
773
774 // SSID
775 printf("wifi%d ssid: %s\n", ap_param.ap_index, vap_info.u.bss_info.ssid);
776
developer0e3ff462023-04-28 17:48:40 +0800777 // SecurityRadiusServer
778 ret = wifi_getApSecurityRadiusServer(ap_param.radio_index, RadiusIP, &RadiusPort, RadiusSecret);
779 if (ret != RETURN_OK)
780 fprintf(stderr, "[Get Security Radius Server failed!!!]\n");
781 else{
782 printf("wifi%d Security Radius Server IP: %s\n", ap_param.ap_index, RadiusIP);
783 printf("wifi%d Security Radius Server Port: %d\n", ap_param.ap_index, RadiusPort);
784 printf("wifi%d Security Radius Server Secret: %s\n", ap_param.ap_index, RadiusSecret);
785 }
786 ret = 0;
787
developer0dff3a92023-03-17 15:08:42 +0800788 // igmpsn_enable
developera982e8f2023-03-09 15:47:10 +0800789 ret = wifi_getRadioIGMPSnoopingEnable(ap_param.radio_index, &igmpsn_state);
790 if (ret != RETURN_OK)
791 fprintf(stderr, "[Get igmpsn_state failed!!!]\n");
792 else
793 printf("wifi%d igmpsn_state: %d\n", ap_param.ap_index, igmpsn_state);
794 ret = 0;
developer223f5982023-02-28 15:59:38 +0800795
796 // wps_state
797 ret = wifi_getApWpsEnable(ap_param.ap_index, &wps_state);
798 if (ret != RETURN_OK)
developera982e8f2023-03-09 15:47:10 +0800799 fprintf(stderr, "[Get wps_state failed!!!]\n");
developer223f5982023-02-28 15:59:38 +0800800 else
801 printf("wifi%d wps_state: %d\n", ap_param.ap_index, wps_state);
802 ret = 0;
803
804 // macfilter
developera982e8f2023-03-09 15:47:10 +0800805 ret = wifi_getApMacAddressControlMode(ap_param.ap_index, &filter_mode);
806 if (ret != RETURN_OK)
807 fprintf(stderr, "[Get macfilter failed!!!]\n");
808 else{
809 if (filter_mode == 0)
810 printf("wifi%d macfilter: disable\n", ap_param.ap_index);
811 else if (filter_mode == 1)
812 printf("wifi%d macfilter: allow\n", ap_param.ap_index);
813 else if (filter_mode == 2)
814 printf("wifi%d macfilter: deny\n", ap_param.ap_index);
815 }
816 ret = 0;
developer223f5982023-02-28 15:59:38 +0800817
818 // maclist
819 printf("wifi%d maclist: \n", ap_param.ap_index);
developera982e8f2023-03-09 15:47:10 +0800820 if (filter_mode == 0)
821 printf("wifi%d macfilter is disable\n", ap_param.ap_index);
822 else if (filter_mode == 1){
823 ret = wifi_getApAclDevices(ap_param.ap_index, macArray, buf_size);
824 if (ret != RETURN_OK)
825 fprintf(stderr, "[Get maclist failed!!!]\n");
826 else
827 printf("%s \n", macArray);
828 ret = 0;
829 }
830 else if (filter_mode == 2){
831 ret = wifi_getApDenyAclDevices(ap_param.ap_index, macArray, buf_size);
832 if (ret != RETURN_OK)
833 fprintf(stderr, "[Get maclist failed!!!]\n");
834 else
835 printf("%s \n", macArray);
836 ret = 0;
837 }
developer223f5982023-02-28 15:59:38 +0800838
839}
840
developer402bf2a2022-10-04 15:20:18 +0800841int apply_uci_config ()
842{
843 struct uci_context *uci_ctx = uci_alloc_context();
844 struct uci_package *uci_pkg = NULL;
845 struct uci_element *e;
846 // struct uci_section *s;
847 const char cfg_name[] = "wireless";
848 int max_radio_num = 0;
849 BOOL parsing_radio = FALSE;
developer4c2edbe2022-10-18 16:36:37 +0800850 int apCount[3] = {0};
developer9567a462022-11-17 20:42:05 +0800851 int staCount[3] = {0};
developer41965b52022-10-19 17:40:23 +0800852 wifi_vap_info_map_t vap_map[3] = {0};
853 int ret = 0;
developer5f8ef5c2023-02-22 15:49:20 +0800854 int i = 0, j = 0;
developer402bf2a2022-10-04 15:20:18 +0800855
856 wifi_getMaxRadioNumber(&max_radio_num);
857 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developer41965b52022-10-19 17:40:23 +0800858 for (i = 0; i < max_radio_num ;i++ ){
859 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
860 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
861 fprintf(stderr, "[Get vap map failed!!!]\n");
developer41965b52022-10-19 17:40:23 +0800862 }
developer5f8ef5c2023-02-22 15:49:20 +0800863
864 /*reset radio's ap number, ap number ++ by uci config */
865 vap_map[i].num_vaps = 0;
developer41965b52022-10-19 17:40:23 +0800866 }
developer402bf2a2022-10-04 15:20:18 +0800867 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
868 uci_free_context(uci_ctx);
869 fprintf(stderr, "%s: load uci failed.\n", __func__);
870 return RETURN_ERR;
871 }
872
873 uci_foreach_element(&uci_pkg->sections, e) {
874
875 struct uci_section *s = uci_to_section(e);
876 struct uci_element *option = NULL;
877 wifi_radio_param radio_param = {0};
developer1ac0e892022-11-25 14:30:05 +0800878 wifi_intf_param intf_param = {0};
developer4c2edbe2022-10-18 16:36:37 +0800879 int phyId = 0;
developer402bf2a2022-10-04 15:20:18 +0800880 radio_param.radio_index = -1;
developer1ac0e892022-11-25 14:30:05 +0800881 intf_param.ap_index = -1;
developer402bf2a2022-10-04 15:20:18 +0800882
883 if (strcmp(s->type, "wifi-device") == 0) {
developer4c2edbe2022-10-18 16:36:37 +0800884 sscanf(s->e.name, "radio%d", &phyId);
885 radio_param.radio_index = phy_index_to_radio(phyId);
developer402bf2a2022-10-04 15:20:18 +0800886 parsing_radio = TRUE;
887 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
888 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer402bf2a2022-10-04 15:20:18 +0800889 parsing_radio = FALSE;
developer402bf2a2022-10-04 15:20:18 +0800890 }
891
892 uci_foreach_element(&s->options, option) {
893
894 struct uci_option *op = uci_to_option(option);
895 if (parsing_radio == TRUE) {
896 // transform the type from input string and store the value in radio_param.
897 if (strcmp(op->e.name, "channel") == 0)
898 set_channel(&radio_param, op->v.string);
899 else if (strcmp(op->e.name, "hwmode") == 0)
900 set_hwmode(&radio_param, op->v.string);
901 else if (strcmp(op->e.name, "htmode") == 0)
902 set_htmode(&radio_param, op->v.string);
903 else if (strcmp(op->e.name, "disabled") == 0)
904 set_disable(&radio_param, op->v.string);
905 else if (strcmp(op->e.name, "band") == 0)
906 set_band(&radio_param, op->v.string);
907 else if (strcmp(op->e.name, "country") == 0)
908 set_country(&radio_param, op->v.string);
909 else if (strcmp(op->e.name, "noscan") == 0)
developer54afa2c2022-10-18 17:44:13 +0800910 set_noscan(&radio_param, op->v.string);
developer1e946fd2022-12-26 17:09:00 +0800911 else if (strcmp(op->e.name, "rxantenna") == 0)
912 set_rxant(&radio_param, op->v.string);
913 else if (strcmp(op->e.name, "txantenna") == 0)
914 set_txant(&radio_param, op->v.string);
developerc4548732023-02-06 20:02:10 +0800915 else if (strcmp(op->e.name, "ht_coex") == 0)
916 set_htcoex(&radio_param, op->v.string);
917 else if (strcmp(op->e.name, "rts") == 0)
918 set_rts(&radio_param, op->v.string);
developer0e3ff462023-04-28 17:48:40 +0800919 else if (strcmp(op->e.name, "background_radar") == 0)
920 set_background_radar(&radio_param, op->v.string);
developer402bf2a2022-10-04 15:20:18 +0800921 else
922 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer0e3ff462023-04-28 17:48:40 +0800923 } else {
developer402bf2a2022-10-04 15:20:18 +0800924 // parsing iface
developer4c2edbe2022-10-18 16:36:37 +0800925 if (strcmp(op->e.name, "device") == 0){
developer1ac0e892022-11-25 14:30:05 +0800926 set_radionum(&intf_param, op->v.string);
developer9567a462022-11-17 20:42:05 +0800927 }else if (strcmp(op->e.name, "mode") == 0){
developerb97b5492022-12-22 19:44:36 +0800928 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer9567a462022-11-17 20:42:05 +0800929 if (strncmp(op->v.string, "sta", 3) == 0) {
developer1ac0e892022-11-25 14:30:05 +0800930 intf_param.sta_mode = TRUE;
931 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
932 staCount[intf_param.radio_index] ++ ;
933 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer9567a462022-11-17 20:42:05 +0800934 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer1ac0e892022-11-25 14:30:05 +0800935 intf_param.sta_mode = FALSE;
936 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
937 apCount[intf_param.radio_index] ++ ;
938 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer9567a462022-11-17 20:42:05 +0800939 }
developer4c2edbe2022-10-18 16:36:37 +0800940 }else if (strcmp(op->e.name, "ssid") == 0){
developer1ac0e892022-11-25 14:30:05 +0800941 set_ssid(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800942 }else if (strcmp(op->e.name, "encryption") == 0){
developer1ac0e892022-11-25 14:30:05 +0800943 set_encryption(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800944 }else if (strcmp(op->e.name, "key") == 0){
developer1ac0e892022-11-25 14:30:05 +0800945 set_key(&intf_param, op->v.string);
developer2bcdef92022-12-13 16:19:09 +0800946 }else if (strcmp(op->e.name, "ifname") == 0){
947 set_ifname(&intf_param, op->v.string);
developerb3f00662022-12-29 09:35:55 +0800948 }else if (strcmp(op->e.name, "wds") == 0){
949 set_wds(&intf_param, op->v.string);
developerc4548732023-02-06 20:02:10 +0800950 }else if (strcmp(op->e.name, "hidden") == 0){
951 set_hidden(&intf_param, op->v.string);
developer223f5982023-02-28 15:59:38 +0800952 }else if (strcmp(op->e.name, "igmpsn_enable") == 0){
953 set_igmpsn_enable(&intf_param, op->v.string);
954 }else if (strcmp(op->e.name, "wps_state") == 0){
955 set_wps_state(&intf_param, op->v.string);
developer223f5982023-02-28 15:59:38 +0800956 }else if (strcmp(op->e.name, "macfilter") == 0){
957 set_macfilter(&intf_param, op->v.string);
958 }else if (strcmp(op->e.name, "maclist") == 0){
959 set_maclist(&intf_param, op->v.string);
developer4c2edbe2022-10-18 16:36:37 +0800960 }else{
developer402bf2a2022-10-04 15:20:18 +0800961 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer223f5982023-02-28 15:59:38 +0800962 }
developer402bf2a2022-10-04 15:20:18 +0800963 }
964 }
developer5f8ef5c2023-02-22 15:49:20 +0800965 if (parsing_radio == TRUE) {
developer402bf2a2022-10-04 15:20:18 +0800966 set_radio_param(radio_param);
developer5f8ef5c2023-02-22 15:49:20 +0800967 }
developer1ac0e892022-11-25 14:30:05 +0800968 else if (intf_param.sta_mode == TRUE)
969 set_sta_param(intf_param);
developer5f8ef5c2023-02-22 15:49:20 +0800970 else {
developer1ac0e892022-11-25 14:30:05 +0800971 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer5f8ef5c2023-02-22 15:49:20 +0800972 vap_map[intf_param.radio_index].num_vaps++;
973 }
developer402bf2a2022-10-04 15:20:18 +0800974 }
developer5f8ef5c2023-02-22 15:49:20 +0800975
976 /* disable all interface, re-enable by UCI configuration */
977
978 fprintf(stderr, "\n----- Disable all interfaces. -----\n");
979
980 for (i = 0; i < max_radio_num; i++ ){
developer0148a3d2023-03-08 09:29:34 +0800981 for(j = max_radio_num*(MAX_NUM_VAP_PER_RADIO-1) + i; j >= 0; j -= max_radio_num) {
982 ret = wifi_setApEnable(j, FALSE);
developer5f8ef5c2023-02-22 15:49:20 +0800983 if (ret != RETURN_OK)
developer0148a3d2023-03-08 09:29:34 +0800984 fprintf(stderr, "[disable ap %d failed!!!]\n", j);
985 else
986 fprintf(stderr, "[disable ap %d success.]\n", j);
developer5f8ef5c2023-02-22 15:49:20 +0800987 }
988 }
developer9567a462022-11-17 20:42:05 +0800989 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer402bf2a2022-10-04 15:20:18 +0800990
developer5f8ef5c2023-02-22 15:49:20 +0800991 for (i = 0; i < max_radio_num ;i++ ){
992
993 fprintf(stderr, "\n create Vap radio:%d num_vaps=%d.\n", i, vap_map[i].num_vaps);
developer41965b52022-10-19 17:40:23 +0800994 ret = wifi_createVAP(i, &vap_map[i]);
995 if (ret != RETURN_OK)
996 fprintf(stderr, "[Apply vap setting failed!!!]\n");
997 }
developer223f5982023-02-28 15:59:38 +0800998
developer0dff3a92023-03-17 15:08:42 +0800999 for (i = 0; i < 3 ;i++ ){
1000 apCount[i] = 0;
1001 staCount[i] = 0;
1002 }
1003
1004 uci_foreach_element(&uci_pkg->sections, e) {
1005
1006 struct uci_section *s = uci_to_section(e);
1007 struct uci_element *option = NULL;
1008 wifi_radio_param radio_param = {0};
1009 wifi_intf_param intf_param = {0};
1010 int phyId = 0;
1011 radio_param.radio_index = -1;
1012 intf_param.ap_index = -1;
1013
1014 if (strcmp(s->type, "wifi-iface") == 0) {
1015 uci_foreach_element(&s->options, option) {
1016
1017 struct uci_option *op = uci_to_option(option);
1018 {
1019 // parsing iface
1020 if (strcmp(op->e.name, "device") == 0){
1021 set_radionum(&intf_param, op->v.string);
1022 }else if (strcmp(op->e.name, "mode") == 0){
1023 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
1024 if (strncmp(op->v.string, "sta", 3) == 0) {
1025 intf_param.sta_mode = TRUE;
1026 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
1027 staCount[intf_param.radio_index] ++ ;
1028 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
1029 } else if (strncmp(op->v.string, "ap", 2) == 0) {
1030 intf_param.sta_mode = FALSE;
1031 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
1032 apCount[intf_param.radio_index] ++ ;
1033 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
1034 }
1035 }else if (strcmp(op->e.name, "wps_cancel") == 0){
1036 set_wps_cancel(&intf_param, op->v.string);
1037 }else if (strcmp(op->e.name, "wps_pushbutton") == 0){
1038 set_wps_pushbutton(&intf_param, op->v.string);
1039 }
1040 }
1041 }
1042
1043 // wps_cancel
1044 fprintf(stderr, "Set wps_cancel: %d \n", intf_param.wps_cancel);
1045 if (intf_param.wps_cancel){
1046 ret = wifi_cancelApWPS(intf_param.ap_index);
1047 if (ret != RETURN_OK)
1048 fprintf(stderr, "[Set wps_cancel failed!!!]\n");
1049 ret = 0;
1050 }
1051
1052 // wps_pushbutton
1053 fprintf(stderr, "Set wps_pushbutton: %d \n", intf_param.wps_pushbutton);
1054 if (intf_param.wps_pushbutton){
1055 ret = wifi_setApWpsButtonPush(intf_param.ap_index);
1056 if (ret != RETURN_OK)
1057 fprintf(stderr, "[Set wps_pushbutton failed!!!]\n");
1058 ret = 0;
1059 }
1060 }
1061 }
1062
developer402bf2a2022-10-04 15:20:18 +08001063 uci_unload(uci_ctx, uci_pkg);
1064 uci_free_context(uci_ctx);
1065 return RETURN_OK;
1066}
1067
developer223f5982023-02-28 15:59:38 +08001068int dump_wifi_status ()
1069{
1070
1071 struct uci_context *uci_ctx = uci_alloc_context();
1072 struct uci_package *uci_pkg = NULL;
1073 struct uci_element *e;
1074 // struct uci_section *s;
1075 const char cfg_name[] = "wireless";
1076 int max_radio_num = 0;
1077 BOOL parsing_radio = FALSE;
1078 int apCount[3] = {0};
1079 int staCount[3] = {0};
1080 wifi_vap_info_map_t vap_map[3] = {0};
1081 int ret = 0;
1082 int i = 0;
1083
1084 wifi_getMaxRadioNumber(&max_radio_num);
1085 fprintf(stderr, "max radio number: %d\n", max_radio_num);
1086 for (i = 0; i < max_radio_num ;i++ ){
1087 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
1088 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
1089 fprintf(stderr, "[Get vap map failed!!!]\n");
1090 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
1091 }
1092 }
1093 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
1094 uci_free_context(uci_ctx);
1095 fprintf(stderr, "%s: load uci failed.\n", __func__);
1096 return RETURN_ERR;
1097 }
1098
1099 uci_foreach_element(&uci_pkg->sections, e) {
1100
1101 struct uci_section *s = uci_to_section(e);
1102 struct uci_element *option = NULL;
1103 wifi_radio_param radio_param = {0};
1104 wifi_intf_param intf_param = {0};
1105 int phyId = 0;
1106 radio_param.radio_index = -1;
1107 intf_param.ap_index = -1;
1108
1109 if (strcmp(s->type, "wifi-device") == 0) {
1110 sscanf(s->e.name, "radio%d", &phyId);
1111 radio_param.radio_index = phy_index_to_radio(phyId);
1112 parsing_radio = TRUE;
1113 fprintf(stderr, "\n----- Start show radio %d config. -----\n", radio_param.radio_index);
1114 } else if (strcmp(s->type, "wifi-iface") == 0) {
1115 parsing_radio = FALSE;
1116 }
1117
1118 uci_foreach_element(&s->options, option) {
1119
1120 struct uci_option *op = uci_to_option(option);
1121 if (parsing_radio == TRUE) {
1122 // transform the type from input string and store the value in radio_param.
1123 } else {
1124 // parsing iface
1125 if (strcmp(op->e.name, "device") == 0){
1126 set_radionum(&intf_param, op->v.string);
1127 }else if (strcmp(op->e.name, "mode") == 0){
1128 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
1129 if (strncmp(op->v.string, "sta", 3) == 0) {
1130 intf_param.sta_mode = TRUE;
1131 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
1132 staCount[intf_param.radio_index] ++ ;
1133 fprintf(stderr, "\n----- Start show sta %d config. -----\n", intf_param.sta_index);
1134 } else if (strncmp(op->v.string, "ap", 2) == 0) {
1135 intf_param.sta_mode = FALSE;
1136 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
1137 apCount[intf_param.radio_index] ++ ;
1138 fprintf(stderr, "\n----- Start show ap %d config. -----\n", intf_param.ap_index);
1139 }
1140 }
1141 }
1142 }
developer0e3ff462023-04-28 17:48:40 +08001143 if (parsing_radio == TRUE){
developer223f5982023-02-28 15:59:38 +08001144 printf("show radio params: \n");
developer0e3ff462023-04-28 17:48:40 +08001145 show_radio_param(radio_param);
1146 }
developer223f5982023-02-28 15:59:38 +08001147 else if (intf_param.sta_mode == TRUE)
1148 printf("show sta params: \n");
1149 // show_sta_param(intf_param);
1150 else{
1151 printf("show ap params: \n");
1152 show_ap_param(intf_param, &vap_map[intf_param.radio_index]);
1153 }
1154 }
1155
1156 uci_unload(uci_ctx, uci_pkg);
1157 uci_free_context(uci_ctx);
1158 return RETURN_OK;
1159
1160}
1161
developer402bf2a2022-10-04 15:20:18 +08001162int main(int argc, char **argv)
1163{
developer223f5982023-02-28 15:59:38 +08001164 if (argc != 2 || (strcmp(argv[1], "reload") != 0 && strcmp(argv[1], "dump") != 0) ){
1165 fprintf(stderr, "Usage: wifi reload/wifi dump.\nThis tool is only for RDKB MSP/SQC test.\n");
developer402bf2a2022-10-04 15:20:18 +08001166 return -1;
1167 }
developer223f5982023-02-28 15:59:38 +08001168 if (strcmp(argv[1], "reload") == 0)
1169 apply_uci_config();
1170 else if (strcmp(argv[1], "dump") == 0)
1171 dump_wifi_status();
developer402bf2a2022-10-04 15:20:18 +08001172 return 0;
1173}