blob: fea1fae6cd623fe8fc204d176da900e57f602e11 [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
8void set_channel(wifi_radio_param *radio_param, char *channel)
9{
developer63d72772022-10-07 09:42:31 +080010 if (strcmp(channel, "auto") == 0) {
11 radio_param->auto_channel = TRUE;
12 radio_param->channel = 0;
13 } else {
developer91f80742022-10-04 15:20:18 +080014 radio_param->auto_channel = FALSE;
developer63d72772022-10-07 09:42:31 +080015 radio_param->channel = strtol(channel, NULL, 10);
developer91f80742022-10-04 15:20:18 +080016 }
developer63d72772022-10-07 09:42:31 +080017 return;
developer91f80742022-10-04 15:20:18 +080018}
19
developer52c6ca22022-10-06 17:16:43 +080020void set_country(wifi_radio_param *radio_param, char *country)
developer91f80742022-10-04 15:20:18 +080021{
22 strcpy(radio_param->country, country);
23}
24
developer52c6ca22022-10-06 17:16:43 +080025void set_band(wifi_radio_param *radio_param, char *band)
developer91f80742022-10-04 15:20:18 +080026{
27 strcpy(radio_param->band, band);
28}
29
30void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
31{
32 if (strncmp(hwmode, "11a", 3) == 0)
33 strcpy(radio_param->hwmode, "a");
34 if (strncmp(hwmode, "11b", 3) == 0)
35 strcpy(radio_param->hwmode, "b");
36 if (strncmp(hwmode, "11g", 3) == 0)
37 strcpy(radio_param->hwmode, "g");
38}
39
40void set_htmode(wifi_radio_param *radio_param, char *htmode)
41{
42 char tmp[16] = {0};
43 char *ptr = htmode;
44 ULONG bandwidth = 0;
45 radio_param->bandwidth = 20;
46 while (*ptr) {
47 if (isdigit(*ptr)) {
48 bandwidth = strtoul(ptr, NULL, 10);
49 radio_param->bandwidth = bandwidth;
50 break;
51 }
52 ptr++;
53 }
54
55 // HT40 -> 11NGHT40PLUS
56 // VHT40+ -> 11ACVHT40PLUS
57 // HE80 -> 11AXHE80
58 if (strstr(htmode, "+") != NULL) {
59 strncpy(tmp, htmode, strlen(htmode) - 1);
60 strcat(tmp, "PLUS");
61 } else if (strstr(htmode, "-") != NULL) {
62 strncpy(tmp, htmode, strlen(htmode) - 1);
63 strcat(tmp, "MINUS");
64 } else
65 strcpy(tmp, htmode);
66
67
68 if (strstr(htmode, "VHT") != NULL) {
69 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
70 } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
71 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
72 } else if (strstr(htmode, "HE") != NULL) {
73 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
74 } else { // NOHT or NONE should be parsed with the band, so just fill the original string.
75 strcpy(radio_param->htmode, tmp);
76 }
77
78}
79
80void set_disable(wifi_radio_param *radio_param, char *disable)
81{
82 if (strcmp(disable, "1") == 0)
83 radio_param->disabled = TRUE;
84 else
85 radio_param->disabled = FALSE;
86}
87
88void set_radionum(wifi_ap_param *ap_param, char *radio_name)
89{
90 int radio_num;
91 char *ptr = radio_name;
92
93 while (*ptr) {
94 if (isdigit(*ptr)) {
95 radio_num = strtoul(ptr, NULL, 10);
96 ap_param->radio_index = radio_num;
97 break;
98 }
99 ptr++;
100 }
101}
102
103void set_ssid(wifi_ap_param *ap_param, char *ssid)
104{
105 strncpy(ap_param->ssid, ssid, 32);
106}
107
108void set_encryption(wifi_ap_param *ap_param, char *encryption_mode)
109{
developerff378f22022-10-13 13:33:57 +0800110 if (strcmp(encryption_mode, "none") == 0) {
111 ap_param->security.mode = wifi_security_mode_none;
112 ap_param->security.encr = wifi_encryption_none;
113 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
114 ap_param->security.mode = wifi_security_mode_wpa2_personal;
115 }else if(strncmp(encryption_mode, "psk-",4) == 0){
116 ap_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
117 }else if(strncmp(encryption_mode, "psk",3) == 0){
118 ap_param->security.mode = wifi_security_mode_wpa_personal;
119 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
120 ap_param->security.mode = wifi_security_mode_wpa2_enterprise;
121 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
122 ap_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
123 }else if(strcmp(encryption_mode, "sae") == 0){
124 ap_param->security.mode = wifi_security_mode_wpa3_personal;
125 }else if(strcmp(encryption_mode, "wpa3") == 0){
126 ap_param->security.mode = wifi_security_mode_wpa3_enterprise;
127 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
128 ap_param->security.mode = wifi_security_mode_wpa3_transition;
developer91f80742022-10-04 15:20:18 +0800129 }
130
developerff378f22022-10-13 13:33:57 +0800131 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
132 ap_param->security.encr = wifi_encryption_aes_tkip;
133 }else if (strstr(encryption_mode, "tkip")){
134 ap_param->security.encr = wifi_encryption_tkip;
135 }else{
136 ap_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800137 }
developerff378f22022-10-13 13:33:57 +0800138
139 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae")){
140 ap_param->security.mfp = wifi_mfp_cfg_required;
141 }else if (!strcmp(encryption_mode, "sae-mixed")){
142 ap_param->security.mfp = wifi_mfp_cfg_optional;
143 }else{
144 ap_param->security.mfp = wifi_mfp_cfg_disabled;
145 }
146
147 if (!strcmp(encryption_mode, "sae")){
148 ap_param->security.u.key.type = wifi_security_key_type_sae;
149 }else if (!strcmp(encryption_mode, "sae-mixed")){
150 ap_param->security.u.key.type = wifi_security_key_type_psk_sae;
151 }else{
152 ap_param->security.u.key.type = wifi_security_key_type_psk;
developer91f80742022-10-04 15:20:18 +0800153 }
developerff378f22022-10-13 13:33:57 +0800154
developer91f80742022-10-04 15:20:18 +0800155}
156
157void set_key(wifi_ap_param *ap_param, char *key)
158{
developerff378f22022-10-13 13:33:57 +0800159 strncpy(ap_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800160}
161
162void set_radio_param(wifi_radio_param radio_parameter)
163{
164 BOOL enable;
165 BOOL current;
developer91f80742022-10-04 15:20:18 +0800166 int ret = 0;
167 struct params param;
developer63d72772022-10-07 09:42:31 +0800168 wifi_radio_operationParam_t operationParam = {0};
169
170 if (radio_parameter.disabled == TRUE) {
171 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
172 return;
173 }
174 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800175
176 fprintf(stderr, "Start setting radio\n");
developer63d72772022-10-07 09:42:31 +0800177 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
178 if (ret != RETURN_OK)
179 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800180
developer91f80742022-10-04 15:20:18 +0800181 // Channel
developer63d72772022-10-07 09:42:31 +0800182 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
183 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800184
developer25e07812022-10-13 15:27:02 +0800185 //bandwidth
186 if (radio_parameter.bandwidth == 20){
187 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
188 }else if (radio_parameter.bandwidth == 40){
189 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
190 }else if (radio_parameter.bandwidth == 80){
191 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
192 }else if (radio_parameter.bandwidth == 160){
193 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
194 }
developer91f80742022-10-04 15:20:18 +0800195 // Country
196 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
197 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
198 if (ret != RETURN_OK)
199 fprintf(stderr, "[Set Country failed!!!]\n");
200 ret = 0;
201
202 // hwmode
203 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
204 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
205 if (ret != RETURN_OK)
206 fprintf(stderr, "[Set hwmode failed!!!]\n");
207 ret = 0;
208
209 // htmode
developer63d72772022-10-07 09:42:31 +0800210 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800211 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800212 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800213 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
214 strcpy(radio_parameter.htmode, "11G");
215
developer63d72772022-10-07 09:42:31 +0800216 if (strstr(radio_parameter.htmode, "HE") != NULL)
217 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800218
developer63d72772022-10-07 09:42:31 +0800219 } else if (strcmp(radio_parameter.band, "5g") == 0) {
220 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800221 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
222 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800223
224 if (strstr(radio_parameter.htmode, "HE") != NULL)
225 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800226 }
227
228 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800229 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800230 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800231 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800232
developer63d72772022-10-07 09:42:31 +0800233 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800234
developer63d72772022-10-07 09:42:31 +0800235 // apply setting
236 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800237 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800238 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800239
240}
241
242void set_ap_param(wifi_ap_param ap_param)
243{
244 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800245 int vap_index_in_map = 0;
246 wifi_vap_info_t vap_info = {0};
247 wifi_vap_info_map_t vap_map = {0};
248
249 ret = wifi_getRadioVapInfoMap(ap_param.radio_index, &vap_map);
250 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
251 fprintf(stderr, "[Get vap map failed!!!]\n");
252 vap_map.num_vaps = MAX_NUM_VAP_PER_RADIO;
253 } else { // get the index of the map
254 for (int i = 0; i < vap_map.num_vaps; i++) {
255 if (vap_map.vap_array[i].vap_index == ap_param.ap_index) {
256 vap_index_in_map = i;
257 break;
258 }
259 }
260 }
261
262 // get current setting
263 vap_info = vap_map.vap_array[vap_index_in_map];
developer91f80742022-10-04 15:20:18 +0800264
265 fprintf(stderr, "Start setting ap\n");
developer91f80742022-10-04 15:20:18 +0800266 // SSID
developer63d72772022-10-07 09:42:31 +0800267 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
268 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800269
developerff378f22022-10-13 13:33:57 +0800270 vap_info.u.bss_info.security.mode = ap_param.security.mode;
271 vap_info.u.bss_info.security.encr = ap_param.security.encr;
272 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
273 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
274 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
275
276 ret = wifi_setApSecurity(ap_param.ap_index, &vap_info.u.bss_info.security);
developer91f80742022-10-04 15:20:18 +0800277 if (ret != RETURN_OK)
developerff378f22022-10-13 13:33:57 +0800278 fprintf(stderr, "[set Security failed!!!]\n");
developer25e07812022-10-13 15:27:02 +0800279
developer63d72772022-10-07 09:42:31 +0800280 // Replace the setting with uci config
281 vap_map.vap_array[vap_index_in_map] = vap_info;
282 ret = wifi_createVAP(ap_param.radio_index, &vap_map);
283 if (ret != RETURN_OK)
284 fprintf(stderr, "[Apply vap setting failed!!!]\n");
285
developer91f80742022-10-04 15:20:18 +0800286 // restart ap
287 wifi_setApEnable(ap_param.ap_index, FALSE);
288 wifi_setApEnable(ap_param.ap_index, TRUE);
289}
290
291int apply_uci_config ()
292{
293 struct uci_context *uci_ctx = uci_alloc_context();
294 struct uci_package *uci_pkg = NULL;
295 struct uci_element *e;
296 // struct uci_section *s;
297 const char cfg_name[] = "wireless";
298 int max_radio_num = 0;
299 BOOL parsing_radio = FALSE;
300
301 wifi_getMaxRadioNumber(&max_radio_num);
302 fprintf(stderr, "max radio number: %d\n", max_radio_num);
303 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
304 uci_free_context(uci_ctx);
305 fprintf(stderr, "%s: load uci failed.\n", __func__);
306 return RETURN_ERR;
307 }
308
309 uci_foreach_element(&uci_pkg->sections, e) {
310
311 struct uci_section *s = uci_to_section(e);
312 struct uci_element *option = NULL;
313 wifi_radio_param radio_param = {0};
314 wifi_ap_param ap_param = {0};
315 radio_param.radio_index = -1;
316 ap_param.ap_index = -1;
317
318 if (strcmp(s->type, "wifi-device") == 0) {
319 sscanf(s->e.name, "radio%d", &radio_param.radio_index);
320 parsing_radio = TRUE;
321 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
322 } else if (strcmp(s->type, "wifi-iface") == 0) {
323 sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
324 parsing_radio = FALSE;
325 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
326 }
327
328 uci_foreach_element(&s->options, option) {
329
330 struct uci_option *op = uci_to_option(option);
331 if (parsing_radio == TRUE) {
332 // transform the type from input string and store the value in radio_param.
333 if (strcmp(op->e.name, "channel") == 0)
334 set_channel(&radio_param, op->v.string);
335 else if (strcmp(op->e.name, "hwmode") == 0)
336 set_hwmode(&radio_param, op->v.string);
337 else if (strcmp(op->e.name, "htmode") == 0)
338 set_htmode(&radio_param, op->v.string);
339 else if (strcmp(op->e.name, "disabled") == 0)
340 set_disable(&radio_param, op->v.string);
341 else if (strcmp(op->e.name, "band") == 0)
342 set_band(&radio_param, op->v.string);
343 else if (strcmp(op->e.name, "country") == 0)
344 set_country(&radio_param, op->v.string);
345 else if (strcmp(op->e.name, "noscan") == 0)
346 set_band(&radio_param, op->v.string);
347 else
348 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
349 } else {
350 // parsing iface
351 if (strcmp(op->e.name, "device") == 0)
352 set_radionum(&ap_param, op->v.string);
353 else if (strcmp(op->e.name, "ssid") == 0)
354 set_ssid(&ap_param, op->v.string);
355 else if (strcmp(op->e.name, "encryption") == 0)
356 set_encryption(&ap_param, op->v.string);
357 else if (strcmp(op->e.name, "key") == 0)
358 set_key(&ap_param, op->v.string);
359 else
360 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
361 }
362 }
363 if (parsing_radio == TRUE)
364 set_radio_param(radio_param);
365 else
366 set_ap_param(ap_param);
367 }
368
369 uci_unload(uci_ctx, uci_pkg);
370 uci_free_context(uci_ctx);
371 return RETURN_OK;
372}
373
374int main(int argc, char **argv)
375{
376 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
377 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
378 return -1;
379 }
380 apply_uci_config();
381 return 0;
382}