blob: b24b97b69cbd08bc17497d08b38a6da66745ceb5 [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
developer465ca0c2022-11-25 14:30:05 +0800159void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800160{
developer50614832022-11-17 20:42:05 +0800161 int radio_num = 0;
162 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800163 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800164
165 while (*ptr) {
166 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800167 phyId = strtoul(ptr, NULL, 10);
168 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800169 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800170 break;
171 }
172 ptr++;
173 }
174}
175
developer465ca0c2022-11-25 14:30:05 +0800176void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800177{
developer465ca0c2022-11-25 14:30:05 +0800178 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800179}
180
developer465ca0c2022-11-25 14:30:05 +0800181void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800182{
developerff378f22022-10-13 13:33:57 +0800183 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800184 intf_param->security.mode = wifi_security_mode_none;
185 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800186 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800187 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800188 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800189 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800190 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800191 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800192 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800193 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800194 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800195 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800196 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800197 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800198 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800199 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800200 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800201 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developer91f80742022-10-04 15:20:18 +0800202 }
203
developerff378f22022-10-13 13:33:57 +0800204 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800205 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800206 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800207 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800208 }else{
developer465ca0c2022-11-25 14:30:05 +0800209 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800210 }
developerff378f22022-10-13 13:33:57 +0800211
212 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800213 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800214 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800215 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800216 }else{
developer465ca0c2022-11-25 14:30:05 +0800217 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800218 }
219
220 if (!strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800221 intf_param->security.u.key.type = wifi_security_key_type_sae;
developerff378f22022-10-13 13:33:57 +0800222 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800223 intf_param->security.u.key.type = wifi_security_key_type_psk_sae;
developerff378f22022-10-13 13:33:57 +0800224 }else{
developer465ca0c2022-11-25 14:30:05 +0800225 intf_param->security.u.key.type = wifi_security_key_type_psk;
developer91f80742022-10-04 15:20:18 +0800226 }
developerff378f22022-10-13 13:33:57 +0800227
developer91f80742022-10-04 15:20:18 +0800228}
229
developer465ca0c2022-11-25 14:30:05 +0800230void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800231{
developer465ca0c2022-11-25 14:30:05 +0800232 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800233}
234
developer7ac3bd52022-12-13 16:19:09 +0800235void set_ifname(wifi_intf_param *intf_param, char *ifname)
236{
237 if (strlen(ifname) > 15)
238 return;
239 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
240}
241
developer50614832022-11-17 20:42:05 +0800242int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800243{
244 FILE *f;
245 char mac_file[64] = {0};
246 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800247
developer50614832022-11-17 20:42:05 +0800248 sprintf(mac_file, "/sys/class/net/wlan%d/address", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800249 f = fopen(mac_file, "r");
250 if (f == NULL)
251 return -1;
252 fgets(mac_address, 20, f);
253 fclose(f);
254
developer50614832022-11-17 20:42:05 +0800255 mac_addr_aton(&(*bssid)[0], mac_address);
256 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800257 return 0;
258}
259
developer91f80742022-10-04 15:20:18 +0800260void set_radio_param(wifi_radio_param radio_parameter)
261{
developer91f80742022-10-04 15:20:18 +0800262 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800263 wifi_radio_operationParam_t operationParam = {0};
264
developer8d8d6302022-10-18 16:36:37 +0800265 if(radio_parameter.radio_index == -1)
266 return;
267
developer63d72772022-10-07 09:42:31 +0800268 if (radio_parameter.disabled == TRUE) {
269 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
270 return;
271 }
developer91f80742022-10-04 15:20:18 +0800272
273 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800274
developerbf812932022-10-17 17:37:29 +0800275 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800276 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
277 if (ret != RETURN_OK)
278 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800279 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800280
developer91f80742022-10-04 15:20:18 +0800281 // Channel
developer63d72772022-10-07 09:42:31 +0800282 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
283 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800284
developer25e07812022-10-13 15:27:02 +0800285 //bandwidth
286 if (radio_parameter.bandwidth == 20){
287 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
288 }else if (radio_parameter.bandwidth == 40){
289 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
290 }else if (radio_parameter.bandwidth == 80){
291 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
292 }else if (radio_parameter.bandwidth == 160){
293 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
294 }
developer91f80742022-10-04 15:20:18 +0800295
296 // htmode
developer63d72772022-10-07 09:42:31 +0800297 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800298 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800299 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800300 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
301 strcpy(radio_parameter.htmode, "11G");
302
developer63d72772022-10-07 09:42:31 +0800303 if (strstr(radio_parameter.htmode, "HE") != NULL)
304 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800305
developer63d72772022-10-07 09:42:31 +0800306 } else if (strcmp(radio_parameter.band, "5g") == 0) {
307 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800308 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
309 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800310
311 if (strstr(radio_parameter.htmode, "HE") != NULL)
312 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800313 }else if (strcmp(radio_parameter.band, "6g") == 0) {
314 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
315 }
developer91f80742022-10-04 15:20:18 +0800316
317 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800318 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800319 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800320 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800321
developer63d72772022-10-07 09:42:31 +0800322 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800323
developer63d72772022-10-07 09:42:31 +0800324 // apply setting
325 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800326 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800327 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800328
developerbb58a932022-11-07 16:58:17 +0800329 // Country
330 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
331 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
332 if (ret != RETURN_OK)
333 fprintf(stderr, "[Set Country failed!!!]\n");
334 ret = 0;
335
336 // hwmode
337 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
338 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
339 if (ret != RETURN_OK)
340 fprintf(stderr, "[Set hwmode failed!!!]\n");
341 ret = 0;
342
343 // noscan
344 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
345 if(strlen(radio_parameter.noscan)){
346 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
347 if (ret != RETURN_OK)
348 fprintf(stderr, "[Set noscan failed!!!]\n");
349 }
350 ret = 0;
351
developer91f80742022-10-04 15:20:18 +0800352}
353
developer465ca0c2022-11-25 14:30:05 +0800354void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800355{
356 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800357 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800358 int phy_index = 0;
developer63d72772022-10-07 09:42:31 +0800359 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800360 BOOL radio_enable = FALSE;
361
developerff42a302022-10-19 17:40:23 +0800362 if(ap_param.radio_index == -1)
363 return;
364
developerbf812932022-10-17 17:37:29 +0800365 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
366 if (radio_enable == FALSE)
367 return;
developer63d72772022-10-07 09:42:31 +0800368
developerff42a302022-10-19 17:40:23 +0800369
370 // get the index of the map
371 for (int i = 0; i < map->num_vaps; i++) {
372 if (map->vap_array[i].vap_index == ap_param.ap_index) {
373 vap_index_in_map = i;
374 break;
developer63d72772022-10-07 09:42:31 +0800375 }
376 }
377
developerff42a302022-10-19 17:40:23 +0800378
developerf7e50b02022-10-14 10:07:58 +0800379 fprintf(stderr, "Start setting ap\n");
380
developerff42a302022-10-19 17:40:23 +0800381 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800382 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800383 phy_index = radio_index_to_phy(vap_info.radio_index);
384 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800385 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800386 return;
developerf7e50b02022-10-14 10:07:58 +0800387 }
developer91f80742022-10-04 15:20:18 +0800388
developer91f80742022-10-04 15:20:18 +0800389 // SSID
developer63d72772022-10-07 09:42:31 +0800390 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
391 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800392
developer7ac3bd52022-12-13 16:19:09 +0800393 // interface
394 if (strlen(ap_param.ifname) != 0) {
395 strncpy(vap_info.vap_name, ap_param.ifname, 16);
396 vap_info.vap_name[15] = "\0";
397 }
398
developerff378f22022-10-13 13:33:57 +0800399 vap_info.u.bss_info.security.mode = ap_param.security.mode;
400 vap_info.u.bss_info.security.encr = ap_param.security.encr;
401 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
402 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
403 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer8d8d6302022-10-18 16:36:37 +0800404
405
developer63d72772022-10-07 09:42:31 +0800406 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800407 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800408}
409
developer465ca0c2022-11-25 14:30:05 +0800410void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800411{
412 wifi_sta_network_t *sta = NULL;
413 mac_address_t sta_mac = {0};
414 char sta_mac_str[20] = {0};
415 char key_mgmt[16] = {0};
416 char pairwise[16] = {0};
417 int phy_index = 0;
418
419 sta = calloc(1, sizeof(wifi_sta_network_t));
420
421 phy_index = radio_index_to_phy(sta_param.radio_index);
422 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
423 mac_addr_ntoa(sta_mac_str, sta_mac);
424 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
425 sta->ssid[31] = '\0';
426 snprintf(sta->psk, 64, "%s", sta_param.password);
427
428 if (sta_param.security.mode == wifi_security_mode_none)
429 strcpy(key_mgmt, "NONE");
430 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
431 strcpy(key_mgmt, "SAE");
432 else
433 strcpy(key_mgmt, "WPA-PSK");
434 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
435
436 if (sta_param.security.encr == wifi_encryption_aes)
437 strcpy(pairwise, "CCMP");
438 else if (sta_param.security.encr == wifi_encryption_tkip)
439 strcpy(pairwise, "TKIP");
440 else
441 strcpy(pairwise, "CCMP TKIP");
442 snprintf(sta->pairwise, 64, "%s", pairwise);
443
444 if (strlen(sta_param.security.u.key.key) > 0)
445 strncpy(sta->psk, sta_param.security.u.key.key, 127);
446 sta->psk[127] = '\0';
447 sta->psk_len = strlen(sta->psk);
448
449 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str);
450
451 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
452 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
453 free(sta);
454 return;
455 }
456 free(sta);
457
458 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
459 fprintf(stderr, "Enable station failed\n");
460 return;
461 }
462}
463
developer91f80742022-10-04 15:20:18 +0800464int apply_uci_config ()
465{
466 struct uci_context *uci_ctx = uci_alloc_context();
467 struct uci_package *uci_pkg = NULL;
468 struct uci_element *e;
469 // struct uci_section *s;
470 const char cfg_name[] = "wireless";
471 int max_radio_num = 0;
472 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800473 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800474 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800475 wifi_vap_info_map_t vap_map[3] = {0};
476 int ret = 0;
477 int i = 0;
developer91f80742022-10-04 15:20:18 +0800478
479 wifi_getMaxRadioNumber(&max_radio_num);
480 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800481 for (i = 0; i < max_radio_num ;i++ ){
482 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
483 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
484 fprintf(stderr, "[Get vap map failed!!!]\n");
485 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
486 }
487 }
developer91f80742022-10-04 15:20:18 +0800488 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
489 uci_free_context(uci_ctx);
490 fprintf(stderr, "%s: load uci failed.\n", __func__);
491 return RETURN_ERR;
492 }
493
494 uci_foreach_element(&uci_pkg->sections, e) {
495
496 struct uci_section *s = uci_to_section(e);
497 struct uci_element *option = NULL;
498 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800499 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800500 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800501 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800502 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800503
504 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800505 sscanf(s->e.name, "radio%d", &phyId);
506 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800507 parsing_radio = TRUE;
508 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
509 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800510 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800511 }
512
513 uci_foreach_element(&s->options, option) {
514
515 struct uci_option *op = uci_to_option(option);
516 if (parsing_radio == TRUE) {
517 // transform the type from input string and store the value in radio_param.
518 if (strcmp(op->e.name, "channel") == 0)
519 set_channel(&radio_param, op->v.string);
520 else if (strcmp(op->e.name, "hwmode") == 0)
521 set_hwmode(&radio_param, op->v.string);
522 else if (strcmp(op->e.name, "htmode") == 0)
523 set_htmode(&radio_param, op->v.string);
524 else if (strcmp(op->e.name, "disabled") == 0)
525 set_disable(&radio_param, op->v.string);
526 else if (strcmp(op->e.name, "band") == 0)
527 set_band(&radio_param, op->v.string);
528 else if (strcmp(op->e.name, "country") == 0)
529 set_country(&radio_param, op->v.string);
530 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800531 set_noscan(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800532 else
533 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
534 } else {
535 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800536 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800537 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800538 }else if (strcmp(op->e.name, "mode") == 0){
developer50614832022-11-17 20:42:05 +0800539 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800540 intf_param.sta_mode = TRUE;
541 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
542 staCount[intf_param.radio_index] ++ ;
543 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800544 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800545 intf_param.sta_mode = FALSE;
546 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
547 apCount[intf_param.radio_index] ++ ;
548 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800549 }
developer465ca0c2022-11-25 14:30:05 +0800550 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer8d8d6302022-10-18 16:36:37 +0800551 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800552 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800553 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800554 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800555 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800556 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800557 }else if (strcmp(op->e.name, "ifname") == 0){
558 set_ifname(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800559 }else{
developer91f80742022-10-04 15:20:18 +0800560 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800561 }
developer91f80742022-10-04 15:20:18 +0800562 }
563 }
564 if (parsing_radio == TRUE)
565 set_radio_param(radio_param);
developer465ca0c2022-11-25 14:30:05 +0800566 else if (intf_param.sta_mode == TRUE)
567 set_sta_param(intf_param);
developer91f80742022-10-04 15:20:18 +0800568 else
developer465ca0c2022-11-25 14:30:05 +0800569 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800570 }
developer50614832022-11-17 20:42:05 +0800571 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800572
developerff42a302022-10-19 17:40:23 +0800573 for (i = 0; i < max_radio_num ;i++ ){
574 ret = wifi_createVAP(i, &vap_map[i]);
575 if (ret != RETURN_OK)
576 fprintf(stderr, "[Apply vap setting failed!!!]\n");
577 }
578
developer91f80742022-10-04 15:20:18 +0800579 uci_unload(uci_ctx, uci_pkg);
580 uci_free_context(uci_ctx);
581 return RETURN_OK;
582}
583
584int main(int argc, char **argv)
585{
586 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
587 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
588 return -1;
589 }
590 apply_uci_config();
591 return 0;
592}