blob: a489bb5204f36c78289ab8242e15a99f05ae5dcf [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
developer50614832022-11-17 20:42:05 +0800159void set_radionum(wifi_ap_param *ap_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);
169 ap_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800170 break;
171 }
172 ptr++;
173 }
174}
175
176void set_ssid(wifi_ap_param *ap_param, char *ssid)
177{
178 strncpy(ap_param->ssid, ssid, 32);
179}
180
181void set_encryption(wifi_ap_param *ap_param, char *encryption_mode)
182{
developerff378f22022-10-13 13:33:57 +0800183 if (strcmp(encryption_mode, "none") == 0) {
184 ap_param->security.mode = wifi_security_mode_none;
185 ap_param->security.encr = wifi_encryption_none;
186 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
187 ap_param->security.mode = wifi_security_mode_wpa2_personal;
188 }else if(strncmp(encryption_mode, "psk-",4) == 0){
189 ap_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
190 }else if(strncmp(encryption_mode, "psk",3) == 0){
191 ap_param->security.mode = wifi_security_mode_wpa_personal;
192 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
193 ap_param->security.mode = wifi_security_mode_wpa2_enterprise;
194 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
195 ap_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
196 }else if(strcmp(encryption_mode, "sae") == 0){
197 ap_param->security.mode = wifi_security_mode_wpa3_personal;
198 }else if(strcmp(encryption_mode, "wpa3") == 0){
199 ap_param->security.mode = wifi_security_mode_wpa3_enterprise;
200 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
201 ap_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") )){
205 ap_param->security.encr = wifi_encryption_aes_tkip;
206 }else if (strstr(encryption_mode, "tkip")){
207 ap_param->security.encr = wifi_encryption_tkip;
208 }else{
209 ap_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")){
213 ap_param->security.mfp = wifi_mfp_cfg_required;
214 }else if (!strcmp(encryption_mode, "sae-mixed")){
215 ap_param->security.mfp = wifi_mfp_cfg_optional;
216 }else{
217 ap_param->security.mfp = wifi_mfp_cfg_disabled;
218 }
219
220 if (!strcmp(encryption_mode, "sae")){
221 ap_param->security.u.key.type = wifi_security_key_type_sae;
222 }else if (!strcmp(encryption_mode, "sae-mixed")){
223 ap_param->security.u.key.type = wifi_security_key_type_psk_sae;
224 }else{
225 ap_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
230void set_key(wifi_ap_param *ap_param, char *key)
231{
developerff378f22022-10-13 13:33:57 +0800232 strncpy(ap_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800233}
234
developer50614832022-11-17 20:42:05 +0800235int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800236{
237 FILE *f;
238 char mac_file[64] = {0};
239 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800240
developer50614832022-11-17 20:42:05 +0800241 sprintf(mac_file, "/sys/class/net/wlan%d/address", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800242 f = fopen(mac_file, "r");
243 if (f == NULL)
244 return -1;
245 fgets(mac_address, 20, f);
246 fclose(f);
247
developer50614832022-11-17 20:42:05 +0800248 mac_addr_aton(&(*bssid)[0], mac_address);
249 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800250 return 0;
251}
252
developer91f80742022-10-04 15:20:18 +0800253void set_radio_param(wifi_radio_param radio_parameter)
254{
developer91f80742022-10-04 15:20:18 +0800255 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800256 wifi_radio_operationParam_t operationParam = {0};
257
developer8d8d6302022-10-18 16:36:37 +0800258 if(radio_parameter.radio_index == -1)
259 return;
260
developer63d72772022-10-07 09:42:31 +0800261 if (radio_parameter.disabled == TRUE) {
262 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
263 return;
264 }
developer91f80742022-10-04 15:20:18 +0800265
266 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800267
developerbf812932022-10-17 17:37:29 +0800268 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800269 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
270 if (ret != RETURN_OK)
271 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800272 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800273
developer91f80742022-10-04 15:20:18 +0800274 // Channel
developer63d72772022-10-07 09:42:31 +0800275 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
276 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800277
developer25e07812022-10-13 15:27:02 +0800278 //bandwidth
279 if (radio_parameter.bandwidth == 20){
280 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
281 }else if (radio_parameter.bandwidth == 40){
282 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
283 }else if (radio_parameter.bandwidth == 80){
284 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
285 }else if (radio_parameter.bandwidth == 160){
286 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
287 }
developer91f80742022-10-04 15:20:18 +0800288
289 // htmode
developer63d72772022-10-07 09:42:31 +0800290 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800291 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800292 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800293 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
294 strcpy(radio_parameter.htmode, "11G");
295
developer63d72772022-10-07 09:42:31 +0800296 if (strstr(radio_parameter.htmode, "HE") != NULL)
297 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800298
developer63d72772022-10-07 09:42:31 +0800299 } else if (strcmp(radio_parameter.band, "5g") == 0) {
300 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800301 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
302 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800303
304 if (strstr(radio_parameter.htmode, "HE") != NULL)
305 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800306 }else if (strcmp(radio_parameter.band, "6g") == 0) {
307 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
308 }
developer91f80742022-10-04 15:20:18 +0800309
310 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800311 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800312 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800313 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800314
developer63d72772022-10-07 09:42:31 +0800315 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800316
developer63d72772022-10-07 09:42:31 +0800317 // apply setting
318 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800319 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800320 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800321
developerbb58a932022-11-07 16:58:17 +0800322 // Country
323 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
324 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
325 if (ret != RETURN_OK)
326 fprintf(stderr, "[Set Country failed!!!]\n");
327 ret = 0;
328
329 // hwmode
330 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
331 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
332 if (ret != RETURN_OK)
333 fprintf(stderr, "[Set hwmode failed!!!]\n");
334 ret = 0;
335
336 // noscan
337 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
338 if(strlen(radio_parameter.noscan)){
339 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
340 if (ret != RETURN_OK)
341 fprintf(stderr, "[Set noscan failed!!!]\n");
342 }
343 ret = 0;
344
developer91f80742022-10-04 15:20:18 +0800345}
346
developerff42a302022-10-19 17:40:23 +0800347void set_ap_param(wifi_ap_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800348{
349 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800350 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800351 int phy_index = 0;
developer63d72772022-10-07 09:42:31 +0800352 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800353 BOOL radio_enable = FALSE;
354
developerff42a302022-10-19 17:40:23 +0800355 if(ap_param.radio_index == -1)
356 return;
357
developerbf812932022-10-17 17:37:29 +0800358 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
359 if (radio_enable == FALSE)
360 return;
developer63d72772022-10-07 09:42:31 +0800361
developerff42a302022-10-19 17:40:23 +0800362
363 // get the index of the map
364 for (int i = 0; i < map->num_vaps; i++) {
365 if (map->vap_array[i].vap_index == ap_param.ap_index) {
366 vap_index_in_map = i;
367 break;
developer63d72772022-10-07 09:42:31 +0800368 }
369 }
370
developerff42a302022-10-19 17:40:23 +0800371
developerf7e50b02022-10-14 10:07:58 +0800372 fprintf(stderr, "Start setting ap\n");
373
developerff42a302022-10-19 17:40:23 +0800374 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800375 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800376 phy_index = radio_index_to_phy(vap_info.radio_index);
377 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800378 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800379 return;
developerf7e50b02022-10-14 10:07:58 +0800380 }
developer91f80742022-10-04 15:20:18 +0800381
developer91f80742022-10-04 15:20:18 +0800382 // SSID
developer63d72772022-10-07 09:42:31 +0800383 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
384 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800385
developerff378f22022-10-13 13:33:57 +0800386 vap_info.u.bss_info.security.mode = ap_param.security.mode;
387 vap_info.u.bss_info.security.encr = ap_param.security.encr;
388 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
389 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
390 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer8d8d6302022-10-18 16:36:37 +0800391
392
developer63d72772022-10-07 09:42:31 +0800393 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800394 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800395}
396
developer50614832022-11-17 20:42:05 +0800397void set_sta_param(wifi_ap_param sta_param)
398{
399 wifi_sta_network_t *sta = NULL;
400 mac_address_t sta_mac = {0};
401 char sta_mac_str[20] = {0};
402 char key_mgmt[16] = {0};
403 char pairwise[16] = {0};
404 int phy_index = 0;
405
406 sta = calloc(1, sizeof(wifi_sta_network_t));
407
408 phy_index = radio_index_to_phy(sta_param.radio_index);
409 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
410 mac_addr_ntoa(sta_mac_str, sta_mac);
411 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
412 sta->ssid[31] = '\0';
413 snprintf(sta->psk, 64, "%s", sta_param.password);
414
415 if (sta_param.security.mode == wifi_security_mode_none)
416 strcpy(key_mgmt, "NONE");
417 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
418 strcpy(key_mgmt, "SAE");
419 else
420 strcpy(key_mgmt, "WPA-PSK");
421 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
422
423 if (sta_param.security.encr == wifi_encryption_aes)
424 strcpy(pairwise, "CCMP");
425 else if (sta_param.security.encr == wifi_encryption_tkip)
426 strcpy(pairwise, "TKIP");
427 else
428 strcpy(pairwise, "CCMP TKIP");
429 snprintf(sta->pairwise, 64, "%s", pairwise);
430
431 if (strlen(sta_param.security.u.key.key) > 0)
432 strncpy(sta->psk, sta_param.security.u.key.key, 127);
433 sta->psk[127] = '\0';
434 sta->psk_len = strlen(sta->psk);
435
436 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str);
437
438 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
439 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
440 free(sta);
441 return;
442 }
443 free(sta);
444
445 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
446 fprintf(stderr, "Enable station failed\n");
447 return;
448 }
449}
450
developer91f80742022-10-04 15:20:18 +0800451int apply_uci_config ()
452{
453 struct uci_context *uci_ctx = uci_alloc_context();
454 struct uci_package *uci_pkg = NULL;
455 struct uci_element *e;
456 // struct uci_section *s;
457 const char cfg_name[] = "wireless";
458 int max_radio_num = 0;
459 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800460 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800461 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800462 wifi_vap_info_map_t vap_map[3] = {0};
463 int ret = 0;
464 int i = 0;
developer91f80742022-10-04 15:20:18 +0800465
466 wifi_getMaxRadioNumber(&max_radio_num);
467 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800468 for (i = 0; i < max_radio_num ;i++ ){
469 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
470 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
471 fprintf(stderr, "[Get vap map failed!!!]\n");
472 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
473 }
474 }
developer91f80742022-10-04 15:20:18 +0800475 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
476 uci_free_context(uci_ctx);
477 fprintf(stderr, "%s: load uci failed.\n", __func__);
478 return RETURN_ERR;
479 }
480
481 uci_foreach_element(&uci_pkg->sections, e) {
482
483 struct uci_section *s = uci_to_section(e);
484 struct uci_element *option = NULL;
485 wifi_radio_param radio_param = {0};
486 wifi_ap_param ap_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800487 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800488 radio_param.radio_index = -1;
489 ap_param.ap_index = -1;
490
491 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800492 sscanf(s->e.name, "radio%d", &phyId);
493 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800494 parsing_radio = TRUE;
495 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
496 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800497 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800498 }
499
500 uci_foreach_element(&s->options, option) {
501
502 struct uci_option *op = uci_to_option(option);
503 if (parsing_radio == TRUE) {
504 // transform the type from input string and store the value in radio_param.
505 if (strcmp(op->e.name, "channel") == 0)
506 set_channel(&radio_param, op->v.string);
507 else if (strcmp(op->e.name, "hwmode") == 0)
508 set_hwmode(&radio_param, op->v.string);
509 else if (strcmp(op->e.name, "htmode") == 0)
510 set_htmode(&radio_param, op->v.string);
511 else if (strcmp(op->e.name, "disabled") == 0)
512 set_disable(&radio_param, op->v.string);
513 else if (strcmp(op->e.name, "band") == 0)
514 set_band(&radio_param, op->v.string);
515 else if (strcmp(op->e.name, "country") == 0)
516 set_country(&radio_param, op->v.string);
517 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800518 set_noscan(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800519 else
520 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
521 } else {
522 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800523 if (strcmp(op->e.name, "device") == 0){
developer91f80742022-10-04 15:20:18 +0800524 set_radionum(&ap_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800525 }else if (strcmp(op->e.name, "mode") == 0){
526 ap_param.mac_offset = staCount[ap_param.radio_index] + apCount[ap_param.radio_index];
527 if (strncmp(op->v.string, "sta", 3) == 0) {
528 ap_param.sta_mode = TRUE;
529 ap_param.sta_index = ap_param.radio_index + staCount[ap_param.radio_index]*max_radio_num;
530 staCount[ap_param.radio_index] ++ ;
531 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", ap_param.sta_index);
532 } else if (strncmp(op->v.string, "ap", 2) == 0) {
533 ap_param.sta_mode = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800534 ap_param.ap_index = ap_param.radio_index + apCount[ap_param.radio_index]*max_radio_num;
developer8d8d6302022-10-18 16:36:37 +0800535 apCount[ap_param.radio_index] ++ ;
developer50614832022-11-17 20:42:05 +0800536 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
537 }
538 ap_param.mac_offset = staCount[ap_param.radio_index] + apCount[ap_param.radio_index];
developer8d8d6302022-10-18 16:36:37 +0800539 }else if (strcmp(op->e.name, "ssid") == 0){
developer91f80742022-10-04 15:20:18 +0800540 set_ssid(&ap_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800541 }else if (strcmp(op->e.name, "encryption") == 0){
developer91f80742022-10-04 15:20:18 +0800542 set_encryption(&ap_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800543 }else if (strcmp(op->e.name, "key") == 0){
developer91f80742022-10-04 15:20:18 +0800544 set_key(&ap_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800545 }else{
developer91f80742022-10-04 15:20:18 +0800546 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800547 }
developer91f80742022-10-04 15:20:18 +0800548 }
549 }
550 if (parsing_radio == TRUE)
551 set_radio_param(radio_param);
developer50614832022-11-17 20:42:05 +0800552 else if (ap_param.sta_mode == TRUE)
553 set_sta_param(ap_param);
developer91f80742022-10-04 15:20:18 +0800554 else
developerff42a302022-10-19 17:40:23 +0800555 set_ap_param(ap_param, &vap_map[ap_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800556 }
developer50614832022-11-17 20:42:05 +0800557 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800558
developerff42a302022-10-19 17:40:23 +0800559 for (i = 0; i < max_radio_num ;i++ ){
560 ret = wifi_createVAP(i, &vap_map[i]);
561 if (ret != RETURN_OK)
562 fprintf(stderr, "[Apply vap setting failed!!!]\n");
563 }
564
developer91f80742022-10-04 15:20:18 +0800565 uci_unload(uci_ctx, uci_pkg);
566 uci_free_context(uci_ctx);
567 return RETURN_OK;
568}
569
570int main(int argc, char **argv)
571{
572 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
573 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
574 return -1;
575 }
576 apply_uci_config();
577 return 0;
578}