blob: 17a8b20f67b26b95b1b466d60663e5fe41f072fa [file] [log] [blame]
developer91f80742022-10-04 15:20:18 +08001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include <uci.h>
6#include "wifi-test-tool.h"
7
developer50614832022-11-17 20:42:05 +08008static int mac_addr_aton(unsigned char *mac_addr, char *arg)
9{
10 sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
11 return 0;
12}
13
14static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
15{
16 snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", arg[0], arg[1],arg[2],arg[3],arg[4],arg[5]);
17 return;
18}
developer8d8d6302022-10-18 16:36:37 +080019
20static int _syscmd(char *cmd, char *retBuf, int retBufSize)
21{
22 FILE *f;
23 char *ptr = retBuf;
24 int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0;
25
26
27 if((f = popen(cmd, "r")) == NULL) {
28 fprintf(stderr,"\npopen %s error\n", cmd);
29 return RETURN_ERR;
30 }
31
32 while(!feof(f))
33 {
34 *ptr = 0;
35 if(bufSize>=128) {
36 bufbytes=128;
37 } else {
38 bufbytes=bufSize-1;
39 }
40
41 fgets(ptr,bufbytes,f);
42 readbytes=strlen(ptr);
43
44 if(!readbytes)
45 break;
46
47 bufSize-=readbytes;
48 ptr += readbytes;
49 }
50 cmd_ret = pclose(f);
51 retBuf[retBufSize-1]=0;
52
53 return cmd_ret >> 8;
54}
55
56int phy_index_to_radio(int phyIndex)
57{
58 char cmd[128] = {0};
59 char buf[64] = {0};
60 int radioIndex = 0;
61 snprintf(cmd, sizeof(cmd), "ls /tmp | grep phy%d | cut -d '-' -f2 | tr -d '\n'", phyIndex);
62 _syscmd(cmd, buf, sizeof(buf));
63
64 if (strlen(buf) == 0 || strstr(buf, "wifi") == NULL) {
65 fprintf(stderr, "%s: failed to get wifi index\n", __func__);
66 return RETURN_ERR;
67 }
68 sscanf(buf, "wifi%d", &radioIndex);
developerc6a27322023-01-13 15:23:06 +080069 return radioIndex;
developer8d8d6302022-10-18 16:36:37 +080070}
71
developer91f80742022-10-04 15:20:18 +080072void set_channel(wifi_radio_param *radio_param, char *channel)
73{
developer63d72772022-10-07 09:42:31 +080074 if (strcmp(channel, "auto") == 0) {
75 radio_param->auto_channel = TRUE;
76 radio_param->channel = 0;
77 } else {
developer91f80742022-10-04 15:20:18 +080078 radio_param->auto_channel = FALSE;
developer63d72772022-10-07 09:42:31 +080079 radio_param->channel = strtol(channel, NULL, 10);
developer91f80742022-10-04 15:20:18 +080080 }
developer63d72772022-10-07 09:42:31 +080081 return;
developer91f80742022-10-04 15:20:18 +080082}
83
developer52c6ca22022-10-06 17:16:43 +080084void set_country(wifi_radio_param *radio_param, char *country)
developer91f80742022-10-04 15:20:18 +080085{
86 strcpy(radio_param->country, country);
87}
88
developer52c6ca22022-10-06 17:16:43 +080089void set_band(wifi_radio_param *radio_param, char *band)
developer91f80742022-10-04 15:20:18 +080090{
91 strcpy(radio_param->band, band);
92}
93
developer6feac682022-10-18 17:44:13 +080094void set_noscan(wifi_radio_param *radio_param, char *noscan)
95{
96 snprintf(radio_param->noscan, 2, "%s", noscan);
97 radio_param->noscan[1] = '\0';
98}
99
developer91f80742022-10-04 15:20:18 +0800100void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
101{
102 if (strncmp(hwmode, "11a", 3) == 0)
103 strcpy(radio_param->hwmode, "a");
104 if (strncmp(hwmode, "11b", 3) == 0)
105 strcpy(radio_param->hwmode, "b");
106 if (strncmp(hwmode, "11g", 3) == 0)
107 strcpy(radio_param->hwmode, "g");
108}
109
110void set_htmode(wifi_radio_param *radio_param, char *htmode)
111{
112 char tmp[16] = {0};
113 char *ptr = htmode;
114 ULONG bandwidth = 0;
115 radio_param->bandwidth = 20;
116 while (*ptr) {
117 if (isdigit(*ptr)) {
118 bandwidth = strtoul(ptr, NULL, 10);
119 radio_param->bandwidth = bandwidth;
120 break;
121 }
122 ptr++;
123 }
124
125 // HT40 -> 11NGHT40PLUS
126 // VHT40+ -> 11ACVHT40PLUS
127 // HE80 -> 11AXHE80
128 if (strstr(htmode, "+") != NULL) {
129 strncpy(tmp, htmode, strlen(htmode) - 1);
130 strcat(tmp, "PLUS");
131 } else if (strstr(htmode, "-") != NULL) {
132 strncpy(tmp, htmode, strlen(htmode) - 1);
133 strcat(tmp, "MINUS");
134 } else
135 strcpy(tmp, htmode);
136
137
138 if (strstr(htmode, "VHT") != NULL) {
139 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
140 } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
141 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
142 } else if (strstr(htmode, "HE") != NULL) {
143 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
144 } else { // NOHT or NONE should be parsed with the band, so just fill the original string.
145 strcpy(radio_param->htmode, tmp);
146 }
147
148}
149
150void set_disable(wifi_radio_param *radio_param, char *disable)
151{
152 if (strcmp(disable, "1") == 0)
153 radio_param->disabled = TRUE;
154 else
155 radio_param->disabled = FALSE;
156}
157
developer128f8aa2022-12-26 17:09:00 +0800158void set_rxant(wifi_radio_param *radio_param, char *mask)
159{
160 radio_param->rxantenna = strtol(mask, NULL, 16);
161}
162
163void set_txant(wifi_radio_param *radio_param, char *mask)
164{
165 radio_param->txantenna = strtol(mask, NULL, 16);
166}
167
developer13ec7352023-02-06 20:02:10 +0800168void set_htcoex(wifi_radio_param *radio_param, char *ht_coex)
169{
170 radio_param->ht_coex = strtol(ht_coex, NULL, 10);
171}
172
173void set_rts(wifi_radio_param *radio_param, char *rts)
174{
175 radio_param->rtsThreshold = strtol(rts, NULL, 10);
176}
177
developer465ca0c2022-11-25 14:30:05 +0800178void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800179{
developer50614832022-11-17 20:42:05 +0800180 int radio_num = 0;
181 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800182 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800183
184 while (*ptr) {
185 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800186 phyId = strtoul(ptr, NULL, 10);
187 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800188 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800189 break;
190 }
191 ptr++;
192 }
193}
194
developer465ca0c2022-11-25 14:30:05 +0800195void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800196{
developer465ca0c2022-11-25 14:30:05 +0800197 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800198}
199
developer465ca0c2022-11-25 14:30:05 +0800200void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800201{
developerff378f22022-10-13 13:33:57 +0800202 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800203 intf_param->security.mode = wifi_security_mode_none;
204 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800205 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800206 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800207 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800208 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800209 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800210 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800211 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800212 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800213 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800214 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800215 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800216 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800217 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800218 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800219 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800220 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerc6a27322023-01-13 15:23:06 +0800221 }else if(strcmp(encryption_mode, "owe") == 0){
222 intf_param->security.mode = wifi_security_mode_owe;
developer91f80742022-10-04 15:20:18 +0800223 }
224
developerff378f22022-10-13 13:33:57 +0800225 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800226 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800227 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800228 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800229 }else{
developer465ca0c2022-11-25 14:30:05 +0800230 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800231 }
developerff378f22022-10-13 13:33:57 +0800232
developerc6a27322023-01-13 15:23:06 +0800233 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer465ca0c2022-11-25 14:30:05 +0800234 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800235 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800236 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800237 }else{
developer465ca0c2022-11-25 14:30:05 +0800238 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800239 }
240
241 if (!strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800242 intf_param->security.u.key.type = wifi_security_key_type_sae;
developerff378f22022-10-13 13:33:57 +0800243 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800244 intf_param->security.u.key.type = wifi_security_key_type_psk_sae;
developerff378f22022-10-13 13:33:57 +0800245 }else{
developer465ca0c2022-11-25 14:30:05 +0800246 intf_param->security.u.key.type = wifi_security_key_type_psk;
developer91f80742022-10-04 15:20:18 +0800247 }
developerff378f22022-10-13 13:33:57 +0800248
developer91f80742022-10-04 15:20:18 +0800249}
250
developer465ca0c2022-11-25 14:30:05 +0800251void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800252{
developer465ca0c2022-11-25 14:30:05 +0800253 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800254}
255
developer7ac3bd52022-12-13 16:19:09 +0800256void set_ifname(wifi_intf_param *intf_param, char *ifname)
257{
258 if (strlen(ifname) > 15)
259 return;
260 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
261}
262
developerb2123782022-12-29 09:35:55 +0800263void set_wds(wifi_intf_param *intf_param, char *wds_enable)
264{
265 intf_param->wds_mode = FALSE;
266 if (strncmp(wds_enable, "1", 1) == 0)
267 intf_param->wds_mode = TRUE;
268}
269
developer13ec7352023-02-06 20:02:10 +0800270void set_hidden(wifi_intf_param *intf_param, char *hidden)
271{
272 intf_param->hidden = strtol(hidden, NULL, 10);
273}
274
developer50614832022-11-17 20:42:05 +0800275int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800276{
277 FILE *f;
278 char mac_file[64] = {0};
279 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800280
developer1ac426a2022-12-22 19:44:36 +0800281 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800282 f = fopen(mac_file, "r");
283 if (f == NULL)
284 return -1;
285 fgets(mac_address, 20, f);
286 fclose(f);
287
developer50614832022-11-17 20:42:05 +0800288 mac_addr_aton(&(*bssid)[0], mac_address);
289 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800290 return 0;
291}
292
developer91f80742022-10-04 15:20:18 +0800293void set_radio_param(wifi_radio_param radio_parameter)
294{
developer91f80742022-10-04 15:20:18 +0800295 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800296 wifi_radio_operationParam_t operationParam = {0};
297
developer8d8d6302022-10-18 16:36:37 +0800298 if(radio_parameter.radio_index == -1)
299 return;
300
developer63d72772022-10-07 09:42:31 +0800301 if (radio_parameter.disabled == TRUE) {
302 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
303 return;
304 }
developer91f80742022-10-04 15:20:18 +0800305
306 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800307
developer128f8aa2022-12-26 17:09:00 +0800308 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
309 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
310 if (ret != RETURN_OK)
311 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
312 }
313
developerbf812932022-10-17 17:37:29 +0800314 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800315 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
316 if (ret != RETURN_OK)
317 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800318 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800319
developer91f80742022-10-04 15:20:18 +0800320 // Channel
developer63d72772022-10-07 09:42:31 +0800321 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
322 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800323
developer25e07812022-10-13 15:27:02 +0800324 //bandwidth
325 if (radio_parameter.bandwidth == 20){
326 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
327 }else if (radio_parameter.bandwidth == 40){
328 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
329 }else if (radio_parameter.bandwidth == 80){
330 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
331 }else if (radio_parameter.bandwidth == 160){
332 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
333 }
developer91f80742022-10-04 15:20:18 +0800334
335 // htmode
developer63d72772022-10-07 09:42:31 +0800336 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800337 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800338 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800339 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
340 strcpy(radio_parameter.htmode, "11G");
341
developer63d72772022-10-07 09:42:31 +0800342 if (strstr(radio_parameter.htmode, "HE") != NULL)
343 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800344
developer63d72772022-10-07 09:42:31 +0800345 } else if (strcmp(radio_parameter.band, "5g") == 0) {
346 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800347 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
348 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800349
350 if (strstr(radio_parameter.htmode, "HE") != NULL)
351 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800352 }else if (strcmp(radio_parameter.band, "6g") == 0) {
353 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
354 }
developer91f80742022-10-04 15:20:18 +0800355
356 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800357 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800358 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800359 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800360
developer63d72772022-10-07 09:42:31 +0800361 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800362
developer13ec7352023-02-06 20:02:10 +0800363 // rtsThreshold, zero means not set
364 if ((radio_parameter.rtsThreshold < 65535) && radio_parameter.rtsThreshold)
365 operationParam.rtsThreshold = radio_parameter.rtsThreshold;
366
367 //ht_coex
368 operationParam.obssCoex = radio_parameter.ht_coex;
369
developer63d72772022-10-07 09:42:31 +0800370 // apply setting
371 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800372 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800373 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800374
developerbb58a932022-11-07 16:58:17 +0800375 // Country
376 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
377 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
378 if (ret != RETURN_OK)
379 fprintf(stderr, "[Set Country failed!!!]\n");
380 ret = 0;
381
382 // hwmode
383 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
384 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
385 if (ret != RETURN_OK)
386 fprintf(stderr, "[Set hwmode failed!!!]\n");
387 ret = 0;
388
389 // noscan
390 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
391 if(strlen(radio_parameter.noscan)){
392 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
393 if (ret != RETURN_OK)
394 fprintf(stderr, "[Set noscan failed!!!]\n");
395 }
396 ret = 0;
397
developer91f80742022-10-04 15:20:18 +0800398}
399
developer465ca0c2022-11-25 14:30:05 +0800400void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800401{
402 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800403 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800404 int phy_index = 0;
developer63d72772022-10-07 09:42:31 +0800405 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800406 BOOL radio_enable = FALSE;
407
developerff42a302022-10-19 17:40:23 +0800408 if(ap_param.radio_index == -1)
409 return;
410
developerbf812932022-10-17 17:37:29 +0800411 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
412 if (radio_enable == FALSE)
413 return;
developer63d72772022-10-07 09:42:31 +0800414
developerff42a302022-10-19 17:40:23 +0800415
416 // get the index of the map
417 for (int i = 0; i < map->num_vaps; i++) {
418 if (map->vap_array[i].vap_index == ap_param.ap_index) {
419 vap_index_in_map = i;
420 break;
developer63d72772022-10-07 09:42:31 +0800421 }
422 }
423
developerff42a302022-10-19 17:40:23 +0800424
developerf7e50b02022-10-14 10:07:58 +0800425 fprintf(stderr, "Start setting ap\n");
426
developerff42a302022-10-19 17:40:23 +0800427 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800428 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800429 phy_index = radio_index_to_phy(vap_info.radio_index);
430 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800431 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800432 return;
developerf7e50b02022-10-14 10:07:58 +0800433 }
developer91f80742022-10-04 15:20:18 +0800434
developer91f80742022-10-04 15:20:18 +0800435 // SSID
developer63d72772022-10-07 09:42:31 +0800436 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
437 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800438
developer7ac3bd52022-12-13 16:19:09 +0800439 // interface
440 if (strlen(ap_param.ifname) != 0) {
441 strncpy(vap_info.vap_name, ap_param.ifname, 16);
442 vap_info.vap_name[15] = "\0";
443 }
444
developerff378f22022-10-13 13:33:57 +0800445 vap_info.u.bss_info.security.mode = ap_param.security.mode;
446 vap_info.u.bss_info.security.encr = ap_param.security.encr;
447 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
448 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
449 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer13ec7352023-02-06 20:02:10 +0800450
451 // hidden
452 vap_info.u.bss_info.showSsid = (ap_param.hidden ? 0 : 1);
developer8d8d6302022-10-18 16:36:37 +0800453
developer63d72772022-10-07 09:42:31 +0800454 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800455 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800456}
457
developer465ca0c2022-11-25 14:30:05 +0800458void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800459{
460 wifi_sta_network_t *sta = NULL;
461 mac_address_t sta_mac = {0};
462 char sta_mac_str[20] = {0};
463 char key_mgmt[16] = {0};
464 char pairwise[16] = {0};
465 int phy_index = 0;
466
467 sta = calloc(1, sizeof(wifi_sta_network_t));
468
469 phy_index = radio_index_to_phy(sta_param.radio_index);
470 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
471 mac_addr_ntoa(sta_mac_str, sta_mac);
472 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
473 sta->ssid[31] = '\0';
474 snprintf(sta->psk, 64, "%s", sta_param.password);
475
476 if (sta_param.security.mode == wifi_security_mode_none)
477 strcpy(key_mgmt, "NONE");
478 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
479 strcpy(key_mgmt, "SAE");
developerc6a27322023-01-13 15:23:06 +0800480 else if (sta_param.security.mode == wifi_security_mode_owe)
481 strcpy(key_mgmt, "OWE");
developer50614832022-11-17 20:42:05 +0800482 else
483 strcpy(key_mgmt, "WPA-PSK");
484 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
485
486 if (sta_param.security.encr == wifi_encryption_aes)
487 strcpy(pairwise, "CCMP");
488 else if (sta_param.security.encr == wifi_encryption_tkip)
489 strcpy(pairwise, "TKIP");
490 else
491 strcpy(pairwise, "CCMP TKIP");
492 snprintf(sta->pairwise, 64, "%s", pairwise);
493
494 if (strlen(sta_param.security.u.key.key) > 0)
495 strncpy(sta->psk, sta_param.security.u.key.key, 127);
496 sta->psk[127] = '\0';
497 sta->psk_len = strlen(sta->psk);
498
developerb2123782022-12-29 09:35:55 +0800499 if (sta_param.wds_mode == TRUE)
500 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
501
502 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer50614832022-11-17 20:42:05 +0800503
504 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
505 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
506 free(sta);
507 return;
508 }
509 free(sta);
510
511 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
512 fprintf(stderr, "Enable station failed\n");
513 return;
514 }
515}
516
developer91f80742022-10-04 15:20:18 +0800517int apply_uci_config ()
518{
519 struct uci_context *uci_ctx = uci_alloc_context();
520 struct uci_package *uci_pkg = NULL;
521 struct uci_element *e;
522 // struct uci_section *s;
523 const char cfg_name[] = "wireless";
524 int max_radio_num = 0;
525 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800526 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800527 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800528 wifi_vap_info_map_t vap_map[3] = {0};
529 int ret = 0;
530 int i = 0;
developer91f80742022-10-04 15:20:18 +0800531
532 wifi_getMaxRadioNumber(&max_radio_num);
533 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800534 for (i = 0; i < max_radio_num ;i++ ){
535 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
536 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
537 fprintf(stderr, "[Get vap map failed!!!]\n");
538 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
539 }
540 }
developer91f80742022-10-04 15:20:18 +0800541 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
542 uci_free_context(uci_ctx);
543 fprintf(stderr, "%s: load uci failed.\n", __func__);
544 return RETURN_ERR;
545 }
546
547 uci_foreach_element(&uci_pkg->sections, e) {
548
549 struct uci_section *s = uci_to_section(e);
550 struct uci_element *option = NULL;
551 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800552 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800553 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800554 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800555 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800556
557 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800558 sscanf(s->e.name, "radio%d", &phyId);
559 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800560 parsing_radio = TRUE;
561 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
562 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800563 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800564 }
565
566 uci_foreach_element(&s->options, option) {
567
568 struct uci_option *op = uci_to_option(option);
569 if (parsing_radio == TRUE) {
570 // transform the type from input string and store the value in radio_param.
571 if (strcmp(op->e.name, "channel") == 0)
572 set_channel(&radio_param, op->v.string);
573 else if (strcmp(op->e.name, "hwmode") == 0)
574 set_hwmode(&radio_param, op->v.string);
575 else if (strcmp(op->e.name, "htmode") == 0)
576 set_htmode(&radio_param, op->v.string);
577 else if (strcmp(op->e.name, "disabled") == 0)
578 set_disable(&radio_param, op->v.string);
579 else if (strcmp(op->e.name, "band") == 0)
580 set_band(&radio_param, op->v.string);
581 else if (strcmp(op->e.name, "country") == 0)
582 set_country(&radio_param, op->v.string);
583 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800584 set_noscan(&radio_param, op->v.string);
developer128f8aa2022-12-26 17:09:00 +0800585 else if (strcmp(op->e.name, "rxantenna") == 0)
586 set_rxant(&radio_param, op->v.string);
587 else if (strcmp(op->e.name, "txantenna") == 0)
588 set_txant(&radio_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800589 else if (strcmp(op->e.name, "ht_coex") == 0)
590 set_htcoex(&radio_param, op->v.string);
591 else if (strcmp(op->e.name, "rts") == 0)
592 set_rts(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800593 else
594 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
595 } else {
596 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800597 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800598 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800599 }else if (strcmp(op->e.name, "mode") == 0){
developer1ac426a2022-12-22 19:44:36 +0800600 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer50614832022-11-17 20:42:05 +0800601 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800602 intf_param.sta_mode = TRUE;
603 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
604 staCount[intf_param.radio_index] ++ ;
605 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800606 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800607 intf_param.sta_mode = FALSE;
608 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
609 apCount[intf_param.radio_index] ++ ;
610 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800611 }
developer8d8d6302022-10-18 16:36:37 +0800612 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800613 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800614 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800615 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800616 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800617 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800618 }else if (strcmp(op->e.name, "ifname") == 0){
619 set_ifname(&intf_param, op->v.string);
developerb2123782022-12-29 09:35:55 +0800620 }else if (strcmp(op->e.name, "wds") == 0){
621 set_wds(&intf_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800622 }else if (strcmp(op->e.name, "hidden") == 0){
623 set_hidden(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800624 }else{
developer91f80742022-10-04 15:20:18 +0800625 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800626 }
developer91f80742022-10-04 15:20:18 +0800627 }
628 }
629 if (parsing_radio == TRUE)
630 set_radio_param(radio_param);
developer465ca0c2022-11-25 14:30:05 +0800631 else if (intf_param.sta_mode == TRUE)
632 set_sta_param(intf_param);
developer91f80742022-10-04 15:20:18 +0800633 else
developer465ca0c2022-11-25 14:30:05 +0800634 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800635 }
developer50614832022-11-17 20:42:05 +0800636 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800637
developerff42a302022-10-19 17:40:23 +0800638 for (i = 0; i < max_radio_num ;i++ ){
639 ret = wifi_createVAP(i, &vap_map[i]);
640 if (ret != RETURN_OK)
641 fprintf(stderr, "[Apply vap setting failed!!!]\n");
642 }
643
developer91f80742022-10-04 15:20:18 +0800644 uci_unload(uci_ctx, uci_pkg);
645 uci_free_context(uci_ctx);
646 return RETURN_OK;
647}
648
649int main(int argc, char **argv)
650{
651 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
652 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
653 return -1;
654 }
655 apply_uci_config();
656 return 0;
657}