blob: 024733c4f7151be670589f68890b8d2a310868de [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
developerf7e50b02022-10-14 10:07:58 +0800162int set_ap_bssid(int radio_index, int offset, mac_address_t *bssid)
163{
164 FILE *f;
165 char mac_file[64] = {0};
166 char mac_address[20] = {0};
167 char *tmp = NULL;
168
169 sprintf(mac_file, "/sys/class/net/wlan%d/address", radio_index);
170 f = fopen(mac_file, "r");
171 if (f == NULL)
172 return -1;
173 fgets(mac_address, 20, f);
174 fclose(f);
175
176 sscanf(mac_address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &(*bssid)[0], &(*bssid)[1], &(*bssid)[2], &(*bssid)[3], &(*bssid)[4], &(*bssid)[5]);
177 (*bssid)[0] += (offset + 1)*2;
178 return 0;
179}
180
developer91f80742022-10-04 15:20:18 +0800181void set_radio_param(wifi_radio_param radio_parameter)
182{
183 BOOL enable;
184 BOOL current;
developer91f80742022-10-04 15:20:18 +0800185 int ret = 0;
186 struct params param;
developer63d72772022-10-07 09:42:31 +0800187 wifi_radio_operationParam_t operationParam = {0};
188
189 if (radio_parameter.disabled == TRUE) {
190 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
191 return;
192 }
developer91f80742022-10-04 15:20:18 +0800193
194 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800195
196 wifi_setRadioEnable(radio_parameter.radio_index, TRUE);
197 sleep(1);
198
199 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800200 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
201 if (ret != RETURN_OK)
202 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800203 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800204
developer91f80742022-10-04 15:20:18 +0800205 // Channel
developer63d72772022-10-07 09:42:31 +0800206 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
207 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800208
developer25e07812022-10-13 15:27:02 +0800209 //bandwidth
210 if (radio_parameter.bandwidth == 20){
211 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
212 }else if (radio_parameter.bandwidth == 40){
213 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
214 }else if (radio_parameter.bandwidth == 80){
215 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
216 }else if (radio_parameter.bandwidth == 160){
217 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
218 }
developer91f80742022-10-04 15:20:18 +0800219 // Country
220 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
221 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
222 if (ret != RETURN_OK)
223 fprintf(stderr, "[Set Country failed!!!]\n");
224 ret = 0;
225
226 // hwmode
227 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
228 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
229 if (ret != RETURN_OK)
230 fprintf(stderr, "[Set hwmode failed!!!]\n");
231 ret = 0;
232
233 // htmode
developer63d72772022-10-07 09:42:31 +0800234 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800235 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800236 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800237 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
238 strcpy(radio_parameter.htmode, "11G");
239
developer63d72772022-10-07 09:42:31 +0800240 if (strstr(radio_parameter.htmode, "HE") != NULL)
241 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800242
developer63d72772022-10-07 09:42:31 +0800243 } else if (strcmp(radio_parameter.band, "5g") == 0) {
244 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800245 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
246 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800247
248 if (strstr(radio_parameter.htmode, "HE") != NULL)
249 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800250 }
251
252 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800253 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800254 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800255 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800256
developer63d72772022-10-07 09:42:31 +0800257 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800258
developer63d72772022-10-07 09:42:31 +0800259 // apply setting
260 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800261 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800262 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800263
264}
265
266void set_ap_param(wifi_ap_param ap_param)
267{
268 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800269 int vap_index_in_map = 0;
270 wifi_vap_info_t vap_info = {0};
271 wifi_vap_info_map_t vap_map = {0};
developerbf812932022-10-17 17:37:29 +0800272 BOOL radio_enable = FALSE;
273
274 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
275 if (radio_enable == FALSE)
276 return;
developer63d72772022-10-07 09:42:31 +0800277
developerf7e50b02022-10-14 10:07:58 +0800278 // get current setting
developer63d72772022-10-07 09:42:31 +0800279 ret = wifi_getRadioVapInfoMap(ap_param.radio_index, &vap_map);
280 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
281 fprintf(stderr, "[Get vap map failed!!!]\n");
282 vap_map.num_vaps = MAX_NUM_VAP_PER_RADIO;
283 } else { // get the index of the map
284 for (int i = 0; i < vap_map.num_vaps; i++) {
285 if (vap_map.vap_array[i].vap_index == ap_param.ap_index) {
286 vap_index_in_map = i;
287 break;
288 }
289 }
290 }
291
developerf7e50b02022-10-14 10:07:58 +0800292 fprintf(stderr, "Start setting ap\n");
293
developer63d72772022-10-07 09:42:31 +0800294 vap_info = vap_map.vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800295 vap_info.u.bss_info.enabled = TRUE;
296 if (set_ap_bssid(vap_info.radio_index, vap_index_in_map, &vap_info.u.bss_info.bssid) == -1) {
297 fprintf(stderr, "Get mac address failed.\n");
298 return -1;
299 }
developer91f80742022-10-04 15:20:18 +0800300
developer91f80742022-10-04 15:20:18 +0800301 // SSID
developer63d72772022-10-07 09:42:31 +0800302 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
303 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800304
developerff378f22022-10-13 13:33:57 +0800305 vap_info.u.bss_info.security.mode = ap_param.security.mode;
306 vap_info.u.bss_info.security.encr = ap_param.security.encr;
307 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
308 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
309 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer63d72772022-10-07 09:42:31 +0800310 // Replace the setting with uci config
311 vap_map.vap_array[vap_index_in_map] = vap_info;
312 ret = wifi_createVAP(ap_param.radio_index, &vap_map);
313 if (ret != RETURN_OK)
314 fprintf(stderr, "[Apply vap setting failed!!!]\n");
315
developer91f80742022-10-04 15:20:18 +0800316 // restart ap
317 wifi_setApEnable(ap_param.ap_index, FALSE);
318 wifi_setApEnable(ap_param.ap_index, TRUE);
319}
320
321int apply_uci_config ()
322{
323 struct uci_context *uci_ctx = uci_alloc_context();
324 struct uci_package *uci_pkg = NULL;
325 struct uci_element *e;
326 // struct uci_section *s;
327 const char cfg_name[] = "wireless";
328 int max_radio_num = 0;
329 BOOL parsing_radio = FALSE;
330
331 wifi_getMaxRadioNumber(&max_radio_num);
332 fprintf(stderr, "max radio number: %d\n", max_radio_num);
333 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
334 uci_free_context(uci_ctx);
335 fprintf(stderr, "%s: load uci failed.\n", __func__);
336 return RETURN_ERR;
337 }
338
339 uci_foreach_element(&uci_pkg->sections, e) {
340
341 struct uci_section *s = uci_to_section(e);
342 struct uci_element *option = NULL;
343 wifi_radio_param radio_param = {0};
344 wifi_ap_param ap_param = {0};
345 radio_param.radio_index = -1;
346 ap_param.ap_index = -1;
347
348 if (strcmp(s->type, "wifi-device") == 0) {
349 sscanf(s->e.name, "radio%d", &radio_param.radio_index);
350 parsing_radio = TRUE;
351 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
352 } else if (strcmp(s->type, "wifi-iface") == 0) {
353 sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
354 parsing_radio = FALSE;
355 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
356 }
357
358 uci_foreach_element(&s->options, option) {
359
360 struct uci_option *op = uci_to_option(option);
361 if (parsing_radio == TRUE) {
362 // transform the type from input string and store the value in radio_param.
363 if (strcmp(op->e.name, "channel") == 0)
364 set_channel(&radio_param, op->v.string);
365 else if (strcmp(op->e.name, "hwmode") == 0)
366 set_hwmode(&radio_param, op->v.string);
367 else if (strcmp(op->e.name, "htmode") == 0)
368 set_htmode(&radio_param, op->v.string);
369 else if (strcmp(op->e.name, "disabled") == 0)
370 set_disable(&radio_param, op->v.string);
371 else if (strcmp(op->e.name, "band") == 0)
372 set_band(&radio_param, op->v.string);
373 else if (strcmp(op->e.name, "country") == 0)
374 set_country(&radio_param, op->v.string);
375 else if (strcmp(op->e.name, "noscan") == 0)
376 set_band(&radio_param, op->v.string);
377 else
378 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
379 } else {
380 // parsing iface
381 if (strcmp(op->e.name, "device") == 0)
382 set_radionum(&ap_param, op->v.string);
383 else if (strcmp(op->e.name, "ssid") == 0)
384 set_ssid(&ap_param, op->v.string);
385 else if (strcmp(op->e.name, "encryption") == 0)
386 set_encryption(&ap_param, op->v.string);
387 else if (strcmp(op->e.name, "key") == 0)
388 set_key(&ap_param, op->v.string);
389 else
390 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
391 }
392 }
393 if (parsing_radio == TRUE)
394 set_radio_param(radio_param);
395 else
396 set_ap_param(ap_param);
397 }
398
399 uci_unload(uci_ctx, uci_pkg);
400 uci_free_context(uci_ctx);
401 return RETURN_OK;
402}
403
404int main(int argc, char **argv)
405{
406 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
407 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
408 return -1;
409 }
410 apply_uci_config();
411 return 0;
412}