blob: af56831cc50b51962f572fa9be01b792ec04775e [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{
110 if (strstr(encryption_mode, "3") != NULL) {
111 strcpy(ap_param->enctyption_mode, "WPA3-");
112 } else if (strstr(encryption_mode, "2") != NULL) {
113 strcpy(ap_param->enctyption_mode, "WPA2-");
114 } else if (strstr(encryption_mode, "1") != NULL) {
115 strcpy(ap_param->enctyption_mode, "WPA-");
116 } else if (strstr(encryption_mode, "mix") != NULL) {
117 strcpy(ap_param->enctyption_mode, "WPA-WPA2-");
118 }
119
120 if (strstr(encryption_mode, "psk") != NULL) {
121 strcat(ap_param->enctyption_mode, "Personal");
122 } else if (strstr(encryption_mode, "wpa") != NULL) {
123 strcat(ap_param->enctyption_mode, "Enterprise");
124 }
125
126 if (strcmp(encryption_mode, "none") == 0) {
127 strcpy(ap_param->enctyption_mode, "None");
128 }
129}
130
131void set_key(wifi_ap_param *ap_param, char *key)
132{
133 strncpy(ap_param->password, key, 64);
134}
135
136void set_radio_param(wifi_radio_param radio_parameter)
137{
138 BOOL enable;
139 BOOL current;
developer91f80742022-10-04 15:20:18 +0800140 int ret = 0;
141 struct params param;
developer63d72772022-10-07 09:42:31 +0800142 wifi_radio_operationParam_t operationParam = {0};
143
144 if (radio_parameter.disabled == TRUE) {
145 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
146 return;
147 }
148 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800149
150 fprintf(stderr, "Start setting radio\n");
developer63d72772022-10-07 09:42:31 +0800151 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
152 if (ret != RETURN_OK)
153 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800154
developer91f80742022-10-04 15:20:18 +0800155 // Channel
developer63d72772022-10-07 09:42:31 +0800156 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
157 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800158
159 // Country
160 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
161 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
162 if (ret != RETURN_OK)
163 fprintf(stderr, "[Set Country failed!!!]\n");
164 ret = 0;
165
166 // hwmode
167 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
168 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
169 if (ret != RETURN_OK)
170 fprintf(stderr, "[Set hwmode failed!!!]\n");
171 ret = 0;
172
173 // htmode
developer63d72772022-10-07 09:42:31 +0800174 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800175 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800176 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800177 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
178 strcpy(radio_parameter.htmode, "11G");
179
developer63d72772022-10-07 09:42:31 +0800180 if (strstr(radio_parameter.htmode, "HE") != NULL)
181 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800182
developer63d72772022-10-07 09:42:31 +0800183 } else if (strcmp(radio_parameter.band, "5g") == 0) {
184 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800185 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
186 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800187
188 if (strstr(radio_parameter.htmode, "HE") != NULL)
189 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800190 }
191
192 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800193 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800194 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800195 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800196
developer63d72772022-10-07 09:42:31 +0800197 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800198
developer63d72772022-10-07 09:42:31 +0800199 // apply setting
200 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800201 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800202 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800203
204}
205
206void set_ap_param(wifi_ap_param ap_param)
207{
208 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800209 int vap_index_in_map = 0;
210 wifi_vap_info_t vap_info = {0};
211 wifi_vap_info_map_t vap_map = {0};
212
213 ret = wifi_getRadioVapInfoMap(ap_param.radio_index, &vap_map);
214 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
215 fprintf(stderr, "[Get vap map failed!!!]\n");
216 vap_map.num_vaps = MAX_NUM_VAP_PER_RADIO;
217 } else { // get the index of the map
218 for (int i = 0; i < vap_map.num_vaps; i++) {
219 if (vap_map.vap_array[i].vap_index == ap_param.ap_index) {
220 vap_index_in_map = i;
221 break;
222 }
223 }
224 }
225
226 // get current setting
227 vap_info = vap_map.vap_array[vap_index_in_map];
developer91f80742022-10-04 15:20:18 +0800228
229 fprintf(stderr, "Start setting ap\n");
developer91f80742022-10-04 15:20:18 +0800230 // SSID
developer63d72772022-10-07 09:42:31 +0800231 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
232 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800233
234 // wpa and security mode
235 fprintf(stderr, "Set encryption mode: %s\n", ap_param.enctyption_mode);
236 ret = wifi_setApSecurityModeEnabled(ap_param.ap_index, ap_param.enctyption_mode);
237 if (ret != RETURN_OK)
238 fprintf(stderr, "[Set encryption mode failed!!!]\n");
239
240 // key
241 if (strlen(ap_param.password) > 0) {
242 fprintf(stderr, "Set password: %s\n", ap_param.password);
243 ret = wifi_setApSecurityKeyPassphrase(ap_param.ap_index, ap_param.password);
244 if (ret != RETURN_OK)
245 fprintf(stderr, "[Set password failed!!!]\n");
246 }
247
developer63d72772022-10-07 09:42:31 +0800248
249 // Replace the setting with uci config
250 vap_map.vap_array[vap_index_in_map] = vap_info;
251 ret = wifi_createVAP(ap_param.radio_index, &vap_map);
252 if (ret != RETURN_OK)
253 fprintf(stderr, "[Apply vap setting failed!!!]\n");
254
developer91f80742022-10-04 15:20:18 +0800255 // restart ap
256 wifi_setApEnable(ap_param.ap_index, FALSE);
257 wifi_setApEnable(ap_param.ap_index, TRUE);
258}
259
260int apply_uci_config ()
261{
262 struct uci_context *uci_ctx = uci_alloc_context();
263 struct uci_package *uci_pkg = NULL;
264 struct uci_element *e;
265 // struct uci_section *s;
266 const char cfg_name[] = "wireless";
267 int max_radio_num = 0;
268 BOOL parsing_radio = FALSE;
269
270 wifi_getMaxRadioNumber(&max_radio_num);
271 fprintf(stderr, "max radio number: %d\n", max_radio_num);
272 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
273 uci_free_context(uci_ctx);
274 fprintf(stderr, "%s: load uci failed.\n", __func__);
275 return RETURN_ERR;
276 }
277
278 uci_foreach_element(&uci_pkg->sections, e) {
279
280 struct uci_section *s = uci_to_section(e);
281 struct uci_element *option = NULL;
282 wifi_radio_param radio_param = {0};
283 wifi_ap_param ap_param = {0};
284 radio_param.radio_index = -1;
285 ap_param.ap_index = -1;
286
287 if (strcmp(s->type, "wifi-device") == 0) {
288 sscanf(s->e.name, "radio%d", &radio_param.radio_index);
289 parsing_radio = TRUE;
290 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
291 } else if (strcmp(s->type, "wifi-iface") == 0) {
292 sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
293 parsing_radio = FALSE;
294 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
295 }
296
297 uci_foreach_element(&s->options, option) {
298
299 struct uci_option *op = uci_to_option(option);
300 if (parsing_radio == TRUE) {
301 // transform the type from input string and store the value in radio_param.
302 if (strcmp(op->e.name, "channel") == 0)
303 set_channel(&radio_param, op->v.string);
304 else if (strcmp(op->e.name, "hwmode") == 0)
305 set_hwmode(&radio_param, op->v.string);
306 else if (strcmp(op->e.name, "htmode") == 0)
307 set_htmode(&radio_param, op->v.string);
308 else if (strcmp(op->e.name, "disabled") == 0)
309 set_disable(&radio_param, op->v.string);
310 else if (strcmp(op->e.name, "band") == 0)
311 set_band(&radio_param, op->v.string);
312 else if (strcmp(op->e.name, "country") == 0)
313 set_country(&radio_param, op->v.string);
314 else if (strcmp(op->e.name, "noscan") == 0)
315 set_band(&radio_param, op->v.string);
316 else
317 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
318 } else {
319 // parsing iface
320 if (strcmp(op->e.name, "device") == 0)
321 set_radionum(&ap_param, op->v.string);
322 else if (strcmp(op->e.name, "ssid") == 0)
323 set_ssid(&ap_param, op->v.string);
324 else if (strcmp(op->e.name, "encryption") == 0)
325 set_encryption(&ap_param, op->v.string);
326 else if (strcmp(op->e.name, "key") == 0)
327 set_key(&ap_param, op->v.string);
328 else
329 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
330 }
331 }
332 if (parsing_radio == TRUE)
333 set_radio_param(radio_param);
334 else
335 set_ap_param(ap_param);
336 }
337
338 uci_unload(uci_ctx, uci_pkg);
339 uci_free_context(uci_ctx);
340 return RETURN_OK;
341}
342
343int main(int argc, char **argv)
344{
345 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
346 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
347 return -1;
348 }
349 apply_uci_config();
350 return 0;
351}