blob: 9c1b4433b8f00dcd572becdc6104319c6681cb71 [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
185 // Country
186 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
187 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
188 if (ret != RETURN_OK)
189 fprintf(stderr, "[Set Country failed!!!]\n");
190 ret = 0;
191
192 // hwmode
193 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
194 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
195 if (ret != RETURN_OK)
196 fprintf(stderr, "[Set hwmode failed!!!]\n");
197 ret = 0;
198
199 // htmode
developer63d72772022-10-07 09:42:31 +0800200 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800201 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800202 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800203 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
204 strcpy(radio_parameter.htmode, "11G");
205
developer63d72772022-10-07 09:42:31 +0800206 if (strstr(radio_parameter.htmode, "HE") != NULL)
207 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800208
developer63d72772022-10-07 09:42:31 +0800209 } else if (strcmp(radio_parameter.band, "5g") == 0) {
210 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800211 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
212 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800213
214 if (strstr(radio_parameter.htmode, "HE") != NULL)
215 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800216 }
217
218 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800219 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800220 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800221 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800222
developer63d72772022-10-07 09:42:31 +0800223 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800224
developer63d72772022-10-07 09:42:31 +0800225 // apply setting
226 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800227 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800228 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800229
230}
231
232void set_ap_param(wifi_ap_param ap_param)
233{
234 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800235 int vap_index_in_map = 0;
236 wifi_vap_info_t vap_info = {0};
237 wifi_vap_info_map_t vap_map = {0};
238
239 ret = wifi_getRadioVapInfoMap(ap_param.radio_index, &vap_map);
240 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
241 fprintf(stderr, "[Get vap map failed!!!]\n");
242 vap_map.num_vaps = MAX_NUM_VAP_PER_RADIO;
243 } else { // get the index of the map
244 for (int i = 0; i < vap_map.num_vaps; i++) {
245 if (vap_map.vap_array[i].vap_index == ap_param.ap_index) {
246 vap_index_in_map = i;
247 break;
248 }
249 }
250 }
251
252 // get current setting
253 vap_info = vap_map.vap_array[vap_index_in_map];
developer91f80742022-10-04 15:20:18 +0800254
255 fprintf(stderr, "Start setting ap\n");
developer91f80742022-10-04 15:20:18 +0800256 // SSID
developer63d72772022-10-07 09:42:31 +0800257 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
258 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800259
developerff378f22022-10-13 13:33:57 +0800260 vap_info.u.bss_info.security.mode = ap_param.security.mode;
261 vap_info.u.bss_info.security.encr = ap_param.security.encr;
262 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
263 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
264 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
265
266 ret = wifi_setApSecurity(ap_param.ap_index, &vap_info.u.bss_info.security);
developer91f80742022-10-04 15:20:18 +0800267 if (ret != RETURN_OK)
developerff378f22022-10-13 13:33:57 +0800268 fprintf(stderr, "[set Security failed!!!]\n");
269
developer63d72772022-10-07 09:42:31 +0800270 // Replace the setting with uci config
271 vap_map.vap_array[vap_index_in_map] = vap_info;
272 ret = wifi_createVAP(ap_param.radio_index, &vap_map);
273 if (ret != RETURN_OK)
274 fprintf(stderr, "[Apply vap setting failed!!!]\n");
275
developer91f80742022-10-04 15:20:18 +0800276 // restart ap
277 wifi_setApEnable(ap_param.ap_index, FALSE);
278 wifi_setApEnable(ap_param.ap_index, TRUE);
279}
280
281int apply_uci_config ()
282{
283 struct uci_context *uci_ctx = uci_alloc_context();
284 struct uci_package *uci_pkg = NULL;
285 struct uci_element *e;
286 // struct uci_section *s;
287 const char cfg_name[] = "wireless";
288 int max_radio_num = 0;
289 BOOL parsing_radio = FALSE;
290
291 wifi_getMaxRadioNumber(&max_radio_num);
292 fprintf(stderr, "max radio number: %d\n", max_radio_num);
293 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
294 uci_free_context(uci_ctx);
295 fprintf(stderr, "%s: load uci failed.\n", __func__);
296 return RETURN_ERR;
297 }
298
299 uci_foreach_element(&uci_pkg->sections, e) {
300
301 struct uci_section *s = uci_to_section(e);
302 struct uci_element *option = NULL;
303 wifi_radio_param radio_param = {0};
304 wifi_ap_param ap_param = {0};
305 radio_param.radio_index = -1;
306 ap_param.ap_index = -1;
307
308 if (strcmp(s->type, "wifi-device") == 0) {
309 sscanf(s->e.name, "radio%d", &radio_param.radio_index);
310 parsing_radio = TRUE;
311 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
312 } else if (strcmp(s->type, "wifi-iface") == 0) {
313 sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
314 parsing_radio = FALSE;
315 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
316 }
317
318 uci_foreach_element(&s->options, option) {
319
320 struct uci_option *op = uci_to_option(option);
321 if (parsing_radio == TRUE) {
322 // transform the type from input string and store the value in radio_param.
323 if (strcmp(op->e.name, "channel") == 0)
324 set_channel(&radio_param, op->v.string);
325 else if (strcmp(op->e.name, "hwmode") == 0)
326 set_hwmode(&radio_param, op->v.string);
327 else if (strcmp(op->e.name, "htmode") == 0)
328 set_htmode(&radio_param, op->v.string);
329 else if (strcmp(op->e.name, "disabled") == 0)
330 set_disable(&radio_param, op->v.string);
331 else if (strcmp(op->e.name, "band") == 0)
332 set_band(&radio_param, op->v.string);
333 else if (strcmp(op->e.name, "country") == 0)
334 set_country(&radio_param, op->v.string);
335 else if (strcmp(op->e.name, "noscan") == 0)
336 set_band(&radio_param, op->v.string);
337 else
338 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
339 } else {
340 // parsing iface
341 if (strcmp(op->e.name, "device") == 0)
342 set_radionum(&ap_param, op->v.string);
343 else if (strcmp(op->e.name, "ssid") == 0)
344 set_ssid(&ap_param, op->v.string);
345 else if (strcmp(op->e.name, "encryption") == 0)
346 set_encryption(&ap_param, op->v.string);
347 else if (strcmp(op->e.name, "key") == 0)
348 set_key(&ap_param, op->v.string);
349 else
350 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
351 }
352 }
353 if (parsing_radio == TRUE)
354 set_radio_param(radio_param);
355 else
356 set_ap_param(ap_param);
357 }
358
359 uci_unload(uci_ctx, uci_pkg);
360 uci_free_context(uci_ctx);
361 return RETURN_OK;
362}
363
364int main(int argc, char **argv)
365{
366 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
367 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
368 return -1;
369 }
370 apply_uci_config();
371 return 0;
372}