blob: 84cdc92211406abd266606ac1a9e043181e8409c [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);
69 fprintf(stderr, "%s: radio index = %d \n", __func__, radioIndex);
70 return radioIndex;
71}
72
developer91f80742022-10-04 15:20:18 +080073void set_channel(wifi_radio_param *radio_param, char *channel)
74{
developer63d72772022-10-07 09:42:31 +080075 if (strcmp(channel, "auto") == 0) {
76 radio_param->auto_channel = TRUE;
77 radio_param->channel = 0;
78 } else {
developer91f80742022-10-04 15:20:18 +080079 radio_param->auto_channel = FALSE;
developer63d72772022-10-07 09:42:31 +080080 radio_param->channel = strtol(channel, NULL, 10);
developer91f80742022-10-04 15:20:18 +080081 }
developer63d72772022-10-07 09:42:31 +080082 return;
developer91f80742022-10-04 15:20:18 +080083}
84
developer52c6ca22022-10-06 17:16:43 +080085void set_country(wifi_radio_param *radio_param, char *country)
developer91f80742022-10-04 15:20:18 +080086{
87 strcpy(radio_param->country, country);
88}
89
developer52c6ca22022-10-06 17:16:43 +080090void set_band(wifi_radio_param *radio_param, char *band)
developer91f80742022-10-04 15:20:18 +080091{
92 strcpy(radio_param->band, band);
93}
94
developer6feac682022-10-18 17:44:13 +080095void set_noscan(wifi_radio_param *radio_param, char *noscan)
96{
97 snprintf(radio_param->noscan, 2, "%s", noscan);
98 radio_param->noscan[1] = '\0';
99}
100
developer91f80742022-10-04 15:20:18 +0800101void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
102{
103 if (strncmp(hwmode, "11a", 3) == 0)
104 strcpy(radio_param->hwmode, "a");
105 if (strncmp(hwmode, "11b", 3) == 0)
106 strcpy(radio_param->hwmode, "b");
107 if (strncmp(hwmode, "11g", 3) == 0)
108 strcpy(radio_param->hwmode, "g");
109}
110
111void set_htmode(wifi_radio_param *radio_param, char *htmode)
112{
113 char tmp[16] = {0};
114 char *ptr = htmode;
115 ULONG bandwidth = 0;
116 radio_param->bandwidth = 20;
117 while (*ptr) {
118 if (isdigit(*ptr)) {
119 bandwidth = strtoul(ptr, NULL, 10);
120 radio_param->bandwidth = bandwidth;
121 break;
122 }
123 ptr++;
124 }
125
126 // HT40 -> 11NGHT40PLUS
127 // VHT40+ -> 11ACVHT40PLUS
128 // HE80 -> 11AXHE80
129 if (strstr(htmode, "+") != NULL) {
130 strncpy(tmp, htmode, strlen(htmode) - 1);
131 strcat(tmp, "PLUS");
132 } else if (strstr(htmode, "-") != NULL) {
133 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);
141 } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
142 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
143 } else if (strstr(htmode, "HE") != NULL) {
144 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
145 } else { // NOHT or NONE should be parsed with the band, so just fill the original string.
146 strcpy(radio_param->htmode, tmp);
147 }
148
149}
150
151void set_disable(wifi_radio_param *radio_param, char *disable)
152{
153 if (strcmp(disable, "1") == 0)
154 radio_param->disabled = TRUE;
155 else
156 radio_param->disabled = FALSE;
157}
158
developer128f8aa2022-12-26 17:09:00 +0800159void set_rxant(wifi_radio_param *radio_param, char *mask)
160{
161 radio_param->rxantenna = strtol(mask, NULL, 16);
162}
163
164void set_txant(wifi_radio_param *radio_param, char *mask)
165{
166 radio_param->txantenna = strtol(mask, NULL, 16);
167}
168
developer465ca0c2022-11-25 14:30:05 +0800169void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800170{
developer50614832022-11-17 20:42:05 +0800171 int radio_num = 0;
172 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800173 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800174
175 while (*ptr) {
176 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800177 phyId = strtoul(ptr, NULL, 10);
178 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800179 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800180 break;
181 }
182 ptr++;
183 }
184}
185
developer465ca0c2022-11-25 14:30:05 +0800186void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800187{
developer465ca0c2022-11-25 14:30:05 +0800188 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800189}
190
developer465ca0c2022-11-25 14:30:05 +0800191void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800192{
developerff378f22022-10-13 13:33:57 +0800193 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800194 intf_param->security.mode = wifi_security_mode_none;
195 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800196 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800197 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800198 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800199 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800200 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800201 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800202 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800203 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800204 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800205 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800206 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800207 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800208 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800209 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800210 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800211 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developer91f80742022-10-04 15:20:18 +0800212 }
213
developerff378f22022-10-13 13:33:57 +0800214 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800215 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800216 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800217 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800218 }else{
developer465ca0c2022-11-25 14:30:05 +0800219 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800220 }
developerff378f22022-10-13 13:33:57 +0800221
222 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800223 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800224 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800225 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800226 }else{
developer465ca0c2022-11-25 14:30:05 +0800227 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800228 }
229
230 if (!strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800231 intf_param->security.u.key.type = wifi_security_key_type_sae;
developerff378f22022-10-13 13:33:57 +0800232 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800233 intf_param->security.u.key.type = wifi_security_key_type_psk_sae;
developerff378f22022-10-13 13:33:57 +0800234 }else{
developer465ca0c2022-11-25 14:30:05 +0800235 intf_param->security.u.key.type = wifi_security_key_type_psk;
developer91f80742022-10-04 15:20:18 +0800236 }
developerff378f22022-10-13 13:33:57 +0800237
developer91f80742022-10-04 15:20:18 +0800238}
239
developer465ca0c2022-11-25 14:30:05 +0800240void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800241{
developer465ca0c2022-11-25 14:30:05 +0800242 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800243}
244
developer7ac3bd52022-12-13 16:19:09 +0800245void set_ifname(wifi_intf_param *intf_param, char *ifname)
246{
247 if (strlen(ifname) > 15)
248 return;
249 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
250}
251
developer50614832022-11-17 20:42:05 +0800252int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800253{
254 FILE *f;
255 char mac_file[64] = {0};
256 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800257
developer1ac426a2022-12-22 19:44:36 +0800258 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800259 f = fopen(mac_file, "r");
260 if (f == NULL)
261 return -1;
262 fgets(mac_address, 20, f);
263 fclose(f);
264
developer50614832022-11-17 20:42:05 +0800265 mac_addr_aton(&(*bssid)[0], mac_address);
266 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800267 return 0;
268}
269
developer91f80742022-10-04 15:20:18 +0800270void set_radio_param(wifi_radio_param radio_parameter)
271{
developer91f80742022-10-04 15:20:18 +0800272 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800273 wifi_radio_operationParam_t operationParam = {0};
274
developer8d8d6302022-10-18 16:36:37 +0800275 if(radio_parameter.radio_index == -1)
276 return;
277
developer63d72772022-10-07 09:42:31 +0800278 if (radio_parameter.disabled == TRUE) {
279 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
280 return;
281 }
developer91f80742022-10-04 15:20:18 +0800282
283 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800284
developer128f8aa2022-12-26 17:09:00 +0800285 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
286 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
287 if (ret != RETURN_OK)
288 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
289 }
290
developerbf812932022-10-17 17:37:29 +0800291 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800292 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
293 if (ret != RETURN_OK)
294 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800295 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800296
developer91f80742022-10-04 15:20:18 +0800297 // Channel
developer63d72772022-10-07 09:42:31 +0800298 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
299 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800300
developer25e07812022-10-13 15:27:02 +0800301 //bandwidth
302 if (radio_parameter.bandwidth == 20){
303 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
304 }else if (radio_parameter.bandwidth == 40){
305 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
306 }else if (radio_parameter.bandwidth == 80){
307 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
308 }else if (radio_parameter.bandwidth == 160){
309 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
310 }
developer91f80742022-10-04 15:20:18 +0800311
312 // htmode
developer63d72772022-10-07 09:42:31 +0800313 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800314 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800315 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800316 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
317 strcpy(radio_parameter.htmode, "11G");
318
developer63d72772022-10-07 09:42:31 +0800319 if (strstr(radio_parameter.htmode, "HE") != NULL)
320 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800321
developer63d72772022-10-07 09:42:31 +0800322 } else if (strcmp(radio_parameter.band, "5g") == 0) {
323 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800324 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
325 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800326
327 if (strstr(radio_parameter.htmode, "HE") != NULL)
328 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800329 }else if (strcmp(radio_parameter.band, "6g") == 0) {
330 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
331 }
developer91f80742022-10-04 15:20:18 +0800332
333 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800334 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800335 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800336 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800337
developer63d72772022-10-07 09:42:31 +0800338 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800339
developer63d72772022-10-07 09:42:31 +0800340 // apply setting
341 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800342 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800343 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800344
developerbb58a932022-11-07 16:58:17 +0800345 // Country
346 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
347 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
348 if (ret != RETURN_OK)
349 fprintf(stderr, "[Set Country failed!!!]\n");
350 ret = 0;
351
352 // hwmode
353 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
354 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
355 if (ret != RETURN_OK)
356 fprintf(stderr, "[Set hwmode failed!!!]\n");
357 ret = 0;
358
359 // noscan
360 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
361 if(strlen(radio_parameter.noscan)){
362 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
363 if (ret != RETURN_OK)
364 fprintf(stderr, "[Set noscan failed!!!]\n");
365 }
366 ret = 0;
367
developer91f80742022-10-04 15:20:18 +0800368}
369
developer465ca0c2022-11-25 14:30:05 +0800370void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800371{
372 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800373 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800374 int phy_index = 0;
developer63d72772022-10-07 09:42:31 +0800375 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800376 BOOL radio_enable = FALSE;
377
developerff42a302022-10-19 17:40:23 +0800378 if(ap_param.radio_index == -1)
379 return;
380
developerbf812932022-10-17 17:37:29 +0800381 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
382 if (radio_enable == FALSE)
383 return;
developer63d72772022-10-07 09:42:31 +0800384
developerff42a302022-10-19 17:40:23 +0800385
386 // get the index of the map
387 for (int i = 0; i < map->num_vaps; i++) {
388 if (map->vap_array[i].vap_index == ap_param.ap_index) {
389 vap_index_in_map = i;
390 break;
developer63d72772022-10-07 09:42:31 +0800391 }
392 }
393
developerff42a302022-10-19 17:40:23 +0800394
developerf7e50b02022-10-14 10:07:58 +0800395 fprintf(stderr, "Start setting ap\n");
396
developerff42a302022-10-19 17:40:23 +0800397 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800398 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800399 phy_index = radio_index_to_phy(vap_info.radio_index);
400 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800401 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800402 return;
developerf7e50b02022-10-14 10:07:58 +0800403 }
developer91f80742022-10-04 15:20:18 +0800404
developer91f80742022-10-04 15:20:18 +0800405 // SSID
developer63d72772022-10-07 09:42:31 +0800406 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
407 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800408
developer7ac3bd52022-12-13 16:19:09 +0800409 // interface
410 if (strlen(ap_param.ifname) != 0) {
411 strncpy(vap_info.vap_name, ap_param.ifname, 16);
412 vap_info.vap_name[15] = "\0";
413 }
414
developerff378f22022-10-13 13:33:57 +0800415 vap_info.u.bss_info.security.mode = ap_param.security.mode;
416 vap_info.u.bss_info.security.encr = ap_param.security.encr;
417 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
418 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
419 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer8d8d6302022-10-18 16:36:37 +0800420
421
developer63d72772022-10-07 09:42:31 +0800422 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800423 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800424}
425
developer465ca0c2022-11-25 14:30:05 +0800426void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800427{
428 wifi_sta_network_t *sta = NULL;
429 mac_address_t sta_mac = {0};
430 char sta_mac_str[20] = {0};
431 char key_mgmt[16] = {0};
432 char pairwise[16] = {0};
433 int phy_index = 0;
434
435 sta = calloc(1, sizeof(wifi_sta_network_t));
436
437 phy_index = radio_index_to_phy(sta_param.radio_index);
438 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
439 mac_addr_ntoa(sta_mac_str, sta_mac);
440 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
441 sta->ssid[31] = '\0';
442 snprintf(sta->psk, 64, "%s", sta_param.password);
443
444 if (sta_param.security.mode == wifi_security_mode_none)
445 strcpy(key_mgmt, "NONE");
446 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
447 strcpy(key_mgmt, "SAE");
448 else
449 strcpy(key_mgmt, "WPA-PSK");
450 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
451
452 if (sta_param.security.encr == wifi_encryption_aes)
453 strcpy(pairwise, "CCMP");
454 else if (sta_param.security.encr == wifi_encryption_tkip)
455 strcpy(pairwise, "TKIP");
456 else
457 strcpy(pairwise, "CCMP TKIP");
458 snprintf(sta->pairwise, 64, "%s", pairwise);
459
460 if (strlen(sta_param.security.u.key.key) > 0)
461 strncpy(sta->psk, sta_param.security.u.key.key, 127);
462 sta->psk[127] = '\0';
463 sta->psk_len = strlen(sta->psk);
464
465 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str);
466
467 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
468 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
469 free(sta);
470 return;
471 }
472 free(sta);
473
474 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
475 fprintf(stderr, "Enable station failed\n");
476 return;
477 }
478}
479
developer91f80742022-10-04 15:20:18 +0800480int apply_uci_config ()
481{
482 struct uci_context *uci_ctx = uci_alloc_context();
483 struct uci_package *uci_pkg = NULL;
484 struct uci_element *e;
485 // struct uci_section *s;
486 const char cfg_name[] = "wireless";
487 int max_radio_num = 0;
488 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800489 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800490 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800491 wifi_vap_info_map_t vap_map[3] = {0};
492 int ret = 0;
493 int i = 0;
developer91f80742022-10-04 15:20:18 +0800494
495 wifi_getMaxRadioNumber(&max_radio_num);
496 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800497 for (i = 0; i < max_radio_num ;i++ ){
498 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
499 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
500 fprintf(stderr, "[Get vap map failed!!!]\n");
501 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
502 }
503 }
developer91f80742022-10-04 15:20:18 +0800504 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
505 uci_free_context(uci_ctx);
506 fprintf(stderr, "%s: load uci failed.\n", __func__);
507 return RETURN_ERR;
508 }
509
510 uci_foreach_element(&uci_pkg->sections, e) {
511
512 struct uci_section *s = uci_to_section(e);
513 struct uci_element *option = NULL;
514 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800515 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800516 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800517 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800518 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800519
520 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800521 sscanf(s->e.name, "radio%d", &phyId);
522 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800523 parsing_radio = TRUE;
524 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
525 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800526 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800527 }
528
529 uci_foreach_element(&s->options, option) {
530
531 struct uci_option *op = uci_to_option(option);
532 if (parsing_radio == TRUE) {
533 // transform the type from input string and store the value in radio_param.
534 if (strcmp(op->e.name, "channel") == 0)
535 set_channel(&radio_param, op->v.string);
536 else if (strcmp(op->e.name, "hwmode") == 0)
537 set_hwmode(&radio_param, op->v.string);
538 else if (strcmp(op->e.name, "htmode") == 0)
539 set_htmode(&radio_param, op->v.string);
540 else if (strcmp(op->e.name, "disabled") == 0)
541 set_disable(&radio_param, op->v.string);
542 else if (strcmp(op->e.name, "band") == 0)
543 set_band(&radio_param, op->v.string);
544 else if (strcmp(op->e.name, "country") == 0)
545 set_country(&radio_param, op->v.string);
546 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800547 set_noscan(&radio_param, op->v.string);
developer128f8aa2022-12-26 17:09:00 +0800548 else if (strcmp(op->e.name, "rxantenna") == 0)
549 set_rxant(&radio_param, op->v.string);
550 else if (strcmp(op->e.name, "txantenna") == 0)
551 set_txant(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800552 else
553 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
554 } else {
555 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800556 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800557 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800558 }else if (strcmp(op->e.name, "mode") == 0){
developer1ac426a2022-12-22 19:44:36 +0800559 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer50614832022-11-17 20:42:05 +0800560 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800561 intf_param.sta_mode = TRUE;
562 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
563 staCount[intf_param.radio_index] ++ ;
564 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800565 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800566 intf_param.sta_mode = FALSE;
567 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
568 apCount[intf_param.radio_index] ++ ;
569 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800570 }
developer8d8d6302022-10-18 16:36:37 +0800571 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800572 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800573 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800574 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800575 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800576 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800577 }else if (strcmp(op->e.name, "ifname") == 0){
578 set_ifname(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800579 }else{
developer91f80742022-10-04 15:20:18 +0800580 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800581 }
developer91f80742022-10-04 15:20:18 +0800582 }
583 }
584 if (parsing_radio == TRUE)
585 set_radio_param(radio_param);
developer465ca0c2022-11-25 14:30:05 +0800586 else if (intf_param.sta_mode == TRUE)
587 set_sta_param(intf_param);
developer91f80742022-10-04 15:20:18 +0800588 else
developer465ca0c2022-11-25 14:30:05 +0800589 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800590 }
developer50614832022-11-17 20:42:05 +0800591 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800592
developerff42a302022-10-19 17:40:23 +0800593 for (i = 0; i < max_radio_num ;i++ ){
594 ret = wifi_createVAP(i, &vap_map[i]);
595 if (ret != RETURN_OK)
596 fprintf(stderr, "[Apply vap setting failed!!!]\n");
597 }
598
developer91f80742022-10-04 15:20:18 +0800599 uci_unload(uci_ctx, uci_pkg);
600 uci_free_context(uci_ctx);
601 return RETURN_OK;
602}
603
604int main(int argc, char **argv)
605{
606 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
607 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
608 return -1;
609 }
610 apply_uci_config();
611 return 0;
612}