blob: 9ca097ac94862b48052baf4737ea0ef3227c5e0d [file] [log] [blame]
developer402bf2a2022-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{
developerab985802022-10-07 09:42:31 +080010 if (strcmp(channel, "auto") == 0) {
11 radio_param->auto_channel = TRUE;
12 radio_param->channel = 0;
13 } else {
developer402bf2a2022-10-04 15:20:18 +080014 radio_param->auto_channel = FALSE;
developerab985802022-10-07 09:42:31 +080015 radio_param->channel = strtol(channel, NULL, 10);
developer402bf2a2022-10-04 15:20:18 +080016 }
developerab985802022-10-07 09:42:31 +080017 return;
developer402bf2a2022-10-04 15:20:18 +080018}
19
developer02a4a1b2022-10-06 17:16:43 +080020void set_country(wifi_radio_param *radio_param, char *country)
developer402bf2a2022-10-04 15:20:18 +080021{
22 strcpy(radio_param->country, country);
23}
24
developer02a4a1b2022-10-06 17:16:43 +080025void set_band(wifi_radio_param *radio_param, char *band)
developer402bf2a2022-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{
developer3ddad2f2022-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;
developer402bf2a2022-10-04 15:20:18 +0800129 }
130
developer3ddad2f2022-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;
developer402bf2a2022-10-04 15:20:18 +0800137 }
developer3ddad2f2022-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;
developer402bf2a2022-10-04 15:20:18 +0800153 }
developer3ddad2f2022-10-13 13:33:57 +0800154
developer402bf2a2022-10-04 15:20:18 +0800155}
156
157void set_key(wifi_ap_param *ap_param, char *key)
158{
developer3ddad2f2022-10-13 13:33:57 +0800159 strncpy(ap_param->security.u.key.key, key, 64);
developer402bf2a2022-10-04 15:20:18 +0800160}
161
developerf9b2ef02022-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
developer402bf2a2022-10-04 15:20:18 +0800181void set_radio_param(wifi_radio_param radio_parameter)
182{
183 BOOL enable;
184 BOOL current;
developer402bf2a2022-10-04 15:20:18 +0800185 int ret = 0;
186 struct params param;
developerab985802022-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 }
193 operationParam.enable = TRUE;
developer402bf2a2022-10-04 15:20:18 +0800194
195 fprintf(stderr, "Start setting radio\n");
developerab985802022-10-07 09:42:31 +0800196 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
197 if (ret != RETURN_OK)
198 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developer402bf2a2022-10-04 15:20:18 +0800199
developer402bf2a2022-10-04 15:20:18 +0800200 // Channel
developerab985802022-10-07 09:42:31 +0800201 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
202 operationParam.channel = radio_parameter.channel;
developer402bf2a2022-10-04 15:20:18 +0800203
developerae3f8412022-10-13 15:27:02 +0800204 //bandwidth
205 if (radio_parameter.bandwidth == 20){
206 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
207 }else if (radio_parameter.bandwidth == 40){
208 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
209 }else if (radio_parameter.bandwidth == 80){
210 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
211 }else if (radio_parameter.bandwidth == 160){
212 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
213 }
developer402bf2a2022-10-04 15:20:18 +0800214 // Country
215 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
216 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
217 if (ret != RETURN_OK)
218 fprintf(stderr, "[Set Country failed!!!]\n");
219 ret = 0;
220
221 // hwmode
222 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
223 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
224 if (ret != RETURN_OK)
225 fprintf(stderr, "[Set hwmode failed!!!]\n");
226 ret = 0;
227
228 // htmode
developerab985802022-10-07 09:42:31 +0800229 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer402bf2a2022-10-04 15:20:18 +0800230 if (strcmp(radio_parameter.band, "2g") == 0) {
developerab985802022-10-07 09:42:31 +0800231 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer402bf2a2022-10-04 15:20:18 +0800232 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
233 strcpy(radio_parameter.htmode, "11G");
234
developerab985802022-10-07 09:42:31 +0800235 if (strstr(radio_parameter.htmode, "HE") != NULL)
236 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer402bf2a2022-10-04 15:20:18 +0800237
developerab985802022-10-07 09:42:31 +0800238 } else if (strcmp(radio_parameter.band, "5g") == 0) {
239 mode |= WIFI_80211_VARIANT_A;
developer402bf2a2022-10-04 15:20:18 +0800240 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
241 strcpy(radio_parameter.htmode, "11A");
developerab985802022-10-07 09:42:31 +0800242
243 if (strstr(radio_parameter.htmode, "HE") != NULL)
244 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer402bf2a2022-10-04 15:20:18 +0800245 }
246
247 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developerab985802022-10-07 09:42:31 +0800248 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer402bf2a2022-10-04 15:20:18 +0800249 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developerab985802022-10-07 09:42:31 +0800250 mode |= WIFI_80211_VARIANT_N;
developer402bf2a2022-10-04 15:20:18 +0800251
developerab985802022-10-07 09:42:31 +0800252 operationParam.variant = mode;
developer402bf2a2022-10-04 15:20:18 +0800253
developerab985802022-10-07 09:42:31 +0800254 // apply setting
255 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer402bf2a2022-10-04 15:20:18 +0800256 if (ret != RETURN_OK)
developerab985802022-10-07 09:42:31 +0800257 fprintf(stderr, "[Apply setting failed!!!]\n");
developer402bf2a2022-10-04 15:20:18 +0800258
259}
260
261void set_ap_param(wifi_ap_param ap_param)
262{
263 int ret = 0;
developerab985802022-10-07 09:42:31 +0800264 int vap_index_in_map = 0;
265 wifi_vap_info_t vap_info = {0};
266 wifi_vap_info_map_t vap_map = {0};
267
developerf9b2ef02022-10-14 10:07:58 +0800268 // get current setting
developerab985802022-10-07 09:42:31 +0800269 ret = wifi_getRadioVapInfoMap(ap_param.radio_index, &vap_map);
270 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
271 fprintf(stderr, "[Get vap map failed!!!]\n");
272 vap_map.num_vaps = MAX_NUM_VAP_PER_RADIO;
273 } else { // get the index of the map
274 for (int i = 0; i < vap_map.num_vaps; i++) {
275 if (vap_map.vap_array[i].vap_index == ap_param.ap_index) {
276 vap_index_in_map = i;
277 break;
278 }
279 }
280 }
281
developerf9b2ef02022-10-14 10:07:58 +0800282 fprintf(stderr, "Start setting ap\n");
283
developerab985802022-10-07 09:42:31 +0800284 vap_info = vap_map.vap_array[vap_index_in_map];
developerf9b2ef02022-10-14 10:07:58 +0800285 vap_info.u.bss_info.enabled = TRUE;
286 if (set_ap_bssid(vap_info.radio_index, vap_index_in_map, &vap_info.u.bss_info.bssid) == -1) {
287 fprintf(stderr, "Get mac address failed.\n");
288 return -1;
289 }
developer402bf2a2022-10-04 15:20:18 +0800290
developer402bf2a2022-10-04 15:20:18 +0800291 // SSID
developerab985802022-10-07 09:42:31 +0800292 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
293 vap_info.u.bss_info.ssid[32] = '\0';
developer402bf2a2022-10-04 15:20:18 +0800294
developer3ddad2f2022-10-13 13:33:57 +0800295 vap_info.u.bss_info.security.mode = ap_param.security.mode;
296 vap_info.u.bss_info.security.encr = ap_param.security.encr;
297 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
298 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
299 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developerab985802022-10-07 09:42:31 +0800300 // Replace the setting with uci config
301 vap_map.vap_array[vap_index_in_map] = vap_info;
302 ret = wifi_createVAP(ap_param.radio_index, &vap_map);
303 if (ret != RETURN_OK)
304 fprintf(stderr, "[Apply vap setting failed!!!]\n");
305
developer402bf2a2022-10-04 15:20:18 +0800306 // restart ap
307 wifi_setApEnable(ap_param.ap_index, FALSE);
308 wifi_setApEnable(ap_param.ap_index, TRUE);
309}
310
311int apply_uci_config ()
312{
313 struct uci_context *uci_ctx = uci_alloc_context();
314 struct uci_package *uci_pkg = NULL;
315 struct uci_element *e;
316 // struct uci_section *s;
317 const char cfg_name[] = "wireless";
318 int max_radio_num = 0;
319 BOOL parsing_radio = FALSE;
320
321 wifi_getMaxRadioNumber(&max_radio_num);
322 fprintf(stderr, "max radio number: %d\n", max_radio_num);
323 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
324 uci_free_context(uci_ctx);
325 fprintf(stderr, "%s: load uci failed.\n", __func__);
326 return RETURN_ERR;
327 }
328
329 uci_foreach_element(&uci_pkg->sections, e) {
330
331 struct uci_section *s = uci_to_section(e);
332 struct uci_element *option = NULL;
333 wifi_radio_param radio_param = {0};
334 wifi_ap_param ap_param = {0};
335 radio_param.radio_index = -1;
336 ap_param.ap_index = -1;
337
338 if (strcmp(s->type, "wifi-device") == 0) {
339 sscanf(s->e.name, "radio%d", &radio_param.radio_index);
340 parsing_radio = TRUE;
341 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
342 } else if (strcmp(s->type, "wifi-iface") == 0) {
343 sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
344 parsing_radio = FALSE;
345 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
346 }
347
348 uci_foreach_element(&s->options, option) {
349
350 struct uci_option *op = uci_to_option(option);
351 if (parsing_radio == TRUE) {
352 // transform the type from input string and store the value in radio_param.
353 if (strcmp(op->e.name, "channel") == 0)
354 set_channel(&radio_param, op->v.string);
355 else if (strcmp(op->e.name, "hwmode") == 0)
356 set_hwmode(&radio_param, op->v.string);
357 else if (strcmp(op->e.name, "htmode") == 0)
358 set_htmode(&radio_param, op->v.string);
359 else if (strcmp(op->e.name, "disabled") == 0)
360 set_disable(&radio_param, op->v.string);
361 else if (strcmp(op->e.name, "band") == 0)
362 set_band(&radio_param, op->v.string);
363 else if (strcmp(op->e.name, "country") == 0)
364 set_country(&radio_param, op->v.string);
365 else if (strcmp(op->e.name, "noscan") == 0)
366 set_band(&radio_param, op->v.string);
367 else
368 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
369 } else {
370 // parsing iface
371 if (strcmp(op->e.name, "device") == 0)
372 set_radionum(&ap_param, op->v.string);
373 else if (strcmp(op->e.name, "ssid") == 0)
374 set_ssid(&ap_param, op->v.string);
375 else if (strcmp(op->e.name, "encryption") == 0)
376 set_encryption(&ap_param, op->v.string);
377 else if (strcmp(op->e.name, "key") == 0)
378 set_key(&ap_param, op->v.string);
379 else
380 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
381 }
382 }
383 if (parsing_radio == TRUE)
384 set_radio_param(radio_param);
385 else
386 set_ap_param(ap_param);
387 }
388
389 uci_unload(uci_ctx, uci_pkg);
390 uci_free_context(uci_ctx);
391 return RETURN_OK;
392}
393
394int main(int argc, char **argv)
395{
396 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
397 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
398 return -1;
399 }
400 apply_uci_config();
401 return 0;
402}