blob: b180b81465f371f11caf0fcecb0e4db72e8396c8 [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
developer50614832022-11-17 20:42:05 +08008static int mac_addr_aton(unsigned char *mac_addr, char *arg)
9{
10 sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
11 return 0;
12}
13
14static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
15{
16 snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", arg[0], arg[1],arg[2],arg[3],arg[4],arg[5]);
17 return;
18}
developer8d8d6302022-10-18 16:36:37 +080019
20static int _syscmd(char *cmd, char *retBuf, int retBufSize)
21{
22 FILE *f;
23 char *ptr = retBuf;
24 int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0;
25
26
27 if((f = popen(cmd, "r")) == NULL) {
28 fprintf(stderr,"\npopen %s error\n", cmd);
29 return RETURN_ERR;
30 }
31
32 while(!feof(f))
33 {
34 *ptr = 0;
35 if(bufSize>=128) {
36 bufbytes=128;
37 } else {
38 bufbytes=bufSize-1;
39 }
40
41 fgets(ptr,bufbytes,f);
42 readbytes=strlen(ptr);
43
44 if(!readbytes)
45 break;
46
47 bufSize-=readbytes;
48 ptr += readbytes;
49 }
50 cmd_ret = pclose(f);
51 retBuf[retBufSize-1]=0;
52
53 return cmd_ret >> 8;
54}
55
56int phy_index_to_radio(int phyIndex)
57{
58 char cmd[128] = {0};
59 char buf[64] = {0};
60 int radioIndex = 0;
61 snprintf(cmd, sizeof(cmd), "ls /tmp | grep phy%d | cut -d '-' -f2 | tr -d '\n'", phyIndex);
62 _syscmd(cmd, buf, sizeof(buf));
63
64 if (strlen(buf) == 0 || strstr(buf, "wifi") == NULL) {
65 fprintf(stderr, "%s: failed to get wifi index\n", __func__);
66 return RETURN_ERR;
67 }
68 sscanf(buf, "wifi%d", &radioIndex);
developerc6a27322023-01-13 15:23:06 +080069 return radioIndex;
developer8d8d6302022-10-18 16:36:37 +080070}
71
developer91f80742022-10-04 15:20:18 +080072void set_channel(wifi_radio_param *radio_param, char *channel)
73{
developer63d72772022-10-07 09:42:31 +080074 if (strcmp(channel, "auto") == 0) {
75 radio_param->auto_channel = TRUE;
76 radio_param->channel = 0;
77 } else {
developer91f80742022-10-04 15:20:18 +080078 radio_param->auto_channel = FALSE;
developer63d72772022-10-07 09:42:31 +080079 radio_param->channel = strtol(channel, NULL, 10);
developer91f80742022-10-04 15:20:18 +080080 }
developer63d72772022-10-07 09:42:31 +080081 return;
developer91f80742022-10-04 15:20:18 +080082}
83
developer52c6ca22022-10-06 17:16:43 +080084void set_country(wifi_radio_param *radio_param, char *country)
developer91f80742022-10-04 15:20:18 +080085{
86 strcpy(radio_param->country, country);
87}
88
developer52c6ca22022-10-06 17:16:43 +080089void set_band(wifi_radio_param *radio_param, char *band)
developer91f80742022-10-04 15:20:18 +080090{
91 strcpy(radio_param->band, band);
92}
93
developer6feac682022-10-18 17:44:13 +080094void set_noscan(wifi_radio_param *radio_param, char *noscan)
95{
96 snprintf(radio_param->noscan, 2, "%s", noscan);
97 radio_param->noscan[1] = '\0';
98}
99
developer91f80742022-10-04 15:20:18 +0800100void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
101{
102 if (strncmp(hwmode, "11a", 3) == 0)
103 strcpy(radio_param->hwmode, "a");
104 if (strncmp(hwmode, "11b", 3) == 0)
105 strcpy(radio_param->hwmode, "b");
106 if (strncmp(hwmode, "11g", 3) == 0)
107 strcpy(radio_param->hwmode, "g");
108}
109
110void set_htmode(wifi_radio_param *radio_param, char *htmode)
111{
112 char tmp[16] = {0};
113 char *ptr = htmode;
114 ULONG bandwidth = 0;
115 radio_param->bandwidth = 20;
116 while (*ptr) {
117 if (isdigit(*ptr)) {
118 bandwidth = strtoul(ptr, NULL, 10);
119 radio_param->bandwidth = bandwidth;
120 break;
121 }
122 ptr++;
123 }
124
125 // HT40 -> 11NGHT40PLUS
126 // VHT40+ -> 11ACVHT40PLUS
127 // HE80 -> 11AXHE80
128 if (strstr(htmode, "+") != NULL) {
129 strncpy(tmp, htmode, strlen(htmode) - 1);
130 strcat(tmp, "PLUS");
131 } else if (strstr(htmode, "-") != NULL) {
132 strncpy(tmp, htmode, strlen(htmode) - 1);
133 strcat(tmp, "MINUS");
134 } else
135 strcpy(tmp, htmode);
136
137
138 if (strstr(htmode, "VHT") != NULL) {
139 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
140 } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
141 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
142 } else if (strstr(htmode, "HE") != NULL) {
143 snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
144 } else { // NOHT or NONE should be parsed with the band, so just fill the original string.
145 strcpy(radio_param->htmode, tmp);
146 }
147
148}
149
150void set_disable(wifi_radio_param *radio_param, char *disable)
151{
152 if (strcmp(disable, "1") == 0)
153 radio_param->disabled = TRUE;
154 else
155 radio_param->disabled = FALSE;
156}
157
developer128f8aa2022-12-26 17:09:00 +0800158void set_rxant(wifi_radio_param *radio_param, char *mask)
159{
160 radio_param->rxantenna = strtol(mask, NULL, 16);
161}
162
163void set_txant(wifi_radio_param *radio_param, char *mask)
164{
165 radio_param->txantenna = strtol(mask, NULL, 16);
166}
167
developer13ec7352023-02-06 20:02:10 +0800168void set_htcoex(wifi_radio_param *radio_param, char *ht_coex)
169{
170 radio_param->ht_coex = strtol(ht_coex, NULL, 10);
171}
172
173void set_rts(wifi_radio_param *radio_param, char *rts)
174{
175 radio_param->rtsThreshold = strtol(rts, NULL, 10);
176}
177
developer465ca0c2022-11-25 14:30:05 +0800178void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800179{
developer50614832022-11-17 20:42:05 +0800180 int radio_num = 0;
181 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800182 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800183
184 while (*ptr) {
185 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800186 phyId = strtoul(ptr, NULL, 10);
187 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800188 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800189 break;
190 }
191 ptr++;
192 }
193}
194
developer465ca0c2022-11-25 14:30:05 +0800195void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800196{
developer465ca0c2022-11-25 14:30:05 +0800197 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800198}
199
developer465ca0c2022-11-25 14:30:05 +0800200void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800201{
developerff378f22022-10-13 13:33:57 +0800202 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800203 intf_param->security.mode = wifi_security_mode_none;
204 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800205 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800206 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800207 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800208 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800209 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800210 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800211 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800212 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800213 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800214 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800215 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800216 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800217 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800218 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800219 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800220 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerc6a27322023-01-13 15:23:06 +0800221 }else if(strcmp(encryption_mode, "owe") == 0){
222 intf_param->security.mode = wifi_security_mode_owe;
developer91f80742022-10-04 15:20:18 +0800223 }
224
developerff378f22022-10-13 13:33:57 +0800225 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800226 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800227 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800228 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800229 }else{
developer465ca0c2022-11-25 14:30:05 +0800230 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800231 }
developerff378f22022-10-13 13:33:57 +0800232
developerc6a27322023-01-13 15:23:06 +0800233 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer465ca0c2022-11-25 14:30:05 +0800234 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800235 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800236 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800237 }else{
developer465ca0c2022-11-25 14:30:05 +0800238 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800239 }
developer91f80742022-10-04 15:20:18 +0800240}
241
developer465ca0c2022-11-25 14:30:05 +0800242void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800243{
developer465ca0c2022-11-25 14:30:05 +0800244 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800245}
246
developer7ac3bd52022-12-13 16:19:09 +0800247void set_ifname(wifi_intf_param *intf_param, char *ifname)
248{
249 if (strlen(ifname) > 15)
250 return;
251 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
252}
253
developerb2123782022-12-29 09:35:55 +0800254void set_wds(wifi_intf_param *intf_param, char *wds_enable)
255{
256 intf_param->wds_mode = FALSE;
257 if (strncmp(wds_enable, "1", 1) == 0)
258 intf_param->wds_mode = TRUE;
259}
260
developer13ec7352023-02-06 20:02:10 +0800261void set_hidden(wifi_intf_param *intf_param, char *hidden)
262{
263 intf_param->hidden = strtol(hidden, NULL, 10);
264}
265
developer50614832022-11-17 20:42:05 +0800266int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800267{
268 FILE *f;
269 char mac_file[64] = {0};
270 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800271
developer1ac426a2022-12-22 19:44:36 +0800272 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800273 f = fopen(mac_file, "r");
274 if (f == NULL)
275 return -1;
276 fgets(mac_address, 20, f);
277 fclose(f);
278
developer50614832022-11-17 20:42:05 +0800279 mac_addr_aton(&(*bssid)[0], mac_address);
280 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800281 return 0;
282}
283
developer91f80742022-10-04 15:20:18 +0800284void set_radio_param(wifi_radio_param radio_parameter)
285{
developer91f80742022-10-04 15:20:18 +0800286 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800287 wifi_radio_operationParam_t operationParam = {0};
288
developer8d8d6302022-10-18 16:36:37 +0800289 if(radio_parameter.radio_index == -1)
290 return;
291
developer63d72772022-10-07 09:42:31 +0800292 if (radio_parameter.disabled == TRUE) {
293 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
294 return;
295 }
developer91f80742022-10-04 15:20:18 +0800296
297 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800298
developer128f8aa2022-12-26 17:09:00 +0800299 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
300 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
301 if (ret != RETURN_OK)
302 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
303 }
304
developerbf812932022-10-17 17:37:29 +0800305 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800306 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
307 if (ret != RETURN_OK)
308 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800309 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800310
developer91f80742022-10-04 15:20:18 +0800311 // Channel
developer63d72772022-10-07 09:42:31 +0800312 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
313 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800314
developer25e07812022-10-13 15:27:02 +0800315 //bandwidth
316 if (radio_parameter.bandwidth == 20){
317 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
318 }else if (radio_parameter.bandwidth == 40){
319 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
320 }else if (radio_parameter.bandwidth == 80){
321 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
322 }else if (radio_parameter.bandwidth == 160){
323 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
324 }
developer91f80742022-10-04 15:20:18 +0800325
326 // htmode
developer63d72772022-10-07 09:42:31 +0800327 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800328 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800329 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800330 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
331 strcpy(radio_parameter.htmode, "11G");
332
developer63d72772022-10-07 09:42:31 +0800333 if (strstr(radio_parameter.htmode, "HE") != NULL)
334 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800335
developer63d72772022-10-07 09:42:31 +0800336 } else if (strcmp(radio_parameter.band, "5g") == 0) {
337 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800338 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
339 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800340
341 if (strstr(radio_parameter.htmode, "HE") != NULL)
342 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800343 }else if (strcmp(radio_parameter.band, "6g") == 0) {
344 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
345 }
developer91f80742022-10-04 15:20:18 +0800346
347 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800348 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800349 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800350 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800351
developer63d72772022-10-07 09:42:31 +0800352 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800353
developer13ec7352023-02-06 20:02:10 +0800354 // rtsThreshold, zero means not set
355 if ((radio_parameter.rtsThreshold < 65535) && radio_parameter.rtsThreshold)
356 operationParam.rtsThreshold = radio_parameter.rtsThreshold;
357
358 //ht_coex
359 operationParam.obssCoex = radio_parameter.ht_coex;
360
developer63d72772022-10-07 09:42:31 +0800361 // apply setting
362 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800363 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800364 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800365
developerbb58a932022-11-07 16:58:17 +0800366 // Country
367 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
368 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
369 if (ret != RETURN_OK)
370 fprintf(stderr, "[Set Country failed!!!]\n");
371 ret = 0;
372
373 // hwmode
374 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
375 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
376 if (ret != RETURN_OK)
377 fprintf(stderr, "[Set hwmode failed!!!]\n");
378 ret = 0;
379
380 // noscan
381 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
382 if(strlen(radio_parameter.noscan)){
383 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
384 if (ret != RETURN_OK)
385 fprintf(stderr, "[Set noscan failed!!!]\n");
386 }
387 ret = 0;
388
developer91f80742022-10-04 15:20:18 +0800389}
390
developer465ca0c2022-11-25 14:30:05 +0800391void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800392{
393 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800394 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800395 int phy_index = 0;
developerbaf61262023-02-17 15:52:18 +0800396 int key_len = 0;
developer63d72772022-10-07 09:42:31 +0800397 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800398 BOOL radio_enable = FALSE;
399
developerff42a302022-10-19 17:40:23 +0800400 if(ap_param.radio_index == -1)
401 return;
402
developerbf812932022-10-17 17:37:29 +0800403 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
404 if (radio_enable == FALSE)
405 return;
developer63d72772022-10-07 09:42:31 +0800406
developerff42a302022-10-19 17:40:23 +0800407
408 // get the index of the map
409 for (int i = 0; i < map->num_vaps; i++) {
410 if (map->vap_array[i].vap_index == ap_param.ap_index) {
411 vap_index_in_map = i;
412 break;
developer63d72772022-10-07 09:42:31 +0800413 }
414 }
415
developerff42a302022-10-19 17:40:23 +0800416
developerf7e50b02022-10-14 10:07:58 +0800417 fprintf(stderr, "Start setting ap\n");
418
developerff42a302022-10-19 17:40:23 +0800419 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800420 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800421 phy_index = radio_index_to_phy(vap_info.radio_index);
422 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800423 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800424 return;
developerf7e50b02022-10-14 10:07:58 +0800425 }
developer91f80742022-10-04 15:20:18 +0800426
developer91f80742022-10-04 15:20:18 +0800427 // SSID
developer63d72772022-10-07 09:42:31 +0800428 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
429 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800430
developer7ac3bd52022-12-13 16:19:09 +0800431 // interface
432 if (strlen(ap_param.ifname) != 0) {
433 strncpy(vap_info.vap_name, ap_param.ifname, 16);
434 vap_info.vap_name[15] = "\0";
435 }
436
developerbaf61262023-02-17 15:52:18 +0800437 // Security
438 if (ap_param.security.mode == wifi_security_mode_wpa3_personal || ap_param.security.mode == wifi_security_mode_wpa3_transition){
439 // OpenWrt script only set psk, here we choose to set both psk and sae.
440 ap_param.security.u.key.type = wifi_security_key_type_psk_sae;
441 } else {
442 key_len = strlen(ap_param.security.u.key.key);
443 if (key_len == 64)
444 ap_param.security.u.key.type = wifi_security_key_type_psk;
445 else if (key_len >= 8 && key_len < 64)
446 ap_param.security.u.key.type = wifi_security_key_type_pass;
447 }
448
developerff378f22022-10-13 13:33:57 +0800449 vap_info.u.bss_info.security.mode = ap_param.security.mode;
450 vap_info.u.bss_info.security.encr = ap_param.security.encr;
451 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
452 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
453 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer13ec7352023-02-06 20:02:10 +0800454
455 // hidden
456 vap_info.u.bss_info.showSsid = (ap_param.hidden ? 0 : 1);
developer8d8d6302022-10-18 16:36:37 +0800457
developer63d72772022-10-07 09:42:31 +0800458 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800459 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800460}
461
developer465ca0c2022-11-25 14:30:05 +0800462void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800463{
464 wifi_sta_network_t *sta = NULL;
465 mac_address_t sta_mac = {0};
466 char sta_mac_str[20] = {0};
467 char key_mgmt[16] = {0};
468 char pairwise[16] = {0};
469 int phy_index = 0;
470
471 sta = calloc(1, sizeof(wifi_sta_network_t));
472
473 phy_index = radio_index_to_phy(sta_param.radio_index);
474 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
475 mac_addr_ntoa(sta_mac_str, sta_mac);
476 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
477 sta->ssid[31] = '\0';
478 snprintf(sta->psk, 64, "%s", sta_param.password);
479
480 if (sta_param.security.mode == wifi_security_mode_none)
481 strcpy(key_mgmt, "NONE");
482 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
483 strcpy(key_mgmt, "SAE");
developerc6a27322023-01-13 15:23:06 +0800484 else if (sta_param.security.mode == wifi_security_mode_owe)
485 strcpy(key_mgmt, "OWE");
developer50614832022-11-17 20:42:05 +0800486 else
487 strcpy(key_mgmt, "WPA-PSK");
488 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
489
490 if (sta_param.security.encr == wifi_encryption_aes)
491 strcpy(pairwise, "CCMP");
492 else if (sta_param.security.encr == wifi_encryption_tkip)
493 strcpy(pairwise, "TKIP");
494 else
495 strcpy(pairwise, "CCMP TKIP");
496 snprintf(sta->pairwise, 64, "%s", pairwise);
497
498 if (strlen(sta_param.security.u.key.key) > 0)
499 strncpy(sta->psk, sta_param.security.u.key.key, 127);
500 sta->psk[127] = '\0';
501 sta->psk_len = strlen(sta->psk);
502
developerb2123782022-12-29 09:35:55 +0800503 if (sta_param.wds_mode == TRUE)
504 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
505
506 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer50614832022-11-17 20:42:05 +0800507
508 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
509 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
510 free(sta);
511 return;
512 }
513 free(sta);
514
515 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
516 fprintf(stderr, "Enable station failed\n");
517 return;
518 }
519}
520
developer91f80742022-10-04 15:20:18 +0800521int apply_uci_config ()
522{
523 struct uci_context *uci_ctx = uci_alloc_context();
524 struct uci_package *uci_pkg = NULL;
525 struct uci_element *e;
526 // struct uci_section *s;
527 const char cfg_name[] = "wireless";
528 int max_radio_num = 0;
529 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800530 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800531 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800532 wifi_vap_info_map_t vap_map[3] = {0};
533 int ret = 0;
534 int i = 0;
developer91f80742022-10-04 15:20:18 +0800535
536 wifi_getMaxRadioNumber(&max_radio_num);
537 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800538 for (i = 0; i < max_radio_num ;i++ ){
539 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
540 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
541 fprintf(stderr, "[Get vap map failed!!!]\n");
542 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
543 }
544 }
developer91f80742022-10-04 15:20:18 +0800545 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
546 uci_free_context(uci_ctx);
547 fprintf(stderr, "%s: load uci failed.\n", __func__);
548 return RETURN_ERR;
549 }
550
551 uci_foreach_element(&uci_pkg->sections, e) {
552
553 struct uci_section *s = uci_to_section(e);
554 struct uci_element *option = NULL;
555 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800556 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800557 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800558 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800559 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800560
561 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800562 sscanf(s->e.name, "radio%d", &phyId);
563 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800564 parsing_radio = TRUE;
565 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
566 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800567 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800568 }
569
570 uci_foreach_element(&s->options, option) {
571
572 struct uci_option *op = uci_to_option(option);
573 if (parsing_radio == TRUE) {
574 // transform the type from input string and store the value in radio_param.
575 if (strcmp(op->e.name, "channel") == 0)
576 set_channel(&radio_param, op->v.string);
577 else if (strcmp(op->e.name, "hwmode") == 0)
578 set_hwmode(&radio_param, op->v.string);
579 else if (strcmp(op->e.name, "htmode") == 0)
580 set_htmode(&radio_param, op->v.string);
581 else if (strcmp(op->e.name, "disabled") == 0)
582 set_disable(&radio_param, op->v.string);
583 else if (strcmp(op->e.name, "band") == 0)
584 set_band(&radio_param, op->v.string);
585 else if (strcmp(op->e.name, "country") == 0)
586 set_country(&radio_param, op->v.string);
587 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800588 set_noscan(&radio_param, op->v.string);
developer128f8aa2022-12-26 17:09:00 +0800589 else if (strcmp(op->e.name, "rxantenna") == 0)
590 set_rxant(&radio_param, op->v.string);
591 else if (strcmp(op->e.name, "txantenna") == 0)
592 set_txant(&radio_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800593 else if (strcmp(op->e.name, "ht_coex") == 0)
594 set_htcoex(&radio_param, op->v.string);
595 else if (strcmp(op->e.name, "rts") == 0)
596 set_rts(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800597 else
598 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
599 } else {
600 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800601 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800602 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800603 }else if (strcmp(op->e.name, "mode") == 0){
developer1ac426a2022-12-22 19:44:36 +0800604 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer50614832022-11-17 20:42:05 +0800605 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800606 intf_param.sta_mode = TRUE;
607 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
608 staCount[intf_param.radio_index] ++ ;
609 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800610 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800611 intf_param.sta_mode = FALSE;
612 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
613 apCount[intf_param.radio_index] ++ ;
614 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800615 }
developer8d8d6302022-10-18 16:36:37 +0800616 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800617 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800618 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800619 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800620 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800621 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800622 }else if (strcmp(op->e.name, "ifname") == 0){
623 set_ifname(&intf_param, op->v.string);
developerb2123782022-12-29 09:35:55 +0800624 }else if (strcmp(op->e.name, "wds") == 0){
625 set_wds(&intf_param, op->v.string);
developer13ec7352023-02-06 20:02:10 +0800626 }else if (strcmp(op->e.name, "hidden") == 0){
627 set_hidden(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800628 }else{
developer91f80742022-10-04 15:20:18 +0800629 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800630 }
developer91f80742022-10-04 15:20:18 +0800631 }
632 }
633 if (parsing_radio == TRUE)
634 set_radio_param(radio_param);
developer465ca0c2022-11-25 14:30:05 +0800635 else if (intf_param.sta_mode == TRUE)
636 set_sta_param(intf_param);
developer91f80742022-10-04 15:20:18 +0800637 else
developer465ca0c2022-11-25 14:30:05 +0800638 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800639 }
developer50614832022-11-17 20:42:05 +0800640 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800641
developerff42a302022-10-19 17:40:23 +0800642 for (i = 0; i < max_radio_num ;i++ ){
643 ret = wifi_createVAP(i, &vap_map[i]);
644 if (ret != RETURN_OK)
645 fprintf(stderr, "[Apply vap setting failed!!!]\n");
646 }
647
developer91f80742022-10-04 15:20:18 +0800648 uci_unload(uci_ctx, uci_pkg);
649 uci_free_context(uci_ctx);
650 return RETURN_OK;
651}
652
653int main(int argc, char **argv)
654{
655 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
656 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
657 return -1;
658 }
659 apply_uci_config();
660 return 0;
661}